establish GbxClient

This commit is contained in:
fanyx 2021-04-07 16:14:15 +02:00
parent 2bf78209bc
commit 58499493d8
3 changed files with 61 additions and 13 deletions

11
main.go
View File

@ -2,11 +2,16 @@ package main
import ( import (
"git.fanyx.xyz/fanyx/goufy/pkg/config" "git.fanyx.xyz/fanyx/goufy/pkg/config"
"git.fanyx.xyz/fanyx/goufy/pkg/connection"
) )
func main() { func main() {
var c config.AppConfig // initiate config
c.ReadConfig() var conf config.AppConfig
conf.ReadConfig()
//initiate connection to trackmania server
var client connection.GbxClient
client.Connect(conf)
// fmt.Printf("#%v", c)
} }

View File

@ -10,8 +10,11 @@ import (
const CONFIGFILE = "config.yaml" const CONFIGFILE = "config.yaml"
const DEFAULTCONFIG = ` const DEFAULTCONFIG = `
host: "127.0.0.1" trackmania:
port: 5000 host: "127.0.0.1"
port: 5000
api:
port: 5111
` `
type TrackmaniaConfig struct { type TrackmaniaConfig struct {
@ -19,17 +22,32 @@ type TrackmaniaConfig struct {
Port int Port int
} }
type ApiConfig struct {
Port int
}
type AppConfig struct { type AppConfig struct {
Trackmania TrackmaniaConfig Trackmania TrackmaniaConfig
Api ApiConfig
} }
func (c *AppConfig) ReadConfig() { 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) yamlFile, err := ioutil.ReadFile(CONFIGFILE)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Print("Done reading yaml File")
// configure config from config.yaml
err = yaml.Unmarshal([]byte(yamlFile), c) err = yaml.Unmarshal([]byte(yamlFile), c)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Print("Read config from yaml File")
} }

View File

@ -1,12 +1,37 @@
package connection package connection
import "net" import (
"fmt"
"log"
"net"
"strconv"
func connect() { "git.fanyx.xyz/fanyx/goufy/pkg/config"
var gbxConnString string )
conn, err := net.Dial("tcp", gbxConnString)
if err != nil { type GbxClient struct {
panic(err) conn net.Conn
} reqHandle int
defer conn.Close() }
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)
} }