Write correct fixed bit

This commit is contained in:
Mark Spanbroek 2020-09-21 15:02:12 +02:00 committed by markspanbroek
parent 46a561fb95
commit 35c1ba1ab4
2 changed files with 12 additions and 1 deletions

View File

@ -53,8 +53,14 @@ proc writeKind(datagram: var seq[byte], kind: PacketKind) =
datagram[0].bits[2] = kind.uint8.bits[6]
datagram[0].bits[3] = kind.uint8.bits[7]
proc newPacketHeader*(datagram: seq[byte]): PacketHeader =
proc readFixedBit(datagram: seq[byte]) =
assert datagram[0].bits[1] == 1
proc writeFixedBit(datagram: var seq[byte]) =
datagram[0].bits[1] = 1
proc newPacketHeader*(datagram: seq[byte]): PacketHeader =
datagram.readFixedBit()
PacketHeader(kind: datagram.readKind(), bytes: datagram)
proc newShortPacketHeader*(): PacketHeader =
@ -62,6 +68,7 @@ proc newShortPacketHeader*(): PacketHeader =
proc write*(datagram: var seq[byte], header: PacketHeader) =
datagram[0..<header.bytes.len] = header.bytes
datagram.writeFixedBit()
datagram.writeKind(header.kind)
proc version*(header: PacketHeader): uint32 =

View File

@ -32,6 +32,10 @@ suite "short headers":
datagram.write(newShortPacketHeader())
check datagram[0].bits[0] == 0
test "writes correct fixed bit":
datagram.write(newShortPacketHeader())
check datagram[0].bits[1] == 1
test "conversion to string":
check $newPacketHeader(@[0b01000000'u8]) == "(kind: packetShort)"