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