Maarten Bosteels a écrit :
1) how is length defined ?
Is it a result of reading the header ?
Is it an instance attribute ?
lenght is an instance attribute and is read in readheader()
2) what happens when there is not enough data to read a header ?
in that case you should also return MessageDecoderResult.NEED_DATA;
No ?
Yes, the message is considered as non decodable. This is handled in the
decodable() method.
public MessageDecoderResult decodable(IoSession session, ByteBuffer in)
{
// Return NEED_DATA if the whole header is not read yet.
if (!readHeader)
{
if (in.remaining() < Constants.HEADER_LEN)
{
return MessageDecoderResult.NEED_DATA;
}
else
{
return MessageDecoderResult.OK;
}
}
else
{
return MessageDecoderResult.OK;
}
}
3) please, show us the code for readHeader(ByteBuffer in)
No problem
private void readHeader(ByteBuffer in)
{
String header;
try
{
header = in.getString(Constants.HEADER_LEN, decoder);
String[] sArray = header.split(",", 2);
timeout = Short.parseShort(sArray[0]);
type = Short.parseShort(sArray[1]);
length = Integer.parseInt(sArray[2]);
}
catch (CharacterCodingException e)
{
}
catch (NumberFormatException e)
{
}
}