You should never assume that the received buffer's position is 0. Also, please make sure you return true or false properly depending on the number of bytes available in the received buffer, because packets can be fragemented or assembled.
HTH,
Basil Gasser wrote:
> I'm using Mina 2.0, the following code is taken from my
> doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out )
> method in my decoder which extends CumulativeProtocolDecoder
> Concerning my protocol, a message contains its type in the second byte
> (position 1 in an array)
>
> // length is stored in byte 3 to 6
> int length = in.getInt(2);
> byte[] foo = in.array();
> for (int u = 0; u<foo.length;u++){
> System.out.print(foo[u]+" ");
> }
> System.out.println();
> // all bytes received to decode message
> byte[] msg = new byte[length];
> int readtype = 0;
> // read type of message
> int designatedtype = in.get(1);
> System.out.println("designated type: "+designatedtype);
> for (int i = 0; i < length; i++) {
> byte b = in.get();
> System.out.print(b+ " ");
> if (i ==1){
> // type is second byte in a message
> readtype = b;
> }
> msg[i] = (byte) b;
> }
> System.out.println();
> System.out.println("read type: "+readtype);
> byte[] current = in.array().clone();
> for (int u = 0; u<current.length;u++){
> System.out.print(current[u]+" ");
> }
> System.out.println();
>
> This code is working perfectly fine in usual cases. However, when I
> start my application in Eclipse, let it run for a while (it is
> exchanging messages perfectly) stop it (I interrupt the execution) and
> then let it run again, I get the following output:
>
> 4 61 0 0 0 10 0 0 9 19 4 31 0 0 0 18 ...
> designated type: 61
> 1 31 0 8 0 0 0 0 4 30
> read type: 31
> 4 61 0 0 0 10 0 0 9 19 4 31 0 0 0 18 ...
>
> Obviously, the expected type is 61. But reading the buffer gives 31. It
> seems that Buffer and underlying array are not in sync as reading twice
> from the buffer gives 31 for the second byte.
>
> Is this a bug in the Mina implementation? Am I doing something wrong
> decoding the message (the system out statements would of course normally
> not be there. So I would call in.get() length times.)?
>
> thanks.
>
> ++ Basil
--
Trustin Lee - Principal Software Engineer, JBoss, Red Hat
--
what we call human nature is actually human habit
--
http://gleamynode.net/
signature.asc
Description: OpenPGP digital signature
