}
}
private void flush( DatagramSession session ) throws IOException
{
DatagramChannel ch = session.getChannel();
Queue writeBufferQueue = session.getWriteBufferQueue();
Queue writeMarkerQueue = session.getWriteMarkerQueue();
ByteBuffer buf;
Object marker;
for( ;; )
{
synchronized( writeBufferQueue )
{
buf = ( ByteBuffer ) writeBufferQueue.first();
marker = writeMarkerQueue.first();
}
if( buf == null )
break;
if( buf.remaining() == 0 )
{
// pop and fire event
synchronized( writeBufferQueue )
{
writeBufferQueue.pop();
writeMarkerQueue.pop();
}
try
{
buf.release();
}
catch( IllegalStateException e )
{
session.getManagerFilterChain().exceptionCaught( session, e );
}
session.increaseWrittenWriteRequests();
session.getManagerFilterChain().dataWritten( session, marker );
continue;
}
SelectionKey key = session.getSelectionKey();
if( key == null )
{
scheduleFlush( session );
break;
}
if( !key.isValid() )
{
continue;
}
int writtenBytes = ch
.send( buf.buf(), session.getRemoteAddress() );
if( writtenBytes == 0 )
{
// Kernel buffer is full