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);
}
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();
}
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);
}
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();
}
}
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;
}
Aggregations