Yes, you're right.<div><br></div><div>I should really create a decoder with LengthFieldBasedFrameDecoder(0xFFFF, 2, 2, 16, 0).</div><div><br></div><div>I was having problem figuring the correct lengthAdjustment until I found the formula from the LengthFieldBasedFrameDecoder source code:</div>
<div><br></div><div>frameLength += lengthAdjustment + lengthFieldEndOffset;</div><div><br></div><div><br></div><div>Again, thanks for your help!</div><div><div><br><br><div class="gmail_quote">On Mon, Sep 21, 2009 at 2:45 PM, Trustin Lee (이희승) <span dir="ltr"><<a href="mailto:trustin@gmail.com">trustin@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Not actually. It is supposed to work with either cases, as there are<br>
lengthAdjustment and initialBytesToStrip.<br>
<br>
initialBytesToStrip should be 0 because you don't want the resulting<br>
frame's header stripped out.<br>
<br>
lengthAdjustment could be 20 or something close to it because there's<br>
an extra data between the length field and the payload.<br>
<br>
The Javadoc says:<br>
<br>
"It is particularly useful when you decode a binary message which has<br>
an integer header field that represents the length of the message body<br>
or the whole message."<br>
<br>
which means, it works regardless whether the length field includes the<br>
header or not. Am I missing something? Could you suggest where to<br>
improve?<br>
<div class="im"><br>
— Trustin Lee, <a href="http://gleamynode.net/" target="_blank">http://gleamynode.net/</a><br>
<br>
<br>
<br>
</div><div><div></div><div class="h5">On Mon, Sep 21, 2009 at 3:24 PM, hezjing <<a href="mailto:hezjing@gmail.com">hezjing@gmail.com</a>> wrote:<br>
> Hi Trustin<br>
><br>
> In my case, the length field is the size of the payload (excluding the<br>
> header).<br>
><br>
> If I read the Javadoc correctly, the LengthFieldBasedFrameDecoder is<br>
> applicable when the length field is the size of the header and payload<br>
> right?<br>
><br>
> :-)<br>
><br>
> On Mon, Sep 21, 2009 at 9:39 AM, Trustin Lee (이희승) <<a href="mailto:trustin@gmail.com">trustin@gmail.com</a>><br>
> wrote:<br>
>><br>
>> Hi Hez,<br>
>><br>
>> Thanks for closing this thread with a good answer and sorry that I'm late.<br>
>><br>
>> Do you find the Javadoc of LengthFieldBasedFrameDecoder difficult to<br>
>> understand? I thought your use case is explained already there. Let<br>
>> me know what you think so that we can improve the documentation.<br>
>><br>
>> Cheers<br>
>><br>
>> — Trustin Lee, <a href="http://gleamynode.net/" target="_blank">http://gleamynode.net/</a><br>
>><br>
>><br>
>><br>
>> On Sun, Sep 20, 2009 at 9:12 PM, hezjing <<a href="mailto:hezjing@gmail.com">hezjing@gmail.com</a>> wrote:<br>
>> > Hi Mike<br>
>> > Hmmm ... do you mean the example of the frame like the following?<br>
>> > <-- HEADER 20 bytes --><-- PAYLOAD 14 bytes --><br>
>> > +-------+---------+----+-----------------------+<br>
>> > | | Length | | Payload |<br>
>> > | | 0x000C | | |<br>
>> > +-------+---------+----+-----------------------+<br>
>> > Total frame size is 34 bytes.<br>
>> > <-- HEADER 20 bytes --><-- PAYLOAD 100 bytes --><br>
>> > +-------+--------+-----+------------------------+<br>
>> > | | Length | | Payload |<br>
>> > | | 0x0064 | | |<br>
>> > +-------+--------+-----+------------------------+<br>
>> > Total frame size is 120 bytes.<br>
>> ><br>
>> ><br>
>> > On Sun, Sep 20, 2009 at 1:41 AM, Mike McGrady<br>
>> > <<a href="mailto:mmcgrady@topiatechnology.com">mmcgrady@topiatechnology.com</a>><br>
>> > wrote:<br>
>> >><br>
>> >> Could you include an example write with this? The two together, I<br>
>> >> suspect, will be most helpful to many people, including me.<br>
>> >> Mike<br>
>> >> On Sep 19, 2009, at 8:11 AM, hezjing wrote:<br>
>> >><br>
>> >> Hi<br>
>> >> To close this thread, my solution is to extends FrameDecoder like the<br>
>> >> following code:<br>
>> >> @ChannelPipelineCoverage("all")<br>
>> >> public class MyFrameDecoder extends FrameDecoder {<br>
>> >> @Override<br>
>> >> protected Object decode(ChannelHandlerContext ctx, Channel channel,<br>
>> >> ChannelBuffer buf) throws Exception {<br>
>> >> // the length field is at 3rd and 4th octet<br>
>> >> if (buf.readableBytes() < 4) {<br>
>> >> return null;<br>
>> >> }<br>
>> >> // The length field is in the buffer.<br>
>> >> buf.markReaderIndex();<br>
>> >> ...<br>
>> >> // Read the length field (the payload size)<br>
>> >> int length = buf.readUnsignedShort();<br>
>> >> // The actual frame size is header (20) + payload size<br>
>> >> length += 20;<br>
>> >> buf.resetReaderIndex();<br>
>> >> if (buf.readableBytes() < length) {<br>
>> >> return null;<br>
>> >> }<br>
>> >> ChannelBuffer frame = buf.readBytes(length);<br>
>> >> return frame;<br>
>> >> }<br>
>> >> }<br>
>> >><br>
>> >> Thank you!<br>
>> >><br>
>> >> On Thu, Sep 10, 2009 at 9:23 PM, hezjing <<a href="mailto:hezjing@gmail.com">hezjing@gmail.com</a>> wrote:<br>
>> >>><br>
>> >>> Hi<br>
>> >>> I have a message containing a fixed length header of 20 bytes,<br>
>> >>> followed<br>
>> >>> by a payload of variable length.<br>
>> >>> The header contains a 2 bytes length field, indicating the length of<br>
>> >>> the<br>
>> >>> payload (excluding the header).<br>
>> >>> For example if the message has 10 bytes payload, then the entire frame<br>
>> >>> length is 30 bytes (20 bytes header + 10 bytes of payload),<br>
>> >>> and the length field is 10.<br>
>> >>> May I know what is the parameter to<br>
>> >>> create LengthFieldBasedFrameDecoder?<br>
>> >>><br>
>> >>> Thank you!<br>
>> >>><br>
>> >>> --<br>
>> >>><br>
>> >>> Hez<br>
>> >><br>
>> >><br>
>> >><br>
>> >> --<br>
>> >><br>
>> >> Hez<br>
>> >> _______________________________________________<br>
>> >> netty-users mailing list<br>
>> >> <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
>> >> <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
>> >><br>
>> >> Mike McGrady<br>
>> >> Principal Investigator AF081-028 AFRL SBIR<br>
>> >> Senior Engineer<br>
>> >> Topia Technology, Inc.<br>
>> >> 1.253.720.3365<br>
>> >> <a href="mailto:mmcgrady@topiatechnology.com">mmcgrady@topiatechnology.com</a><br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >> _______________________________________________<br>
>> >> netty-users mailing list<br>
>> >> <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
>> >> <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
>> >><br>
>> ><br>
>> ><br>
>> ><br>
>> > --<br>
>> ><br>
>> > Hez<br>
>> ><br>
>> > _______________________________________________<br>
>> > netty-users mailing list<br>
>> > <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
>> > <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
>> ><br>
>> ><br>
>><br>
>> _______________________________________________<br>
>> netty-users mailing list<br>
>> <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
>> <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
><br>
><br>
><br>
> --<br>
><br>
> Hez<br>
><br>
> _______________________________________________<br>
> netty-users mailing list<br>
> <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
> <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
><br>
><br>
<br>
_______________________________________________<br>
netty-users mailing list<br>
<a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><br>Hez<br>
</div></div>