Yes, you&#39;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">&lt;<a href="mailto:trustin@gmail.com">trustin@gmail.com</a>&gt;</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&#39;t want the resulting<br>
frame&#39;s header stripped out.<br>
<br>
lengthAdjustment could be 20 or something close to it because there&#39;s<br>
an extra data between the length field and the payload.<br>
<br>
The Javadoc says:<br>
<br>
&quot;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.&quot;<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 &lt;<a href="mailto:hezjing@gmail.com">hezjing@gmail.com</a>&gt; wrote:<br>
&gt; Hi Trustin<br>
&gt;<br>
&gt; In my case, the length field is the size of the payload (excluding the<br>
&gt; header).<br>
&gt;<br>
&gt; If I read the Javadoc correctly, the LengthFieldBasedFrameDecoder is<br>
&gt; applicable when the length field is the size of the header and payload<br>
&gt; right?<br>
&gt;<br>
&gt; :-)<br>
&gt;<br>
&gt; On Mon, Sep 21, 2009 at 9:39 AM, Trustin Lee (이희승) &lt;<a href="mailto:trustin@gmail.com">trustin@gmail.com</a>&gt;<br>
&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hi Hez,<br>
&gt;&gt;<br>
&gt;&gt; Thanks for closing this thread with a good answer and sorry that I&#39;m late.<br>
&gt;&gt;<br>
&gt;&gt; Do you find the Javadoc of LengthFieldBasedFrameDecoder difficult to<br>
&gt;&gt; understand?  I thought your use case is explained already there.  Let<br>
&gt;&gt; me know what you think so that we can improve the documentation.<br>
&gt;&gt;<br>
&gt;&gt; Cheers<br>
&gt;&gt;<br>
&gt;&gt; — Trustin Lee, <a href="http://gleamynode.net/" target="_blank">http://gleamynode.net/</a><br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; On Sun, Sep 20, 2009 at 9:12 PM, hezjing &lt;<a href="mailto:hezjing@gmail.com">hezjing@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt; Hi Mike<br>
&gt;&gt; &gt; Hmmm ... do you mean the example of the frame like the following?<br>
&gt;&gt; &gt; &lt;-- HEADER 20 bytes --&gt;&lt;-- PAYLOAD 14 bytes --&gt;<br>
&gt;&gt; &gt; +-------+---------+----+-----------------------+<br>
&gt;&gt; &gt; |       | Length  |    | Payload               |<br>
&gt;&gt; &gt; |       | 0x000C  |    |                       |<br>
&gt;&gt; &gt; +-------+---------+----+-----------------------+<br>
&gt;&gt; &gt; Total frame size is 34 bytes.<br>
&gt;&gt; &gt; &lt;-- HEADER 20 bytes --&gt;&lt;-- PAYLOAD 100 bytes --&gt;<br>
&gt;&gt; &gt; +-------+--------+-----+------------------------+<br>
&gt;&gt; &gt; |       | Length |     | Payload                |<br>
&gt;&gt; &gt; |       | 0x0064 |     |                        |<br>
&gt;&gt; &gt; +-------+--------+-----+------------------------+<br>
&gt;&gt; &gt; Total frame size is 120 bytes.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Sun, Sep 20, 2009 at 1:41 AM, Mike McGrady<br>
&gt;&gt; &gt; &lt;<a href="mailto:mmcgrady@topiatechnology.com">mmcgrady@topiatechnology.com</a>&gt;<br>
&gt;&gt; &gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Could you include an example write with this?  The two together, I<br>
&gt;&gt; &gt;&gt; suspect, will be most helpful to many people, including me.<br>
&gt;&gt; &gt;&gt; Mike<br>
&gt;&gt; &gt;&gt; On Sep 19, 2009, at 8:11 AM, hezjing wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Hi<br>
&gt;&gt; &gt;&gt; To close this thread, my solution is to extends FrameDecoder like the<br>
&gt;&gt; &gt;&gt; following code:<br>
&gt;&gt; &gt;&gt; @ChannelPipelineCoverage(&quot;all&quot;)<br>
&gt;&gt; &gt;&gt; public class MyFrameDecoder extends FrameDecoder {<br>
&gt;&gt; &gt;&gt;     @Override<br>
&gt;&gt; &gt;&gt;     protected Object decode(ChannelHandlerContext ctx, Channel channel,<br>
&gt;&gt; &gt;&gt; ChannelBuffer buf) throws Exception {<br>
&gt;&gt; &gt;&gt;         // the length field is at 3rd and 4th octet<br>
&gt;&gt; &gt;&gt;         if (buf.readableBytes() &lt; 4) {<br>
&gt;&gt; &gt;&gt;             return null;<br>
&gt;&gt; &gt;&gt;         }<br>
&gt;&gt; &gt;&gt;         // The length field is in the buffer.<br>
&gt;&gt; &gt;&gt;         buf.markReaderIndex();<br>
&gt;&gt; &gt;&gt;         ...<br>
&gt;&gt; &gt;&gt;         // Read the length field (the payload size)<br>
&gt;&gt; &gt;&gt;         int length = buf.readUnsignedShort();<br>
&gt;&gt; &gt;&gt;         // The actual frame size is header (20) + payload size<br>
&gt;&gt; &gt;&gt;         length += 20;<br>
&gt;&gt; &gt;&gt;         buf.resetReaderIndex();<br>
&gt;&gt; &gt;&gt;         if (buf.readableBytes() &lt; length) {<br>
&gt;&gt; &gt;&gt;             return null;<br>
&gt;&gt; &gt;&gt;         }<br>
&gt;&gt; &gt;&gt;         ChannelBuffer frame = buf.readBytes(length);<br>
&gt;&gt; &gt;&gt;         return frame;<br>
&gt;&gt; &gt;&gt;     }<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Thank you!<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; On Thu, Sep 10, 2009 at 9:23 PM, hezjing &lt;<a href="mailto:hezjing@gmail.com">hezjing@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Hi<br>
&gt;&gt; &gt;&gt;&gt; I have a message containing a fixed length header of 20 bytes,<br>
&gt;&gt; &gt;&gt;&gt; followed<br>
&gt;&gt; &gt;&gt;&gt; by a payload of variable length.<br>
&gt;&gt; &gt;&gt;&gt; The header contains a 2 bytes length field, indicating the length of<br>
&gt;&gt; &gt;&gt;&gt; the<br>
&gt;&gt; &gt;&gt;&gt; payload (excluding the header).<br>
&gt;&gt; &gt;&gt;&gt; For example if the message has 10 bytes payload, then the entire frame<br>
&gt;&gt; &gt;&gt;&gt; length is 30 bytes (20 bytes header + 10 bytes of payload),<br>
&gt;&gt; &gt;&gt;&gt; and the length field is 10.<br>
&gt;&gt; &gt;&gt;&gt; May I know what is the parameter to<br>
&gt;&gt; &gt;&gt;&gt; create LengthFieldBasedFrameDecoder?<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Thank you!<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; --<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Hez<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; --<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Hez<br>
&gt;&gt; &gt;&gt; _______________________________________________<br>
&gt;&gt; &gt;&gt; netty-users mailing list<br>
&gt;&gt; &gt;&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt;&gt; &gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Mike McGrady<br>
&gt;&gt; &gt;&gt; Principal Investigator AF081-028 AFRL SBIR<br>
&gt;&gt; &gt;&gt; Senior Engineer<br>
&gt;&gt; &gt;&gt; Topia Technology, Inc.<br>
&gt;&gt; &gt;&gt; 1.253.720.3365<br>
&gt;&gt; &gt;&gt; <a href="mailto:mmcgrady@topiatechnology.com">mmcgrady@topiatechnology.com</a><br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; _______________________________________________<br>
&gt;&gt; &gt;&gt; netty-users mailing list<br>
&gt;&gt; &gt;&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt;&gt; &gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; --<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Hez<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt; &gt; netty-users mailing list<br>
&gt;&gt; &gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt;&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; netty-users mailing list<br>
&gt;&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt;<br>
&gt; Hez<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; netty-users mailing list<br>
&gt; <a href="mailto:netty-users@lists.jboss.org">netty-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/netty-users" target="_blank">https://lists.jboss.org/mailman/listinfo/netty-users</a><br>
&gt;<br>
&gt;<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>