-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
61 lines (53 loc) · 1.09 KB
/
protocol.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
package rcon
import (
"bytes"
"fmt"
"io"
)
type PacketType int32
const (
SERVERDATA_AUTH = PacketType(3)
SERVERDATA_AUTH_RESPONSE = PacketType(2)
SERVERDATA_EXECCOMMAND = PacketType(2)
SERVERDATA_RESPONSE_VALUE = PacketType(0)
)
type Packet struct {
Size, ID int32
Type PacketType
Body string
}
func NewPacket(packetType PacketType, packetID int32, body string) *Packet {
return &Packet{
ID: packetID,
Size: int32(len(body) + 10),
Type: packetType,
Body: body,
}
}
func (p *Packet) Write(w io.Writer) (err error) {
buf := new(bytes.Buffer)
{
writeLE(buf, p.Size)
writeLE(buf, p.ID)
writeLE(buf, p.Type)
buf.WriteString(p.Body)
buf.WriteByte(0x00)
buf.WriteByte(0x00)
if _, err = io.Copy(w, buf); err != nil {
err = fmt.Errorf("cant write a packet: %w", err)
}
}
return
}
func (p *Packet) Read(r io.Reader) error {
var err error
{
p.Size = readLE[int32](r)
p.ID = readLE[int32](r)
p.Type = readLE[PacketType](r)
if p.Type != SERVERDATA_AUTH && p.Size-10 > 0 {
p.Body = readBytes[string](r, uint(p.Size)-10)
}
}
return err
}