Search in sources :

Example 41 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 42 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 43 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 44 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)

Example 45 with SocketChannel

use of java.nio.channels.SocketChannel in project http-kit by http-kit.

the class HttpClient method doWrite.

private void doWrite(SelectionKey key) {
    Request req = (Request) key.attachment();
    SocketChannel ch = (SocketChannel) key.channel();
    try {
        if (req instanceof HttpsRequest) {
            HttpsRequest httpsReq = (HttpsRequest) req;
            if (httpsReq.handshaken) {
                // will flip to OP_READ
                httpsReq.writeWrappedRequest();
            } else {
                buffer.clear();
                if (httpsReq.doHandshake(buffer) < 0) {
                    // will be a No status exception
                    req.finish();
                }
            }
        } else {
            ByteBuffer[] buffers = req.request;
            ch.write(buffers);
            if (!buffers[buffers.length - 1].hasRemaining()) {
                key.interestOps(OP_READ);
            }
        }
    } catch (IOException e) {
        if (!cleanAndRetryIfBroken(key, req)) {
            req.finish(e);
        }
    } catch (Exception e) {
        // rarely happen
        req.finish(e);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ProtocolException(org.httpkit.ProtocolException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

SocketChannel (java.nio.channels.SocketChannel)759 ServerSocketChannel (java.nio.channels.ServerSocketChannel)337 IOException (java.io.IOException)321 InetSocketAddress (java.net.InetSocketAddress)228 ByteBuffer (java.nio.ByteBuffer)188 SelectionKey (java.nio.channels.SelectionKey)126 Socket (java.net.Socket)101 Test (org.junit.Test)87 ClosedChannelException (java.nio.channels.ClosedChannelException)63 ServerSocket (java.net.ServerSocket)49 Selector (java.nio.channels.Selector)48 SocketAddress (java.net.SocketAddress)36 ClosedSelectorException (java.nio.channels.ClosedSelectorException)33 ConnectException (java.net.ConnectException)27 CancelledKeyException (java.nio.channels.CancelledKeyException)27 ArrayList (java.util.ArrayList)27 SocketTimeoutException (java.net.SocketTimeoutException)25 SelectableChannel (java.nio.channels.SelectableChannel)23 HashMap (java.util.HashMap)23 OutputStream (java.io.OutputStream)22