go-teeworlds-protocol/teeworlds.go

129 lines
2.6 KiB
Go
Raw Normal View History

2024-06-02 02:58:09 +00:00
package main
2024-06-01 02:34:53 +00:00
import (
"bufio"
"fmt"
"net"
2024-06-01 03:01:48 +00:00
"os"
"strconv"
2024-06-01 02:34:53 +00:00
"time"
"github.com/teeworlds-go/go-teeworlds-protocol/messages7"
"github.com/teeworlds-go/go-teeworlds-protocol/network7"
"github.com/teeworlds-go/go-teeworlds-protocol/protocol7"
2024-06-01 02:34:53 +00:00
)
const (
2024-06-01 03:45:32 +00:00
maxPacksize = 1400
2024-06-01 02:34:53 +00:00
)
func getConnection(serverIp string, serverPort int) (net.Conn, error) {
conn, err := net.Dial("udp", fmt.Sprintf("%s:%d", serverIp, serverPort))
2024-06-01 02:34:53 +00:00
if err != nil {
fmt.Printf("Some error %v", err)
}
2024-06-01 03:01:48 +00:00
return conn, err
}
2024-06-01 02:34:53 +00:00
func readNetwork(ch chan<- []byte, conn net.Conn) {
buf := make([]byte, maxPacksize)
2024-06-01 02:34:53 +00:00
for {
len, err := bufio.NewReader(conn).Read(buf)
packet := make([]byte, len)
copy(packet[:], buf[:])
2024-06-01 02:34:53 +00:00
if err == nil {
ch <- packet
} else {
fmt.Printf("Some error %v\n", err)
break
}
}
conn.Close()
}
func main() {
ch := make(chan []byte, maxPacksize)
serverIp := "127.0.0.1"
serverPort := 8303
if len(os.Args) > 1 {
if os.Args[1][0] == '-' {
fmt.Println("usage: ./teeworlds [serverIp] [serverPort]")
os.Exit(1)
}
serverIp = os.Args[1]
}
if len(os.Args) > 2 {
var err error
serverPort, err = strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
}
conn, err := getConnection(serverIp, serverPort)
2024-06-01 03:01:48 +00:00
if err != nil {
fmt.Printf("error connecting %v\n", err)
os.Exit(1)
}
client := &protocol7.Connection{
ClientToken: [4]byte{0x01, 0x02, 0x03, 0x04},
ServerToken: [4]byte{0xff, 0xff, 0xff, 0xff},
2024-06-18 05:08:56 +00:00
Ack: 0,
2024-06-19 04:57:32 +00:00
Players: make([]protocol7.Player, network7.MaxClients),
2024-06-01 03:45:32 +00:00
}
go readNetwork(ch, conn)
2024-06-01 03:01:48 +00:00
2024-06-20 04:29:26 +00:00
tokenPacket := client.CtrlToken()
conn.Write(tokenPacket.Pack(client))
2024-06-01 02:34:53 +00:00
for {
time.Sleep(10_000_000)
select {
case msg := <-ch:
packet := &protocol7.Packet{}
err := packet.Unpack(msg)
2024-06-20 03:08:52 +00:00
if err != nil {
panic(err)
}
response := client.OnPacket(packet)
if response != nil {
2024-06-20 05:27:46 +00:00
// example of inspecting incoming trafic
2024-06-22 04:38:17 +00:00
for _, msg := range packet.Messages {
switch msg := msg.(type) {
case *messages7.SvChat:
// inspect incoming traffic
fmt.Printf("got incoming chat msg: %s\n", msg.Message)
default:
2024-06-20 05:27:46 +00:00
}
}
// example of modifying outgoing traffic
for i, msg := range response.Messages {
2024-06-22 04:38:17 +00:00
switch msg := msg.(type) {
case *messages7.SvChat:
// inspect outgoing traffic
fmt.Printf("got outgoing chat msg: %s\n", msg.Message)
// change outgoing traffic
msg.Message += " (edited by go)"
packet.Messages[i] = msg
default:
}
}
if len(response.Messages) > 0 || response.Header.Flags.Resend {
conn.Write(response.Pack(client))
}
2024-06-20 03:08:52 +00:00
}
2024-06-01 02:34:53 +00:00
default:
// do nothing
}
}
}