Ok, then the isCompletePacket() could look something like:
private boolean isCompletePacket(ByteBuffer in) {
// Assuming header is always 4 bytes and the data_length value
occupies 2 bytes
if (in.remaining() < 1 + 4 + 2) {
// Too few bytes in buffer.
return false;
}
in.get(); // Skip start byte (1 byte)
in.getInt(); // Skip header (4 bytes)
short data_length = in.getShort(); // Get data_length field (2 bytes)
// in's position is now directly after the data_length field
return in.remaining() >= data_length;
}
So what we're doing here is first checking if enough bytes have been
received to be able to read the data_length field. Then we check if
there are at least data_length bytes in the buffer after the data_length
field.
HTH
/Niklas
Sungwon Jung wrote:
Thank you Niklas Therning.
packet structure is start_byte + header + data_length + data.
so I have no end byte. just data_length.
On 10/15/07, Niklas Therning <[EMAIL PROTECTED]> wrote:
If I understand your code correctly you look for the start of a new
packet. Shouldn't you check for the packet end instead? Just because you
see the start of a new packet it doesn't necessarily mean the in
variable contains a complete packet.
The CumulativeProtocolDecoder will remember the received bytes until
your doDecode() method returns true. Your code should look something like:
if (isCompletePacket(in)) {
// Create the MessageReq object from the bytes and call out.write()
} else {
// No complete packet in in. Wait for more data by returning false.
return false;
}
Do you have some means of determining if a complete packet is in the in
variable?
HTH
/Niklas
Sungwon Jung wrote:
sorry.
I received large packet, prints "RAW DATA" twice.
first, print RAW DATA include packet header.
second, print RAW DATA (just data).
sorry, English is poor.
<all code>
public class PacketReqAnalysis extends CumulativeProtocolDecoder
{
protected boolean doDecode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
logger.debug( "RAW DATA - " + in.getHexDump() );
int start = in.get() & 0xFF;
if( start == PacketDefine.START ) {
ByteBuffer pk = ByteBuffer.allocate( 2, false );
pk.setAutoExpand( true );
pk.put( (byte)start );
pk.putShort( (short)(in.getShort() & 0xFFFF) );
pk.put( (byte)in.get() );
pk.put( (byte)in.get() );
int size = in.getInt();
pk.putInt( size );
pk.put( (byte)in.get() );
pk.put( (byte)in.get() );
byte[] bytes = new byte[ size ];
in.get( bytes );
pk.put( bytes );
MessageReq request = new MessageReq( session, pk, out,
start );
out.write( acu_request );
return true;
} else {
logger.debug( "out doDecode-------------------" );
logger.debug( "buf is " + in.getHexDump() );
logger.debug( "out doDecode-------------------" );
return false;
}
}
}
On 10/15/07, Niklas Therning <[EMAIL PROTECTED]> wrote:
Sungwon Jung wrote:
Hello. I'm using MINA 1.1.2.
I'm using CumulativeProtocolDecoder.
When I recieved large size packet(about 220bytes), the packet is splited.
How can I assemble the splited packet?
Thank you.
my code>
public class PacketReqAnalysis extends CumulativeProtocolDecoder
{
protected boolean doDecode(IoSession session, ByteBuffer in,
ProtocolDecoderOutput out) throws Exception {
if( start == PacketDefine.START ) {
return true;
} else {
return false;
}
}
}
Your code looks weird. Is this really all code? Where does the start
variable come from?
/Niklas