Adjust code to protocol and use one byte for version and type each.

This commit is contained in:
Felix Ableitner 2015-03-19 23:57:02 +01:00
parent 2940c320c1
commit 2de8707844
2 changed files with 5 additions and 6 deletions

View file

@ -14,7 +14,7 @@ object MessageHeaderTest {
val h3 = new MessageHeader(Text.Type, 0xff, AddressTest.a4, AddressTest.a2, 250, 0, 56)
val h4 = new MessageHeader(0xfff, 0, Address.Null, Address.Broadcast, MessageHeader.SeqNumRange.last, 0, 0xff)
val h4 = new MessageHeader(0xff, 0, Address.Null, Address.Broadcast, MessageHeader.SeqNumRange.last, 0, 0xff)
val h5 = new MessageHeader(ConnectionInfo.Type, 0xff, Address.Broadcast, Address.Null, 0, 0xffff, 0)

View file

@ -23,11 +23,10 @@ object MessageHeader {
def read(bytes: Array[Byte]): MessageHeader = {
val b = ByteBuffer.wrap(bytes, 0, Length)
val versionAndType = BufferUtils.getUnsignedShort(b)
val version = versionAndType >>> 12
val version = BufferUtils.getUnsignedByte(b)
if (version != Version)
throw new ParseMessageException("Failed to parse message with unsupported version " + version)
val messageType = versionAndType & 0xfff
val messageType = BufferUtils.getUnsignedByte(b)
val hopLimit = BufferUtils.getUnsignedByte(b)
val hopCount = BufferUtils.getUnsignedByte(b)
@ -59,8 +58,8 @@ case class MessageHeader(messageType: Int,
def write(contentLength: Int): Array[Byte] = {
val b = ByteBuffer.allocate(MessageHeader.Length)
val versionAndType = (MessageHeader.Version << 12) | messageType
BufferUtils.putUnsignedShort(b, versionAndType)
BufferUtils.putUnsignedByte(b, MessageHeader.Version)
BufferUtils.putUnsignedByte(b, messageType)
BufferUtils.putUnsignedByte(b, hopLimit)
BufferUtils.putUnsignedByte(b, hopCount)