View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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      // Does not need to be volatile as it's accessed by only one thread.
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  }