summaryrefslogtreecommitdiff
path: root/danmaku/dmpkg/decode.go
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-07 02:48:46 +0800
committerKeuin <[email protected]>2022-09-07 02:48:46 +0800
commit8e15d802865ed57db0018c15ea5559c8bd44c01f (patch)
tree48f4632a1ad044bd7f7f8da3ebe2bb03ab4ca6fe /danmaku/dmpkg/decode.go
parent88234ca8fffc4e120adbe0d38071b625ad2f43c7 (diff)
First working version. Just a POC.
Diffstat (limited to 'danmaku/dmpkg/decode.go')
-rw-r--r--danmaku/dmpkg/decode.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/danmaku/dmpkg/decode.go b/danmaku/dmpkg/decode.go
new file mode 100644
index 0000000..7d9f796
--- /dev/null
+++ b/danmaku/dmpkg/decode.go
@@ -0,0 +1,44 @@
+package dmpkg
+
+import (
+ "bytes"
+ "fmt"
+ "github.com/lunixbochs/struc"
+)
+
+func DecodeExchange(data []byte) (exc DanmakuExchange, err error) {
+ if ln := len(data); ln < kHeaderLength {
+ err = fmt.Errorf("incomplete datagram: length = %v < %v", ln, kHeaderLength)
+ return
+ }
+
+ // unpack header
+ var exchangeHeader DanmakuExchangeHeader
+ err = struc.Unpack(bytes.NewReader(data[:kHeaderLength]), &exchangeHeader)
+ if err != nil {
+ err = fmt.Errorf("cannot unpack exchange header: %w", err)
+ return
+ }
+ headerLength := exchangeHeader.HeaderLength
+
+ // validate header length, fail fast if not match
+ if headerLength != kHeaderLength {
+ err = fmt.Errorf("invalid header length, "+
+ "the protocol implementation might be obsolete: %v != %v", headerLength, kHeaderLength)
+ return
+ }
+
+ // special process
+ // TODO decouple this
+ // The server OpHeartbeatAck contains an extra 4-bytes header entry in the body, maybe a heat value
+ var body []byte
+ // copy body
+ body = make([]byte, exchangeHeader.Length-uint32(headerLength))
+ copy(body, data[headerLength:])
+
+ exc = DanmakuExchange{
+ DanmakuExchangeHeader: exchangeHeader,
+ Body: body,
+ }
+ return
+}