public IoFuture connect()

in core/src/main/java/org/apache/mina/transport/nio/NioTcpClient.java [110:252]


    public IoFuture<IoSession> connect(SocketAddress remoteAddress) {
        Assert.assertNotNull(remoteAddress, "remoteAddress");

        SocketChannel clientSocket;
        try {
            clientSocket = SocketChannel.open();
        } catch (IOException e) {
            throw new MinaRuntimeException("can't create a new socket, out of file descriptors ?", e);
        }

        try {
            clientSocket.socket().setSoTimeout(getConnectTimeoutMillis());
        } catch (SocketException e) {
            throw new MinaRuntimeException("can't set socket timeout", e);
        }

        // non blocking
        try {
            clientSocket.configureBlocking(false);
        } catch (IOException e) {
            throw new MinaRuntimeException("can't configure socket as non-blocking", e);
        }

        // apply idle configuration
        // Has to be final, as it's used in a inner class...
        final NioTcpSession session = new NioTcpSession(this, clientSocket, readWriteSelectorPool.getSelectorLoop(),
                idleChecker);
        TcpSessionConfig config = getSessionConfig();

        session.getConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, config.getIdleTimeInMillis(IdleStatus.READ_IDLE));
        session.getConfig().setIdleTimeInMillis(IdleStatus.WRITE_IDLE,
                config.getIdleTimeInMillis(IdleStatus.WRITE_IDLE));

        // apply the default service socket configuration
        Boolean keepAlive = config.isKeepAlive();

        if (keepAlive != null) {
            session.getConfig().setKeepAlive(keepAlive);
        }

        Boolean oobInline = config.isOobInline();

        if (oobInline != null) {
            session.getConfig().setOobInline(oobInline);
        }

        Boolean reuseAddress = config.isReuseAddress();

        if (reuseAddress != null) {
            session.getConfig().setReuseAddress(reuseAddress);
        }

        Boolean tcpNoDelay = config.isTcpNoDelay();

        if (tcpNoDelay != null) {
            session.getConfig().setTcpNoDelay(tcpNoDelay);
        }

        Integer receiveBufferSize = config.getReadBufferSize();

        if (receiveBufferSize != null) {
            session.getConfig().setReadBufferSize(receiveBufferSize);
        } else {
            int rcvBufferSize;
            try {
                rcvBufferSize = clientSocket.socket().getReceiveBufferSize();
            } catch (SocketException e) {
                throw new MinaRuntimeException("can't configure socket receive buffer size", e);
            }
            session.getConfig().setReadBufferSize(rcvBufferSize);
        }

        Integer sendBufferSize = config.getSendBufferSize();

        if (sendBufferSize != null) {
            session.getConfig().setSendBufferSize(sendBufferSize);
        } else {
            int sndBufferSize;
            try {
                sndBufferSize = clientSocket.socket().getSendBufferSize();
            } catch (SocketException e) {
                throw new MinaRuntimeException("can't configure socket send buffe size", e);
            }
            session.getConfig().setSendBufferSize(sndBufferSize);
        }

        Integer trafficClass = config.getTrafficClass();

        if (trafficClass != null) {
            session.getConfig().setTrafficClass(trafficClass);
        }

        Integer soLinger = config.getSoLinger();

        if (soLinger != null) {
            session.getConfig().setSoLinger(soLinger);
        }

        // Set the secured flag if the service is to be used over SSL/TLS
        if (config.isSecured()) {
            session.initSecure(config.getSslContext());
        }

        // connect to a running server. We get an immediate result if
        // the socket is blocking, and either true or false if it's non blocking
        boolean connected;
        try {
            connected = clientSocket.connect(remoteAddress);
        } catch (IOException e) {
            ConnectFuture future = new ConnectFuture();
            future.cannotConnect(e);
            return future;
        }

        ConnectFuture connectFuture = new ConnectFuture();
        session.setConnectFuture(connectFuture);

        if (!connected) {
            // async connection, let's the connection complete in background, the selector loop will detect when the
            // connection is successful
            connectSelectorLoop.register(false, true, false, false, session, clientSocket, new RegistrationCallback() {

                @Override
                public void done(SelectionKey selectionKey) {
                    session.setSelectionKey(selectionKey);
                }
            });
        } else {
            // already connected (probably a loopback connection, or a blocking socket)
            // register for read
            connectSelectorLoop.register(false, false, true, false, session, clientSocket, new RegistrationCallback() {

                @Override
                public void done(SelectionKey selectionKey) {
                    session.setSelectionKey(selectionKey);
                }
            });

            session.setConnected();
        }

        return connectFuture;
    }