forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_ws.go
458 lines (398 loc) · 12.7 KB
/
session_ws.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"bytes"
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/golang/protobuf/proto"
"net"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/jsonpb"
"github.com/gorilla/websocket"
"github.com/heroiclabs/nakama-common/rtapi"
"go.uber.org/atomic"
"go.uber.org/zap"
)
var ErrSessionQueueFull = errors.New("session outgoing queue full")
type sessionWS struct {
sync.Mutex
logger *zap.Logger
config Config
id uuid.UUID
format SessionFormat
userID uuid.UUID
username *atomic.String
vars map[string]string
expiry int64
clientIP string
clientPort string
ctx context.Context
ctxCancelFn context.CancelFunc
jsonpbMarshaler *jsonpb.Marshaler
jsonpbUnmarshaler *jsonpb.Unmarshaler
wsMessageType int
pingPeriodDuration time.Duration
pongWaitDuration time.Duration
writeWaitDuration time.Duration
sessionRegistry SessionRegistry
matchmaker Matchmaker
tracker Tracker
pipeline *Pipeline
runtime *Runtime
stopped bool
conn *websocket.Conn
receivedMessageCounter int
pingTimer *time.Timer
pingTimerCAS *atomic.Uint32
outgoingCh chan []byte
}
func NewSessionWS(logger *zap.Logger, config Config, format SessionFormat, userID uuid.UUID, username string, vars map[string]string, expiry int64, clientIP string, clientPort string, jsonpbMarshaler *jsonpb.Marshaler, jsonpbUnmarshaler *jsonpb.Unmarshaler, conn *websocket.Conn, sessionRegistry SessionRegistry, matchmaker Matchmaker, tracker Tracker, pipeline *Pipeline, runtime *Runtime) Session {
sessionID := uuid.Must(uuid.NewV4())
sessionLogger := logger.With(zap.String("uid", userID.String()), zap.String("sid", sessionID.String()))
sessionLogger.Info("New WebSocket session connected", zap.Uint8("format", uint8(format)))
ctx, ctxCancelFn := context.WithCancel(context.Background())
wsMessageType := websocket.TextMessage
if format == SessionFormatProtobuf {
wsMessageType = websocket.BinaryMessage
}
return &sessionWS{
logger: sessionLogger,
config: config,
id: sessionID,
format: format,
userID: userID,
username: atomic.NewString(username),
vars: vars,
expiry: expiry,
clientIP: clientIP,
clientPort: clientPort,
ctx: ctx,
ctxCancelFn: ctxCancelFn,
jsonpbMarshaler: jsonpbMarshaler,
jsonpbUnmarshaler: jsonpbUnmarshaler,
wsMessageType: wsMessageType,
pingPeriodDuration: time.Duration(config.GetSocket().PingPeriodMs) * time.Millisecond,
pongWaitDuration: time.Duration(config.GetSocket().PongWaitMs) * time.Millisecond,
writeWaitDuration: time.Duration(config.GetSocket().WriteWaitMs) * time.Millisecond,
sessionRegistry: sessionRegistry,
matchmaker: matchmaker,
tracker: tracker,
pipeline: pipeline,
runtime: runtime,
stopped: false,
conn: conn,
receivedMessageCounter: config.GetSocket().PingBackoffThreshold,
pingTimer: time.NewTimer(time.Duration(config.GetSocket().PingPeriodMs) * time.Millisecond),
pingTimerCAS: atomic.NewUint32(1),
outgoingCh: make(chan []byte, config.GetSocket().OutgoingQueueSize),
}
}
func (s *sessionWS) Logger() *zap.Logger {
return s.logger
}
func (s *sessionWS) ID() uuid.UUID {
return s.id
}
func (s *sessionWS) UserID() uuid.UUID {
return s.userID
}
func (s *sessionWS) ClientIP() string {
return s.clientIP
}
func (s *sessionWS) ClientPort() string {
return s.clientPort
}
func (s *sessionWS) Context() context.Context {
return s.ctx
}
func (s *sessionWS) Username() string {
return s.username.Load()
}
func (s *sessionWS) SetUsername(username string) {
s.username.Store(username)
}
func (s *sessionWS) Vars() map[string]string {
return s.vars
}
func (s *sessionWS) Expiry() int64 {
return s.expiry
}
func (s *sessionWS) Consume() {
// Fire an event for session start.
if fn := s.runtime.EventSessionStart(); fn != nil {
fn(s.userID.String(), s.username.Load(), s.vars, s.expiry, s.id.String(), s.clientIP, s.clientPort, time.Now().UTC().Unix())
}
s.conn.SetReadLimit(s.config.GetSocket().MaxMessageSizeBytes)
if err := s.conn.SetReadDeadline(time.Now().Add(s.pongWaitDuration)); err != nil {
s.logger.Warn("Failed to set initial read deadline", zap.Error(err))
s.Close("failed to set initial read deadline")
return
}
s.conn.SetPongHandler(func(string) error {
s.maybeResetPingTimer()
return nil
})
// Start a routine to process outbound messages.
go s.processOutgoing()
var reason string
IncomingLoop:
for {
messageType, data, err := s.conn.ReadMessage()
if err != nil {
// Ignore "normal" WebSocket errors.
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
// Ignore underlying connection being shut down while read is waiting for data.
if e, ok := err.(*net.OpError); !ok || e.Err.Error() != "use of closed network connection" {
s.logger.Debug("Error reading message from client", zap.Error(err))
reason = err.Error()
}
}
break
}
if messageType != s.wsMessageType {
// Expected text but received binary, or expected binary but received text.
// Disconnect client if it attempts to use this kind of mixed protocol mode.
s.logger.Debug("Received unexpected WebSocket message type", zap.Int("expected", s.wsMessageType), zap.Int("actual", messageType))
reason = "received unexpected WebSocket message type"
break
}
s.receivedMessageCounter--
if s.receivedMessageCounter <= 0 {
s.receivedMessageCounter = s.config.GetSocket().PingBackoffThreshold
if !s.maybeResetPingTimer() {
// Problems resetting the ping timer indicate an error so we need to close the loop.
break
}
}
request := &rtapi.Envelope{}
switch s.format {
case SessionFormatProtobuf:
err = proto.Unmarshal(data, request)
case SessionFormatJson:
fallthrough
default:
err = s.jsonpbUnmarshaler.Unmarshal(bytes.NewReader(data), request)
}
if err != nil {
// If the payload is malformed the client is incompatible or misbehaving, either way disconnect it now.
s.logger.Warn("Received malformed payload", zap.Binary("data", data))
reason = "received malformed payload"
break
}
switch request.Cid {
case "":
if !s.pipeline.ProcessRequest(s.logger, s, request) {
reason = "error processing message"
break IncomingLoop
}
default:
requestLogger := s.logger.With(zap.String("cid", request.Cid))
if !s.pipeline.ProcessRequest(requestLogger, s, request) {
reason = "error processing message"
break IncomingLoop
}
}
}
s.Close(reason)
}
func (s *sessionWS) maybeResetPingTimer() bool {
// If there's already a reset in progress there's no need to wait.
if !s.pingTimerCAS.CAS(1, 0) {
return true
}
defer s.pingTimerCAS.CAS(0, 1)
s.Lock()
if s.stopped {
s.Unlock()
return false
}
// CAS ensures concurrency is not a problem here.
if !s.pingTimer.Stop() {
select {
case <-s.pingTimer.C:
default:
}
}
s.pingTimer.Reset(s.pingPeriodDuration)
err := s.conn.SetReadDeadline(time.Now().Add(s.pongWaitDuration))
s.Unlock()
if err != nil {
s.logger.Warn("Failed to set read deadline", zap.Error(err))
s.Close("failed to set read deadline")
return false
}
return true
}
func (s *sessionWS) processOutgoing() {
var reason string
OutgoingLoop:
for {
select {
case <-s.ctx.Done():
// Session is closing, close the outgoing process routine.
break OutgoingLoop
case <-s.pingTimer.C:
// Periodically send pings.
if msg, ok := s.pingNow(); !ok {
// If ping fails the session will be stopped, clean up the loop.
reason = msg
break OutgoingLoop
}
case payload := <-s.outgoingCh:
s.Lock()
if s.stopped {
// The connection may have stopped between the payload being queued on the outgoing channel and reaching here.
// If that's the case then abort outgoing processing at this point and exit.
s.Unlock()
break OutgoingLoop
}
// Process the outgoing message queue.
if err := s.conn.SetWriteDeadline(time.Now().Add(s.writeWaitDuration)); err != nil {
s.Unlock()
s.logger.Warn("Failed to set write deadline", zap.Error(err))
reason = err.Error()
break OutgoingLoop
}
if err := s.conn.WriteMessage(s.wsMessageType, payload); err != nil {
s.Unlock()
s.logger.Warn("Could not write message", zap.Error(err))
reason = err.Error()
break OutgoingLoop
}
s.Unlock()
}
}
s.Close(reason)
}
func (s *sessionWS) pingNow() (string, bool) {
s.Lock()
if s.stopped {
s.Unlock()
return "", false
}
if err := s.conn.SetWriteDeadline(time.Now().Add(s.writeWaitDuration)); err != nil {
s.Unlock()
s.logger.Warn("Could not set write deadline to ping", zap.Error(err))
return err.Error(), false
}
err := s.conn.WriteMessage(websocket.PingMessage, []byte{})
s.Unlock()
if err != nil {
s.logger.Warn("Could not send ping", zap.Error(err))
return err.Error(), false
}
return "", true
}
func (s *sessionWS) Format() SessionFormat {
return s.format
}
func (s *sessionWS) Send(envelope *rtapi.Envelope, reliable bool) error {
var payload []byte
var err error
switch s.format {
case SessionFormatProtobuf:
payload, err = proto.Marshal(envelope)
case SessionFormatJson:
fallthrough
default:
var buf bytes.Buffer
if err = s.jsonpbMarshaler.Marshal(&buf, envelope); err == nil {
payload = buf.Bytes()
}
}
if err != nil {
s.logger.Warn("Could not marshal envelope", zap.Error(err))
return err
}
if s.logger.Core().Enabled(zap.DebugLevel) {
switch envelope.Message.(type) {
case *rtapi.Envelope_Error:
s.logger.Debug("Sending error message", zap.Binary("payload", payload))
default:
s.logger.Debug(fmt.Sprintf("Sending %T message", envelope.Message), zap.Any("envelope", envelope))
}
}
return s.SendBytes(payload, reliable)
}
func (s *sessionWS) SendBytes(payload []byte, reliable bool) error {
s.Lock()
if s.stopped {
s.Unlock()
return nil
}
// Attempt to queue messages and observe failures.
select {
case s.outgoingCh <- payload:
s.Unlock()
return nil
default:
// The outgoing queue is full, likely because the remote client can't keep up.
// Terminate the connection immediately because the only alternative that doesn't block the server is
// to start dropping messages, which might cause unexpected behaviour.
s.Unlock()
s.logger.Warn("Could not write message, session outgoing queue full")
s.Close(ErrSessionQueueFull.Error())
return ErrSessionQueueFull
}
}
func (s *sessionWS) Close(reason string) {
s.Lock()
if s.stopped {
s.Unlock()
return
}
s.stopped = true
s.Unlock()
// Cancel any ongoing operations tied to this session.
s.ctxCancelFn()
if s.logger.Core().Enabled(zap.DebugLevel) {
s.logger.Info("Cleaning up closed client connection")
}
// When connection close originates internally in the session, ensure cleanup of external resources and references.
if err := s.matchmaker.RemoveAll(s.id); err != nil {
s.logger.Warn("Failed to remove all matchmaking tickets", zap.Error(err))
}
if s.logger.Core().Enabled(zap.DebugLevel) {
s.logger.Info("Cleaned up closed connection matchmaker")
}
s.tracker.UntrackAll(s.id)
if s.logger.Core().Enabled(zap.DebugLevel) {
s.logger.Info("Cleaned up closed connection tracker")
}
s.sessionRegistry.Remove(s.id)
if s.logger.Core().Enabled(zap.DebugLevel) {
s.logger.Info("Cleaned up closed connection session registry")
}
// Clean up internals.
s.pingTimer.Stop()
close(s.outgoingCh)
// Send close message.
if err := s.conn.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(s.writeWaitDuration)); err != nil {
// This may not be possible if the socket was already fully closed by an error.
s.logger.Debug("Could not send close message", zap.Error(err))
}
// Close WebSocket.
if err := s.conn.Close(); err != nil {
s.logger.Debug("Could not close", zap.Error(err))
}
s.logger.Info("Closed client connection")
// Fire an event for session end.
if fn := s.runtime.EventSessionEnd(); fn != nil {
fn(s.userID.String(), s.username.Load(), s.vars, s.expiry, s.id.String(), s.clientIP, s.clientPort, time.Now().UTC().Unix(), reason)
}
}