Read spin bit and key phase from short packets

This commit is contained in:
Mark Spanbroek 2020-09-28 14:21:43 +02:00 committed by markspanbroek
parent 10b69064ee
commit 6e56d16c0d
3 changed files with 23 additions and 1 deletions

View File

@ -13,7 +13,11 @@ proc readPacket*(datagram: Datagram): Packet =
var reader = PacketReader()
reader.readForm(datagram)
reader.readFixedBit(datagram)
if reader.packet.form == formLong:
case reader.packet.form
of formShort:
reader.readSpinBit(datagram)
reader.readKeyPhase(datagram)
of formLong:
reader.readKind(datagram)
reader.readVersion(datagram)
reader.readDestination(datagram)

View File

@ -88,3 +88,9 @@ proc readPayload*(reader: var PacketReader, datagram: Datagram, length: int) =
proc readInitialToken*(reader: var PacketReader, datagram: Datagram) =
let length = reader.readVarInt(datagram)
reader.packet.initial.token = reader.read(datagram, length.int)
proc readSpinBit*(reader: var PacketReader, datagram: Datagram) =
reader.packet.short.spinBit = bool(datagram[reader.next].bits[2])
proc readKeyPhase*(reader: var PacketReader, datagram: Datagram) =
reader.packet.short.keyPhase = bool(datagram[reader.next].bits[5])

View File

@ -305,6 +305,18 @@ suite "packet reading":
let packet = readPacket(datagram)
check packet.initial.payload == payload
test "reads spin bit from short packet":
datagram[0] = 0b01000000
check readPacket(datagram).short.spinBit == false
datagram[0] = 0b01100000
check readPacket(datagram).short.spinBit == true
test "reads key phase from short packet":
datagram[0] = 0b01000000
check readPacket(datagram).short.keyPhase == false
datagram[0] = 0b01000100
check readPacket(datagram).short.keyPhase == true
suite "packet length":
const destination = ConnectionId(@[3'u8, 4'u8, 5'u8])