@Override public NioChannel acceptChannel(AsynchronousServerSocketChannel listener) throws IOException { try { return new NioChannel(listener.accept().get()); } catch (Exception e) { throw new IOException(e); } } }
@Override public <A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler) { this.read(dst, 0L, TimeUnit.MILLISECONDS, attachment, handler); }
private synchronized void addToBB(byte[] buf, int offset, int length) throws IOException { while (length > 0) { int thisTime = length; if (socket.getBufHandler().getWriteBuffer().position() == socket.getBufHandler().getWriteBuffer().capacity() || socket.getBufHandler().getWriteBuffer().remaining()==0) { flushBuffer(); } if (thisTime > socket.getBufHandler().getWriteBuffer().remaining()) { thisTime = socket.getBufHandler().getWriteBuffer().remaining(); } socket.getBufHandler().getWriteBuffer().put(buf, offset, thisTime); length = length - thisTime; offset = offset + thisTime; } NioEndpoint.KeyAttachment ka = (NioEndpoint.KeyAttachment)socket.getAttachment(false); if ( ka!= null ) ka.access();//prevent timeouts for just doing client writes }
private int readSocket(byte[] buf, int pos, int n, boolean block) throws IOException { int nRead = 0; socket.getBufHandler().getReadBuffer().clear(); socket.getBufHandler().getReadBuffer().limit(n); if ( block ) { Selector selector = null; NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment)socket.getAttachment(false); if ( att == null ) throw new IOException("Key must be cancelled."); nRead = pool.read(socket.getBufHandler().getReadBuffer(),socket,selector,att.getTimeout()); } catch ( EOFException eof ) { nRead = -1; nRead = socket.read(socket.getBufHandler().getReadBuffer()); socket.getBufHandler().getReadBuffer().flip(); socket.getBufHandler().getReadBuffer().limit(nRead); socket.getBufHandler().getReadBuffer().get(buf, pos, nRead); return nRead; } else if (nRead == -1) {
sc.setSendFile(true); WritableByteChannel wc = ((sc instanceof SecureNioChannel)?sc:sc.getIOChannel()); if (sc.getOutboundRemaining()>0) { if (sc.flushOutbound()) { attachment.access(); if ( sd.length <= 0 && sc.getOutboundRemaining()<=0) { if (log.isDebugEnabled()) { log.debug("Send file complete for:"+sd.fileName); return false; }finally { if (sc!=null) sc.setSendFile(false);
socketProperties.getDirectBuffer()); channel = new NioChannel(socket, bufhandler); channel.setIOChannel(socket); if ( channel instanceof SecureNioChannel ) { SSLEngine engine = createSSLEngine(); ((SecureNioChannel)channel).reset(engine); } else { channel.reset();
SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector()); if ( key == null ) throw new IOException("Key no longer registered"); KeyReference reference = new KeyReference(); while ( (!timedout) && buf.hasRemaining()) { if (keycount > 0) { //only write if we were registered for a write int cnt = socket.write(buf); //write the data if (cnt == -1) throw new EOFException();
int cnt = 0; if ( keycount > 0 ) { //only write if we were registered for a write cnt = socket.write(buf); //write the data if (cnt == -1) throw new EOFException(); if (key==null) key = socket.getIOChannel().register(selector, SelectionKey.OP_WRITE); else key.interestOps(SelectionKey.OP_WRITE); keycount = selector.select(writeTimeout);
@Override public void flush() throws IOException { NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment) nioChannel.getAttachment(false); if (att == null) { throw new IOException("Key must be cancelled"); } long writeTimeout = att.getTimeout(); Selector selector = null; try { selector = pool.get(); } catch ( IOException x ) { //ignore } try { do { if (nioChannel.flush(true, selector, writeTimeout)) { break; } } while (true); } finally { if (selector != null) { pool.put(selector); } } }
socket.getBufHandler().getReadBuffer().clear(); if ( block ) { Selector selector = null; (NioEndpoint.KeyAttachment) socket.getAttachment(false); if (att == null) { throw new IOException("Key must be cancelled."); nRead = pool.read(socket.getBufHandler().getReadBuffer(), socket, selector, socket.getIOChannel().socket().getSoTimeout()); } catch ( EOFException eof ) { nRead = -1; nRead = socket.read(socket.getBufHandler().getReadBuffer()); socket.getBufHandler().getReadBuffer().flip(); socket.getBufHandler().getReadBuffer().limit(nRead); expand(nRead + pos); socket.getBufHandler().getReadBuffer().get(buf, pos, nRead); lastValid = pos + nRead; return nRead;
@Override protected boolean hasMoreDataToFlush() { return (flipped && socket.getBufHandler().getWriteBuffer().remaining()>0) || (!flipped && socket.getBufHandler().getWriteBuffer().position() > 0); }
@Override protected void registerWriteInterest() throws IOException { NioEndpoint.KeyAttachment att = (NioEndpoint.KeyAttachment)socket.getAttachment(false); if (att == null) { throw new IOException("Key must be cancelled"); } att.getPoller().add(socket, SelectionKey.OP_WRITE); }
@Override public void reset() throws IOException { super.reset(); sslEngine = null; sniComplete = false; handshakeComplete = false; closed = false; closing = false; netInBuffer.clear(); }
WritableByteChannel wc = ((sc instanceof SecureNioChannel)?sc:sc.getIOChannel()); if (sc.getOutboundRemaining()>0) { if (sc.flushOutbound()) { socketWrapper.updateLastWrite(); if (sd.length <= 0 && sc.getOutboundRemaining()<=0) { if (log.isDebugEnabled()) { log.debug("Send file complete for: "+sd.fileName);
@Override protected void doClose() throws IOException { channel.close(); } }
int cnt = 0; if ( keycount > 0 ) { //only read if we were registered for a read cnt = socket.read(buf); if (cnt == -1) throw new EOFException(); read += cnt; if (key==null) key = socket.getIOChannel().register(selector, SelectionKey.OP_READ); else key.interestOps(SelectionKey.OP_READ); keycount = selector.select(readTimeout);
private int fillReadBuffer(boolean block, ByteBuffer to) throws IOException { int nRead; NioChannel channel = getSocket(); if (block) { Selector selector = null; try { selector = pool.get(); } catch (IOException x) { // Ignore } try { NioEndpoint.NioSocketWrapper att = (NioEndpoint.NioSocketWrapper) channel .getAttachment(); if (att == null) { throw new IOException("Key must be cancelled."); } nRead = pool.read(to, channel, selector, att.getReadTimeout()); } finally { if (selector != null) { pool.put(selector); } } } else { nRead = channel.read(to); if (nRead == -1) { throw new EOFException(); } } return nRead; }
@Override public <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) { write(src, 0L, TimeUnit.MILLISECONDS, attachment, handler); }
/** * <p> * Tells whether or not this channel is closed. * </p> * * @return <tt>true</tt> if, and only if, this channel is closed */ public boolean isClosed() { return !isOpen(); }