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