53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package gbxremote
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"log"
|
|
"net"
|
|
"strconv"
|
|
|
|
"git.fanyx.xyz/fanyx/goufy/pkg/config"
|
|
)
|
|
|
|
type GbxClient struct {
|
|
conn net.Conn
|
|
reqHandle int
|
|
}
|
|
|
|
func (client *GbxClient) Connect(c config.AppConfig) {
|
|
// create connection string from config
|
|
gbxConnString := c.Trackmania.Host + ":" + strconv.FormatInt(int64(c.Trackmania.Port), 10)
|
|
|
|
var err error
|
|
//initiate GbxClient
|
|
client.conn, err = net.Dial("tcp", gbxConnString)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer client.conn.Close()
|
|
|
|
// handshake
|
|
buf := make([]byte, 4)
|
|
_, err = client.conn.Read(buf)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
// read handshake length
|
|
len := binary.LittleEndian.Uint32(buf)
|
|
// break handshake if length over 64
|
|
if len > 64 {
|
|
log.Fatal("Handshake is not GBXRemote")
|
|
}
|
|
// otherwise continue to read protocol string
|
|
buf = make([]byte, len)
|
|
_, err = client.conn.Read(buf)
|
|
|
|
p := string(buf[:])
|
|
if p != "GBXRemote 2" {
|
|
log.Fatal("Protocol not GBXRemote 2")
|
|
}
|
|
|
|
log.Println("Connection established!")
|
|
log.Println("Host:", c.Trackmania.Host, "- Port:", c.Trackmania.Port)
|
|
}
|