Search in sources :

Example 46 with SocketChannel

use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.

the class NioConnection method closeConnection.

protected void closeConnection(SelectionKey key) {
    if (key != null) {
        SocketChannel channel = (SocketChannel) key.channel();
        key.cancel();
        try {
            if (channel != null) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Closing socket " + channel.socket());
                }
                channel.close();
            }
        } catch (IOException ignore) {
        }
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) IOException(java.io.IOException)

Example 47 with SocketChannel

use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.

the class NioConnection method logTrace.

protected void logTrace(Exception e, SelectionKey key, int loc) {
    if (s_logger.isTraceEnabled()) {
        Socket socket = null;
        if (key != null) {
            SocketChannel ch = (SocketChannel) key.channel();
            if (ch != null) {
                socket = ch.socket();
            }
        }
        s_logger.trace("Location " + loc + ": Socket " + socket + " closed on read.  Probably -1 returned.");
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) Socket(java.net.Socket)

Example 48 with SocketChannel

use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.

the class NioConnection method connect.

protected void connect(SelectionKey key) throws IOException {
    SocketChannel socketChannel = (SocketChannel) key.channel();
    try {
        socketChannel.finishConnect();
        key.interestOps(SelectionKey.OP_READ);
        Socket socket = socketChannel.socket();
        if (!socket.getKeepAlive()) {
            socket.setKeepAlive(true);
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Connected to " + socket);
        }
        Link link = new Link((InetSocketAddress) socket.getRemoteSocketAddress(), this);
        link.setKey(key);
        key.attach(link);
        Task task = _factory.create(Task.Type.CONNECT, link, null);
        _executor.execute(task);
    } catch (IOException e) {
        logTrace(e, key, 2);
        terminate(key);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) IOException(java.io.IOException) Socket(java.net.Socket)

Example 49 with SocketChannel

use of java.nio.channels.SocketChannel in project CloudStack-archive by CloudStack-extras.

the class NioConnection method logDebug.

protected void logDebug(Exception e, SelectionKey key, int loc) {
    if (s_logger.isDebugEnabled()) {
        Socket socket = null;
        if (key != null) {
            SocketChannel ch = (SocketChannel) key.channel();
            if (ch != null) {
                socket = ch.socket();
            }
        }
        s_logger.debug("Location " + loc + ": Socket " + socket + " closed on read.  Probably -1 returned: " + e.getMessage());
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) Socket(java.net.Socket)

Example 50 with SocketChannel

use of java.nio.channels.SocketChannel in project jeromq by zeromq.

the class TcpListener method acceptEvent.

@Override
public void acceptEvent() {
    SocketChannel fd = null;
    try {
        fd = accept();
        Utils.tuneTcpSocket(fd);
        Utils.tuneTcpKeepalives(fd, options.tcpKeepAlive, options.tcpKeepAliveCnt, options.tcpKeepAliveIdle, options.tcpKeepAliveIntvl);
    } catch (IOException e) {
        //  If connection was reset by the peer in the meantime, just ignore it.
        //  TODO: Handle specific errors like ENFILE/EMFILE etc.
        socket.eventAcceptFailed(endpoint, ZError.exccode(e));
        return;
    }
    //  Create the engine object for this connection.
    StreamEngine engine = null;
    try {
        engine = new StreamEngine(fd, options, endpoint);
    } catch (ZError.InstantiationException e) {
        socket.eventAcceptFailed(endpoint, ZError.EINVAL);
        return;
    }
    //  Choose I/O thread to run connecter in. Given that we are already
    //  running in an I/O thread, there must be at least one available.
    IOThread ioThread = chooseIoThread(options.affinity);
    //  Create and launch a session object.
    SessionBase session = SessionBase.create(ioThread, false, socket, options, new Address(fd.socket().getRemoteSocketAddress()));
    session.incSeqnum();
    launchChild(session);
    sendAttach(session, engine, false);
    socket.eventAccepted(endpoint, fd);
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) IOException(java.io.IOException)

Aggregations

SocketChannel (java.nio.channels.SocketChannel)662 IOException (java.io.IOException)298 ServerSocketChannel (java.nio.channels.ServerSocketChannel)285 InetSocketAddress (java.net.InetSocketAddress)202 ByteBuffer (java.nio.ByteBuffer)163 SelectionKey (java.nio.channels.SelectionKey)105 Socket (java.net.Socket)87 Test (org.junit.Test)81 ClosedChannelException (java.nio.channels.ClosedChannelException)50 Selector (java.nio.channels.Selector)42 SocketAddress (java.net.SocketAddress)35 ServerSocket (java.net.ServerSocket)31 ClosedSelectorException (java.nio.channels.ClosedSelectorException)28 CancelledKeyException (java.nio.channels.CancelledKeyException)27 ConnectException (java.net.ConnectException)26 ArrayList (java.util.ArrayList)26 SocketTimeoutException (java.net.SocketTimeoutException)23 SelectableChannel (java.nio.channels.SelectableChannel)21 SocketException (java.net.SocketException)20 HashMap (java.util.HashMap)20