From 58499493d851d514f612828250e44306dcccfa00 Mon Sep 17 00:00:00 2001 From: fanyx Date: Wed, 7 Apr 2021 16:14:15 +0200 Subject: [PATCH] establish GbxClient --- main.go | 11 +++++++--- pkg/config/config.go | 22 +++++++++++++++++-- pkg/connection/connection.go | 41 +++++++++++++++++++++++++++++------- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index e57a8fe..db794c0 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,16 @@ package main import ( "git.fanyx.xyz/fanyx/goufy/pkg/config" + "git.fanyx.xyz/fanyx/goufy/pkg/connection" ) func main() { - var c config.AppConfig - c.ReadConfig() + // initiate config + var conf config.AppConfig + conf.ReadConfig() + + //initiate connection to trackmania server + var client connection.GbxClient + client.Connect(conf) - // fmt.Printf("#%v", c) } diff --git a/pkg/config/config.go b/pkg/config/config.go index f0fd027..32b26e5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -10,8 +10,11 @@ import ( const CONFIGFILE = "config.yaml" const DEFAULTCONFIG = ` -host: "127.0.0.1" -port: 5000 +trackmania: + host: "127.0.0.1" + port: 5000 +api: + port: 5111 ` type TrackmaniaConfig struct { @@ -19,17 +22,32 @@ type TrackmaniaConfig struct { Port int } +type ApiConfig struct { + Port int +} + type AppConfig struct { Trackmania TrackmaniaConfig + Api ApiConfig } func (c *AppConfig) ReadConfig() { + // read default config + err := yaml.Unmarshal([]byte(DEFAULTCONFIG), c) + if err != nil { + log.Fatal(err) + } + log.Print("Initiated the default config") + // open config.yaml yamlFile, err := ioutil.ReadFile(CONFIGFILE) if err != nil { log.Fatal(err) } + log.Print("Done reading yaml File") + // configure config from config.yaml err = yaml.Unmarshal([]byte(yamlFile), c) if err != nil { log.Fatal(err) } + log.Print("Read config from yaml File") } diff --git a/pkg/connection/connection.go b/pkg/connection/connection.go index d7feddb..bc8f53d 100644 --- a/pkg/connection/connection.go +++ b/pkg/connection/connection.go @@ -1,12 +1,37 @@ package connection -import "net" +import ( + "fmt" + "log" + "net" + "strconv" -func connect() { - var gbxConnString string - conn, err := net.Dial("tcp", gbxConnString) - if err != nil { - panic(err) - } - defer conn.Close() + "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) }