-
Notifications
You must be signed in to change notification settings - Fork 1
/
packetreader.go
157 lines (126 loc) · 2.69 KB
/
packetreader.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
package main
import (
"encoding/binary"
"fmt"
)
type InPacket struct {
PacketType uint8
Flags uint8
ConnId uint16
PacketId uint16
Length uint16
Data []byte
}
type PacketReader struct {
remote Remote
control chan bool
bufferPool *BufferPool
outChan chan *InPacket
buf []byte
}
func NewPacketReader(bufferPool *BufferPool, remote Remote) *PacketReader {
pr := &PacketReader{
remote,
make(chan bool),
bufferPool,
make(chan *InPacket, 100),
make([]byte, 0, 100)}
return pr
}
func (this *PacketReader) GetOutputChannel() (outChan chan *InPacket) {
return this.outChan
}
func (this *PacketReader) Start() {
go this.run()
}
func (this *PacketReader) Stop() {
this.control <- true
}
func (this *PacketReader) run() {
done := false
for !done {
select {
case buf := <-this.remote.GetReadChan():
this.processBuffer(buf)
case done = <-this.control:
}
}
}
func (this *PacketReader) processBuffer(buf []byte) {
this.buf = append(this.buf, buf...)
this.bufferPool.ReleaseBuffer(buf)
maxIx := len(this.buf) - 14
ij := 0
packetCount := 0
var pacBuf []byte
for ij <= maxIx {
ix := ij
found := false
//for ix <= maxIx {
id := binary.BigEndian.Uint32(this.buf[ix:])
if id == PACKET_ID {
pacBuf = this.buf[ix:]
ix += 4
found = true
// break
}
// ix++
//}
if !found {
// No packet found at all.
fmt.Printf("Bad data? %d bytes\n", len(this.buf))
for _, c := range this.buf {
fmt.Printf("%2x ", c)
}
fmt.Println()
this.buf = this.buf[:0]
return
}
if ix > maxIx+4 {
// Not enough bytes for a header.
fmt.Printf("Incomplete packet\n")
this.buf = this.buf[ij:]
return
}
pacType := this.buf[ix]
ix++
pacFlags := this.buf[ix]
ix++
connId := binary.BigEndian.Uint16(this.buf[ix:])
ix += 2
packId := binary.BigEndian.Uint16(this.buf[ix:])
ix += 4 // Skip checksum
length := binary.BigEndian.Uint16(this.buf[ix:])
ix += 2
if ix+int(length) > len(this.buf) {
// Not enough bytes for all the data
this.buf = this.buf[ij:]
return
}
checksum := calculateChecksum(pacBuf, length+14)
if checksum != 0xffff {
fmt.Printf("Bad checksum\n")
this.buf = this.buf[ix:]
return
}
packet := &InPacket{
PacketType: pacType,
Flags: pacFlags,
ConnId: connId,
PacketId: packId,
Length: length,
Data: this.buf[ix : ix+int(length)]}
packetCount++
this.outChan <- packet
ij = ix + int(length)
if (pacFlags & PF_PadByte) == PF_PadByte {
ij++
}
}
if ij >= len(this.buf) {
this.buf = this.buf[:0]
} else {
fmt.Printf("Clearing %d of %d bytes from buffer.\n", ij, len(this.buf))
this.buf = this.buf[ij:]
}
}