Automatically compute chunk header size on pack
This commit is contained in:
parent
0f9cb77677
commit
7c4a3be31c
|
@ -3,11 +3,14 @@ package messages7
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ClStartInfo struct {
|
type ClStartInfo struct {
|
||||||
|
header *chunk7.ChunkHeader
|
||||||
|
|
||||||
Name string
|
Name string
|
||||||
Clan string
|
Clan string
|
||||||
Country int
|
Country int
|
||||||
|
@ -96,3 +99,11 @@ func (info *ClStartInfo) Unpack(u *packer.Unpacker) {
|
||||||
info.ColorFeet = u.GetInt()
|
info.ColorFeet = u.GetInt()
|
||||||
info.ColorEyes = u.GetInt()
|
info.ColorEyes = u.GetInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *ClStartInfo) Header() *chunk7.ChunkHeader {
|
||||||
|
return msg.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *ClStartInfo) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
msg.header = header
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package messages7
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
@ -37,3 +38,10 @@ func (msg CtrlAccept) Pack() []byte {
|
||||||
|
|
||||||
func (msg *CtrlAccept) Unpack(u *packer.Unpacker) {
|
func (msg *CtrlAccept) Unpack(u *packer.Unpacker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlAccept) Header() *chunk7.ChunkHeader {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlAccept) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package messages7
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
@ -39,3 +40,10 @@ func (msg CtrlConnect) Pack() []byte {
|
||||||
func (msg *CtrlConnect) Unpack(u *packer.Unpacker) {
|
func (msg *CtrlConnect) Unpack(u *packer.Unpacker) {
|
||||||
msg.Token = [4]byte(u.Data())
|
msg.Token = [4]byte(u.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlConnect) Header() *chunk7.ChunkHeader {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlConnect) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package messages7
|
package messages7
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
@ -30,3 +31,10 @@ func (msg CtrlKeepAlive) Pack() []byte {
|
||||||
|
|
||||||
func (msg *CtrlKeepAlive) Unpack(u *packer.Unpacker) {
|
func (msg *CtrlKeepAlive) Unpack(u *packer.Unpacker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlKeepAlive) Header() *chunk7.ChunkHeader {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlKeepAlive) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ package messages7
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
@ -42,3 +43,10 @@ func (msg CtrlToken) Pack() []byte {
|
||||||
func (msg *CtrlToken) Unpack(u *packer.Unpacker) {
|
func (msg *CtrlToken) Unpack(u *packer.Unpacker) {
|
||||||
msg.Token = [4]byte(u.Data())
|
msg.Token = [4]byte(u.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlToken) Header() *chunk7.ChunkHeader {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *CtrlToken) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package messages7
|
package messages7
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EnterGame struct {
|
type EnterGame struct {
|
||||||
|
header *chunk7.ChunkHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg EnterGame) MsgId() int {
|
func (msg EnterGame) MsgId() int {
|
||||||
|
@ -30,3 +32,11 @@ func (msg EnterGame) Pack() []byte {
|
||||||
|
|
||||||
func (msg *EnterGame) Unpack(u *packer.Unpacker) {
|
func (msg *EnterGame) Unpack(u *packer.Unpacker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *EnterGame) Header() *chunk7.ChunkHeader {
|
||||||
|
return msg.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *EnterGame) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
msg.header = header
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package messages7
|
package messages7
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
|
header *chunk7.ChunkHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg Info) MsgId() int {
|
func (msg Info) MsgId() int {
|
||||||
|
@ -37,3 +39,11 @@ func (msg *Info) Unpack(u *packer.Unpacker) {
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
panic("not implemented")
|
panic("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *Info) Header() *chunk7.ChunkHeader {
|
||||||
|
return msg.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *Info) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
msg.header = header
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package messages7
|
package messages7
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
@ -12,4 +13,7 @@ type NetMessage interface {
|
||||||
Vital() bool
|
Vital() bool
|
||||||
Pack() []byte
|
Pack() []byte
|
||||||
Unpack(u *packer.Unpacker)
|
Unpack(u *packer.Unpacker)
|
||||||
|
|
||||||
|
Header() *chunk7.ChunkHeader
|
||||||
|
SetHeader(chunkHeader *chunk7.ChunkHeader)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package messages7
|
package messages7
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Ready struct {
|
type Ready struct {
|
||||||
|
header *chunk7.ChunkHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg Ready) MsgId() int {
|
func (msg Ready) MsgId() int {
|
||||||
|
@ -30,3 +32,11 @@ func (msg Ready) Pack() []byte {
|
||||||
|
|
||||||
func (msg *Ready) Unpack(u *packer.Unpacker) {
|
func (msg *Ready) Unpack(u *packer.Unpacker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *Ready) Header() *chunk7.ChunkHeader {
|
||||||
|
return msg.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *Ready) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
msg.header = header
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package messages7
|
package messages7
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReadyToEnter struct {
|
type ReadyToEnter struct {
|
||||||
|
header *chunk7.ChunkHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg ReadyToEnter) MsgId() int {
|
func (msg ReadyToEnter) MsgId() int {
|
||||||
|
@ -30,3 +32,11 @@ func (msg ReadyToEnter) Pack() []byte {
|
||||||
|
|
||||||
func (msg *ReadyToEnter) Unpack(u *packer.Unpacker) {
|
func (msg *ReadyToEnter) Unpack(u *packer.Unpacker) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *ReadyToEnter) Header() *chunk7.ChunkHeader {
|
||||||
|
return msg.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *ReadyToEnter) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
msg.header = header
|
||||||
|
}
|
||||||
|
|
|
@ -3,11 +3,14 @@ package messages7
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SvChat struct {
|
type SvChat struct {
|
||||||
|
header *chunk7.ChunkHeader
|
||||||
|
|
||||||
Mode int
|
Mode int
|
||||||
ClientId int
|
ClientId int
|
||||||
TargetId int
|
TargetId int
|
||||||
|
@ -45,3 +48,11 @@ func (msg *SvChat) Unpack(u *packer.Unpacker) {
|
||||||
msg.TargetId = u.GetInt()
|
msg.TargetId = u.GetInt()
|
||||||
msg.Message = u.GetString()
|
msg.Message = u.GetString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *SvChat) Header() *chunk7.ChunkHeader {
|
||||||
|
return msg.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *SvChat) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
msg.header = header
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package messages7
|
package messages7
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
"github.com/teeworlds-go/teeworlds/network7"
|
"github.com/teeworlds-go/teeworlds/network7"
|
||||||
"github.com/teeworlds-go/teeworlds/packer"
|
"github.com/teeworlds-go/teeworlds/packer"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SvClientInfo struct {
|
type SvClientInfo struct {
|
||||||
|
header *chunk7.ChunkHeader
|
||||||
|
|
||||||
ClientId int
|
ClientId int
|
||||||
Local bool
|
Local bool
|
||||||
Team int
|
Team int
|
||||||
|
@ -74,3 +77,11 @@ func (info *SvClientInfo) Unpack(u *packer.Unpacker) {
|
||||||
info.ColorEyes = u.GetInt()
|
info.ColorEyes = u.GetInt()
|
||||||
info.Silent = u.GetInt() != 0
|
info.Silent = u.GetInt() != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (msg *SvClientInfo) Header() *chunk7.ChunkHeader {
|
||||||
|
return msg.header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (msg *SvClientInfo) SetHeader(header *chunk7.ChunkHeader) {
|
||||||
|
msg.header = header
|
||||||
|
}
|
||||||
|
|
|
@ -55,17 +55,20 @@ func PackChunk(msg messages7.NetMessage, connection *Connection) []byte {
|
||||||
msgAndSys := packer.PackInt(msgId)
|
msgAndSys := packer.PackInt(msgId)
|
||||||
payload := msg.Pack()
|
payload := msg.Pack()
|
||||||
|
|
||||||
// TODO: support resend
|
if msg.Header() == nil {
|
||||||
chunkHeader := chunk7.ChunkHeader{
|
header := &chunk7.ChunkHeader{
|
||||||
Flags: chunk7.ChunkFlags{
|
Flags: chunk7.ChunkFlags{
|
||||||
Vital: msg.Vital(),
|
Vital: msg.Vital(),
|
||||||
},
|
},
|
||||||
Size: len(msgAndSys) + len(payload),
|
|
||||||
Seq: connection.Sequence,
|
Seq: connection.Sequence,
|
||||||
}
|
}
|
||||||
|
msg.SetHeader(header)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.Header().Size = len(msgAndSys) + len(payload)
|
||||||
|
|
||||||
data := slices.Concat(
|
data := slices.Concat(
|
||||||
chunkHeader.Pack(),
|
msg.Header().Pack(),
|
||||||
msgAndSys,
|
msgAndSys,
|
||||||
payload,
|
payload,
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,8 +4,76 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/teeworlds-go/teeworlds/chunk7"
|
||||||
|
"github.com/teeworlds-go/teeworlds/messages7"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// update chunk headers
|
||||||
|
|
||||||
|
func TestPackUpdateChunkHeaders(t *testing.T) {
|
||||||
|
// The chunk header is nil by default
|
||||||
|
packet := Packet{}
|
||||||
|
packet.Messages = append(packet.Messages, &messages7.SvChat{Message: "foo"})
|
||||||
|
|
||||||
|
{
|
||||||
|
got := packet.Messages[0].Header()
|
||||||
|
|
||||||
|
if got != nil {
|
||||||
|
t.Errorf("got %v, wanted %v", got, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// When packing the chunk header will be set automatically
|
||||||
|
// Based on the current context
|
||||||
|
conn := &Connection{Sequence: 1}
|
||||||
|
packet.Pack(conn)
|
||||||
|
|
||||||
|
{
|
||||||
|
got := packet.Messages[0].Header()
|
||||||
|
want := &chunk7.ChunkHeader{
|
||||||
|
Flags: chunk7.ChunkFlags{
|
||||||
|
Vital: true,
|
||||||
|
},
|
||||||
|
Size: 8,
|
||||||
|
Seq: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("got %v, wanted %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the chunk header is already set
|
||||||
|
// Packing will only update the size
|
||||||
|
|
||||||
|
var chat *messages7.SvChat
|
||||||
|
var ok bool
|
||||||
|
if chat, ok = packet.Messages[0].(*messages7.SvChat); ok {
|
||||||
|
chat.Message = "hello world"
|
||||||
|
packet.Messages[0] = chat
|
||||||
|
} else {
|
||||||
|
t.Fatal("failed to cast chat message")
|
||||||
|
}
|
||||||
|
|
||||||
|
packet.Pack(conn)
|
||||||
|
|
||||||
|
{
|
||||||
|
got := packet.Messages[0].Header()
|
||||||
|
want := &chunk7.ChunkHeader{
|
||||||
|
Flags: chunk7.ChunkFlags{
|
||||||
|
Vital: true,
|
||||||
|
},
|
||||||
|
Size: 16,
|
||||||
|
Seq: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(got, want) {
|
||||||
|
t.Errorf("got %v, wanted %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// pack header
|
// pack header
|
||||||
|
|
||||||
func TestPackHeader(t *testing.T) {
|
func TestPackHeader(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue