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