public void testGoodHeader() { byte[] data = getGoodHeader(); TCByteBuffer buffer = TCByteBufferFactory.getInstance(false, WireProtocolHeader.MAX_LENGTH); buffer.put(data); buffer.flip(); WireProtocolHeader header = new WireProtocolHeader(buffer); System.out.println(header); assertTrue(header.isChecksumValid()); assertTrue(header.getVersion() == WireProtocolHeader.VERSION_2); assertTrue(header.getHeaderLength() == 8); assertTrue(header.getTypeOfService() == 2); assertTrue(header.getTimeToLive() == 3); assertTrue(header.getTotalPacketLength() == 255); assertTrue(header.getChecksum() == 2744585179L); assertTrue( Arrays.equals( header.getSourceAddress(), new byte[] {(byte) 0xFF, (byte) 1, (byte) 0xFF, (byte) 1})); assertTrue( Arrays.equals( header.getDestinationAddress(), new byte[] {(byte) 1, (byte) 0xFF, (byte) 1, (byte) 0xFF})); assertTrue(header.getSourcePort() == 43605); assertTrue(header.getDestinationPort() == 21930); assertTrue(header.getMessageCount() == 5); assertTrue(header.getOptions().length == 0); try { header.validate(); } catch (WireProtocolHeaderFormatException e) { fail(e.getMessage()); } // changing data in the header should cause the checksum to need to be recomputed header.setVersion((byte) (header.getVersion() + 1)); assertFalse(header.isChecksumValid()); // Fix and validate the checksum header.computeChecksum(); assertTrue(header.isChecksumValid()); }
private TCNetworkMessage processPayloadData(TCConnection source, TCByteBuffer[] data) throws TCProtocolException { for (int i = 0; i < data.length; i++) { final TCByteBuffer buffer = data[i]; if (!buffer.hasRemaining()) { buffer.flip(); dataBytesNeeded -= buffer.limit(); bufferIndex++; if (dataBytesNeeded < 0) { throw new TCProtocolException("More data in buffers than expected"); } } else { break; } } if (0 == dataBytesNeeded) { if (bufferIndex != dataBuffers.length) { throw new TCProtocolException("Not all buffers consumed"); } // message is complete! TCNetworkMessage msg = createMessage(source, header, dataBuffers); if (logger.isDebugEnabled()) { logger.debug("Message complete on connection " + source + ": " + msg.toString()); } return msg; } Assert.eval(dataBytesNeeded > 0); // data portion not done, try again later return null; }