forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
189 lines (172 loc) · 6.82 KB
/
pipeline.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
// 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"
"strings"
"github.com/heroiclabs/nakama-common/rtapi"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
)
type Pipeline struct {
logger *zap.Logger
config Config
db *sql.DB
protojsonMarshaler *protojson.MarshalOptions
protojsonUnmarshaler *protojson.UnmarshalOptions
sessionRegistry SessionRegistry
statusRegistry StatusRegistry
matchRegistry MatchRegistry
partyRegistry PartyRegistry
matchmaker Matchmaker
tracker Tracker
router MessageRouter
runtime *Runtime
node string
}
func NewPipeline(logger *zap.Logger, config Config, db *sql.DB, protojsonMarshaler *protojson.MarshalOptions, protojsonUnmarshaler *protojson.UnmarshalOptions, sessionRegistry SessionRegistry, statusRegistry StatusRegistry, matchRegistry MatchRegistry, partyRegistry PartyRegistry, matchmaker Matchmaker, tracker Tracker, router MessageRouter, runtime *Runtime) *Pipeline {
return &Pipeline{
logger: logger,
config: config,
db: db,
protojsonMarshaler: protojsonMarshaler,
protojsonUnmarshaler: protojsonUnmarshaler,
sessionRegistry: sessionRegistry,
statusRegistry: statusRegistry,
matchRegistry: matchRegistry,
partyRegistry: partyRegistry,
matchmaker: matchmaker,
tracker: tracker,
router: router,
runtime: runtime,
node: config.GetName(),
}
}
func (p *Pipeline) ProcessRequest(logger *zap.Logger, session Session, in *rtapi.Envelope) bool {
if logger.Core().Enabled(zap.DebugLevel) { // remove extra heavy reflection processing
logger.Debug(fmt.Sprintf("Received %T message", in.Message), zap.Any("message", in.Message))
}
if in.Message == nil {
_ = session.Send(&rtapi.Envelope{Cid: in.Cid, Message: &rtapi.Envelope_Error{Error: &rtapi.Error{
Code: int32(rtapi.Error_MISSING_PAYLOAD),
Message: "Missing message.",
}}}, true)
return false
}
var pipelineFn func(*zap.Logger, Session, *rtapi.Envelope) (bool, *rtapi.Envelope)
switch in.Message.(type) {
case *rtapi.Envelope_ChannelJoin:
pipelineFn = p.channelJoin
case *rtapi.Envelope_ChannelLeave:
pipelineFn = p.channelLeave
case *rtapi.Envelope_ChannelMessageSend:
pipelineFn = p.channelMessageSend
case *rtapi.Envelope_ChannelMessageUpdate:
pipelineFn = p.channelMessageUpdate
case *rtapi.Envelope_ChannelMessageRemove:
pipelineFn = p.channelMessageRemove
case *rtapi.Envelope_MatchCreate:
pipelineFn = p.matchCreate
case *rtapi.Envelope_MatchDataSend:
pipelineFn = p.matchDataSend
case *rtapi.Envelope_MatchJoin:
pipelineFn = p.matchJoin
case *rtapi.Envelope_MatchLeave:
pipelineFn = p.matchLeave
case *rtapi.Envelope_MatchmakerAdd:
pipelineFn = p.matchmakerAdd
case *rtapi.Envelope_MatchmakerRemove:
pipelineFn = p.matchmakerRemove
case *rtapi.Envelope_Ping:
pipelineFn = p.ping
case *rtapi.Envelope_Pong:
pipelineFn = p.pong
case *rtapi.Envelope_Rpc:
pipelineFn = p.rpc
case *rtapi.Envelope_StatusFollow:
pipelineFn = p.statusFollow
case *rtapi.Envelope_StatusUnfollow:
pipelineFn = p.statusUnfollow
case *rtapi.Envelope_StatusUpdate:
pipelineFn = p.statusUpdate
case *rtapi.Envelope_PartyCreate:
pipelineFn = p.partyCreate
case *rtapi.Envelope_PartyJoin:
pipelineFn = p.partyJoin
case *rtapi.Envelope_PartyLeave:
pipelineFn = p.partyLeave
case *rtapi.Envelope_PartyPromote:
pipelineFn = p.partyPromote
case *rtapi.Envelope_PartyAccept:
pipelineFn = p.partyAccept
case *rtapi.Envelope_PartyRemove:
pipelineFn = p.partyRemove
case *rtapi.Envelope_PartyClose:
pipelineFn = p.partyClose
case *rtapi.Envelope_PartyJoinRequestList:
pipelineFn = p.partyJoinRequestList
case *rtapi.Envelope_PartyMatchmakerAdd:
pipelineFn = p.partyMatchmakerAdd
case *rtapi.Envelope_PartyMatchmakerRemove:
pipelineFn = p.partyMatchmakerRemove
case *rtapi.Envelope_PartyDataSend:
pipelineFn = p.partyDataSend
default:
// If we reached this point the envelope was valid but the contents are missing or unknown.
// Usually caused by a version mismatch, and should cause the session making this pipeline request to close.
logger.Error("Unrecognizable payload received.", zap.Any("payload", in))
_ = session.Send(&rtapi.Envelope{Cid: in.Cid, Message: &rtapi.Envelope_Error{Error: &rtapi.Error{
Code: int32(rtapi.Error_UNRECOGNIZED_PAYLOAD),
Message: "Unrecognized message.",
}}}, true)
return false
}
var messageName, messageNameID string
switch in.Message.(type) {
case *rtapi.Envelope_Rpc:
// No before/after hooks on RPC.
default:
messageName = fmt.Sprintf("%T", in.Message)
messageNameID = strings.ToLower(messageName)
if fn := p.runtime.BeforeRt(messageNameID); fn != nil {
hookResult, hookErr := fn(session.Context(), logger, session.UserID().String(), session.Username(), session.Vars(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), session.Lang(), in)
if hookErr != nil {
// Errors from before hooks do not close the session.
_ = session.Send(&rtapi.Envelope{Cid: in.Cid, Message: &rtapi.Envelope_Error{Error: &rtapi.Error{
Code: int32(rtapi.Error_RUNTIME_FUNCTION_EXCEPTION),
Message: hookErr.Error(),
}}}, true)
return true
} else if hookResult == nil {
// If result is nil, requested resource is disabled. Sessions calling disabled resources will be closed.
logger.Warn("Intercepted a disabled resource.", zap.String("resource", messageName))
_ = session.Send(&rtapi.Envelope{Cid: in.Cid, Message: &rtapi.Envelope_Error{Error: &rtapi.Error{
Code: int32(rtapi.Error_UNRECOGNIZED_PAYLOAD),
Message: "Requested resource was not found.",
}}}, true)
return false
}
in = hookResult
}
}
success, out := pipelineFn(logger, session, in)
if success && messageName != "" {
// Unsuccessful operations do not trigger after hooks.
if fn := p.runtime.AfterRt(messageNameID); fn != nil {
_ = fn(session.Context(), logger, session.UserID().String(), session.Username(), session.Vars(), session.Expiry(), session.ID().String(), session.ClientIP(), session.ClientPort(), session.Lang(), out, in)
}
}
return true
}