From 79d6308a4a651b69cb283b4408d3dcc3f69fd74f Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Thu, 20 Jun 2024 11:19:54 +0800 Subject: [PATCH] Remove network dependency from protocol parser Following the sans io principle https://sans-io.readthedocs.io/ --- messages7/ctrl_keep_alive.go | 29 +++++++++++++++++++++++++++++ protocol7/connection.go | 26 +------------------------- teeworlds.go | 3 +-- 3 files changed, 31 insertions(+), 27 deletions(-) create mode 100644 messages7/ctrl_keep_alive.go diff --git a/messages7/ctrl_keep_alive.go b/messages7/ctrl_keep_alive.go new file mode 100644 index 0000000..d3ff73a --- /dev/null +++ b/messages7/ctrl_keep_alive.go @@ -0,0 +1,29 @@ +package messages7 + +import ( + "github.com/teeworlds-go/teeworlds/network7" +) + +type CtrlKeepAlive struct { +} + +func (msg CtrlKeepAlive) MsgId() int { + return network7.MsgCtrlKeepAlive +} + +func (msg CtrlKeepAlive) MsgType() network7.MsgType { + return network7.TypeControl +} + +func (msg CtrlKeepAlive) System() bool { + return false +} + +func (msg CtrlKeepAlive) Vital() bool { + return false +} + +func (msg CtrlKeepAlive) Pack() []byte { + return []byte{network7.MsgCtrlKeepAlive} +} + diff --git a/protocol7/connection.go b/protocol7/connection.go index 2ba7633..009557f 100644 --- a/protocol7/connection.go +++ b/protocol7/connection.go @@ -3,9 +3,7 @@ package protocol7 import ( "bytes" "fmt" - "net" "os" - "slices" "github.com/teeworlds-go/huffman" "github.com/teeworlds-go/teeworlds/chunk7" @@ -21,7 +19,6 @@ type Player struct { type Connection struct { ClientToken [4]byte ServerToken [4]byte - Conn net.Conn // The amount of vital chunks received Ack int @@ -64,27 +61,6 @@ func (connection *Connection) CtrlToken() *Packet { return response } -func (c *Connection) SendCtrlMsg(data []byte) { - header := PacketHeader{ - Flags: PacketFlags{ - Connless: false, - Compression: false, - Resend: false, - Control: true, - }, - Ack: c.Ack, - NumChunks: 0, - Token: c.ServerToken, - } - - packet := slices.Concat(header.Pack(), data) - c.Conn.Write(packet) -} - -func (c *Connection) SendKeepAlive() { - c.SendCtrlMsg([]byte{byte(network7.MsgCtrlKeepAlive)}) -} - func (client *Connection) MsgStartInfo() messages7.ClStartInfo { return messages7.ClStartInfo{ Name: "gopher", @@ -129,7 +105,7 @@ func (connection *Connection) OnSystemMsg(msg int, chunk chunk7.Chunk, u *packer } else if msg == network7.MsgSysSnapSingle { // tick := u.GetInt() // fmt.Printf("got snap single tick=%d\n", tick) - connection.SendKeepAlive() + response.Messages = append(response.Messages, messages7.CtrlKeepAlive{}) } else { fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data) } diff --git a/teeworlds.go b/teeworlds.go index cefc356..6ecaf54 100644 --- a/teeworlds.go +++ b/teeworlds.go @@ -53,12 +53,11 @@ func main() { client := &protocol7.Connection{ ClientToken: [4]byte{0x01, 0x02, 0x03, 0x04}, ServerToken: [4]byte{0xff, 0xff, 0xff, 0xff}, - Conn: conn, Ack: 0, Players: make([]protocol7.Player, network7.MaxClients), } - go readNetwork(ch, client.Conn) + go readNetwork(ch, conn) packet := client.CtrlToken() conn.Write(packet.Pack(client))