@Override
protected int doWriteMessages(final MessageBuf<Object> messageQueue,
final boolean lastSpin) throws Exception {
// expects a message
final UdtMessage message = (UdtMessage) messageQueue.peek();
final ByteBuf byteBuf = message.data();
final int messageSize = byteBuf.readableBytes();
final long writtenBytes;
if (byteBuf.nioBufferCount() == 1) {
writtenBytes = javaChannel().write(byteBuf.nioBuffer());
} else {
writtenBytes = javaChannel().write(byteBuf.nioBuffers());
}
final SelectionKey key = selectionKey();
final int interestOps = key.interestOps();
// did not write the message
if (writtenBytes <= 0 && messageSize > 0) {
if (lastSpin) {
if ((interestOps & OP_WRITE) == 0) {
key.interestOps(interestOps | OP_WRITE);
}
}
return 0;
}
// wrote message completely
if (writtenBytes != messageSize) {
throw new Error(
"Provider error: failed to write message. Provider library should be upgraded.");
}
// wrote the message queue completely - clear OP_WRITE.
if (messageQueue.isEmpty()) {
if ((interestOps & OP_WRITE) != 0) {
key.interestOps(interestOps & ~OP_WRITE);
}
}
messageQueue.remove();
message.release();
return 1;
}