protected void setupSelectorOptions() { // For now, selection keys will either be in OP_READ // or OP_WRITE. So while we are writing a buffer, we // will not be reading. This is way simpler and less // error prone... it can always be changed when everything // else works if we are looking to micro-optimize. // Setup options based on the current state of // the endpoints. This could potentially be more // efficiently done as change requests... or simply // keeping a thread-safe set of endpoints with pending // writes. For most cases, it shouldn't matter. for( Map.Entry<NioEndpoint,SelectionKey> e : endpointKeys.entrySet() ) { if( e.getKey().hasPending() ) { e.getValue().interestOps(SelectionKey.OP_WRITE); } } }
protected void write( SelectionKey key ) throws IOException { NioEndpoint p = (NioEndpoint)key.attachment(); SocketChannel c = (SocketChannel)key.channel(); // We will send what we can and move on. ByteBuffer current = p.peekPending(); if( current == NioEndpoint.CLOSE_MARKER ) { // This connection wants to be closed now closeEndpoint(p); // Nothing more to do return; } c.write( current ); // If we wrote all of that packet then we need to remove it if( current.remaining() == 0 ) { p.removePending(); } // If we happened to empty the pending queue then let's read // again. if( !p.hasPending() ) { key.interestOps( SelectionKey.OP_READ ); } }
protected void setupSelectorOptions() { // For now, selection keys will either be in OP_READ // or OP_WRITE. So while we are writing a buffer, we // will not be reading. This is way simpler and less // error prone... it can always be changed when everything // else works if we are looking to micro-optimize. // Setup options based on the current state of // the endpoints. This could potentially be more // efficiently done as change requests... or simply // keeping a thread-safe set of endpoints with pending // writes. For most cases, it shouldn't matter. for( Map.Entry<NioEndpoint,SelectionKey> e : endpointKeys.entrySet() ) { if( e.getKey().hasPending() ) { e.getValue().interestOps(SelectionKey.OP_WRITE); } } }
protected void setupSelectorOptions() { // For now, selection keys will either be in OP_READ // or OP_WRITE. So while we are writing a buffer, we // will not be reading. This is way simpler and less // error prone... it can always be changed when everything // else works if we are looking to micro-optimize. // Setup options based on the current state of // the endpoints. This could potentially be more // efficiently done as change requests... or simply // keeping a thread-safe set of endpoints with pending // writes. For most cases, it shouldn't matter. for( Map.Entry<NioEndpoint,SelectionKey> e : endpointKeys.entrySet() ) { if( e.getKey().hasPending() ) { e.getValue().interestOps(SelectionKey.OP_WRITE); } } }
protected void write( SelectionKey key ) throws IOException { NioEndpoint p = (NioEndpoint)key.attachment(); SocketChannel c = (SocketChannel)key.channel(); // We will send what we can and move on. ByteBuffer current = p.peekPending(); if( current == NioEndpoint.CLOSE_MARKER ) { // This connection wants to be closed now closeEndpoint(p); // Nothing more to do return; } c.write( current ); // If we wrote all of that packet then we need to remove it if( current.remaining() == 0 ) { p.removePending(); } // If we happened to empty the pending queue then let's read // again. if( !p.hasPending() ) { key.interestOps( SelectionKey.OP_READ ); } }
protected void write( SelectionKey key ) throws IOException { NioEndpoint p = (NioEndpoint)key.attachment(); SocketChannel c = (SocketChannel)key.channel(); // We will send what we can and move on. ByteBuffer current = p.peekPending(); if( current == NioEndpoint.CLOSE_MARKER ) { // This connection wants to be closed now closeEndpoint(p); // Nothing more to do return; } c.write( current ); // If we wrote all of that packet then we need to remove it if( current.remaining() == 0 ) { p.removePending(); } // If we happened to empty the pending queue then let's read // again. if( !p.hasPending() ) { key.interestOps( SelectionKey.OP_READ ); } }