unpack with struct :o

This commit is contained in:
Artem Titoulenko 2021-11-10 15:52:55 -05:00
parent 27aea95084
commit a23cdd7092
2 changed files with 17 additions and 9 deletions

21
main.go
View file

@ -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)
}

View file

@ -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))
}