establish GbxClient
This commit is contained in:
parent
2bf78209bc
commit
58499493d8
11
main.go
11
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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue