-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
175 lines (135 loc) · 3.8 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package packet
import (
"github.com/pkg/errors"
)
type Packet struct {
OpCode int16
Index int16
Size int16
Data []byte
}
func (p *Packet) New(opCode int16) {
p.OpCode = opCode
p.Data = make([]byte, 0)
}
func (p *Packet) Fill(data []byte) {
p.Size = (int16)((int16(data[0]&0xFF) << 8) | int16(data[1]&0xFF))
p.OpCode = (int16)((int16(data[2]&0xFF) << 8) | int16(data[3]&0xFF))
p.Data = make([]byte, 0)
for i := 4; i < len(data); i++ {
p.Data = append(p.Data, data[i])
}
}
func (p *Packet) Resume(data []byte) {
for i := 0; i < len(data); i++ {
p.Data = append(p.Data, data[i])
}
}
func (p *Packet) IsDone() bool {
if p.Size == int16(len(p.Data)) {
return true
} else {
return false
}
}
func (p *Packet) GetBytes() []byte {
size := len(p.Data)
var ret []byte
ret = append(ret, (byte)((size>>8)&0xFF))
ret = append(ret, (byte)(size&0xFF))
ret = append(ret, (byte)((p.OpCode>>8)&0xFF))
ret = append(ret, (byte)(p.OpCode&0xFF))
for i := 0; i < len(p.Data); i++ {
ret = append(ret, p.Data[i])
}
return ret
}
func (p *Packet) WriteByte(value byte) {
p.Data = append(p.Data, (byte)(value&0xFF))
p.Size++
}
func (p *Packet) WriteShort(value int16) {
p.Data = append(p.Data, (byte)((value>>8)&0xFF))
p.Data = append(p.Data, (byte)(value&0xFF))
p.Size += 2
}
func (p *Packet) WriteInteger(value int) {
p.Data = append(p.Data, (byte)((value>>24)&0xFF))
p.Data = append(p.Data, (byte)((value>>16)&0xFF))
p.Data = append(p.Data, (byte)((value>>8)&0xFF))
p.Data = append(p.Data, (byte)((value)&0xFF))
p.Size += 4
}
func (p *Packet) WriteString(value string) {
b := []byte(value)
p.Data = append(p.Data, (byte)((len(b)>>8)&0xFF))
p.Data = append(p.Data, (byte)(len(b)&0xFF))
for i := 0; i < len(b); i++ {
p.Data = append(p.Data, (byte)(b[i]&0xFF))
}
p.Size += int16(len(b)) + 2
}
func (p *Packet) WriteBytes(value []byte) {
p.Data = append(p.Data, (byte)((len(value)>>8)&0xFF))
p.Data = append(p.Data, (byte)(len(value)&0xFF))
for i := 0; i < len(value); i++ {
p.Data = append(p.Data, (byte)(value[i]&0xFF))
}
p.Size += int16(len(value)) + 2
}
func (p *Packet) ReadByte() (byte, error) {
if p.Index > int16(len(p.Data)) {
return 0, errors.New("index out of range")
}
ret := (byte)(p.Data[p.Index] & 0xFF)
p.Index++
return ret, nil
}
func (p *Packet) ReadShort() (int16, error) {
if p.Index+1 > int16(len(p.Data)) {
return 0, errors.New("index out of range")
}
ret := (int16)((int16(p.Data[p.Index]&0xFF) << 8) | int16(p.Data[p.Index+1]&0xFF))
p.Index += 2
return ret, nil
}
func (p *Packet) ReadInteger() (int, error) {
if p.Index+3 > int16(len(p.Data)) {
return 0, errors.New("index out of range")
}
ret := (int)((int(p.Data[p.Index]&0xFF) << 24) | (int(p.Data[p.Index+1]&0xFF) << 16) | (int(p.Data[p.Index+2]&0xFF) << 8) | int(p.Data[p.Index+3]&0xFF))
p.Index += 4
return ret, nil
}
func (p *Packet) ReadString() (string, error) {
if p.Index+1 > int16(len(p.Data)) {
return "", errors.New("index out of range")
}
length := (int16)((int16(p.Data[p.Index]&0xFF) << 8) | int16(p.Data[p.Index+1]&0xFF))
p.Index += 2
if p.Index+length-1 > int16(len(p.Data)) {
return "", errors.New("index out of range")
}
var bret = make([]byte, 0)
for i := p.Index; i < p.Index+length; i++ {
bret = append(bret, (byte)(p.Data[i]&0xFF))
}
p.Index += length
return string(bret), nil
}
func (p *Packet) ReadBytes() ([]byte, error) {
if p.Index+1 > int16(len(p.Data)) {
return []byte{}, errors.New("index out of range")
}
length := (int16)((int16(p.Data[p.Index]&0xFF) << 8) | int16(p.Data[p.Index+1]&0xFF))
p.Index += 2
if p.Index+length-1 > int16(len(p.Data)) {
return []byte{}, errors.New("index out of range")
}
var bret = make([]byte, 0)
for i := p.Index; i < p.Index+length; i++ {
bret = append(bret, (byte)(p.Data[i]&0xFF))
}
p.Index += length
return bret, nil
}