boolean completed = false;
if( reading_short_header ) {
short_header_buff.limit( SS, SHORT_HEADER_SIZE ); //ensure proper limit
if( short_header_buff.position( SS ) == SHORT_HEADER_SIZE ) {
completed = true;
}
}
else {
long_header_buff.limit( SS, LONG_HEADER_SIZE ); //ensure proper limit
if( long_header_buff.position( SS ) == LONG_HEADER_SIZE ) {
completed = true;
}
}
if( completed ) {
if( reading_short_header ) {
message_length = short_header_buff.get( SS, 0 );
System.out.println( "NOTE: short message length = " +message_length );
if( message_length == 0xFF ) { //long part of header still needs to be read
reading_short_header = false;
completed = false;
}
else {
message_id = short_header_buff.get( SS, 1 ) >>> 4; //use leftmost 4 bits
message_sub_id = short_header_buff.get( SS, 1 ) & 0x0F; //use rightmost 4 bits
}
}
else if( reading_handshake ) {
long_header_buff.position( SS, 0 ); //rewind
message_length = long_header_buff.getInt( SS );
System.out.println( "NOTE: handshake message length = " +message_length );
message_id = -1; //negative to signify stream handshake
reading_handshake = false;
reading_short_header = true;
}
else { //reading long part of header
message_length = (short_header_buff.get( SS, 1 ) << 20) |
(long_header_buff.get( SS, 0 ) << 12) |
(long_header_buff.get( SS, 1 ) << 4) |
(long_header_buff.get( SS, 2 ) >>> 4);
System.out.println( "NOTE: long message length = " +message_length );
message_id = (long_header_buff.get( SS, 2 ) << 4) |
(long_header_buff.get( SS, 3 ) >> 4 );
message_sub_id = long_header_buff.get( SS, 3 ) & 0x0F;
reading_short_header = true;
}
}
if( completed ) {
short_header_buff.position( SS, 0 ); //reset buffer positions for next read round
long_header_buff.position( SS, 0 );
}
return completed;