From 8f4004a2cec77c2d9b67b4bd54e7e6190691335a Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Wed, 19 Jun 2024 12:57:32 +0800 Subject: [PATCH] Move constants to network7 package --- network7/network7.go | 25 +++++++++++++++++ packer/packer.go | 6 ++++ protocol7/connection.go | 61 ++++++++++++++--------------------------- teeworlds.go | 5 ++-- 4 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 network7/network7.go diff --git a/network7/network7.go b/network7/network7.go new file mode 100644 index 0000000..0bc4b92 --- /dev/null +++ b/network7/network7.go @@ -0,0 +1,25 @@ +package network7 + +const ( + MaxClients = 64 + + MsgCtrlKeepAlive ControlMsg = 0x00 + MsgCtrlConnect ControlMsg = 0x01 + MsgCtrlAccept ControlMsg = 0x02 + MsgCtrlToken ControlMsg = 0x05 + MsgCtrlClose ControlMsg = 0x04 + + MsgSysMapChange NetMsg = 2 + MsgSysConReady NetMsg = 5 + MsgSysSnapSingle NetMsg = 8 + + MsgGameSvMotd NetMsg = 1 + MsgGameSvChat NetMsg = 3 + MsgGameReadyToEnter NetMsg = 8 + MsgGameSvClientInfo NetMsg = 18 + MsgGameClStartInfo NetMsg = 27 +) + +type ControlMsg int +type NetMsg int + diff --git a/packer/packer.go b/packer/packer.go index 852d8fa..5b51647 100644 --- a/packer/packer.go +++ b/packer/packer.go @@ -2,6 +2,8 @@ package packer import ( "slices" + + "github.com/teeworlds-go/teeworlds/network7" ) type Unpacker struct { @@ -125,6 +127,10 @@ func PackBool(b bool) []byte { return []byte{0x00} } +func PackMsg(msg network7.NetMsg) []byte { + return PackInt(int(msg)) +} + func PackInt(num int) []byte { res := []byte{0x00} idx := 0 diff --git a/protocol7/connection.go b/protocol7/connection.go index 28eb981..534ab8f 100644 --- a/protocol7/connection.go +++ b/protocol7/connection.go @@ -10,30 +10,11 @@ import ( "github.com/teeworlds-go/huffman" "github.com/teeworlds-go/teeworlds/chunk" message "github.com/teeworlds-go/teeworlds/messages" + "github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/packer" "github.com/teeworlds-go/teeworlds/packet" ) -const ( - MaxClients = 64 - - msgCtrlKeepAlive = 0x00 - msgCtrlConnect = 0x01 - msgCtrlAccept = 0x02 - msgCtrlToken = 0x05 - msgCtrlClose = 0x04 - - msgSysMapChange = 2 - msgSysConReady = 5 - msgSysSnapSingle = 8 - - msgGameSvMotd = 1 - msgGameSvChat = 3 - msgGameReadyToEnter = 8 - msgGameSvClientInfo = 18 - msgGameClStartInfo = 27 -) - type Player struct { Info message.SvClientInfo } @@ -81,7 +62,7 @@ func (c *Connection) SendCtrlMsg(data []byte) { } func (c *Connection) SendKeepAlive() { - c.SendCtrlMsg([]byte{msgCtrlKeepAlive}) + c.SendCtrlMsg([]byte{byte(network7.MsgCtrlKeepAlive)}) } func (c *Connection) SendReady() { @@ -92,7 +73,7 @@ func (c *Connection) SendReady() { } type ChunkArgs struct { - MsgId int + MsgId network7.NetMsg System bool Flags chunk.ChunkFlags Payload []byte @@ -105,7 +86,7 @@ func (client *Connection) PackChunk(c ChunkArgs) []byte { } client.Sequence++ - msgAndSys := packer.PackInt(c.MsgId) + msgAndSys := packer.PackMsg(c.MsgId) chunkHeader := chunk.ChunkHeader{ Flags: c.Flags, @@ -177,7 +158,7 @@ func (client *Connection) SendStartInfo() { } payload := client.PackChunk(ChunkArgs{ - MsgId: msgGameClStartInfo, + MsgId: network7.MsgGameClStartInfo, Flags: chunk.ChunkFlags{ Vital: true, }, @@ -203,14 +184,14 @@ func byteSliceToString(s []byte) string { return string(s) } -func (client *Connection) OnSystemMsg(msg int, chunk chunk.Chunk, u *packer.Unpacker) { - if msg == msgSysMapChange { +func (client *Connection) OnSystemMsg(msg network7.NetMsg, chunk chunk.Chunk, u *packer.Unpacker) { + if msg == network7.MsgSysMapChange { fmt.Println("got map change") client.SendReady() - } else if msg == msgSysConReady { + } else if msg == network7.MsgSysConReady { fmt.Println("got ready") client.SendStartInfo() - } else if msg == msgSysSnapSingle { + } else if msg == network7.MsgSysSnapSingle { // tick := u.GetInt() // fmt.Printf("got snap single tick=%d\n", tick) client.SendKeepAlive() @@ -228,22 +209,22 @@ func (client *Connection) OnMotd(motd string) { fmt.Printf("[motd] %s\n", motd) } -func (client *Connection) OnGameMsg(msg int, chunk chunk.Chunk, u *packer.Unpacker) { - if msg == msgGameReadyToEnter { +func (client *Connection) OnGameMsg(msg network7.NetMsg, chunk chunk.Chunk, u *packer.Unpacker) { + if msg == network7.MsgGameReadyToEnter { fmt.Println("got ready to enter") client.SendEnterGame() - } else if msg == msgGameSvMotd { + } else if msg == network7.MsgGameSvMotd { motd := u.GetString() if motd != "" { client.OnMotd(motd) } - } else if msg == msgGameSvChat { + } else if msg == network7.MsgGameSvChat { mode := u.GetInt() clientId := u.GetInt() targetId := u.GetInt() message := u.GetString() client.OnChatMessage(mode, clientId, targetId, message) - } else if msg == msgGameSvClientInfo { + } else if msg == network7.MsgGameSvClientInfo { clientId := packer.UnpackInt(chunk.Data[1:]) client.Players[clientId].Info.Unpack(u) @@ -269,9 +250,9 @@ func (client *Connection) OnMessage(chunk chunk.Chunk) { msg >>= 1 if sys { - client.OnSystemMsg(msg, chunk, &u) + client.OnSystemMsg(network7.NetMsg(msg), chunk, &u) } else { - client.OnGameMsg(msg, chunk, &u) + client.OnGameMsg(network7.NetMsg(msg), chunk, &u) } } @@ -290,16 +271,16 @@ func (client *Connection) OnPacket(data []byte) { header.Unpack(headerRaw) if header.Flags.Control { - ctrlMsg := payload[0] + ctrlMsg := network7.ControlMsg(payload[0]) fmt.Printf("got ctrl msg %d\n", ctrlMsg) - if ctrlMsg == msgCtrlToken { + if ctrlMsg == network7.MsgCtrlToken { copy(client.ServerToken[:], payload[1:5]) fmt.Printf("got server token %x\n", client.ServerToken) - client.SendCtrlMsg(slices.Concat([]byte{msgCtrlConnect}, client.ClientToken[:])) - } else if ctrlMsg == msgCtrlAccept { + client.SendCtrlMsg(slices.Concat([]byte{byte(network7.MsgCtrlConnect)}, client.ClientToken[:])) + } else if ctrlMsg == network7.MsgCtrlAccept { fmt.Println("got accept") client.SendInfo() - } else if ctrlMsg == msgCtrlClose { + } else if ctrlMsg == network7.MsgCtrlClose { // TODO: get length from packet header to determine if a reason is set or not // len(data) -> is 1400 (maxPacketLen) diff --git a/teeworlds.go b/teeworlds.go index 0c5bfe0..0cdcac0 100644 --- a/teeworlds.go +++ b/teeworlds.go @@ -7,13 +7,12 @@ import ( "os" "time" + "github.com/teeworlds-go/teeworlds/network7" "github.com/teeworlds-go/teeworlds/protocol7" ) const ( maxPacksize = 1400 - - MaxClients = 64 ) func getConnection() (net.Conn, error) { @@ -56,7 +55,7 @@ func main() { ServerToken: [4]byte{0xff, 0xff, 0xff, 0xff}, Conn: conn, Ack: 0, - Players: make([]protocol7.Player, MaxClients), + Players: make([]protocol7.Player, network7.MaxClients), } go readNetwork(ch, client.Conn)