From 4d05818f15a142477d1e5afef5f27798db46f8cc Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Fri, 21 Jun 2024 11:00:40 +0800 Subject: [PATCH] Compress packet payload if compression flag is set --- protocol7/packet.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/protocol7/packet.go b/protocol7/packet.go index b35cda1..138d405 100644 --- a/protocol7/packet.go +++ b/protocol7/packet.go @@ -3,6 +3,7 @@ package protocol7 import ( "slices" + "github.com/teeworlds-go/huffman" "github.com/teeworlds-go/teeworlds/chunk7" "github.com/teeworlds-go/teeworlds/messages7" "github.com/teeworlds-go/teeworlds/network7" @@ -96,6 +97,16 @@ func (packet *Packet) Pack(connection *Connection) []byte { packet.Header.Flags.Control = true } + if packet.Header.Flags.Compression { + // TODO: store huffman object in connection to avoid reallocating memory + huff := huffman.Huffman{} + var err error + payload, err = huff.Compress(payload) + if err != nil { + panic(err) + } + } + return slices.Concat( packet.Header.Pack(), payload,