Compress packet payload if compression flag is set

This commit is contained in:
ChillerDragon 2024-06-21 11:00:40 +08:00
parent 4f37d5de48
commit 4d05818f15

View file

@ -3,6 +3,7 @@ package protocol7
import ( import (
"slices" "slices"
"github.com/teeworlds-go/huffman"
"github.com/teeworlds-go/teeworlds/chunk7" "github.com/teeworlds-go/teeworlds/chunk7"
"github.com/teeworlds-go/teeworlds/messages7" "github.com/teeworlds-go/teeworlds/messages7"
"github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/network7"
@ -96,6 +97,16 @@ func (packet *Packet) Pack(connection *Connection) []byte {
packet.Header.Flags.Control = true 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( return slices.Concat(
packet.Header.Pack(), packet.Header.Pack(),
payload, payload,