1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.channel.socket.nio;
17
18 import org.jboss.netty.channel.ChannelException;
19 import org.jboss.netty.channel.ChannelFactory;
20 import org.jboss.netty.channel.ChannelFuture;
21 import org.jboss.netty.channel.ChannelPipeline;
22 import org.jboss.netty.channel.ChannelSink;
23 import org.jboss.netty.logging.InternalLogger;
24 import org.jboss.netty.logging.InternalLoggerFactory;
25 import org.jboss.netty.util.Timeout;
26
27 import java.io.IOException;
28 import java.net.SocketAddress;
29 import java.nio.channels.SocketChannel;
30
31 import static org.jboss.netty.channel.Channels.*;
32
33 final class NioClientSocketChannel extends NioSocketChannel {
34
35 private static final InternalLogger logger =
36 InternalLoggerFactory.getInstance(NioClientSocketChannel.class);
37
38 private static SocketChannel newSocket() {
39 SocketChannel socket;
40 try {
41 socket = SocketChannel.open();
42 } catch (IOException e) {
43 throw new ChannelException("Failed to open a socket.", e);
44 }
45
46 boolean success = false;
47 try {
48 socket.configureBlocking(false);
49 success = true;
50 } catch (IOException e) {
51 throw new ChannelException("Failed to enter non-blocking mode.", e);
52 } finally {
53 if (!success) {
54 try {
55 socket.close();
56 } catch (IOException e) {
57 if (logger.isWarnEnabled()) {
58 logger.warn(
59 "Failed to close a partially initialized socket.",
60 e);
61 }
62 }
63 }
64 }
65
66 return socket;
67 }
68
69 volatile ChannelFuture connectFuture;
70 volatile boolean boundManually;
71
72
73 long connectDeadlineNanos;
74 volatile SocketAddress requestedRemoteAddress;
75
76 volatile Timeout timoutTimer;
77
78 NioClientSocketChannel(
79 ChannelFactory factory, ChannelPipeline pipeline,
80 ChannelSink sink, NioWorker worker) {
81
82 super(null, factory, pipeline, sink, newSocket(), worker);
83 fireChannelOpen(this);
84 }
85 }