It's no use to use CumulativeProtocolDecoder in UDP because UDP never
guarantees the order of packets. Messages can be mixed up. :)
HTH,
Trustin
On Dec 12, 2007 4:27 AM, Frederic Soulier <[EMAIL PROTECTED]> wrote:
> Hi
>
> I have an issue with Datagrams and the CumulativeProtocolDecoder
> which doesn't seem to cumulate!
> (working just fine with TCP).
>
> I have a Decoder extending CumulativeProtocolDecoder.
>
> protected boolean doDecode(IoSession session, ByteBuffer in,
> ProtocolDecoderOutput out)
> throws Exception
> {
> int startIndex = in.position(); // Save buffer position
> boolean done = doDecodeWithoutBackingUp(session, in, out);
> if (!done)
> {
> // Reset buffer to start as-if doDecodeWithoutBackingUp
> had never run
> in.position(startIndex);
> }
> return done;
> }
>
>
> protected boolean doDecodeWithoutBackingUp(IoSession ioSession,
> ByteBuffer in, ProtocolDecoderOutput out)
> throws Exception
> {
> if (in.remaining() < TYPE_SIZE)
> {
> return false; // Need more data, we don't have the type
> of the message
> }
> else
> {
> type = in.get();
> if (type == TYPE_MSG)
> {
> if (in.remaining() < TRANSACTION_ID_SIZE)
> {
> return false; // Need more data, we don't have
> the transaction id
> }
> else
> {
> transactionId = in.getInt();
>
> // do we have the message length?
> if (in.remaining() <
> CodecUtils.MESSAGE_LENGTH_SIZE)
> {
> return false; // Need more data, we don't have
> the message length
> }
> else
> {
> int dataLength = in.getInt(); // Read 4-byte
> int data length
>
> // do we have all the data?
> if (in.remaining() < dataLength)
> {
> // Here we're already read 9
> bytes (1 byte + 1 int + 1 int) but
> ==> // we don't have all we need so
> we return false and next time
> // around we'll have more
> return false; // Need more data,
> we don't have the complete data
> }
> else
> {
> ** HERE WE PROCESS A FULL
> MESSAGE **
>
> return true; // Please carry on giving
> us more data to decode
> }
> }
> }
> }
> else
> {
> throw new MySuperDuperException();
> }
> }
> }
>
> The buffer (in.remaining = 30)
> I read 9 bytes from the buffer to be able to decide whether to read
> more or not.
> in.remaining = 20 but my data length is 50 so I need more bytes and
> return false.
> Another datagram is received in the meantime, I expect the new
> datagram to be accumulated behind what I haven't processed
> (considering I returned true).
>
> Instead I only get the new datagram and the previous stuff I haven't
> processed yet is gone to the big bit bucket in the sky!
>
> Is the Cumulative stuff supposed to work with UDP the same way it
> works with TCP? As I wrote at the top it works prefectly with TCP.
>
> Thx.
>
> --
> Frederic P. Soulier
> OpenPGP key available on http://pgpkeys.mit.edu/
> 1024D/BA6700ED 49A6 8E8E 4230 8D41 1ADE B649 3203 1DD2 BA67 00ED
>
>
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6