final int writeSpinCount = config.getWriteSpinCount();
synchronized (session.getWriteLock()) {
session.setInWriteNowLoop(true);
while (true) {
Object obj = session.getCurrentWrite();
SendBuffer buf = null;
if (obj == null) {
obj = writeBuffer.poll();
session.setCurrentWrite(obj);
if (session.getCurrentWrite() == null) {
removeOpWrite = true;
session.setWriteSuspended(false);
break;
}
if (obj == Session.CLOSE_FLAG) {
open = false;
} else {
buf = sendBufferPool.acquire(obj);
session.setCurrentWriteBuffer(buf);
}
} else {
if (obj == Session.CLOSE_FLAG)
open = false;
else
buf = session.getCurrentWriteBuffer();
}
try {
log.debug("0> session is open: {}", open);
if (!open) {
log.debug("receive close flag");
assert buf == null;
session.resetCurrentWriteAndWriteBuffer();
// buf = null;
// obj = null;
clearOpWrite(session);
close(session.getSelectionKey());
break;
}
long localWrittenBytes;
for (int i = writeSpinCount; i > 0; i--) {
localWrittenBytes = buf.transferTo(ch);
if (localWrittenBytes != 0) {
writtenBytes += localWrittenBytes;
break;
}
if (buf.finished()) {
break;
}
}
if (buf.finished()) {
// Successful write - proceed to the next message.
buf.release();
session.resetCurrentWriteAndWriteBuffer();
// obj = null;
// buf = null;
} else {
// Not written fully - perhaps the kernel buffer is
// full.
addOpWrite = true;
session.setWriteSuspended(true);
break;
}
} catch (AsynchronousCloseException e) {
// Doesn't need a user attention - ignore.
} catch (Throwable t) {
buf.release();
session.resetCurrentWriteAndWriteBuffer();
// buf = null;
// obj = null;
eventManager.executeExceptionTask(session, t);
if (t instanceof IOException) {