/** * Perform write operation(s) on channel which is now ready for * writing */ private void performWrite(SelectionKey key) { if (Thread.currentThread().isInterrupted()) { return; } Connection c = (Connection) key.attachment(); try { c.write(); } catch (IOException e) { log.log(Level.FINE, " write failed", e); try { c.close(); } catch (IOException e2) {// ignore } } }
/** * Complete asynchronous connect operation. <em>Note that * asynchronous connect does not work properly in 1.4, * so you should not use this if you run anything older * than 1.5/5.0</em>. * */ private void performConnect(SelectionKey key) { if (Thread.currentThread().isInterrupted()) { return; } Connection c = (Connection) key.attachment(); try { c.connect(); } catch (IOException e) { log.log(Level.FINE, "connect failed", e); try { c.close(); } catch (IOException e2) { log.log(Level.FINE, "close failed", e); } } }
/** * Perform read operation on channel which is now ready for reading */ private void performRead(SelectionKey key) { if (Thread.currentThread().isInterrupted()) { return; } Connection c = (Connection) key.attachment(); try { c.read(); } catch (IOException e) { log.log(Level.FINE, "read failed", e); try { c.close(); } catch (IOException e2) { log.log(Level.FINE, "close failed", e); } } }
/** * This method is called from the selectLoop() method in order to * process new incoming connections. */ private synchronized void processNewConnections() { synchronized (newConnections) { while (!newConnections.isEmpty()) { Connection conn = newConnections.removeFirst(); try { conn.socketChannel().register(selector, conn.selectOps(), conn); } catch (ClosedChannelException e) { log.log(Level.WARNING, "register channel failed", e); return; } } } }
public void close() { for (Connection c = connectionPool.getConnection(); c != null; c = connectionPool.getConnection()) { try { c.close(); } catch (IOException e) { logWarning("closing", e); } } }
/** * Batch version of modifyInterestOps(). * * @return Returns a <code>this</code> reference for chaining */ public Listener modifyInterestOpsBatch(Connection connection, int op, boolean set) { return modifyInterestOpsBatch( connection.socketChannel().keyFor(selector), op, set); }
channel.register(selector, conn.selectOps(), conn); } catch (java.nio.channels.ClosedChannelException e) { log.log(Level.WARNING, "register channel failed", e);
SocketChannel channel = newConn.socketChannel();
/** * This is the preferred way of modifying interest ops, giving a * Connection rather than a SelectionKey as input. This way the * we can look it up and ensure the correct SelectionKey is always * used. * * @return Returns a <code>this</code> reference for chaining */ public Listener modifyInterestOps(Connection connection, int op, boolean set) { return modifyInterestOps(connection.socketChannel().keyFor(selector), op, set); }