-
Notifications
You must be signed in to change notification settings - Fork 8
/
subscriber.go
228 lines (188 loc) · 4.75 KB
/
subscriber.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package zanarkand
import (
"bufio"
"bytes"
"compress/zlib"
"fmt"
"io"
"sync"
)
// Subscriber describes the interface for individual Frame segment subscribers.
type Subscriber interface {
Subscribe(*Sniffer)
Close()
}
type readerPool struct {
body sync.Pool
bare sync.Pool
zlib sync.Pool
}
func newReaderPool() *readerPool {
return &readerPool{
body: sync.Pool{New: func() interface{} { return bytes.NewReader(nil) }},
bare: sync.Pool{New: func() interface{} { return bufio.NewReader(nil) }},
zlib: sync.Pool{},
}
}
// GameEventSubscriber is a Subscriber for GameEvent segments.
type GameEventSubscriber struct {
IngressEvents chan *GameEventMessage
EgressEvents chan *GameEventMessage
}
// NewGameEventSubscriber returns a Subscriber handle with channels for inbound and outbound GameEventMessages.
func NewGameEventSubscriber() *GameEventSubscriber {
return &GameEventSubscriber{
IngressEvents: make(chan *GameEventMessage),
EgressEvents: make(chan *GameEventMessage),
}
}
// Subscribe starts the GameEventSubscriber.
func (g *GameEventSubscriber) Subscribe(s *Sniffer) error {
if !s.Active {
go s.Start()
}
pool := newReaderPool()
for {
frame, err := s.NextFrame()
if err != nil {
return fmt.Errorf("error retrieving next frame: %w", err)
}
// Setup our Message reader
b := pool.body.Get().(*bytes.Reader)
if b != nil {
b.Reset(frame.Body)
}
r := pool.bare.Get().(*bufio.Reader)
if r != nil {
r.Reset(b)
}
z := pool.zlib.Get().(io.ReadCloser)
if z != nil {
err = z.(zlib.Resetter).Reset(b, nil)
if err != nil {
return fmt.Errorf("error resetting ZLIB decoder: %w", err)
}
} else {
z, err = zlib.NewReader(b)
if err != nil {
return fmt.Errorf("error creating ZLIB decoder: %w", err)
}
}
if frame.Compressed {
r.Reset(z.(io.ReadCloser))
}
for i := 0; i < int(frame.Count); i++ {
header := new(GenericHeader)
err := header.Decode(r)
if err != nil {
return ErrDecodingFailure{Err: err}
}
if header.Segment == GameEvent {
msg := new(GameEventMessage)
err = msg.Decode(r)
if err != nil {
return ErrDecodingFailure{Err: err}
}
switch frame.Direction() {
case FrameIngress:
g.IngressEvents <- msg
case FrameEgress:
g.EgressEvents <- msg
default:
return ErrDecodingFailure{Err: fmt.Errorf("unexpected frame direction")}
}
}
}
// Return our readers to the pool - the pool will get GC'd when the function exits
z.Close()
pool.zlib.Put(z)
pool.bare.Put(r)
pool.body.Put(b)
// We're done with the current frame, if Sniffer is stopped then exit,
// allowing user to start a new subscriber routine.
if !s.Active {
return nil
}
}
}
// Close will stop a sniffer, drain the channels, then close the channels.
func (g *GameEventSubscriber) Close(s *Sniffer) {
s.Stop()
close(g.IngressEvents)
close(g.EgressEvents)
}
// KeepaliveSubscriber is a Subscriber for Keepalive segments.
type KeepaliveSubscriber struct {
Events chan *KeepaliveMessage
}
// NewKeepaliveSubscriber returns a Subscriber handle. As the traffic is minimal, this subscriber uses a single Event channel.
func NewKeepaliveSubscriber() *KeepaliveSubscriber {
return &KeepaliveSubscriber{
Events: make(chan *KeepaliveMessage),
}
}
// Subscribe starts the KeepaliveSubscriber.
func (k *KeepaliveSubscriber) Subscribe(s *Sniffer) error {
if !s.Active {
go s.Start()
}
pool := newReaderPool()
for {
frame, err := s.NextFrame()
if err != nil {
return fmt.Errorf("error retrieving next frame: %s", err)
}
// Setup our Message reader
b := pool.body.Get().(*bytes.Reader)
if b != nil {
b.Reset(frame.Body)
}
r := pool.bare.Get().(*bufio.Reader)
if r != nil {
r.Reset(b)
}
z := pool.zlib.Get().(io.ReadCloser)
if z != nil {
err = z.(zlib.Resetter).Reset(b, nil)
if err != nil {
return fmt.Errorf("error resetting ZLIB decoder: %w", err)
}
} else {
z, err = zlib.NewReader(b)
if err != nil {
return fmt.Errorf("error creating ZLIB decoder: %w", err)
}
}
if frame.Compressed {
r.Reset(z.(io.ReadCloser))
}
for i := 0; i < int(frame.Count); i++ {
header := new(GenericHeader)
err := header.Decode(r)
if err != nil {
return ErrDecodingFailure{Err: err}
}
if header.Segment == ServerPing || header.Segment == ServerPong {
msg := new(KeepaliveMessage)
err = msg.Decode(r)
if err != nil {
return ErrDecodingFailure{Err: err}
}
k.Events <- msg
}
}
// Return our readers to the pool
z.Close()
pool.zlib.Put(z)
pool.bare.Put(r)
pool.body.Put(b)
if !s.Active {
return nil
}
}
}
// Close will stop a sniffer, drain the channel, then close the channel.
func (k *KeepaliveSubscriber) Close(s *Sniffer) {
s.Stop()
close(k.Events)
}