Hi,
I would remove the loop from your implementation. Mina laready does
this loop for you:
protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {
int remain = in.remaining();
if (remain >= FRAME_LENGTH){
byte [] data = new byte [FRAME_LENGTH];
in.get(data,0,FRAME_LENGTH);
out.write(data);
out.flush();
System.out.println("remain data:"+remain);
return true;
}
return false;
}
I don't think you need the flush in this case.
regards,
Maarten
On Mon, Apr 6, 2009 at 10:32 AM, Ashish <[email protected]> wrote:
>>
>> The message format is the fixed-length message which is defined by
>> FRAME_LENGTH.
>> The following is my doDecode method:
>>
>> protected boolean doDecode(IoSession session, IoBuffer in,
>> ProtocolDecoderOutput out) throws Exception {
>> int remain = in.remaining();
>> if (remain >= FRAME_LENGTH){
>> byte [] data = new byte [FRAME_LENGTH];
>> while (remain >= FRAME_LENGTH){
>> in.get(data,0,FRAME_LENGTH);
>> out.write(data);
>> out.flush();
>> System.out.println("remain data:"+remain);
>> remain = in.remaining();
>> }
>> return false;
>> }
>> return false;
>> }
>> The server keep sending message to my client and I decode the message in
>> this doDecode.
>> In my test, the "remain data" in the console seemed to be keep going up util
>> got the java heap space outofmemoryError.
>> Three notes:
>> 1> Why I always use return false: I don't understand the diff between true
>> or false. At first, I thought the when I got the enough data to put into the
>> out, I should use return true. But I got error. So I alway use false and
>> currently, the logic seems correct.
>
> Use true, when you have received all the data necessary to decode. And
> once you have complete data, then only you write to protocol decoder
> output.
>
> Simple pseduo code
> 1. check if the incoming data equals to data needed
> 2. No return false, reset the position to beginning
> 3. keep in this loop till, you have all the data
> 4. All the data received, write to decoder output and return true
>
>
> Check out this page on wiki
>
> http://mina.apache.org/tutorial-on-protocolcodecfilter-for-mina-2x.html
>
>> 2> Why I use out.flush. I found if I don't do that, the messageReceived
>> method couldn't get correct every message. Some message will lost and others
>> may be duplicated.
>
> Not sure on this. Sorry
>
>> 3> The data transfer speed is about 1MB/s.
>>
>> Any friends help me?
>> Thank you in advance!
>>
>> --
>> Best Regards
>> DAI Jun
>>
>
>
>
> --
> thanks
> ashish
>
> Blog: http://www.ashishpaliwal.com/blog
> My Photo Galleries: http://www.pbase.com/ashishpaliwal
>