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 return
} }
type FLAP struct {
Channel uint8
SequenceNumber uint16
DataLength uint16
}
flap := FLAP{}
panicIfError(binary.Read(messageBuf, binary.BigEndian, &flap))
// Start parsing FLAP header // Start parsing FLAP header
channel := mustReadNBytes(messageBuf, 1)[0] log.Println("Message for channel: ", flap.Channel)
log.Println("Message for channel: ", channel) log.Println("Datagram Sequence Number: ", flap.SequenceNumber)
log.Println("Data Length: ", flap.DataLength)
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))
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
@ -42,3 +43,7 @@ func mustReadNBytes(buf *bytes.Buffer, n int) []byte {
panicIfError(err) panicIfError(err)
return res return res
} }
func mustRead(buf *bytes.Buffer, dest interface{}) {
panicIfError(binary.Read(buf, binary.BigEndian, dest))
}