1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.udt.nio;
17
18 import com.barchart.udt.TypeUDT;
19 import com.barchart.udt.nio.SocketChannelUDT;
20 import io.netty.buffer.ByteBuf;
21 import io.netty.channel.Channel;
22 import io.netty.channel.ChannelException;
23 import io.netty.channel.ChannelMetadata;
24 import io.netty.channel.ChannelOutboundBuffer;
25 import io.netty.util.internal.SocketUtils;
26 import io.netty.channel.nio.AbstractNioMessageChannel;
27 import io.netty.channel.udt.DefaultUdtChannelConfig;
28 import io.netty.channel.udt.UdtChannel;
29 import io.netty.channel.udt.UdtChannelConfig;
30 import io.netty.channel.udt.UdtMessage;
31 import io.netty.util.internal.logging.InternalLogger;
32 import io.netty.util.internal.logging.InternalLoggerFactory;
33
34 import java.io.IOException;
35 import java.net.InetSocketAddress;
36 import java.net.SocketAddress;
37 import java.security.AccessController;
38 import java.security.PrivilegedActionException;
39 import java.security.PrivilegedExceptionAction;
40 import java.util.List;
41
42 import static java.nio.channels.SelectionKey.*;
43
44
45
46
47
48
49
50
51 @Deprecated
52 public class NioUdtMessageConnectorChannel extends AbstractNioMessageChannel implements UdtChannel {
53
54 private static final InternalLogger logger =
55 InternalLoggerFactory.getInstance(NioUdtMessageConnectorChannel.class);
56
57 private static final ChannelMetadata METADATA = new ChannelMetadata(false);
58
59 private final UdtChannelConfig config;
60
61 public NioUdtMessageConnectorChannel() {
62 this(TypeUDT.DATAGRAM);
63 }
64
65 public NioUdtMessageConnectorChannel(final Channel parent, final SocketChannelUDT channelUDT) {
66 super(parent, channelUDT, OP_READ);
67 try {
68 channelUDT.configureBlocking(false);
69 switch (channelUDT.socketUDT().status()) {
70 case INIT:
71 case OPENED:
72 config = new DefaultUdtChannelConfig(this, channelUDT, true);
73 break;
74 default:
75 config = new DefaultUdtChannelConfig(this, channelUDT, false);
76 break;
77 }
78 } catch (final Exception e) {
79 try {
80 channelUDT.close();
81 } catch (final Exception e2) {
82 logger.warn("Failed to close channel.", e2);
83 }
84 throw new ChannelException("Failed to configure channel.", e);
85 }
86 }
87
88 public NioUdtMessageConnectorChannel(final SocketChannelUDT channelUDT) {
89 this(null, channelUDT);
90 }
91
92 public NioUdtMessageConnectorChannel(final TypeUDT type) {
93 this(NioUdtProvider.newConnectorChannelUDT(type));
94 }
95
96 @Override
97 public UdtChannelConfig config() {
98 return config;
99 }
100
101 @Override
102 protected void doBind(final SocketAddress localAddress) throws Exception {
103 privilegedBind(javaChannel(), localAddress);
104 }
105
106 @Override
107 protected void doClose() throws Exception {
108 javaChannel().close();
109 }
110
111 @Override
112 protected boolean doConnect(final SocketAddress remoteAddress,
113 final SocketAddress localAddress) throws Exception {
114 doBind(localAddress != null? localAddress : new InetSocketAddress(0));
115 boolean success = false;
116 try {
117 final boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
118 if (!connected) {
119 selectionKey().interestOps(
120 selectionKey().interestOps() | OP_CONNECT);
121 }
122 success = true;
123 return connected;
124 } finally {
125 if (!success) {
126 doClose();
127 }
128 }
129 }
130
131 @Override
132 protected void doDisconnect() throws Exception {
133 doClose();
134 }
135
136 @Override
137 protected void doFinishConnect() throws Exception {
138 if (javaChannel().finishConnect()) {
139 selectionKey().interestOps(
140 selectionKey().interestOps() & ~OP_CONNECT);
141 } else {
142 throw new Error(
143 "Provider error: failed to finish connect. Provider library should be upgraded.");
144 }
145 }
146
147 @Override
148 protected int doReadMessages(List<Object> buf) throws Exception {
149
150 final int maximumMessageSize = config.getReceiveBufferSize();
151
152 final ByteBuf byteBuf = config.getAllocator().directBuffer(
153 maximumMessageSize);
154
155 final int receivedMessageSize = byteBuf.writeBytes(javaChannel(),
156 maximumMessageSize);
157
158 if (receivedMessageSize <= 0) {
159 byteBuf.release();
160 return 0;
161 }
162
163 if (receivedMessageSize >= maximumMessageSize) {
164 javaChannel().close();
165 throw new ChannelException(
166 "Invalid config : increase receive buffer size to avoid message truncation");
167 }
168
169
170 buf.add(new UdtMessage(byteBuf));
171
172 return 1;
173 }
174
175 @Override
176 protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
177
178 final UdtMessage message = (UdtMessage) msg;
179
180 final ByteBuf byteBuf = message.content();
181
182 final int messageSize = byteBuf.readableBytes();
183 if (messageSize == 0) {
184 return true;
185 }
186
187 final long writtenBytes;
188 if (byteBuf.nioBufferCount() == 1) {
189 writtenBytes = javaChannel().write(byteBuf.nioBuffer());
190 } else {
191 writtenBytes = javaChannel().write(byteBuf.nioBuffers());
192 }
193
194
195 if (writtenBytes > 0 && writtenBytes != messageSize) {
196 throw new Error(
197 "Provider error: failed to write message. Provider library should be upgraded.");
198 }
199
200 return writtenBytes > 0;
201 }
202
203 @Override
204 public boolean isActive() {
205 final SocketChannelUDT channelUDT = javaChannel();
206 return channelUDT.isOpen() && channelUDT.isConnectFinished();
207 }
208
209 @Override
210 protected SocketChannelUDT javaChannel() {
211 return (SocketChannelUDT) super.javaChannel();
212 }
213
214 @Override
215 protected SocketAddress localAddress0() {
216 return javaChannel().socket().getLocalSocketAddress();
217 }
218
219 @Override
220 public ChannelMetadata metadata() {
221 return METADATA;
222 }
223
224 @Override
225 protected SocketAddress remoteAddress0() {
226 return javaChannel().socket().getRemoteSocketAddress();
227 }
228
229 @Override
230 public InetSocketAddress localAddress() {
231 return (InetSocketAddress) super.localAddress();
232 }
233
234 @Override
235 public InetSocketAddress remoteAddress() {
236 return (InetSocketAddress) super.remoteAddress();
237 }
238
239 private static void privilegedBind(final SocketChannelUDT socketChannel, final SocketAddress localAddress)
240 throws IOException {
241 try {
242 AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
243 @Override
244 public Void run() throws IOException {
245 socketChannel.bind(localAddress);
246 return null;
247 }
248 });
249 } catch (PrivilegedActionException e) {
250 throw (IOException) e.getCause();
251 }
252 }
253 }