I have a system which has variable length TCPIP messages where first 2 bytes
suggest the length of the message including the header. So, a full_message =
header(i.e.,total_length) + payload

Full_Message=17AAABBCCCCDDEFGH where
Header(total_length)=2(header)+15(payload)

I tried using Apache Mina’s using PrefixedStringDecoder and
PrefixedStringCodecFactory but here in the header indicates the full length
of the message but for this message, it was different header-2.

So I am using CumulativeProtocolDecoder and decoding a small message works
fine but for a very large message seems it goes into a loop. Here is the
sample code snippet that I am using for it.

class MyDecoder extends CumulativeProtocolDecoder {

private String encoding = “UTF-8”;

public MyDecoder (String encoding) {
    this.encoding = encoding;
}

protected boolean doDecode(IoSession session, IoBuffer in,
ProtocolDecoderOutput out) throws Exception {

    int start = in.position();
    int messagelength = in.getShort() - 2;

        if (0 < in.remaining() && in.remaining() <= messagelength) {

            byte[] buf = new byte[in.remaining()];
            in.get(buf);
            out.write(new String(buf, encoding));
            return true;
        } else {
           return false;
        }
}
}





--
Sent from: 
http://apache-mina.10907.n7.nabble.com/Apache-MINA-User-Forum-f31345.html

Reply via email to