-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
303 lines (223 loc) · 6.39 KB
/
session.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package eio
import (
"errors"
"io"
"net/http"
"sync"
"time"
"github.com/byonchev/go-engine.io/internal/config"
"github.com/byonchev/go-engine.io/internal/logger"
"github.com/byonchev/go-engine.io/internal/packet"
"github.com/byonchev/go-engine.io/internal/transport"
"github.com/byonchev/go-engine.io/internal/utils"
)
// Session holds information for a single connected client
type Session struct {
id string
config config.Config
supportedTransports map[string]bool
transport transport.Transport
events chan<- interface{}
handshaked bool
closed bool
sending sync.WaitGroup
lastPingTime time.Time
}
// NewSession creates a new client session
func NewSession(config config.Config, events chan<- interface{}) *Session {
supportedTransports := make(map[string]bool)
for _, transport := range config.Transports {
supportedTransports[transport] = true
}
return &Session{
id: utils.GenerateBase64ID(),
config: config,
supportedTransports: supportedTransports,
events: events,
handshaked: false,
closed: false,
}
}
// HandleRequest is the bridge between the engine.io endpoint and the selected transport
func (session *Session) HandleRequest(writer http.ResponseWriter, request *http.Request) {
defer request.Body.Close()
query := request.URL.Query()
requestedTransport := query.Get("transport")
if !session.transportSupported(requestedTransport) {
logger.Error("Transport ", requestedTransport, " is not supported")
writer.WriteHeader(http.StatusBadRequest)
return
}
if session.transport == nil {
session.transport = session.createTransport(requestedTransport)
}
if !session.handshaked {
go session.handshake()
}
if session.isUpgradeRequest(requestedTransport) {
err := session.upgrade(writer, request, requestedTransport)
if err != nil {
logger.Error("Transport upgrade error: ", err)
writer.WriteHeader(http.StatusBadRequest)
}
return
}
session.transport.HandleRequest(writer, request)
}
// Send enqueues packets for sending
func (session *Session) Send(packet packet.Packet) error {
if session.closed {
return errors.New("session closed")
}
session.sending.Add(1)
err := session.transport.Send(packet)
session.sending.Done()
return err
}
// Close changes the session state and closes the channels
func (session *Session) Close(reason string) {
if session.closed {
return
}
session.closed = true
session.sending.Wait()
if session.transport != nil {
session.transport.Shutdown()
}
session.debug("Session closed. Reason: ", reason)
session.emit(DisconnectEvent{session.id, reason})
close(session.events)
}
// ID returns the session ID
func (session *Session) ID() string {
return session.id
}
// Expired check if session is closed or last ping was not before (ping interval + ping timeout)
func (session *Session) Expired() bool {
now := time.Now()
threshold := session.config.PingInterval + session.config.PingTimeout
return session.closed || session.lastPingTime.Add(threshold).Before(now)
}
func (session *Session) handshake() {
packet := utils.CreateHandshakePacket(session.id, session.transport, session.config)
err := session.Send(packet)
if err != nil {
logger.Error("Handshake error: ", err, "for", packet)
return
}
session.debug("Session created")
session.handshaked = true
session.ping()
go session.receivePackets()
session.emit(ConnectEvent{session.id})
}
func (session *Session) ping() {
session.lastPingTime = time.Now()
}
func (session *Session) receivePackets() {
for !session.closed {
transport := session.transport
received, err := transport.Receive()
switch err {
case io.EOF:
if !session.transport.Running() {
session.Close("EOF")
return
}
continue
case nil:
session.handlePacket(received)
default:
logger.Error("Receive error: ", err)
}
}
}
func (session *Session) handlePacket(received packet.Packet) {
session.ping()
switch received.Type {
case packet.Ping:
session.handlePing(received)
case packet.Close:
session.handleClose(received)
case packet.Message:
session.handleMessage(received)
}
}
func (session *Session) handlePing(ping packet.Packet) {
session.debug("Ping received")
session.debug("Sending pong")
session.Send(packet.NewPong(ping.Data))
}
func (session *Session) handleClose(close packet.Packet) {
session.Close("close packet received")
}
func (session *Session) handleMessage(message packet.Packet) {
session.debug("Message received: ", message.Data)
event := MessageEvent{
SessionID: session.id,
Binary: message.Binary,
Data: message.Data,
}
session.emit(event)
}
func (session *Session) upgrade(writer http.ResponseWriter, request *http.Request, target string) error {
if !session.upgradeSupported(target) {
return errors.New("not supported")
}
upgrade := session.createTransport(target)
upgrade.HandleRequest(writer, request)
if !upgrade.Running() {
return errors.New("transport failure")
}
session.debug("Upgrading transport")
for {
received, err := upgrade.Receive()
if err != nil {
return err
}
if received.Type == packet.Ping && string(received.Data) == "probe" {
session.debug("Upgrade probe received")
err := upgrade.Send(packet.NewPong(received.Data))
if err != nil {
return err
}
session.debug("Poll cycle initiated")
session.transport.Send(packet.NewNOOP())
continue
}
if received.Type == packet.Upgrade {
session.debug("Upgrade packet recevied")
session.transport.Shutdown()
session.transport = upgrade
break
}
}
return nil
}
func (session *Session) transportSupported(requested string) bool {
return session.supportedTransports[requested]
}
func (session *Session) upgradeSupported(requested string) bool {
allowUpgrades := session.config.AllowUpgrades
possibleUpgrades := session.transport.Upgrades()
return allowUpgrades && utils.StringSliceContains(possibleUpgrades, requested)
}
func (session *Session) isUpgradeRequest(requested string) bool {
return session.transport.Type() != requested
}
func (session *Session) createTransport(requested string) transport.Transport {
return transport.NewTransport(requested, session.config)
}
func (session *Session) emit(event interface{}) {
go func() {
session.events <- event
}()
}
func (session *Session) debug(data ...interface{}) {
prefix := []interface{}{
"[ ",
session.id,
" ] ",
}
logger.Debug(append(prefix, data...)...)
}