Remove network dependency from protocol parser

Following the sans io principle
https://sans-io.readthedocs.io/
This commit is contained in:
ChillerDragon 2024-06-20 11:19:54 +08:00
parent ec59c03379
commit 79d6308a4a
3 changed files with 31 additions and 27 deletions

View file

@ -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}
}

View file

@ -3,9 +3,7 @@ package protocol7
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"net"
"os" "os"
"slices"
"github.com/teeworlds-go/huffman" "github.com/teeworlds-go/huffman"
"github.com/teeworlds-go/teeworlds/chunk7" "github.com/teeworlds-go/teeworlds/chunk7"
@ -21,7 +19,6 @@ type Player struct {
type Connection struct { type Connection struct {
ClientToken [4]byte ClientToken [4]byte
ServerToken [4]byte ServerToken [4]byte
Conn net.Conn
// The amount of vital chunks received // The amount of vital chunks received
Ack int Ack int
@ -64,27 +61,6 @@ func (connection *Connection) CtrlToken() *Packet {
return response 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 { func (client *Connection) MsgStartInfo() messages7.ClStartInfo {
return messages7.ClStartInfo{ return messages7.ClStartInfo{
Name: "gopher", Name: "gopher",
@ -129,7 +105,7 @@ func (connection *Connection) OnSystemMsg(msg int, chunk chunk7.Chunk, u *packer
} else if msg == network7.MsgSysSnapSingle { } else if msg == network7.MsgSysSnapSingle {
// tick := u.GetInt() // tick := u.GetInt()
// fmt.Printf("got snap single tick=%d\n", tick) // fmt.Printf("got snap single tick=%d\n", tick)
connection.SendKeepAlive() response.Messages = append(response.Messages, messages7.CtrlKeepAlive{})
} else { } else {
fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data) fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data)
} }

View file

@ -53,12 +53,11 @@ func main() {
client := &protocol7.Connection{ client := &protocol7.Connection{
ClientToken: [4]byte{0x01, 0x02, 0x03, 0x04}, ClientToken: [4]byte{0x01, 0x02, 0x03, 0x04},
ServerToken: [4]byte{0xff, 0xff, 0xff, 0xff}, ServerToken: [4]byte{0xff, 0xff, 0xff, 0xff},
Conn: conn,
Ack: 0, Ack: 0,
Players: make([]protocol7.Player, network7.MaxClients), Players: make([]protocol7.Player, network7.MaxClients),
} }
go readNetwork(ch, client.Conn) go readNetwork(ch, conn)
packet := client.CtrlToken() packet := client.CtrlToken()
conn.Write(packet.Pack(client)) conn.Write(packet.Pack(client))