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