// Loop until told to stop. When stop was requested, loop until
// the queue is served.
while (!stop || (stop && sendQueue.size() > 0)) {
try {
// Wait for two minutes for a message to send
PeerMessage message = sendQueue.poll(
PeerExchange.KEEP_ALIVE_IDLE_MINUTES,
TimeUnit.MINUTES);
if (message == null) {
if (stop) {
return;
}
message = PeerMessage.KeepAliveMessage.craft();
}
logger.trace("Sending {} to {}", message, peer);
ByteBuffer data = message.getData();
long size = 0;
while (!stop && data.hasRemaining()) {
int written = channel.write(data);
size += written;
if (written < 0) {