forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_ws.go
278 lines (240 loc) · 7.72 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
// Copyright 2017 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 (
"sync"
"time"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/gorilla/websocket"
"bytes"
"github.com/gogo/protobuf/jsonpb"
"go.uber.org/atomic"
"go.uber.org/zap"
)
type wsSession struct {
sync.Mutex
logger *zap.Logger
config Config
id string
userID string
handle *atomic.String
lang string
format SessionFormat
expiry int64
stopped bool
conn *websocket.Conn
jsonpbMarshaler *jsonpb.Marshaler
jsonpbUnmarshaler *jsonpb.Unmarshaler
pingTicker *time.Ticker
pingTickerStopCh chan bool
unregister func(s session)
}
// NewWSSession creates a new session which encapsulates a WebSocket connection.
func NewWSSession(logger *zap.Logger, config Config, userID string, handle string, lang string, format SessionFormat, expiry int64, websocketConn *websocket.Conn, jsonpbMarshaler *jsonpb.Marshaler,
jsonpbUnmarshaler *jsonpb.Unmarshaler, unregister func(s session)) session {
sessionID := generateNewId()
sessionLogger := logger.With(zap.String("uid", userID), zap.String("sid", sessionID))
sessionLogger.Debug("New WS session connected")
return &wsSession{
logger: sessionLogger,
config: config,
id: sessionID,
userID: userID,
handle: atomic.NewString(handle),
lang: lang,
format: format,
expiry: expiry,
conn: websocketConn,
jsonpbMarshaler: jsonpbMarshaler,
jsonpbUnmarshaler: jsonpbUnmarshaler,
stopped: false,
pingTicker: time.NewTicker(time.Duration(config.GetSocket().PingPeriodMs) * time.Millisecond),
pingTickerStopCh: make(chan bool),
unregister: unregister,
}
}
func (s *wsSession) Logger() *zap.Logger {
return s.logger
}
func (s *wsSession) ID() string {
return s.id
}
func (s *wsSession) UserID() string {
return s.userID
}
func (s *wsSession) Handle() string {
return s.handle.Load()
}
func (s *wsSession) SetHandle(handle string) {
s.handle.Store(handle)
}
func (s *wsSession) Lang() string {
return s.lang
}
func (s *wsSession) Expiry() int64 {
return s.expiry
}
func (s *wsSession) Consume(processRequest func(logger *zap.Logger, session session, envelope *Envelope, reliable bool)) {
defer s.cleanupClosedConnection()
s.conn.SetReadLimit(s.config.GetSocket().MaxMessageSizeBytes)
s.conn.SetReadDeadline(time.Now().Add(time.Duration(s.config.GetSocket().PongWaitMs) * time.Millisecond))
s.conn.SetPongHandler(func(string) error {
s.conn.SetReadDeadline(time.Now().Add(time.Duration(s.config.GetSocket().PongWaitMs) * time.Millisecond))
return nil
})
// Send an initial ping immediately, then at intervals.
s.pingNow()
go s.pingPeriodically()
for {
_, data, err := s.conn.ReadMessage()
if err != nil {
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
s.logger.Warn("Error reading message from client", zap.Error(err))
}
break
}
request := &Envelope{}
switch s.format {
case SessionFormatJson:
err = s.jsonpbUnmarshaler.Unmarshal(bytes.NewReader(data), request)
default:
err = proto.Unmarshal(data, request)
}
if err != nil {
if s.format == SessionFormatJson {
s.logger.Warn("Received malformed payload", zap.String("data", string(data)))
} else {
s.logger.Warn("Received malformed payload", zap.Any("data", data))
}
s.Send(ErrorMessage(request.CollationId, UNRECOGNIZED_PAYLOAD, "Unrecognized payload"), true)
} else {
// TODO Add session-global context here to cancel in-progress operations when the session is closed.
requestLogger := s.logger.With(zap.String("cid", request.CollationId))
processRequest(requestLogger, s, request, true)
}
}
}
func (s *wsSession) Unregister() {
s.unregister(s)
}
func (s *wsSession) pingPeriodically() {
for {
select {
case <-s.pingTicker.C:
if !s.pingNow() {
// If ping fails the session will be stopped, clean up the loop.
return
}
case <-s.pingTickerStopCh:
return
}
}
}
func (s *wsSession) pingNow() bool {
s.Lock()
if s.stopped {
s.Unlock()
return false
}
s.conn.SetWriteDeadline(time.Now().Add(time.Duration(s.config.GetSocket().WriteWaitMs) * time.Millisecond))
err := s.conn.WriteMessage(websocket.PingMessage, []byte{})
s.Unlock()
if err != nil {
s.logger.Warn("Could not send ping. Closing channel", zap.String("remoteAddress", s.conn.RemoteAddr().String()), zap.Error(err))
s.cleanupClosedConnection() // The connection has already failed
return false
}
// Server heartbeat.
err = s.Send(&Envelope{Payload: &Envelope_Heartbeat{&Heartbeat{Timestamp: nowMs()}}}, true)
if err != nil {
s.logger.Warn("Could not send heartbeat", zap.String("remoteAddress", s.conn.RemoteAddr().String()), zap.Error(err))
}
return true
}
func (s *wsSession) Format() SessionFormat {
return s.format
}
func (s *wsSession) Send(envelope *Envelope, reliable bool) error {
// NOTE: WebSocket sessions ignore the reliable flag and will always deliver messages reliably.
s.logger.Debug(fmt.Sprintf("Sending %T message", envelope.Payload), zap.String("cid", envelope.CollationId))
switch s.format {
case SessionFormatJson:
payload, err := s.jsonpbMarshaler.MarshalToString(envelope)
if err != nil {
s.logger.Warn("Could not marshall Response to json", zap.Error(err))
return err
}
return s.SendBytes([]byte(payload), reliable)
default:
payload, err := proto.Marshal(envelope)
if err != nil {
s.logger.Warn("Could not marshall Response to byte[]", zap.Error(err))
return err
}
return s.SendBytes(payload, reliable)
}
}
func (s *wsSession) SendBytes(payload []byte, reliable bool) error {
// NOTE: WebSocket sessions ignore the reliable flag and will always deliver messages reliably.
s.Lock()
defer s.Unlock()
if s.stopped {
return nil
}
var err error
s.conn.SetWriteDeadline(time.Now().Add(time.Duration(s.config.GetSocket().WriteWaitMs) * time.Millisecond))
switch s.format {
case SessionFormatJson:
err = s.conn.WriteMessage(websocket.TextMessage, payload)
default:
err = s.conn.WriteMessage(websocket.BinaryMessage, payload)
}
if err != nil {
s.logger.Warn("Could not write message", zap.Error(err))
}
return err
}
func (s *wsSession) cleanupClosedConnection() {
s.Lock()
if s.stopped {
s.Unlock()
return
}
s.stopped = true
s.Unlock()
s.logger.Debug("Cleaning up closed client connection", zap.String("remoteAddress", s.conn.RemoteAddr().String()))
s.unregister(s)
s.pingTicker.Stop()
close(s.pingTickerStopCh)
s.conn.Close()
s.logger.Debug("Closed client connection")
}
func (s *wsSession) Close() {
s.Lock()
if s.stopped {
s.Unlock()
return
}
s.stopped = true
s.Unlock()
s.pingTicker.Stop()
close(s.pingTickerStopCh)
err := s.conn.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(time.Duration(s.config.GetSocket().WriteWaitMs)*time.Millisecond))
if err != nil {
s.logger.Warn("Could not send close message. Closing prematurely.", zap.String("remoteAddress", s.conn.RemoteAddr().String()), zap.Error(err))
}
s.conn.Close()
s.logger.Debug("Closed client connection")
}