/
packet.go
150 lines (133 loc) · 3.15 KB
/
packet.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package protocol
import (
"github.com/sauerbraten/waiter/pkg/geom"
"github.com/sauerbraten/waiter/pkg/protocol/cubecode"
)
// Packet represents a Sauerbraten UDP packet.
type Packet []byte
// PutInt writes an int32 to the packet buffer.
func (p *Packet) PutInt(v int32) {
if -0x7F < v && v < 0x80 {
*p = append(*p, byte(v))
} else if -0x7FFF <= v && v < 0x8000 {
*p = append(*p, 0x80, byte(v), byte(v>>8))
} else {
*p = append(*p, 0x81, byte(v), byte(v>>8), byte(v>>16), byte(v>>24))
}
}
// PutUint writes a uint32 to the packet buffer. It only keeps the 28 lowest bits!
func (p *Packet) PutUint(v uint32) {
if v < (1 << 7) {
*p = append(*p, byte(v))
} else if v < (1 << 14) {
*p = append(*p, (byte(v)&0x7F)|0x80, byte(v>>7))
} else if v < (1 << 21) {
*p = append(*p, (byte(v)&0x7F)|0x80, (byte(v>>7)&0x7F)|0x80, byte(v>>14))
} else {
*p = append(*p, (byte(v)&0x7F)|0x80, (byte(v>>7)&0x7F)|0x80, (byte(v>>14)&0x7F)|0x80, byte(v>>21))
}
}
// PutInt writes a string to the packet buffer, encoding it with Sauer's conversion table at the same time.
func (p *Packet) PutString(s string) {
for _, r := range s {
cpoint := cubecode.FromUnicode(r)
if cpoint == 0 {
continue
}
p.PutInt(cpoint)
}
(*p) = append(*p, 0x00)
}
func (p *Packet) GetByte() (byte, bool) {
if len(*p) < 1 {
return 0, false
}
b := (*p)[0]
(*p) = (*p)[1:]
return b, true
}
// GetInt returns the integer value encoded in the next byte(s) of the packet.
func (p *Packet) GetInt() (int32, bool) {
b, ok := p.GetByte()
if !ok {
return -1, false
}
switch b {
default:
// most often, the value is only one byte
return int32(int8(b)), true
case 0x80:
// value is contained in the next two bytes
if len(*p) < 2 {
return -1, false
}
v := int32((*p)[0]) + int32(int8((*p)[1]))<<8
(*p) = (*p)[2:]
return v, true
case 0x81:
// value is contained in the next four bytes
if len(*p) < 4 {
return -1, false
}
v := int32((*p)[0]) + int32((*p)[1])<<8 + int32((*p)[2])<<16 + int32(int8((*p)[3]))<<24
(*p) = (*p)[4:]
return v, true
}
}
func (p *Packet) GetUint() (v uint32, ok bool) {
b, ok := p.GetByte()
if !ok {
return 0, false
}
v += uint32(b)
if v&(1<<7) != 0 {
b, ok := p.GetByte()
if !ok {
return 0, false
}
v += (uint32(b) << 7) - (1 << 7)
}
if v&(1<<14) != 0 {
b, ok := p.GetByte()
if !ok {
return 0, false
}
v += (uint32(b) << 14) - (1 << 14)
}
if v&(1<<21) != 0 {
b, ok := p.GetByte()
if !ok {
return 0, false
}
v += (uint32(b) << 14) - (1 << 14)
}
if v&(1<<28) != 0 {
v += uint32(0xF) << 28 // fills up the top bits with ones to keep sign (to handle int32 inputs)
}
return v, true
}
// GetString returns a string of the next bytes up to 0x00.
func (p *Packet) GetString() (s string, ok bool) {
var cpoint int32
for {
cpoint, ok = p.GetInt()
if !ok {
return s, false
}
if cpoint == 0 {
return s, true
}
s += string(cubecode.ToUnicode(cpoint))
}
}
func (p *Packet) GetVector() (*geom.Vector, bool) {
xyz := [3]float64{}
for i := range xyz {
coord, ok := p.GetInt()
if !ok {
return nil, false
}
xyz[i] = float64(coord)
}
return geom.NewVector(xyz[0], xyz[1], xyz[2]), true
}