forked from kidoman/go-steam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rconsocket.go
86 lines (79 loc) · 1.64 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
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(400 * time.Millisecond)); err != nil {
return err
}
_, err := s.conn.Write(p)
if err != nil {
return err
}
return nil
}
func (s *rconSocket) receive() (_ []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
buf := new(bytes.Buffer)
tr := io.TeeReader(s.conn, buf)
total := int(readLong(tr))
log.WithFields(logrus.Fields{
"total": total + 4,
}).Debug("steam: reading packet")
for total > 0 {
log.WithFields(logrus.Fields{
"bytes": total,
}).Debug("steam: reading")
b := make([]byte, total)
if err := s.conn.SetReadDeadline(time.Now().Add(400 * time.Millisecond)); err != nil {
return nil, err
}
n, err := s.conn.Read(b)
if n > 0 {
log.WithFields(logrus.Fields{
"bytes": n,
}).Debug("steam: read")
_, err := buf.Write(b)
if err != nil {
return nil, err
}
total -= n
}
if err != nil {
log.WithFields(logrus.Fields{
"err": err,
}).Error("steam: could not receive data")
if err == io.EOF {
return nil, err
}
}
log.WithFields(logrus.Fields{
"bytes": total,
}).Debug("steam: remaining")
}
log.WithFields(logrus.Fields{
"size": buf.Len(),
}).Debug("steam: read packet")
return buf.Bytes(), nil
}