gottdad/packet.go

126 lines
2.7 KiB
Go
Raw Normal View History

2020-06-14 16:27:57 +02:00
package main
2020-06-14 16:45:07 +02:00
import (
"bytes"
2020-06-14 17:23:07 +02:00
"encoding/binary"
2020-06-14 16:45:07 +02:00
)
2020-06-14 16:27:57 +02:00
const (
AdminPacketAdminJoin uint8 = 0
AdminPacketAdminQuit
AdminPacketAdminUpdateFrequency
AdminPacketAdminPoll
AdminPacketAdminChat
AdminPacketAdminRCon
AdminPacketAdminGamescript
AdminPacketAdminPing
AdminPacketServerFull = 100
AdminPacketServerBanned
AdminPacketServerError
AdminPacketServerProtocol
AdminPacketServerWelcome
AdminPacketServerNewgame
AdminPacketServerShutdown
AdminPacketServerDate
AdminPacketServerClientJoin
AdminPacketServerClientInfo
AdminPacketServerClientUpdate
AdminPacketServerClientQuit
AdminPacketServerClientError
AdminPacketServerCompanyNew
AdminPacketServerCompanyInfo
AdminPacketServerCompanyUpdate
AdminPacketServerCompanyRemove
AdminPacketServerCompanyEconomy
AdminPacketServerCompanyStats
AdminPacketServerChat
AdminPacketServerRCon
AdminPacketServerConsole
AdminPacketServerCmdNames
AdminPacketServerCmdLogging
AdminPacketServerGamescript
AdminPacketServerRConEnd
AdminPacketServerPong
InvalidAdminPacket = 0xFF
)
2020-06-14 16:45:07 +02:00
type Packet struct {
PLength uint16
PType uint8
}
type PacketAdminJoin struct {
Packet
2020-06-14 17:12:58 +02:00
Password string
AppName string
AppVersion string
2020-06-14 16:45:07 +02:00
}
2020-06-14 23:31:26 +02:00
type PacketAdminServerProtocol struct {
Packet
ProtocolVersion uint8
FurtherData bool
UpdatePacketType uint16
FrequenciesAllowed uint16
}
type PacketAdminServerWelcome struct {
Packet
ServerName string
OpenTTDVersion string
Dedicated bool
MapName string
MapSeed uint32
MapLandscape uint8
MapStartDate uint32
MapX uint16
MapY uint16
}
2020-06-14 16:45:07 +02:00
func (p *PacketAdminJoin) Bytes() []byte {
buf := new(bytes.Buffer)
2020-06-14 17:38:58 +02:00
p.PLength = uint16(len(p.Password) + len(p.AppName) + len(p.AppVersion) + 6)
2020-06-14 17:23:07 +02:00
2020-06-14 17:31:47 +02:00
binary.Write(buf, binary.LittleEndian, p.PLength)
binary.Write(buf, binary.LittleEndian, p.PType)
2020-06-14 17:23:07 +02:00
buf.WriteString(p.Password)
2020-06-14 17:34:19 +02:00
buf.WriteByte(0)
2020-06-14 17:23:07 +02:00
buf.WriteString(p.AppName)
2020-06-14 17:34:19 +02:00
buf.WriteByte(0)
2020-06-14 17:23:07 +02:00
buf.WriteString(p.AppVersion)
2020-06-14 17:34:19 +02:00
buf.WriteByte(0)
2020-06-14 17:12:58 +02:00
2020-06-14 16:45:07 +02:00
return buf.Bytes()
}
2020-06-14 23:31:26 +02:00
func (p *AdminPacketServerWelcome) Read(b []byte) {
r := bufio.NewReader(bytes.NewReader(b))
p.ServerName, _ = r.ReadString(0)
p.OpenTTDVersion, _ = r.ReadString(0)
c, _ := r.ReadByte()
p.Dedicated = bool(c)
p.MapName, _ = r.ReadString(0)
bs := make([]byte, 13)
_, _ = r.Read(bs)
p.MapSeed = binary.LittleEndian.Uint32(bs[0:])
p.MapLandscape = uint8(bs[4:1])
p.MapStartDate = binary.LittleEndian.Uint32(bs[5:])
p.MapX = binary.LittleEndian.Uint16(bs[9:])
p.MapY = binary.LittleEndian.Uint16(bs[11:])
}
func handlePacket(p Packet, b []byte) {
switch p.PType {
case AdminPacketServerProtocol:
case AdminPacketServerWelcome:
sp := PacketAdminServerWelcome{
2020-06-14 23:32:22 +02:00
Packet: p,
2020-06-14 23:31:26 +02:00
}
sp.Read(b)
logInfoDebug("AdminPacketServerWelcome : %v", sp)
default:
}
}