-
Notifications
You must be signed in to change notification settings - Fork 33
/
wire.go
122 lines (103 loc) · 2.16 KB
/
wire.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package steam
import (
"bytes"
"encoding/binary"
"io"
"math"
"strconv"
)
type parseError string
func (e parseError) Error() string {
return string(e)
}
var errCouldNotReadData = parseError("steam: could not read data")
var errNotEnoughDataInResponse = parseError("steam: not enough data in response")
var errBadData = parseError("steam: bad data in response")
func readByte(r io.Reader) byte {
buf := make([]byte, 1)
_, err := io.ReadFull(r, buf)
must(err)
return buf[0]
}
func readBytes(r io.Reader, n int) (buf []byte) {
buf = make([]byte, n)
_, err := io.ReadFull(r, buf)
must(err)
return
}
func readShort(r io.Reader) (v int16) {
must(binary.Read(r, binary.LittleEndian, &v))
return
}
func readLong(r io.Reader) (v int32) {
must(binary.Read(r, binary.LittleEndian, &v))
return
}
func readULong(r io.Reader) (v uint32) {
must(binary.Read(r, binary.LittleEndian, &v))
return
}
func readLongLong(r io.Reader) (v int64) {
must(binary.Read(r, binary.LittleEndian, &v))
return
}
func readString(r io.Reader) string {
if buf, ok := r.(*bytes.Buffer); ok {
// See if we are being passed a bytes.Buffer.
// Fast path.
bytes, err := buf.ReadBytes(0)
must(err)
return string(bytes)
}
var buf bytes.Buffer
for {
b := make([]byte, 1)
_, err := io.ReadFull(r, b)
must(err)
buf.WriteByte(b[0])
if b[0] == 0 {
break
}
}
return buf.String()
}
func readFloat(r io.Reader) float32 {
v := readULong(r)
return math.Float32frombits(v)
}
func toInt(v interface{}) int {
switch v := v.(type) {
case byte:
return int(v)
case int16:
return int(v)
case int32:
return int(v)
case int64:
return int(v)
case string:
i, err := strconv.Atoi(v)
if err != nil {
panic(errBadData)
}
return i
}
panic(errBadData)
}
func writeRequestPrefix(buf *bytes.Buffer) {
buf.Write(requestPrefix)
}
var requestPrefix = []byte{0xFF, 0xFF, 0xFF, 0xFF}
func writeString(buf *bytes.Buffer, v string) {
buf.WriteString(v)
buf.WriteByte(0)
}
func writeByte(buf *bytes.Buffer, v byte) {
buf.WriteByte(v)
}
func writeLong(buf *bytes.Buffer, v int32) {
must(binary.Write(buf, binary.LittleEndian, v))
}
func writeNull(buf *bytes.Buffer) {
buf.WriteByte(0)
}