-
Notifications
You must be signed in to change notification settings - Fork 3
/
packet.go
39 lines (32 loc) · 1.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
package main
type packet struct {
opcode uint8
length int
data []byte
currentOffset int
}
func (p *packet) incOffset() {
p.currentOffset++
}
func (p *packet) readLong() uint64 {
l := uint64(uint64(p.data[p.currentOffset])<<56 | uint64(p.data[p.currentOffset+1])<<48 | uint64(p.data[p.currentOffset+2])<<40 | uint64(p.data[p.currentOffset+3])<<32 | uint64(p.data[p.currentOffset+4])<<24 | uint64(p.data[p.currentOffset+5])<<16 | uint64(p.data[p.currentOffset+6])<<8 | uint64(p.data[p.currentOffset+7]))
p.currentOffset += 8
return l
}
func (p *packet) readInt() uint32 {
i := uint32(uint32(p.data[p.currentOffset])<<24 | uint32(p.data[p.currentOffset+1])<<16 | uint32(p.data[p.currentOffset+2])<<8 | uint32(p.data[p.currentOffset+3]))
p.currentOffset += 4
return i
}
func (p *packet) readShort() uint16 {
si := uint16((uint16(p.data[p.currentOffset] & 0xFF) << 8) | uint16(p.data[p.currentOffset+1] & 0xFF))
p.currentOffset += 2
return si
}
func (p *packet) readByte() byte {
defer p.incOffset()
return p.data[p.currentOffset]
}
func NewPacket(opcode uint8, length int, data []byte) packet {
return packet{opcode, length, data, 0}
}