forked from kidoman/go-steam
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rconsocket.go
89 lines (70 loc) · 1.53 KB
/
rconsocket.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package steam
import (
"bytes"
"io"
"net"
"time"
"github.com/Sirupsen/logrus"
)
type rconSocket struct {
conn net.Conn
}
func newRCONSocket(dial DialFn, addr string) (*rconSocket, error) {
conn, err := dial("tcp", addr)
if err != nil {
return nil, err
}
return &rconSocket{conn}, nil
}
func (s *rconSocket) close() {
s.conn.Close()
}
func (s *rconSocket) send(p []byte) error {
if err := s.conn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil {
return err
}
_, err := s.conn.Write(p)
return err
}
func (s *rconSocket) receive() (_ []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
if err := s.conn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
return nil, err
}
var buf bytes.Buffer
tr := io.TeeReader(s.conn, &buf)
total := int(readLong(tr))
log.WithFields(logrus.Fields{
"total": total + 4,
}).Debug("steam: reading rcon packet")
for total > 0 {
log.WithFields(logrus.Fields{
"bytes": total,
}).Debug("steam: reading rcon response")
b := make([]byte, total)
n, err := s.conn.Read(b)
if n > 0 {
log.WithFields(logrus.Fields{
"bytes": n,
}).Debug("steam: read rcon response")
if _, err := buf.Write(b); err != nil {
return nil, err
}
total -= n
}
if err != nil {
return nil, err
}
log.WithFields(logrus.Fields{
"bytes": total,
}).Debug("steam: remaining rcon response")
}
log.WithFields(logrus.Fields{
"size": buf.Len(),
}).Debug("steam: read rcon packet")
return buf.Bytes(), nil
}