Hi, Thank you for your help. 1> I use the loop to improve the performance. At my first implmentation, I didn't use the loop and the remaining increased fast since the server send data to my client very fast while if every time i only get bytes of FRAME_LENGTH, the remaining would keep increase since in the time which I used to get the data from input, the server sent more data to my client. 2> I use flush to get the correct message in messageReceived method. If I don't use this loop, the flush is needn't. But while I use the loop, if i don't use the flush, wrong data would received in messageReceived method. For instance, in the loop, firstly, i write 1,2,3, and secondly, i write 4,5,6. in the messageReceived method, i may get 4,5,6 twice and couldn't get 1,2,3. I don't know why. But when I used flush, this was solved.
2009/4/6 Maarten Bosteels <[email protected]> > 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 > > > -- Best Regards DAI Jun
