Hi, Your solution did not work for me. I tried your code but it give me error while decoding.
Actually my message contain bytes and character both hence if I use charset then it will not decode bytes part of message and give me exception. Please let me know other solution to handle fragmentation. Regards, Nikunj Patel On Tue, Jun 30, 2015 at 9:48 PM, Chad Beaulac <[email protected]> wrote: > Can you use the supplied PrefixedStringEncode and PrefixedStringDecoder? > > // Encode String messages up to 16k big, first two bytes are size of string > PrefixedStringEncoder(Charset.forName("UTF-8"),2, 1024*16); > > // Decode String messages up to 16k big, first two bytes are size of string > PrefixedStringDecoder(Charset.forName("UTF-8"),2, 1024*16); > > // Example Codec > public class MyCodecFactory implements ProtocolCodecFactory { > /** > * @param session Not in use > * @return Instance of {@link ProtobufDecoder} > */ > @Override > public ProtocolDecoder getDecoder(IoSession session) > { > return new PrefixedStringDecoder(Charset.forName("UTF-8"), 2, 1024*16); > } > > /** > * @param session Not in use > * @return Instance of {@link ProtobufEncoder} > */ > @Override > public ProtocolEncoder getEncoder(IoSession session) > { > return new PrefixedStringEncoder(Charset.forName("UTF-8"), 2, 1024*16); > } > } > > > On Tue, Jun 30, 2015 at 6:15 AM, Emmanuel Lécharny <[email protected]> > wrote: > > > Le 30/06/15 12:41, Nikunj Patel a écrit : > > > Dear Sir, > > > > > > I have developed network application which communicate to server. Now > > some > > > time due to TCP queuing, I got multiple message at same time. > > > > Which is perfectly normal. > > > > > To handle > > > that I write the decoder which work for fine for first message but for > > > second message it not identified it. My message structure is 2 Byte in > > hex > > > Message length + message body. I am attaching decoder here with. I want > > > split all message base on its length while receiving multiple message. > > The attachement didn't make it. > > > > Anyway, it's your decoder's task to iterate through everything it has > > received, and to decode the messages. > > > > Assuming your messages are encoded using a length first, a body next, > > you should first decode the length, and then read the following bytes up > > to length. You also have to take care of fragmentation : there is no > > guarantee that a message will be fully received. When you have been able > > to read the full length, then if you have some remaining bytes in the > > incoming buffer, then you have to iterate. > > > > Frgamenation handling : the received data could not contain the expected > > length (and that's not only true for the data, but for the 'length' > > itself, if it's not a byte). You have to keep an accumulator in your > > session to deal with that. You can have a look at the > > CumulativeProtocolDecoder code which does that. > > ( > > > http://grepcode.com/file/repo1.maven.org/maven2/org.apache.mina/mina-core/1.1.6/org/apache/mina/filter/codec/CumulativeProtocolDecoder.java > > ) > > > > > > > > > > > > > -- > *Confidentiality Notice: The information contained in this email and any > accompanying attachment(s) is intended only for the use of the intended > recipient and may be confidential and/or privileged. If any reader of this > communication is not the intended recipient, unauthorized use, disclosure > or copying is strictly prohibited, and may be unlawful. If you have > received this communication in error, please immediately notify the sender > by return email, and delete the original message and all copies from your > system. Thank you. * > -- Regards, Nikunj Patel.
