1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
package org.activeio.net;
|
20
|
|
|
21
|
|
import java.io.IOException;
|
22
|
|
import java.net.InetAddress;
|
23
|
|
import java.net.Socket;
|
24
|
|
import java.net.SocketAddress;
|
25
|
|
import java.net.SocketException;
|
26
|
|
import java.nio.ByteBuffer;
|
27
|
|
import java.nio.channels.SocketChannel;
|
28
|
|
|
29
|
|
import org.activeio.Disposable;
|
30
|
|
import org.activeio.packet.ByteBufferPacket;
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
public class NIOBaseChannel implements SocketMetadata, Disposable {
|
38
|
|
|
39
|
|
protected final SocketChannel socketChannel;
|
40
|
|
protected final Socket socket;
|
41
|
|
private final boolean useDirect;
|
42
|
|
private int curentSoTimeout;
|
43
|
|
private boolean disposed;
|
44
|
|
private final String name;
|
45
|
|
|
46
|
44
|
protected NIOBaseChannel(SocketChannel socketChannel, boolean useDirect) throws IOException {
|
47
|
44
|
this.socketChannel = socketChannel;
|
48
|
44
|
this.useDirect = useDirect;
|
49
|
44
|
this.socket = this.socketChannel.socket();
|
50
|
|
|
51
|
44
|
if( useDirect ) {
|
52
|
44
|
socket.setSendBufferSize(ByteBufferPacket.DEFAULT_DIRECT_BUFFER_SIZE);
|
53
|
44
|
socket.setReceiveBufferSize(ByteBufferPacket.DEFAULT_DIRECT_BUFFER_SIZE);
|
54
|
|
} else {
|
55
|
0
|
socket.setSendBufferSize(ByteBufferPacket.DEFAULT_BUFFER_SIZE);
|
56
|
0
|
socket.setReceiveBufferSize(ByteBufferPacket.DEFAULT_BUFFER_SIZE);
|
57
|
|
}
|
58
|
|
|
59
|
44
|
this.name = "NIO Socket Connection: "+getLocalSocketAddress()+" -> "+getRemoteSocketAddress();
|
60
|
|
}
|
61
|
|
|
62
|
22
|
protected ByteBuffer allocateBuffer() {
|
63
|
22
|
if( useDirect ) {
|
64
|
22
|
return ByteBuffer.allocateDirect(ByteBufferPacket.DEFAULT_DIRECT_BUFFER_SIZE);
|
65
|
|
} else {
|
66
|
0
|
return ByteBuffer.allocate(ByteBufferPacket.DEFAULT_BUFFER_SIZE);
|
67
|
|
}
|
68
|
|
}
|
69
|
|
|
70
|
2064
|
protected void setSoTimeout(int i) throws SocketException {
|
71
|
2064
|
if( curentSoTimeout != i ) {
|
72
|
16
|
socket.setSoTimeout(i);
|
73
|
16
|
curentSoTimeout = i;
|
74
|
|
}
|
75
|
|
}
|
76
|
44
|
public Object narrow(Class target) {
|
77
|
44
|
if( target.isAssignableFrom(getClass()) ) {
|
78
|
44
|
return this;
|
79
|
|
}
|
80
|
0
|
return null;
|
81
|
|
}
|
82
|
|
|
83
|
2006
|
public String toString() {
|
84
|
2006
|
return name;
|
85
|
|
}
|
86
|
|
|
87
|
50
|
public void dispose() {
|
88
|
50
|
if (disposed)
|
89
|
6
|
return;
|
90
|
|
|
91
|
44
|
try {
|
92
|
44
|
socketChannel.close();
|
93
|
|
} catch (IOException ignore) {
|
94
|
|
}
|
95
|
44
|
disposed = true;
|
96
|
|
}
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
0
|
public void flush() throws IOException {
|
102
|
|
}
|
103
|
|
|
104
|
0
|
public InetAddress getInetAddress() {
|
105
|
0
|
return socket.getInetAddress();
|
106
|
|
}
|
107
|
0
|
public boolean getKeepAlive() throws SocketException {
|
108
|
0
|
return socket.getKeepAlive();
|
109
|
|
}
|
110
|
0
|
public InetAddress getLocalAddress() {
|
111
|
0
|
return socket.getLocalAddress();
|
112
|
|
}
|
113
|
0
|
public int getLocalPort() {
|
114
|
0
|
return socket.getLocalPort();
|
115
|
|
}
|
116
|
62
|
public SocketAddress getLocalSocketAddress() {
|
117
|
62
|
return socket.getLocalSocketAddress();
|
118
|
|
}
|
119
|
0
|
public boolean getOOBInline() throws SocketException {
|
120
|
0
|
return socket.getOOBInline();
|
121
|
|
}
|
122
|
0
|
public int getPort() {
|
123
|
0
|
return socket.getPort();
|
124
|
|
}
|
125
|
0
|
public int getReceiveBufferSize() throws SocketException {
|
126
|
0
|
return socket.getReceiveBufferSize();
|
127
|
|
}
|
128
|
62
|
public SocketAddress getRemoteSocketAddress() {
|
129
|
62
|
return socket.getRemoteSocketAddress();
|
130
|
|
}
|
131
|
0
|
public boolean getReuseAddress() throws SocketException {
|
132
|
0
|
return socket.getReuseAddress();
|
133
|
|
}
|
134
|
0
|
public int getSendBufferSize() throws SocketException {
|
135
|
0
|
return socket.getSendBufferSize();
|
136
|
|
}
|
137
|
0
|
public int getSoLinger() throws SocketException {
|
138
|
0
|
return socket.getSoLinger();
|
139
|
|
}
|
140
|
0
|
public int getSoTimeout() throws SocketException {
|
141
|
0
|
return socket.getSoTimeout();
|
142
|
|
}
|
143
|
0
|
public boolean getTcpNoDelay() throws SocketException {
|
144
|
0
|
return socket.getTcpNoDelay();
|
145
|
|
}
|
146
|
0
|
public int getTrafficClass() throws SocketException {
|
147
|
0
|
return socket.getTrafficClass();
|
148
|
|
}
|
149
|
0
|
public boolean isBound() {
|
150
|
0
|
return socket.isBound();
|
151
|
|
}
|
152
|
0
|
public boolean isClosed() {
|
153
|
0
|
return socket.isClosed();
|
154
|
|
}
|
155
|
0
|
public boolean isConnected() {
|
156
|
0
|
return socket.isConnected();
|
157
|
|
}
|
158
|
0
|
public void setKeepAlive(boolean on) throws SocketException {
|
159
|
0
|
socket.setKeepAlive(on);
|
160
|
|
}
|
161
|
0
|
public void setOOBInline(boolean on) throws SocketException {
|
162
|
0
|
socket.setOOBInline(on);
|
163
|
|
}
|
164
|
0
|
public void setReceiveBufferSize(int size) throws SocketException {
|
165
|
0
|
socket.setReceiveBufferSize(size);
|
166
|
|
}
|
167
|
0
|
public void setReuseAddress(boolean on) throws SocketException {
|
168
|
0
|
socket.setReuseAddress(on);
|
169
|
|
}
|
170
|
0
|
public void setSendBufferSize(int size) throws SocketException {
|
171
|
0
|
socket.setSendBufferSize(size);
|
172
|
|
}
|
173
|
0
|
public void setSoLinger(boolean on, int linger) throws SocketException {
|
174
|
0
|
socket.setSoLinger(on, linger);
|
175
|
|
}
|
176
|
36
|
public void setTcpNoDelay(boolean on) throws SocketException {
|
177
|
36
|
socket.setTcpNoDelay(on);
|
178
|
|
}
|
179
|
0
|
public void setTrafficClass(int tc) throws SocketException {
|
180
|
0
|
socket.setTrafficClass(tc);
|
181
|
|
}
|
182
|
|
}
|