protected void cancel( SelectionKey key, SocketChannel c ) throws IOException { NioEndpoint p = (NioEndpoint)key.attachment(); log.log( Level.FINE, "Closing channel endpoint:{0}.", p ); Object o = endpointKeys.remove(p); log.log( Level.FINE, "Endpoint keys size:{0}", endpointKeys.size() ); key.cancel(); c.close(); removeEndpoint( p, c ); }
@Override public void run() { final Selector selector = this.selector; for (;;) { ++acceptCount; try { selector.select(1000L); Set<SelectionKey> keys = selector.selectedKeys(); try { for (SelectionKey key : keys) { if (key.isValid() && key.isAcceptable()) { accept(); } else { key.cancel(); } } } finally { keys.clear(); } } catch (Throwable e) { LOGGER.warn(getName(), e); } } }
@Override public void process(SelectionKey key) throws IOException { final Object attachment = key.attachment(); final SelectableChannel channel = key.channel(); if (attachment == null) { key.cancel(); return; } if (!(attachment instanceof ReadAttachment)) { key.cancel(); return; } if (!(channel instanceof SocketChannel)) { key.cancel(); return; } final SocketChannel socketChannel = (SocketChannel) channel; final ReadAttachment readAttachment = (ReadAttachment) attachment; logger.trace("drop invalid key {}", channel); readAttachment.getConnectionListener().onError(socketChannel, new CancelledKeyException()); }
@Override public void run() { if (key != null && key.isValid()) { SocketChannel channel = (SocketChannel) key.channel(); Beat beat = (Beat) key.attachment(); if (channel.isConnected()) { return; } try { channel.finishConnect(); } catch (Exception ignore) { } try { beat.finishCheck(false, false, beat.getTask().getCheckRTNormalized() * 2, "tcp:timeout"); key.cancel(); key.channel().close(); } catch (Exception ignore) { } } } }
protected void cancel( NioEndpoint p ) throws IOException { SelectionKey key = endpointKeys.remove(p); if( key == null ) { //log.log( Level.FINE, "Endpoint already closed:{0}.", p ); return; // already closed it } log.log( Level.FINE, "Endpoint keys size:{0}", endpointKeys.size() ); log.log( Level.FINE, "Closing endpoint:{0}.", p ); SocketChannel c = (SocketChannel)key.channel(); // Note: key.cancel() is specifically thread safe. One of // the few things one can do with a key from another // thread. key.cancel(); c.close(); removeEndpoint( p, c ); }
key.interestOps(SelectionKey.OP_READ); int keyCount = selector.select(this.getSoTimeout()); if (keyCount == 0 && this.getSoTimeout() > 0 && ((System.currentTimeMillis() - now) >= this.getSoTimeout())) { throw new SocketTimeoutException("Timeout during handshake"); if (key!=null) try {key.cancel();} catch (Exception ignore) {} if (selector!=null) try {selector.close();} catch (Exception ignore) {}
SelectionKey k = i.next(); i.remove(); NioServerSocketChannel channel = (NioServerSocketChannel) k.attachment(); k.cancel(); channel.close(); } catch (SocketTimeoutException e) {
sk.cancel(); this.selector.selectNow(); // clear the cancelled key SelectionKey tmpsk = sc.getSelectableChannel().register(this.tmpSel, if (events == 0) { logger.info("stuck selection key detected on {}", sc); tmpsk.cancel(); if (tmpsk.isValid() && tmpsk.isReadable()) { tmpsk.cancel(); this.tmpSel.selectNow(); // clear canceled key this.selectorRegistrations.remove(sc); } else if (tmpsk.isValid() && tmpsk.isWritable()) { tmpsk.cancel(); this.tmpSel.selectNow(); // clear canceled key sc.registerWithSelector2(this.selector); } else if (!tmpsk.isValid()) { tmpsk.cancel(); this.tmpSel.selectNow(); // clear canceled key sc.registerWithSelector2(this.selector); logger.warn("Unexpected Exception:", ex); try { tmpsk.cancel();
private void processSelectedKeys(Set<SelectionKey> selectedKeys) { // check if the set is empty and if so just return to not create garbage by // creating a new Iterator every time even if there is nothing to process. // See https://github.com/netty/netty/issues/597 if (selectedKeys.isEmpty()) { return; } for (Iterator<SelectionKey> i = selectedKeys.iterator(); i.hasNext();) { SelectionKey k = i.next(); i.remove(); if (!k.isValid()) { close(k); continue; } try { if (k.isConnectable()) { connect(k); } } catch (Throwable t) { NioClientSocketChannel ch = (NioClientSocketChannel) k.attachment(); ch.connectFuture.setFailure(t); fireExceptionCaught(ch, t); k.cancel(); // Some JDK implementations run into an infinite loop without this. ch.worker.close(ch, succeededFuture(ch)); } } }
void cancelKey(final SelectionKey key, final boolean block) { assert key.selector() == selector; final SelectableChannel channel = key.channel(); if (currentThread() == this) { log.logf(FQCN, Logger.Level.TRACE, null, "Cancelling key %s of %s (same thread)", key, channel); try { key.cancel(); try { selector.selectNow(); key.cancel(); } catch (Throwable t) { log.logf(FQCN, Logger.Level.TRACE, t, "Error cancelling key %s of %s (same thread, old locking)", key, channel); log.logf(FQCN, Logger.Level.TRACE, null, "Cancelling key %s of %s (other thread)", key, channel); try { key.cancel(); if (block) { final SelectNowTask task = new SelectNowTask();
@Override public void run() { final Selector selector = this.selector; for (;;) { ++connectCount; try { selector.select(1000L); connect(selector); Set<SelectionKey> keys = selector.selectedKeys(); try { for (SelectionKey key : keys) { Object att = key.attachment(); if (att != null && key.isValid() && key.isConnectable()) { finishConnect(key, att); } else { key.cancel(); } } } finally { keys.clear(); } } catch (Throwable e) { LOGGER.warn(name, e); } } }
@Override public void process(SelectionKey key) throws IOException { SelectableChannel channel = key.channel(); if (!(channel instanceof SocketChannel)) { logger.warn("incorrect instance of channel. The key is cancelled"); key.cancel(); return; } SocketChannel socketChannel = (SocketChannel) channel; logger.trace("server {} get new data from {}", myServerSocketLocalAddress, socketChannel); Object attachment = key.attachment(); if (!(attachment instanceof ReadAttachment)) { logger.warn("incorrect instance of attachment for channel {}", new Object[]{socketChannel.socket()}); socketChannel.close(); return; } ConnectionListener connectionListener = ((ReadAttachment) attachment).getConnectionListener(); connectionListener.onNewDataAvailable(socketChannel); }
@Override public void run() { Beat beat = (Beat) key.attachment(); SocketChannel channel = (SocketChannel) key.channel(); try { if (!beat.isValid()) { key.cancel(); key.channel().close(); if (key.isValid() && key.isConnectable()) { if (key.isValid() && key.isReadable()) { key.cancel(); key.channel().close(); } else { key.cancel(); key.channel().close(); } catch (Exception ignore) {