View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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  
31  /**
32   *
33   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
34   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
35   *
36   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
37   *
38   */
39  final class NioClientSocketChannel extends NioSocketChannel {
40  
41      private static final InternalLogger logger =
42          InternalLoggerFactory.getInstance(NioClientSocketChannel.class);
43  
44      private static SocketChannel newSocket() {
45          SocketChannel socket;
46          try {
47              socket = SocketChannel.open();
48          } catch (IOException e) {
49              throw new ChannelException("Failed to open a socket.", e);
50          }
51  
52          boolean success = false;
53          try {
54              socket.configureBlocking(false);
55              success = true;
56          } catch (IOException e) {
57              throw new ChannelException("Failed to enter non-blocking mode.", e);
58          } finally {
59              if (!success) {
60                  try {
61                      socket.close();
62                  } catch (IOException e) {
63                      logger.warn(
64                              "Failed to close a partially initialized socket.",
65                              e);
66                  }
67              }
68          }
69  
70          return socket;
71      }
72  
73      volatile ChannelFuture connectFuture;
74      volatile boolean boundManually;
75  
76      // Does not need to be volatile as it's accessed by only one thread.
77      long connectDeadlineNanos;
78  
79      NioClientSocketChannel(
80              ChannelFactory factory, ChannelPipeline pipeline,
81              ChannelSink sink, NioWorker worker) {
82  
83          super(null, factory, pipeline, sink, newSocket(), worker);
84          fireChannelOpen(this);
85      }
86  }