From 54d86fd1dff88778d32a7d462ec60f1f1733d3f9 Mon Sep 17 00:00:00 2001 From: fanyx Date: Thu, 8 Apr 2021 23:02:56 +0200 Subject: [PATCH] able to establish connection to trackmania server --- main.go | 8 +++--- pkg/connection/connection.go | 37 -------------------------- pkg/gbxremote/gbxremote.go | 51 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 40 deletions(-) delete mode 100644 pkg/connection/connection.go create mode 100644 pkg/gbxremote/gbxremote.go diff --git a/main.go b/main.go index db794c0..8f3143e 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,7 @@ package main import ( "git.fanyx.xyz/fanyx/goufy/pkg/config" - "git.fanyx.xyz/fanyx/goufy/pkg/connection" + "git.fanyx.xyz/fanyx/goufy/pkg/gbxremote" ) func main() { @@ -10,8 +10,10 @@ func main() { var conf config.AppConfig conf.ReadConfig() - //initiate connection to trackmania server - var client connection.GbxClient + //fmt.Printf("%#v\n", conf) + + // initial connection to trackmania server + var client gbxremote.GbxClient client.Connect(conf) } diff --git a/pkg/connection/connection.go b/pkg/connection/connection.go deleted file mode 100644 index bc8f53d..0000000 --- a/pkg/connection/connection.go +++ /dev/null @@ -1,37 +0,0 @@ -package connection - -import ( - "fmt" - "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) - } - - fmt.Println("read content:", buf) -} diff --git a/pkg/gbxremote/gbxremote.go b/pkg/gbxremote/gbxremote.go new file mode 100644 index 0000000..8d0467b --- /dev/null +++ b/pkg/gbxremote/gbxremote.go @@ -0,0 +1,51 @@ +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 successfully!") +}