Search in sources :

Example 86 with SelectionKey

use of java.nio.channels.SelectionKey in project zm-mailbox by Zimbra.

the class ZimbraSocketAcceptor method accept.

@Override
protected NioSession accept(IoProcessor<NioSession> processor, ServerSocketChannel handle) throws Exception {
    SelectionKey key = handle.keyFor(selector);
    if ((key == null) || (!key.isValid()) || (!key.isAcceptable())) {
        return null;
    }
    // accept the connection from the client
    SocketChannel ch = handle.accept();
    if (ch == null) {
        return null;
    }
    return new NioSocketSession(this, processor, ch);
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel)

Example 87 with SelectionKey

use of java.nio.channels.SelectionKey in project zm-mailbox by Zimbra.

the class ZimbraSocketAcceptor method close.

@Override
protected void close(ServerSocketChannel handle) throws Exception {
    SelectionKey key = handle.keyFor(selector);
    if (key != null) {
        key.cancel();
    }
    handle.close();
}
Also used : SelectionKey(java.nio.channels.SelectionKey)

Example 88 with SelectionKey

use of java.nio.channels.SelectionKey in project cloudstack by apache.

the class NioClient method init.

@Override
protected void init() throws IOException {
    _selector = Selector.open();
    Task task = null;
    try {
        _clientConnection = SocketChannel.open();
        s_logger.info("Connecting to " + _host + ":" + _port);
        final InetSocketAddress peerAddr = new InetSocketAddress(_host, _port);
        _clientConnection.connect(peerAddr);
        _clientConnection.configureBlocking(false);
        final SSLContext sslContext = Link.initSSLContext(true);
        SSLEngine sslEngine = sslContext.createSSLEngine(_host, _port);
        sslEngine.setUseClientMode(true);
        sslEngine.setEnabledProtocols(SSLUtils.getSupportedProtocols(sslEngine.getEnabledProtocols()));
        sslEngine.beginHandshake();
        if (!Link.doHandshake(_clientConnection, sslEngine, true)) {
            s_logger.error("SSL Handshake failed while connecting to host: " + _host + " port: " + _port);
            _selector.close();
            throw new IOException("SSL Handshake failed while connecting to host: " + _host + " port: " + _port);
        }
        s_logger.info("SSL: Handshake done");
        s_logger.info("Connected to " + _host + ":" + _port);
        final Link link = new Link(peerAddr, this);
        link.setSSLEngine(sslEngine);
        final SelectionKey key = _clientConnection.register(_selector, SelectionKey.OP_READ);
        link.setKey(key);
        key.attach(link);
        // Notice we've already connected due to the handshake, so let's get the
        // remaining task done
        task = _factory.create(Task.Type.CONNECT, link, null);
    } catch (final GeneralSecurityException e) {
        _selector.close();
        throw new IOException("Failed to initialise security", e);
    } catch (final IOException e) {
        _selector.close();
        throw e;
    }
    _executor.submit(task);
}
Also used : SelectionKey(java.nio.channels.SelectionKey) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) GeneralSecurityException(java.security.GeneralSecurityException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException)

Example 89 with SelectionKey

use of java.nio.channels.SelectionKey in project aerospike-client-java by aerospike.

the class NioEventLoop method runCommands.

private void runCommands() throws Exception {
    registerCommands();
    runScheduled();
    awakened.set(false);
    selector.select(selectorTimeout);
    if (awakened.get()) {
        selector.wakeup();
    }
    final Set<SelectionKey> keys = selector.selectedKeys();
    if (keys.isEmpty()) {
        return;
    }
    try {
        final Iterator<SelectionKey> iter = keys.iterator();
        while (iter.hasNext()) {
            final SelectionKey key = iter.next();
            if (!key.isValid()) {
                continue;
            }
            processKey(key);
        }
    } finally {
        keys.clear();
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey)

Example 90 with SelectionKey

use of java.nio.channels.SelectionKey in project Aeron by real-logic.

the class ControlTransportPoller method registerForRead.

public SelectionKey registerForRead(final SendChannelEndpoint transport) {
    SelectionKey key = null;
    try {
        transports = ArrayUtil.add(transports, transport);
        key = transport.receiveDatagramChannel().register(selector, SelectionKey.OP_READ, transport);
    } catch (final ClosedChannelException ex) {
        LangUtil.rethrowUnchecked(ex);
    }
    return key;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException)

Aggregations

SelectionKey (java.nio.channels.SelectionKey)202 IOException (java.io.IOException)93 SocketChannel (java.nio.channels.SocketChannel)59 Selector (java.nio.channels.Selector)44 ServerSocketChannel (java.nio.channels.ServerSocketChannel)41 ClosedChannelException (java.nio.channels.ClosedChannelException)30 InetSocketAddress (java.net.InetSocketAddress)27 CancelledKeyException (java.nio.channels.CancelledKeyException)25 ByteBuffer (java.nio.ByteBuffer)24 ClosedSelectorException (java.nio.channels.ClosedSelectorException)17 SelectableChannel (java.nio.channels.SelectableChannel)13 Test (org.junit.Test)13 Iterator (java.util.Iterator)9 Socket (java.net.Socket)7 ArrayList (java.util.ArrayList)7 SocketTimeoutException (java.net.SocketTimeoutException)6 AbstractSelectionKey (java.nio.channels.spi.AbstractSelectionKey)6 EOFException (java.io.EOFException)5 DatagramChannel (java.nio.channels.DatagramChannel)5 HashSet (java.util.HashSet)5