forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_registry.go
350 lines (315 loc) · 10.4 KB
/
match_registry.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
// 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 (
"database/sql"
"fmt"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/heroiclabs/nakama/api"
"github.com/heroiclabs/nakama/social"
"github.com/satori/go.uuid"
"github.com/yuin/gopher-lua"
"go.uber.org/zap"
"sync"
"time"
)
var (
MatchFilterValue = uint8(0)
MatchFilterPtr = &MatchFilterValue
MatchFilterAny = map[uint8]*uint8{StreamModeMatchRelayed: MatchFilterPtr, StreamModeMatchAuthoritative: MatchFilterPtr}
MatchFilterRelayed = map[uint8]*uint8{StreamModeMatchRelayed: MatchFilterPtr}
MatchFilterAuthoritative = map[uint8]*uint8{StreamModeMatchAuthoritative: MatchFilterPtr}
)
type MatchPresence struct {
Node string
UserID uuid.UUID
SessionID uuid.UUID
Username string
}
type MatchJoinResult struct {
Allow bool
Reason string
Label string
}
type MatchRegistry interface {
// Create and start a new match, given a Lua module name.
NewMatch(name string, params interface{}) (*MatchHandler, error)
// Return a match handler by ID, only from the local node.
GetMatch(id uuid.UUID) *MatchHandler
// Remove a tracked match and ensure all its presences are cleaned up.
// Does not ensure the match process itself is no longer running, that must be handled separately.
RemoveMatch(id uuid.UUID, stream PresenceStream)
// List (and optionally filter) currently running matches.
// This can list across both authoritative and relayed matches.
ListMatches(limit int, authoritative *wrappers.BoolValue, label *wrappers.StringValue, minSize *wrappers.Int32Value, maxSize *wrappers.Int32Value) []*api.Match
// Stop the match registry and close all matches it's tracking.
Stop()
// Pass a user join attempt to a match handler. Returns if the match was found, if the join was accepted, a reason for any rejection, and the match label.
Join(id uuid.UUID, node string, userID, sessionID uuid.UUID, username, fromNode string) (bool, bool, string, string)
// Notify a match handler that a user has left or disconnected.
// Expects that the caller has already determined the match is hosted on the current node.
Leave(id uuid.UUID, presences []*MatchPresence)
// Called by match handlers to request the removal fo a match participant.
Kick(stream PresenceStream, presences []*MatchPresence)
// Pass a data payload (usually from a user) to the appropriate match handler.
// Assumes that the data sender has already been validated as a match participant before this call.
SendData(id uuid.UUID, node string, userID, sessionID uuid.UUID, username, fromNode string, opCode int64, data []byte, receiveTime int64)
}
type LocalMatchRegistry struct {
sync.RWMutex
logger *zap.Logger
db *sql.DB
config Config
socialClient *social.Client
leaderboardCache LeaderboardCache
sessionRegistry *SessionRegistry
tracker Tracker
router MessageRouter
stdLibs map[string]lua.LGFunction
modules *sync.Map
once *sync.Once
node string
matches map[uuid.UUID]*MatchHandler
}
func NewLocalMatchRegistry(logger *zap.Logger, db *sql.DB, config Config, socialClient *social.Client, leaderboardCache LeaderboardCache, sessionRegistry *SessionRegistry, tracker Tracker, router MessageRouter, stdLibs map[string]lua.LGFunction, once *sync.Once, node string) MatchRegistry {
return &LocalMatchRegistry{
logger: logger,
db: db,
config: config,
socialClient: socialClient,
leaderboardCache: leaderboardCache,
sessionRegistry: sessionRegistry,
tracker: tracker,
router: router,
stdLibs: stdLibs,
once: once,
node: node,
matches: make(map[uuid.UUID]*MatchHandler),
}
}
func (r *LocalMatchRegistry) NewMatch(name string, params interface{}) (*MatchHandler, error) {
id := uuid.Must(uuid.NewV4())
match, err := NewMatchHandler(r.logger, r.db, r.config, r.socialClient, r.leaderboardCache, r.sessionRegistry, r, r.tracker, r.router, r.stdLibs, r.once, id, r.node, name, params)
if err != nil {
return nil, err
}
r.Lock()
r.matches[id] = match
r.Unlock()
return match, nil
}
func (r *LocalMatchRegistry) GetMatch(id uuid.UUID) *MatchHandler {
var mh *MatchHandler
r.RLock()
mh = r.matches[id]
r.RUnlock()
return mh
}
func (r *LocalMatchRegistry) RemoveMatch(id uuid.UUID, stream PresenceStream) {
r.Lock()
delete(r.matches, id)
r.Unlock()
r.tracker.UntrackByStream(stream)
}
func (r *LocalMatchRegistry) ListMatches(limit int, authoritative *wrappers.BoolValue, label *wrappers.StringValue, minSize *wrappers.Int32Value, maxSize *wrappers.Int32Value) []*api.Match {
var modes map[uint8]*uint8
if authoritative == nil {
modes = MatchFilterAny
} else if authoritative.Value {
modes = MatchFilterAuthoritative
} else {
modes = MatchFilterRelayed
}
// Initial list of candidate matches.
matches := r.tracker.CountByStreamModeFilter(modes)
// Results.
results := make([]*api.Match, 0, limit)
// Track authoritative matches that have been checked already, if authoritative results are allowed.
var checked map[uuid.UUID]struct{}
if authoritative == nil || authoritative.Value {
checked = make(map[uuid.UUID]struct{})
}
// Maybe filter by label.
for stream, size := range matches {
if stream.Mode == StreamModeMatchRelayed {
if label != nil {
// Any label filter fails for relayed matches.
continue
}
if minSize != nil && minSize.Value > size {
// Too few users.
continue
}
if maxSize != nil && maxSize.Value < size {
// Too many users.
continue
}
matchID := fmt.Sprintf("%v.", stream.Subject.String())
results = append(results, &api.Match{
MatchId: matchID,
Authoritative: false,
// No label.
Size: size,
})
if len(results) == limit {
break
}
} else if stream.Mode == StreamModeMatchAuthoritative {
// Authoritative matches that have already been checked.
checked[stream.Subject] = struct{}{}
if minSize != nil && minSize.Value > size {
// Too few users.
continue
}
if maxSize != nil && maxSize.Value < size {
// Too many users.
continue
}
mh := r.GetMatch(stream.Subject)
if mh == nil || (label != nil && label.Value != mh.Label) {
continue
}
results = append(results, &api.Match{
MatchId: mh.IDStr,
Authoritative: true,
Label: &wrappers.StringValue{Value: mh.Label},
Size: size,
})
if len(results) == limit {
break
}
} else {
r.logger.Warn("Ignoring unknown stream mode in match listing operation", zap.Uint8("mode", stream.Mode))
}
}
// Return results here if:
// 1. We have enough results.
// or
// 2. Not enough results, but we're not allowed to return potentially empty authoritative matches.
if len(results) == limit || ((authoritative != nil && !authoritative.Value) || (minSize != nil && minSize.Value > 0)) {
return results
}
// Otherwise look for empty matches to help fulfil the request, but ensure no duplicates.
r.RLock()
for _, mh := range r.matches {
if _, ok := checked[mh.ID]; ok {
// Already checked and discarded this match for failing a filter, skip it.
continue
}
if label != nil && label.Value != mh.Label {
// Label mismatch.
continue
}
size := int32(r.tracker.CountByStream(mh.Stream))
if minSize != nil && minSize.Value > size {
// Too few users.
continue
}
if maxSize != nil && maxSize.Value < size {
// Too many users.
continue
}
results = append(results, &api.Match{
MatchId: mh.IDStr,
Authoritative: true,
Label: &wrappers.StringValue{Value: mh.Label},
Size: size,
})
if len(results) == limit {
break
}
}
r.RUnlock()
return results
}
func (r *LocalMatchRegistry) Stop() {
r.Lock()
for id, mh := range r.matches {
mh.Close()
delete(r.matches, id)
}
r.Unlock()
}
func (r *LocalMatchRegistry) Join(id uuid.UUID, node string, userID, sessionID uuid.UUID, username, fromNode string) (bool, bool, string, string) {
if node != r.node {
return false, false, "", ""
}
var mh *MatchHandler
var ok bool
r.RLock()
mh, ok = r.matches[id]
r.RUnlock()
if !ok {
return false, false, "", ""
}
resultCh := make(chan *MatchJoinResult, 1)
if !mh.QueueCall(JoinAttempt(resultCh, userID, sessionID, username, fromNode)) {
// The match call queue was full, so will be closed and therefore can't be joined.
return true, false, "", ""
}
// Set up a limit to how long the call will wait, default is 10 seconds.
ticker := time.NewTicker(time.Second * 10)
select {
case <-ticker.C:
ticker.Stop()
// The join attempt has timed out, join is assumed to be rejected.
return true, false, "", ""
case r := <-resultCh:
ticker.Stop()
// The join attempt has returned a result.
return true, r.Allow, r.Reason, r.Label
}
}
func (r *LocalMatchRegistry) Leave(id uuid.UUID, presences []*MatchPresence) {
var mh *MatchHandler
var ok bool
r.RLock()
mh, ok = r.matches[id]
r.RUnlock()
if !ok {
return
}
// Doesn't matter if the call queue was full here. If the match is being closed then leaves don't matter anyway.
mh.QueueCall(Leave(presences))
}
func (r *LocalMatchRegistry) Kick(stream PresenceStream, presences []*MatchPresence) {
for _, presence := range presences {
if presence.Node != r.node {
continue
}
r.tracker.Untrack(presence.SessionID, stream, presence.UserID)
}
}
func (r *LocalMatchRegistry) SendData(id uuid.UUID, node string, userID, sessionID uuid.UUID, username, fromNode string, opCode int64, data []byte, receiveTime int64) {
if node != r.node {
return
}
var mh *MatchHandler
var ok bool
r.RLock()
mh, ok = r.matches[id]
r.RUnlock()
if !ok {
return
}
mh.QueueData(&MatchDataMessage{
UserID: userID,
SessionID: sessionID,
Username: username,
Node: node,
OpCode: opCode,
Data: data,
ReceiveTime: receiveTime,
})
}