Thanks Emmanuel; Yes I believe, in your decoder if you extend CumulativeProtocolDecoder, it takes care of fragmented messages in different PDU's (It waits till they are complete). But you are right if there's more than one message in the same PDU, it's little bit harder to handle given that message sizes vary for each message type. If we talk about your scenario where server receives 1 78 byte powerup message 1 85 byte healthcheck message and some other bytes for an incomplete message, the first two message will be sent to message handler and will be processed. But how can I take care of the other partial package, does cumulativeprotocoldecoder handle this incomplete message(save it somewhere wait until we receive rest of it) or is it simply discarded? Is this issue related to Nagle's Algorithm?
Do I have to use ((SocketSessionConfig) connector.getSessionConfig()).setTcpNoDelay(false) to ensure that I will receive only one complete message given that I implement MessageDecoder correctly, but I feel like setting tcpnodelay false will negatively affect the performance. Is there any way that I can just prevent partial messages in the PDU? I am happy receiving multiple messages but I want to be able to deal with partial messages somehow instead of discarding them. Following is code of my current decoder. To process multiple messages I need to look for message start and message end in the byte array. And each message will be written to ProtocolDecoderOutput. But I don't know what to do with the incomplete message. http://mina.apache.org/handling-packet-fragementation.html doesn't have an example for this scenario. * public* *class* MessageDecoder *extends* CumulativeProtocolDecoder { *protected* *boolean* doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) *throws* Exception { *if* (in.remaining() >= 31) { *int* len = in.remaining(); *byte*[] stream = *new* *byte*[len]; in.get(stream); Message message = *new* Message(stream); out.write(message); *return* *true*; } *else* { *return* *false*; } } } On Fri, May 15, 2009 at 3:40 AM, Emmanuel Lecharny <[email protected]>wrote: > Erinc Arikan wrote: > >> Thanks for the response. >> I actually figured out my problem I was stuffing and unstuffing data in my >> pojo, It looks like that's the problem I can't believe how I totally >> forgot >> about that. >> >> > Cool ! > >> As you have mentioned the following code for my decoder will work ok for >> LAN >> scenarios. It will be problematic if I receive 78 bytes one by one. How >> can >> I handle that scenario if message reaches server in parts? There can be >> many >> fragmented messages that might be coming from different clients. How can >> my >> server figure out which message belongs to which historical message and >> merge them correctly to make a complete message? How can the following >> code >> be improved? Is there a working example that I can take a look at? >> >> > First, every connection is associated with a session in MINA, so if a > fragment is received, it belongs to a session. Now, the only thing you have > to do is to accumulate the message in a temporary buffer, and store this > buffer within this session until the message is completed. > > Be aware that you not only can receive a fragment, but may be - depending > on your protocol - more than one message in the same PDU (ie you may receive > 200 bytes, 2 times 78 bytes plus some more bytes associated with another > message). You have to deal with that. > > If the protocol is a text based one, you may have a look to the > documentation : > > http://mina.apache.org/handling-packet-fragementation.html > > The tutorial explain how to handle fragmented messages, using a class > extending the CumulativeProtocolDecoder class. > > Hope it helps... > > > -- > -- > cordialement, regards, > Emmanuel Lécharny > www.iktek.com > directory.apache.org > > >
