Skip to content

Instantly share code, notes, and snippets.

@clebertsuconic
Created August 12, 2014 20:25
Show Gist options
  • Save clebertsuconic/4a3d472b67f6ff262f58 to your computer and use it in GitHub Desktop.
Save clebertsuconic/4a3d472b67f6ff262f58 to your computer and use it in GitHub Desktop.
private void write(final ChannelHandlerContext ctx) {
synchronized (lock) {
while (true) {
int pending = transport.pending();
if (pending > 0) {
final int size = pending - offset;
if (size > 0) {
ByteBuf buffer = Unpooled.buffer(size);
ByteBuffer head = transport.head();
head.position(offset);
buffer.writeBytes(head);
ChannelFuture chf = ctx.writeAndFlush(buffer);
offset += size;
chf.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture chf) {
if (chf.isSuccess()) {
synchronized (lock) {
transport.pop(size);
offset -= size;
}
write(ctx); // <<<< I don't like this!!!!!
dispatch();
} else {
// ???
}
}
});
} else {
return;
}
} else {
if (pending < 0) {
closeOnFlush(ctx.channel());
}
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment