1 /*
2 * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Additionally licensed with:
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32 package org.spf4j.io.tcp.proxy;
33
34 import java.io.IOException;
35 import java.nio.ByteBuffer;
36 import javax.annotation.Nullable;
37
38 /**
39 *
40 * @author zoly
41 */
42 public interface Sniffer {
43
44 /**
45 * Invoked on data receive/transmission.
46 * @param data - the byte buffer containing the data.
47 * @param nrBytes - number of bytes in the buffer. The data in the buffer is from position-nrBytes to position.
48 * nrBytes will be -1 on EOF.
49 * @return new nrReadValue if we aim to mutate buffer, returning -1 will simulate a EOF.
50 */
51 int received(ByteBuffer data, int nrBytes);
52
53
54 /**
55 * Allows to intercept read errors and change/suppress them.
56 * @param ex
57 * @return A exception you want to propagate, or null in case we do not want to propagate exception.
58 */
59 @Nullable
60 default IOException received(final IOException ex) {
61 return ex;
62 }
63
64 }