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 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      // Does not need to be volatile as it's accessed by only one thread.
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  }