go-teeworlds-protocol/chunk/chunk.go

36 lines
661 B
Go
Raw Normal View History

2024-06-07 07:38:35 +00:00
package chunk
const (
2024-06-17 04:24:08 +00:00
chunkFlagVital = 1
2024-06-07 07:38:35 +00:00
chunkFlagResend = 2
)
type ChunkFlags struct {
2024-06-17 04:24:08 +00:00
Vital bool
2024-06-07 07:38:35 +00:00
Resend bool
}
type ChunkHeader struct {
Flags ChunkFlags
Size int
// sequence number
// will be acknowledged in the packet header ack
2024-06-17 04:24:08 +00:00
Seq int
2024-06-07 07:38:35 +00:00
}
type Chunk struct {
Header ChunkHeader
2024-06-17 04:24:08 +00:00
Data []byte
2024-06-07 07:38:35 +00:00
}
func (header *ChunkHeader) Unpack(data []byte) {
flagBits := (data[0] >> 6) & 0x03
header.Flags.Vital = (flagBits & chunkFlagVital) != 0
header.Flags.Resend = (flagBits & chunkFlagResend) != 0
2024-06-17 04:24:08 +00:00
header.Size = (int(data[0]&0x3F) << 6) | (int(data[1]) & 0x3F)
2024-06-07 07:38:35 +00:00
if header.Flags.Vital {
2024-06-17 04:24:08 +00:00
header.Seq = int((data[1]&0xC0)<<2) | int(data[2])
2024-06-07 07:38:35 +00:00
}
}