From 64351ad7d603d1604a51cecbf446524f6e7ad158 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 27 Dec 2012 23:08:54 +0100 Subject: [PATCH] [#861] Let SctpMessageCompletionHandler fire inboundMessageBufferUpdated() only if needed --- .../sctp/SctpMessageCompletionHandler.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/sctp/SctpMessageCompletionHandler.java b/codec/src/main/java/io/netty/handler/codec/sctp/SctpMessageCompletionHandler.java index 3f4f8fcf99..5b35ed505a 100644 --- a/codec/src/main/java/io/netty/handler/codec/sctp/SctpMessageCompletionHandler.java +++ b/codec/src/main/java/io/netty/handler/codec/sctp/SctpMessageCompletionHandler.java @@ -19,14 +19,36 @@ package io.netty.handler.codec.sctp; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundMessageHandler; import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.channel.socket.SctpMessage; import java.util.HashMap; import java.util.Map; +/** + * {@link ChannelInboundMessageHandlerAdapter} which will take care of handle fragemented {@link SctpMessage}s, so + * only complete {@link SctpMessage}s will be forwarded to the next + * {@link ChannelInboundMessageHandler}. + */ public class SctpMessageCompletionHandler extends ChannelInboundMessageHandlerAdapter { private final Map fragments = new HashMap(); + private boolean assembled; + + @Override + protected boolean beginMessageReceived(ChannelHandlerContext ctx) throws Exception { + assembled = false; + return super.beginMessageReceived(ctx); + } + + @Override + protected void endMessageReceived(ChannelHandlerContext ctx) throws Exception { + if (assembled) { + assembled = false; + ctx.fireInboundBufferUpdated(); + } + super.endMessageReceived(ctx); + } @Override protected void messageReceived(ChannelHandlerContext ctx, SctpMessage msg) throws Exception { @@ -46,7 +68,7 @@ public class SctpMessageCompletionHandler extends ChannelInboundMessageHandlerAd if (isComplete && !frag.readable()) { //data chunk is not fragmented - fireAssembledMessage(ctx, msg); + handleAssembledMessage(ctx, msg); } else if (!isComplete && frag.readable()) { //more message to complete fragments.put(streamIdentifier, Unpooled.wrappedBuffer(frag, byteBuf)); @@ -57,15 +79,15 @@ public class SctpMessageCompletionHandler extends ChannelInboundMessageHandlerAd protocolIdentifier, streamIdentifier, Unpooled.wrappedBuffer(frag, byteBuf)); - fireAssembledMessage(ctx, assembledMsg); + handleAssembledMessage(ctx, assembledMsg); } else { //first incomplete message fragments.put(streamIdentifier, byteBuf); } } - protected void fireAssembledMessage(ChannelHandlerContext ctx, SctpMessage assembledMsg) { + private void handleAssembledMessage(ChannelHandlerContext ctx, SctpMessage assembledMsg) { ctx.nextInboundMessageBuffer().add(assembledMsg); - ctx.fireInboundBufferUpdated(); + assembled = true; } }