Ack and keep alive to not time out
This commit is contained in:
parent
199e8a39de
commit
cf6efb1e3f
|
@ -1,5 +1,7 @@
|
|||
package packet
|
||||
|
||||
import "slices"
|
||||
|
||||
const (
|
||||
packetFlagControl = 1
|
||||
packetFlagResend = 2
|
||||
|
@ -19,6 +21,43 @@ type PacketHeader struct {
|
|||
Ack int
|
||||
NumChunks int
|
||||
Token [4]byte
|
||||
|
||||
// connless
|
||||
ResponseToken [4]byte
|
||||
}
|
||||
|
||||
func (header *PacketHeader) Pack() []byte {
|
||||
flags := 0
|
||||
if header.Flags.Control {
|
||||
flags |= packetFlagControl
|
||||
}
|
||||
if header.Flags.Resend {
|
||||
flags |= packetFlagResend
|
||||
}
|
||||
if header.Flags.Compression {
|
||||
flags |= packetFlagCompression
|
||||
}
|
||||
if header.Flags.Connless {
|
||||
flags |= packetFlagConnless
|
||||
}
|
||||
|
||||
if header.Flags.Connless {
|
||||
version := 1
|
||||
return slices.Concat(
|
||||
[]byte{byte(((packetFlagConnless << 2) & 0x0fc) | (version & 0x03))},
|
||||
header.Token[:],
|
||||
header.ResponseToken[:],
|
||||
)
|
||||
}
|
||||
|
||||
return slices.Concat(
|
||||
[]byte{
|
||||
byte(((flags << 2) & 0xfc) | ((header.Ack >> 8) & 0x03)),
|
||||
byte(header.Ack & 0x0ff),
|
||||
byte(header.NumChunks),
|
||||
},
|
||||
header.Token[:],
|
||||
)
|
||||
}
|
||||
|
||||
func (header *PacketHeader) Unpack(packet []byte) {
|
||||
|
|
|
@ -6,7 +6,29 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
// pack
|
||||
// pack header
|
||||
|
||||
func TestPackHeader(t *testing.T) {
|
||||
header := PacketHeader{
|
||||
Flags: PacketFlags{
|
||||
Connless: false,
|
||||
Compression: false,
|
||||
Resend: false,
|
||||
Control: true,
|
||||
},
|
||||
Ack: 10,
|
||||
NumChunks: 0,
|
||||
Token: [4]byte{0xcf, 0x2e, 0xde, 0x1d},
|
||||
}
|
||||
got := header.Pack()
|
||||
want := []byte{0x04, 0x0a, 0x00, 0xcf, 0x2e, 0xde, 0x1d}
|
||||
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got %v, wanted %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// pack flags
|
||||
|
||||
func TestPackFlagsUnset(t *testing.T) {
|
||||
flags := PacketFlags{
|
||||
|
|
42
teeworlds.go
42
teeworlds.go
|
@ -18,13 +18,15 @@ import (
|
|||
const (
|
||||
maxPacksize = 1400
|
||||
|
||||
msgCtrlConnect = 0x01
|
||||
msgCtrlAccept = 0x02
|
||||
msgCtrlToken = 0x05
|
||||
msgCtrlClose = 0x04
|
||||
msgCtrlKeepAlive = 0x00
|
||||
msgCtrlConnect = 0x01
|
||||
msgCtrlAccept = 0x02
|
||||
msgCtrlToken = 0x05
|
||||
msgCtrlClose = 0x04
|
||||
|
||||
msgSysMapChange = 2
|
||||
msgSysConReady = 5
|
||||
msgSysMapChange = 2
|
||||
msgSysConReady = 5
|
||||
msgSysSnapSingle = 8
|
||||
|
||||
msgGameReadyToEnter = 8
|
||||
msgGameMotd = 1
|
||||
|
@ -68,12 +70,24 @@ type TeeworldsClient struct {
|
|||
clientToken [4]byte
|
||||
serverToken [4]byte
|
||||
conn net.Conn
|
||||
|
||||
Ack int
|
||||
}
|
||||
|
||||
func (client TeeworldsClient) sendCtrlMsg(data []byte) {
|
||||
flags := []byte{0x04, 0x00, 0x00}
|
||||
packet := slices.Concat(flags, client.serverToken[:], data)
|
||||
header := packet.PacketHeader{
|
||||
Flags: packet.PacketFlags{
|
||||
Connless: false,
|
||||
Compression: false,
|
||||
Resend: false,
|
||||
Control: true,
|
||||
},
|
||||
Ack: client.Ack,
|
||||
NumChunks: 0,
|
||||
Token: client.serverToken,
|
||||
}
|
||||
|
||||
packet := slices.Concat(header.Pack(), data)
|
||||
client.conn.Write(packet)
|
||||
}
|
||||
|
||||
|
@ -86,6 +100,10 @@ func (client TeeworldsClient) sendReady() {
|
|||
client.conn.Write(packet)
|
||||
}
|
||||
|
||||
func (client TeeworldsClient) sendKeepAlive() {
|
||||
client.sendCtrlMsg([]byte{msgCtrlKeepAlive})
|
||||
}
|
||||
|
||||
func (client TeeworldsClient) sendInfo() {
|
||||
info := []byte{0x40, 0x28, 0x01, 0x03, 0x30, 0x2E, 0x37, 0x20, 0x38, 0x30, 0x32, 0x66,
|
||||
0x31, 0x62, 0x65, 0x36, 0x30, 0x61, 0x30, 0x35, 0x36, 0x36, 0x35, 0x66,
|
||||
|
@ -176,6 +194,10 @@ func (client *TeeworldsClient) onSystemMsg(msg int, chunk chunk.Chunk, u *packer
|
|||
} else if msg == msgSysConReady {
|
||||
fmt.Println("got ready")
|
||||
client.sendStartInfo()
|
||||
} else if msg == msgSysSnapSingle {
|
||||
tick := u.GetInt()
|
||||
fmt.Printf("got snap single tick=%d\n", tick)
|
||||
client.sendKeepAlive()
|
||||
} else {
|
||||
fmt.Printf("unknown system message id=%d data=%x\n", msg, chunk.Data)
|
||||
}
|
||||
|
@ -193,6 +215,10 @@ func (client *TeeworldsClient) onGameMsg(msg int, chunk chunk.Chunk, u *packer.U
|
|||
func (client *TeeworldsClient) onMessage(chunk chunk.Chunk) {
|
||||
fmt.Printf("got chunk size=%d data=%v\n", chunk.Header.Size, chunk.Data)
|
||||
|
||||
if chunk.Header.Flags.Vital {
|
||||
client.Ack++
|
||||
}
|
||||
|
||||
u := packer.Unpacker{}
|
||||
u.Reset(chunk.Data)
|
||||
|
||||
|
|
Loading…
Reference in a new issue