-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
45 lines (36 loc) · 960 Bytes
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package claymore
import (
"net"
"fmt"
"io/ioutil"
)
// Connection
type Connection struct{
ServerAddress string
}
// Create new instance of connection
func NewConnection(serverAddress string) Connection {
return Connection{serverAddress}
}
// Send request to claymore server
func (c *Connection) Request(packet []byte) ([]byte, error) {
emptyBytes := []byte{}
tcpAddr, err := net.ResolveTCPAddr("tcp", c.ServerAddress)
if err != nil {
return emptyBytes, fmt.Errorf("resolve TCP address failed: %s", err.Error())
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return emptyBytes, fmt.Errorf("dial failed: %s", err.Error())
}
defer conn.Close()
_, err = conn.Write(packet)
if err != nil {
return emptyBytes, fmt.Errorf("write to server failed: %s", err.Error())
}
reply, err := ioutil.ReadAll(conn)
if err != nil {
return emptyBytes, fmt.Errorf("read from server failed: %s", err.Error())
}
return reply, nil
}