From a23cdd709222dfe8e0b630decc1f92ea72881e41 Mon Sep 17 00:00:00 2001 From: Artem Titoulenko Date: Wed, 10 Nov 2021 15:52:55 -0500 Subject: [PATCH] unpack with struct :o --- main.go | 21 ++++++++++++--------- util.go | 5 +++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 7cb566c..e1ac43c 100644 --- a/main.go +++ b/main.go @@ -109,14 +109,17 @@ func handleMessage(ctx context.Context, buf []byte) { return } + type FLAP struct { + Channel uint8 + SequenceNumber uint16 + DataLength uint16 + } + + flap := FLAP{} + panicIfError(binary.Read(messageBuf, binary.BigEndian, &flap)) + // Start parsing FLAP header - channel := mustReadNBytes(messageBuf, 1)[0] - log.Println("Message for channel: ", channel) - - datagramSeqNum := mustReadNBytes(messageBuf, 2) - log.Println("Datagram Sequence Number: ", binary.BigEndian.Uint16(datagramSeqNum)) - - dataLength := mustReadNBytes(messageBuf, 2) - log.Println("Data Length: ", binary.BigEndian.Uint16(dataLength)) - + log.Println("Message for channel: ", flap.Channel) + log.Println("Datagram Sequence Number: ", flap.SequenceNumber) + log.Println("Data Length: ", flap.DataLength) } diff --git a/util.go b/util.go index 6f6a0b8..2aa5091 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "encoding/binary" "encoding/hex" "fmt" "io" @@ -42,3 +43,7 @@ func mustReadNBytes(buf *bytes.Buffer, n int) []byte { panicIfError(err) return res } + +func mustRead(buf *bytes.Buffer, dest interface{}) { + panicIfError(binary.Read(buf, binary.BigEndian, dest)) +}