final int remaining = buffer.remaining();
if (remaining > 0) {
final SocketUDT socket = socketUDT;
final boolean isBlocking = isBlockingMode;
final int sizeSent;
try {
if (isBlocking) {
begin(); // JDK contract for NIO blocking calls
}
if (buffer.isDirect()) {
sizeSent = socket.send(buffer);
} else {
assert buffer.hasArray();
byte[] array = buffer.array();
int position = buffer.position();
int limit = buffer.limit();
sizeSent = socket.send(array, position, limit);
if (0 < sizeSent && sizeSent <= remaining) {
buffer.position(position + sizeSent);
}
}
} finally {
if (isBlocking) {
end(true); // JDK contract for NIO blocking calls
}
}
// see contract for send()
if (sizeSent < 0) {
log.trace("no buffer space for send; socketID={}",
socket.getSocketId());
// logStatus();
// log.info("writeCount={} writeSize={}", writeCount,
// writeSize);
// System.exit(1);
return 0;
}
if (sizeSent == 0) {
log.trace("send timeout; socketID={}", socket.getSocketId());
return 0;
}
if (sizeSent <= remaining) {
// writeSize.addAndGet(sizeSent);
return sizeSent;
} else { // should not happen
log.error("unexpected: sizeSent > remaining; socketID={}",
socket.getSocketId());
return 0;
}
} else {
return 0;