do {
// get a write request from the queue. We left it in the queue,
// just in case we can't write all of the message content into
// the channel : we will have to retrieve the message later
final WriteRequest writeRequest = writeQueue.peek();
if (writeRequest == null) {
// Nothing to write : we are done
break;
}
// The message is necessarily a ByteBuffer at this point
ByteBuffer buf = (ByteBuffer) writeRequest.getMessage();
// Note that if the connection is secured, the buffer
// already contains encrypted data.
// Try to write the data, and get back the number of bytes
// actually written
int written = ((SocketChannel) channel).write(buf);
if (IS_DEBUG) {
LOG.debug("wrote {} bytes to {}", written, this);
}
if (written > 0) {
incrementWrittenBytes(written);
}
// Update the idle status for this session
idleChecker.sessionWritten(this, System.currentTimeMillis());
// Ok, we may not have written everything. Check that.
if (buf.remaining() == 0) {
// completed write request, let's remove it (we use poll() instead
// of remove(), because remove() may throw an exception if the
// queue is empty.
writeQueue.poll();
// complete the future if we have one (we should...)
final DefaultWriteFuture future = (DefaultWriteFuture) writeRequest.getFuture();
if (future != null) {
future.complete();
}