Reduced code duplication of AbstractXnioChannelHandler
This commit is contained in:
parent
880241366b
commit
4fa8e3adfc
@ -109,16 +109,13 @@ public abstract class AbstractXnioChannelHandler implements IoHandler<java.nio.c
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void handleWritable(java.nio.channels.Channel channel) {
|
public void handleWritable(java.nio.channels.Channel channel) {
|
||||||
// TODO Code cleanup & optimization
|
|
||||||
BaseXnioChannel c = XnioChannelRegistry.getChannel(channel);
|
BaseXnioChannel c = XnioChannelRegistry.getChannel(channel);
|
||||||
if (channel instanceof GatheringByteChannel) {
|
int writtenBytes = 0;
|
||||||
boolean open = true;
|
boolean open = true;
|
||||||
boolean addOpWrite = false;
|
boolean addOpWrite = false;
|
||||||
|
|
||||||
MessageEvent evt;
|
MessageEvent evt;
|
||||||
ChannelBuffer buf;
|
ChannelBuffer buf;
|
||||||
int bufIdx;
|
int bufIdx;
|
||||||
int writtenBytes = 0;
|
|
||||||
|
|
||||||
Queue<MessageEvent> writeBuffer = c.writeBuffer;
|
Queue<MessageEvent> writeBuffer = c.writeBuffer;
|
||||||
synchronized (c.writeLock) {
|
synchronized (c.writeLock) {
|
||||||
@ -140,7 +137,9 @@ public abstract class AbstractXnioChannelHandler implements IoHandler<java.nio.c
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
final int writeSpinCount = c.getConfig().getWriteSpinCount();
|
final int writeSpinCount = c.getConfig().getWriteSpinCount();
|
||||||
|
boolean sent = false;
|
||||||
for (int i = writeSpinCount; i > 0; i --) {
|
for (int i = writeSpinCount; i > 0; i --) {
|
||||||
|
if (channel instanceof GatheringByteChannel) {
|
||||||
int localWrittenBytes = buf.getBytes(
|
int localWrittenBytes = buf.getBytes(
|
||||||
bufIdx,
|
bufIdx,
|
||||||
(GatheringByteChannel) channel,
|
(GatheringByteChannel) channel,
|
||||||
@ -151,148 +150,20 @@ public abstract class AbstractXnioChannelHandler implements IoHandler<java.nio.c
|
|||||||
writtenBytes += localWrittenBytes;
|
writtenBytes += localWrittenBytes;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (bufIdx == buf.writerIndex()) {
|
|
||||||
// Successful write - proceed to the next message.
|
|
||||||
evt.getFuture().setSuccess();
|
|
||||||
evt = null;
|
|
||||||
} else {
|
|
||||||
// Not written fully - perhaps the kernel buffer is full.
|
|
||||||
c.currentWriteEvent = evt;
|
|
||||||
c.currentWriteIndex = bufIdx;
|
|
||||||
addOpWrite = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (AsynchronousCloseException e) {
|
|
||||||
// Doesn't need a user attention - ignore.
|
|
||||||
} catch (Throwable t) {
|
|
||||||
evt.getFuture().setFailure(t);
|
|
||||||
evt = null;
|
|
||||||
fireExceptionCaught(c, t);
|
|
||||||
if (t instanceof IOException) {
|
|
||||||
open = false;
|
|
||||||
c.closeNow(succeededFuture(c));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fireWriteComplete(c, writtenBytes);
|
|
||||||
|
|
||||||
if (open) {
|
|
||||||
if (addOpWrite && channel instanceof SuspendableWriteChannel) {
|
|
||||||
((SuspendableWriteChannel) channel).resumeWrites();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (channel instanceof MultipointWritableMessageChannel) {
|
} else if (channel instanceof MultipointWritableMessageChannel) {
|
||||||
boolean open = true;
|
ByteBuffer nioBuf = buf.toByteBuffer(bufIdx, buf.writerIndex() - bufIdx);
|
||||||
boolean addOpWrite = false;
|
int nioBufSize = nioBuf.remaining();
|
||||||
|
SocketAddress remoteAddress = evt.getRemoteAddress();
|
||||||
MessageEvent evt;
|
|
||||||
ChannelBuffer buf;
|
|
||||||
int bufIdx;
|
|
||||||
int writtenBytes = 0;
|
|
||||||
SocketAddress remoteAddress;
|
|
||||||
|
|
||||||
Queue<MessageEvent> writeBuffer = c.writeBuffer;
|
|
||||||
synchronized (c.writeLock) {
|
|
||||||
evt = c.currentWriteEvent;
|
|
||||||
for (;;) {
|
|
||||||
if (evt == null) {
|
|
||||||
evt = writeBuffer.poll();
|
|
||||||
if (evt == null) {
|
|
||||||
c.currentWriteEvent = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = (ChannelBuffer) evt.getMessage();
|
|
||||||
bufIdx = buf.readerIndex();
|
|
||||||
} else {
|
|
||||||
buf = (ChannelBuffer) evt.getMessage();
|
|
||||||
bufIdx = c.currentWriteIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
remoteAddress = evt.getRemoteAddress();
|
|
||||||
if (remoteAddress == null) {
|
if (remoteAddress == null) {
|
||||||
remoteAddress = c.getRemoteAddress();
|
remoteAddress = c.getRemoteAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
final int writeSpinCount = c.getConfig().getWriteSpinCount();
|
|
||||||
boolean sent = false;
|
|
||||||
for (int i = writeSpinCount; i > 0; i --) {
|
|
||||||
ByteBuffer nioBuf = buf.toByteBuffer(bufIdx, buf.writerIndex() - bufIdx);
|
|
||||||
int nioBufSize = nioBuf.remaining();
|
|
||||||
sent = ((MultipointWritableMessageChannel) channel).send(remoteAddress, nioBuf);
|
sent = ((MultipointWritableMessageChannel) channel).send(remoteAddress, nioBuf);
|
||||||
if (sent) {
|
if (sent) {
|
||||||
bufIdx += nioBufSize;
|
bufIdx += nioBufSize;
|
||||||
writtenBytes += nioBufSize;
|
writtenBytes += nioBufSize;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (sent) {
|
|
||||||
// Successful write - proceed to the next message.
|
|
||||||
evt.getFuture().setSuccess();
|
|
||||||
evt = null;
|
|
||||||
} else {
|
|
||||||
// Not written fully - perhaps the kernel buffer is full.
|
|
||||||
c.currentWriteEvent = evt;
|
|
||||||
c.currentWriteIndex = bufIdx;
|
|
||||||
addOpWrite = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (AsynchronousCloseException e) {
|
|
||||||
// Doesn't need a user attention - ignore.
|
|
||||||
} catch (Throwable t) {
|
|
||||||
evt.getFuture().setFailure(t);
|
|
||||||
evt = null;
|
|
||||||
fireExceptionCaught(c, t);
|
|
||||||
if (t instanceof IOException) {
|
|
||||||
open = false;
|
|
||||||
c.closeNow(succeededFuture(c));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fireWriteComplete(c, writtenBytes);
|
|
||||||
|
|
||||||
if (open && addOpWrite) {
|
|
||||||
((SuspendableWriteChannel) channel).resumeWrites();
|
|
||||||
}
|
|
||||||
} else if (channel instanceof WritableMessageChannel) {
|
} else if (channel instanceof WritableMessageChannel) {
|
||||||
boolean open = true;
|
|
||||||
boolean addOpWrite = false;
|
|
||||||
|
|
||||||
MessageEvent evt;
|
|
||||||
ChannelBuffer buf;
|
|
||||||
int bufIdx;
|
|
||||||
int writtenBytes = 0;
|
|
||||||
|
|
||||||
Queue<MessageEvent> writeBuffer = c.writeBuffer;
|
|
||||||
synchronized (c.writeLock) {
|
|
||||||
evt = c.currentWriteEvent;
|
|
||||||
for (;;) {
|
|
||||||
if (evt == null) {
|
|
||||||
evt = writeBuffer.poll();
|
|
||||||
if (evt == null) {
|
|
||||||
c.currentWriteEvent = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = (ChannelBuffer) evt.getMessage();
|
|
||||||
bufIdx = buf.readerIndex();
|
|
||||||
} else {
|
|
||||||
buf = (ChannelBuffer) evt.getMessage();
|
|
||||||
bufIdx = c.currentWriteIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
final int writeSpinCount = c.getConfig().getWriteSpinCount();
|
|
||||||
boolean sent = false;
|
|
||||||
for (int i = writeSpinCount; i > 0; i --) {
|
|
||||||
ByteBuffer nioBuf = buf.toByteBuffer(bufIdx, buf.writerIndex() - bufIdx);
|
ByteBuffer nioBuf = buf.toByteBuffer(bufIdx, buf.writerIndex() - bufIdx);
|
||||||
int nioBufSize = nioBuf.remaining();
|
int nioBufSize = nioBuf.remaining();
|
||||||
sent = ((WritableMessageChannel) channel).send(nioBuf);
|
sent = ((WritableMessageChannel) channel).send(nioBuf);
|
||||||
@ -301,9 +172,12 @@ public abstract class AbstractXnioChannelHandler implements IoHandler<java.nio.c
|
|||||||
writtenBytes += nioBufSize;
|
writtenBytes += nioBufSize;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unsupported channel type: " + channel.getClass().getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sent) {
|
if (bufIdx == buf.writerIndex() || sent) {
|
||||||
// Successful write - proceed to the next message.
|
// Successful write - proceed to the next message.
|
||||||
evt.getFuture().setSuccess();
|
evt.getFuture().setSuccess();
|
||||||
evt = null;
|
evt = null;
|
||||||
@ -328,9 +202,12 @@ public abstract class AbstractXnioChannelHandler implements IoHandler<java.nio.c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (writtenBytes > 0) {
|
||||||
fireWriteComplete(c, writtenBytes);
|
fireWriteComplete(c, writtenBytes);
|
||||||
|
}
|
||||||
|
|
||||||
if (open && addOpWrite) {
|
if (open) {
|
||||||
|
if (addOpWrite && channel instanceof SuspendableWriteChannel) {
|
||||||
((SuspendableWriteChannel) channel).resumeWrites();
|
((SuspendableWriteChannel) channel).resumeWrites();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user