Java 类org.jboss.netty.channel.socket.nio.NioSocketChannelConfig 实例源码

项目:nfs-client-java    文件:Connection.java   
/**
 * If there is no current connection, start a new tcp connection asynchronously.
 * 
 * @throws RpcException
 */
protected void connect() throws RpcException {
    if (_state.equals(State.CONNECTED)) {
        return;
    }

    final ChannelFuture oldChannelFuture = _channelFuture;

    if (LOG.isDebugEnabled()) {
        String logPrefix = _usePrivilegedPort ? "usePrivilegedPort " : "";
        LOG.debug("{}connecting to {}", logPrefix, getRemoteAddress());
    }
    _state = State.CONNECTING;

    if (_usePrivilegedPort) {
        _channel = bindToPrivilegedPort();
        _channelFuture = _channel.connect(getRemoteAddress());
    } else {
        _channelFuture = _clientBootstrap.connect();
        _channel = _channelFuture.getChannel();
    }

    NioSocketChannelConfig cfg = (NioSocketChannelConfig) _channel.getConfig();
    cfg.setWriteBufferHighWaterMark(MAX_SENDING_QUEUE_SIZE);

    _channelFuture.addListener(new ChannelFutureListener() {
        /* (non-Javadoc)
         * @see org.jboss.netty.channel.ChannelFutureListener#operationComplete(org.jboss.netty.channel.ChannelFuture)
         */
        public void operationComplete(ChannelFuture future) {
            if (_channelFuture.isSuccess()) {
                _state = State.CONNECTED;
                oldChannelFuture.setSuccess();
            } else {
                _state = State.DISCONNECTED;
                oldChannelFuture.cancel();
            }

        }
    });

}