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.FileRegion;
25 import io.netty.channel.nio.AbstractNioByteChannel;
26 import io.netty.channel.udt.DefaultUdtChannelConfig;
27 import io.netty.channel.udt.UdtChannel;
28 import io.netty.channel.udt.UdtChannelConfig;
29 import io.netty.util.internal.SocketUtils;
30 import io.netty.util.internal.logging.InternalLogger;
31 import io.netty.util.internal.logging.InternalLoggerFactory;
32
33 import java.io.IOException;
34 import java.net.InetSocketAddress;
35 import java.net.SocketAddress;
36 import java.security.AccessController;
37 import java.security.PrivilegedActionException;
38 import java.security.PrivilegedExceptionAction;
39
40 import static java.nio.channels.SelectionKey.*;
41
42
43
44
45
46
47 @Deprecated
48 public class NioUdtByteConnectorChannel extends AbstractNioByteChannel implements UdtChannel {
49
50 private static final InternalLogger logger =
51 InternalLoggerFactory.getInstance(NioUdtByteConnectorChannel.class);
52
53 private static final ChannelMetadata METADATA = new ChannelMetadata(false);
54
55 private final UdtChannelConfig config;
56
57 public NioUdtByteConnectorChannel() {
58 this(TypeUDT.STREAM);
59 }
60
61 public NioUdtByteConnectorChannel(final Channel parent, final SocketChannelUDT channelUDT) {
62 super(parent, channelUDT);
63 try {
64 channelUDT.configureBlocking(false);
65 switch (channelUDT.socketUDT().status()) {
66 case INIT:
67 case OPENED:
68 config = new DefaultUdtChannelConfig(this, channelUDT, true);
69 break;
70 default:
71 config = new DefaultUdtChannelConfig(this, channelUDT, false);
72 break;
73 }
74 } catch (final Exception e) {
75 try {
76 channelUDT.close();
77 } catch (final Exception e2) {
78 if (logger.isWarnEnabled()) {
79 logger.warn("Failed to close channel.", e2);
80 }
81 }
82 throw new ChannelException("Failed to configure channel.", e);
83 }
84 }
85
86 public NioUdtByteConnectorChannel(final SocketChannelUDT channelUDT) {
87 this(null, channelUDT);
88 }
89
90 public NioUdtByteConnectorChannel(final TypeUDT type) {
91 this(NioUdtProvider.newConnectorChannelUDT(type));
92 }
93
94 @Override
95 public UdtChannelConfig config() {
96 return config;
97 }
98
99 @Override
100 protected void doBind(final SocketAddress localAddress) throws Exception {
101 privilegedBind(javaChannel(), localAddress);
102 }
103
104 @Override
105 protected void doClose() throws Exception {
106 javaChannel().close();
107 }
108
109 @Override
110 protected boolean doConnect(final SocketAddress remoteAddress,
111 final SocketAddress localAddress) throws Exception {
112 doBind(localAddress != null? localAddress : new InetSocketAddress(0));
113 boolean success = false;
114 try {
115 final boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
116 if (!connected) {
117 selectionKey().interestOps(
118 selectionKey().interestOps() | OP_CONNECT);
119 }
120 success = true;
121 return connected;
122 } finally {
123 if (!success) {
124 doClose();
125 }
126 }
127 }
128
129 @Override
130 protected void doDisconnect() throws Exception {
131 doClose();
132 }
133
134 @Override
135 protected void doFinishConnect() throws Exception {
136 if (javaChannel().finishConnect()) {
137 selectionKey().interestOps(
138 selectionKey().interestOps() & ~OP_CONNECT);
139 } else {
140 throw new Error(
141 "Provider error: failed to finish connect. Provider library should be upgraded.");
142 }
143 }
144
145 @Override
146 protected int doReadBytes(final ByteBuf byteBuf) throws Exception {
147 return byteBuf.writeBytes(javaChannel(), byteBuf.writableBytes());
148 }
149
150 @Override
151 protected int doWriteBytes(final ByteBuf byteBuf) throws Exception {
152 final int expectedWrittenBytes = byteBuf.readableBytes();
153 return byteBuf.readBytes(javaChannel(), expectedWrittenBytes);
154 }
155
156 @Override
157 protected long doWriteFileRegion(FileRegion region) throws Exception {
158 throw new UnsupportedOperationException();
159 }
160
161 @Override
162 public boolean isActive() {
163 final SocketChannelUDT channelUDT = javaChannel();
164 return channelUDT.isOpen() && channelUDT.isConnectFinished();
165 }
166
167 @Override
168 protected SocketChannelUDT javaChannel() {
169 return (SocketChannelUDT) super.javaChannel();
170 }
171
172 @Override
173 protected SocketAddress localAddress0() {
174 return javaChannel().socket().getLocalSocketAddress();
175 }
176
177 @Override
178 public ChannelMetadata metadata() {
179 return METADATA;
180 }
181
182 @Override
183 protected SocketAddress remoteAddress0() {
184 return javaChannel().socket().getRemoteSocketAddress();
185 }
186
187 @Override
188 public InetSocketAddress localAddress() {
189 return (InetSocketAddress) super.localAddress();
190 }
191
192 @Override
193 public InetSocketAddress remoteAddress() {
194 return (InetSocketAddress) super.remoteAddress();
195 }
196
197 private static void privilegedBind(final SocketChannelUDT socketChannel, final SocketAddress localAddress)
198 throws IOException {
199 try {
200 AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
201 @Override
202 public Void run() throws IOException {
203 socketChannel.bind(localAddress);
204 return null;
205 }
206 });
207 } catch (PrivilegedActionException e) {
208 throw (IOException) e.getCause();
209 }
210 }
211
212 }