[#952] Allow to switch to single message decoding mode on the fly

This commit is contained in:
Norman Maurer 2013-01-18 08:20:27 +01:00
parent 4dd462d0b5
commit f2d84f75d6
3 changed files with 51 additions and 1 deletions

View File

@ -44,6 +44,28 @@ import io.netty.channel.ChannelInboundByteHandlerAdapter;
*/
public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter {
private volatile boolean singleDecode;
/**
* If set then only one message is decoded on each {@link #inboundBufferUpdated(ChannelHandlerContext)} call.
* This may be useful if you need to do some protocol upgrade and want to make sure nothing is mixed up.
*
* Default is {@code false} as this has performance impacts.
*/
public void setSingleDecode(boolean singleDecode) {
this.singleDecode = singleDecode;
}
/**
* If {@code true} then only one message is decoded on each
* {@link #inboundBufferUpdated(ChannelHandlerContext)} call.
*
* Default is {@code false} as this has performance impacts.
*/
public boolean isSingleDecode() {
return singleDecode;
}
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
callDecode(ctx, in, ctx.nextInboundByteBuffer());
@ -91,7 +113,7 @@ public abstract class ByteToByteDecoder extends ChannelInboundByteHandlerAdapter
ctx.fireExceptionCaught(new DecoderException(t));
}
}
if (oldInSize == in.readableBytes()) {
if (oldInSize == in.readableBytes() || isSingleDecode()) {
break;
}
}

View File

@ -44,6 +44,28 @@ public abstract class ByteToMessageDecoder
private ChannelHandlerContext ctx;
private volatile boolean singleDecode;
/**
* If set then only one message is decoded on each {@link #inboundBufferUpdated(ChannelHandlerContext)} call.
* This may be useful if you need to do some protocol upgrade and want to make sure nothing is mixed up.
*
* Default is {@code false} as this has performance impacts.
*/
public void setSingleDecode(boolean singleDecode) {
this.singleDecode = singleDecode;
}
/**
* If {@code true} then only one message is decoded on each
* {@link #inboundBufferUpdated(ChannelHandlerContext)} call.
*
* Default is {@code false} as this has performance impacts.
*/
public boolean isSingleDecode() {
return singleDecode;
}
@Override
public void beforeAdd(ChannelHandlerContext ctx) throws Exception {
this.ctx = ctx;
@ -109,6 +131,9 @@ public abstract class ByteToMessageDecoder
if (ChannelHandlerUtil.unfoldAndAdd(ctx, o, true)) {
decoded = true;
if (isSingleDecode()) {
break;
}
} else {
break;
}

View File

@ -432,6 +432,9 @@ public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
// A successful decode
if (ChannelHandlerUtil.unfoldAndAdd(ctx, result, true)) {
decoded = true;
if (isSingleDecode()) {
break;
}
}
} catch (Throwable t) {
if (decoded) {