1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
package org.activeio.adapter;
|
18
|
|
|
19
|
|
import java.io.IOException;
|
20
|
|
import java.io.InputStream;
|
21
|
|
import java.io.OutputStream;
|
22
|
|
import java.net.InetAddress;
|
23
|
|
import java.net.Socket;
|
24
|
|
import java.net.SocketAddress;
|
25
|
|
import java.net.SocketException;
|
26
|
|
import java.nio.channels.SocketChannel;
|
27
|
|
|
28
|
|
import org.activeio.Packet;
|
29
|
|
import org.activeio.SynchChannel;
|
30
|
|
import org.activeio.net.SocketMetadata;
|
31
|
|
import org.activeio.packet.ByteArrayPacket;
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
public class SynchChannelToSocketAdapter extends Socket {
|
42
|
|
|
43
|
|
private final SynchChannel channel;
|
44
|
|
private final SynchChannelInputStream inputStream;
|
45
|
|
private final SynchChannelOutputStream outputStream;
|
46
|
|
private final SocketMetadata socketMetadata;
|
47
|
|
private final Packet urgentPackget = new ByteArrayPacket(new byte[1]);
|
48
|
|
boolean closed;
|
49
|
|
|
50
|
4
|
public SynchChannelToSocketAdapter(SynchChannel channel) {
|
51
|
4
|
this(channel, (SocketMetadata)channel.narrow(SocketMetadata.class));
|
52
|
|
}
|
53
|
|
|
54
|
4
|
public SynchChannelToSocketAdapter(SynchChannel channel, SocketMetadata socketMetadata) {
|
55
|
4
|
this.channel = channel;
|
56
|
4
|
this.socketMetadata = socketMetadata;
|
57
|
4
|
this.inputStream = new SynchChannelInputStream(channel);
|
58
|
4
|
this.outputStream = new SynchChannelOutputStream(channel);
|
59
|
|
}
|
60
|
|
|
61
|
2
|
public boolean isConnected() {
|
62
|
2
|
return true;
|
63
|
|
}
|
64
|
|
|
65
|
0
|
public boolean isBound() {
|
66
|
0
|
return true;
|
67
|
|
}
|
68
|
|
|
69
|
0
|
public boolean isClosed() {
|
70
|
0
|
return closed;
|
71
|
|
}
|
72
|
|
|
73
|
0
|
public void bind(SocketAddress bindpoint) throws IOException {
|
74
|
0
|
throw new IOException("Not supported");
|
75
|
|
}
|
76
|
|
|
77
|
6
|
public synchronized void close() throws IOException {
|
78
|
6
|
if( closed )
|
79
|
2
|
return;
|
80
|
4
|
closed = true;
|
81
|
4
|
inputStream.close();
|
82
|
4
|
outputStream.close();
|
83
|
4
|
channel.dispose();
|
84
|
|
}
|
85
|
|
|
86
|
0
|
public void connect(SocketAddress endpoint) throws IOException {
|
87
|
0
|
throw new IOException("Not supported");
|
88
|
|
}
|
89
|
|
|
90
|
0
|
public void connect(SocketAddress endpoint, int timeout) throws IOException {
|
91
|
0
|
throw new IOException("Not supported");
|
92
|
|
}
|
93
|
|
|
94
|
0
|
public SocketChannel getChannel() {
|
95
|
0
|
return null;
|
96
|
|
}
|
97
|
|
|
98
|
4
|
public InputStream getInputStream() throws IOException {
|
99
|
4
|
return inputStream;
|
100
|
|
}
|
101
|
|
|
102
|
4
|
public OutputStream getOutputStream() throws IOException {
|
103
|
4
|
return outputStream;
|
104
|
|
}
|
105
|
|
|
106
|
0
|
public boolean isInputShutdown() {
|
107
|
0
|
return inputStream.isClosed();
|
108
|
|
}
|
109
|
|
|
110
|
0
|
public boolean isOutputShutdown() {
|
111
|
0
|
return outputStream.isClosed();
|
112
|
|
}
|
113
|
|
|
114
|
0
|
public void sendUrgentData(int data) throws IOException {
|
115
|
0
|
urgentPackget.clear();
|
116
|
0
|
urgentPackget.write(data);
|
117
|
0
|
urgentPackget.flip();
|
118
|
0
|
channel.write(urgentPackget);
|
119
|
|
}
|
120
|
|
|
121
|
0
|
public int getSoTimeout() throws SocketException {
|
122
|
0
|
return (int) inputStream.getTimeout();
|
123
|
|
}
|
124
|
|
|
125
|
12
|
public synchronized void setSoTimeout(int timeout) throws SocketException {
|
126
|
12
|
inputStream.setTimeout(timeout);
|
127
|
|
}
|
128
|
|
|
129
|
0
|
public void shutdownOutput() throws IOException {
|
130
|
0
|
outputStream.close();
|
131
|
|
}
|
132
|
|
|
133
|
0
|
public void shutdownInput() throws IOException {
|
134
|
0
|
inputStream.close();
|
135
|
|
}
|
136
|
|
|
137
|
4
|
protected SocketMetadata getSocketMetadata() throws SocketException {
|
138
|
4
|
if( socketMetadata == null )
|
139
|
0
|
throw new SocketException("No socket metadata available.");
|
140
|
4
|
return socketMetadata;
|
141
|
|
}
|
142
|
|
|
143
|
4
|
public InetAddress getInetAddress() {
|
144
|
4
|
if( socketMetadata ==null )
|
145
|
0
|
return null;
|
146
|
4
|
return socketMetadata.getInetAddress();
|
147
|
|
}
|
148
|
0
|
public boolean getKeepAlive() throws SocketException {
|
149
|
0
|
return getSocketMetadata().getKeepAlive();
|
150
|
|
}
|
151
|
0
|
public InetAddress getLocalAddress() {
|
152
|
0
|
if( socketMetadata ==null )
|
153
|
0
|
return null;
|
154
|
0
|
return socketMetadata.getLocalAddress();
|
155
|
|
}
|
156
|
2
|
public int getLocalPort() {
|
157
|
2
|
if( socketMetadata ==null )
|
158
|
0
|
return -1;
|
159
|
2
|
return socketMetadata.getLocalPort();
|
160
|
|
}
|
161
|
0
|
public SocketAddress getLocalSocketAddress() {
|
162
|
0
|
if( socketMetadata ==null )
|
163
|
0
|
return null;
|
164
|
0
|
return socketMetadata.getLocalSocketAddress();
|
165
|
|
}
|
166
|
0
|
public boolean getOOBInline() throws SocketException {
|
167
|
0
|
return getSocketMetadata().getOOBInline();
|
168
|
|
}
|
169
|
2
|
public int getPort() {
|
170
|
2
|
if( socketMetadata ==null )
|
171
|
0
|
return -1;
|
172
|
2
|
return socketMetadata.getPort();
|
173
|
|
}
|
174
|
0
|
public int getReceiveBufferSize() throws SocketException {
|
175
|
0
|
return getSocketMetadata().getReceiveBufferSize();
|
176
|
|
}
|
177
|
0
|
public SocketAddress getRemoteSocketAddress() {
|
178
|
0
|
if( socketMetadata ==null )
|
179
|
0
|
return null;
|
180
|
0
|
return socketMetadata.getRemoteSocketAddress();
|
181
|
|
}
|
182
|
0
|
public boolean getReuseAddress() throws SocketException {
|
183
|
0
|
return getSocketMetadata().getReuseAddress();
|
184
|
|
}
|
185
|
0
|
public int getSendBufferSize() throws SocketException {
|
186
|
0
|
return getSocketMetadata().getSendBufferSize();
|
187
|
|
}
|
188
|
0
|
public int getSoLinger() throws SocketException {
|
189
|
0
|
return getSocketMetadata().getSoLinger();
|
190
|
|
}
|
191
|
0
|
public boolean getTcpNoDelay() throws SocketException {
|
192
|
0
|
return getSocketMetadata().getTcpNoDelay();
|
193
|
|
}
|
194
|
0
|
public int getTrafficClass() throws SocketException {
|
195
|
0
|
return getSocketMetadata().getTrafficClass();
|
196
|
|
}
|
197
|
0
|
public void setKeepAlive(boolean on) throws SocketException {
|
198
|
0
|
getSocketMetadata().setKeepAlive(on);
|
199
|
|
}
|
200
|
0
|
public void setOOBInline(boolean on) throws SocketException {
|
201
|
0
|
getSocketMetadata().setOOBInline(on);
|
202
|
|
}
|
203
|
0
|
public void setReceiveBufferSize(int size) throws SocketException {
|
204
|
0
|
getSocketMetadata().setReceiveBufferSize(size);
|
205
|
|
}
|
206
|
0
|
public void setReuseAddress(boolean on) throws SocketException {
|
207
|
0
|
getSocketMetadata().setReuseAddress(on);
|
208
|
|
}
|
209
|
0
|
public void setSendBufferSize(int size) throws SocketException {
|
210
|
0
|
getSocketMetadata().setSendBufferSize(size);
|
211
|
|
}
|
212
|
2
|
public void setSoLinger(boolean on, int linger) throws SocketException {
|
213
|
2
|
getSocketMetadata().setSoLinger(on, linger);
|
214
|
|
}
|
215
|
2
|
public void setTcpNoDelay(boolean on) throws SocketException {
|
216
|
2
|
getSocketMetadata().setTcpNoDelay(on);
|
217
|
|
}
|
218
|
0
|
public void setTrafficClass(int tc) throws SocketException {
|
219
|
0
|
getSocketMetadata().setTrafficClass(tc);
|
220
|
|
}
|
221
|
|
}
|
222
|
|
|