forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
359 lines (315 loc) · 9.62 KB
/
app.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
// Copyright (c) nano Author and TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package pitaya
import (
"encoding/gob"
"os"
"os/signal"
"reflect"
"strings"
"syscall"
"time"
"github.com/google/uuid"
"github.com/topfreegames/pitaya/acceptor"
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/internal/codec"
"github.com/topfreegames/pitaya/internal/message"
"github.com/topfreegames/pitaya/logger"
"github.com/topfreegames/pitaya/remote"
"github.com/topfreegames/pitaya/serialize"
"github.com/topfreegames/pitaya/serialize/protobuf"
"github.com/topfreegames/pitaya/service"
"github.com/topfreegames/pitaya/session"
"github.com/topfreegames/pitaya/timer"
)
// ServerMode represents a server mode
type ServerMode byte
const (
_ ServerMode = iota
// Cluster represents a server running with connection to other servers
Cluster
// Standalone represents a server running without connection to other servers
Standalone
)
// App is the base app struct
type App struct {
server *cluster.Server
debug bool
startAt time.Time
dieChan chan bool
acceptors []acceptor.Acceptor
heartbeat time.Duration
packetDecoder codec.PacketDecoder
packetEncoder codec.PacketEncoder
serializer serialize.Serializer
serviceDiscovery cluster.ServiceDiscovery
rpcServer cluster.RPCServer
rpcClient cluster.RPCClient
serverMode ServerMode
onSessionBind func(*session.Session)
configured bool
}
var (
app = &App{
server: &cluster.Server{
ID: uuid.New().String(),
Type: "game",
Data: map[string]string{},
Frontend: true,
},
debug: false,
startAt: time.Now(),
dieChan: make(chan bool),
acceptors: []acceptor.Acceptor{},
heartbeat: 30 * time.Second,
packetDecoder: codec.NewPomeloPacketDecoder(),
packetEncoder: codec.NewPomeloPacketEncoder(),
serverMode: Standalone,
serializer: protobuf.NewSerializer(),
configured: false,
}
log = logger.Log
remoteService *service.RemoteService
handlerService *service.HandlerService
)
// Configure configures the app
func Configure(isFrontend bool, serverType string, serverMode ServerMode) {
if app.configured {
log.Warn("pitaya configured twice!")
}
app.server.Frontend = isFrontend
app.server.Type = serverType
app.serverMode = serverMode
app.configured = true
}
// AddAcceptor adds a new acceptor to app
func AddAcceptor(ac acceptor.Acceptor) {
if !app.server.Frontend {
log.Error("tried to add an acceptor to a backend server, skipping")
return
}
app.acceptors = append(app.acceptors, ac)
}
// SetDebug toggles debug on/off
func SetDebug(debug bool) {
app.debug = debug
}
// SetPacketDecoder changes the decoder used to parse messages received
func SetPacketDecoder(d codec.PacketDecoder) {
app.packetDecoder = d
}
// SetPacketEncoder changes the encoder used to package outgoing messages
func SetPacketEncoder(e codec.PacketEncoder) {
app.packetEncoder = e
}
// SetHeartbeatTime sets the heartbeat time
func SetHeartbeatTime(interval time.Duration) {
app.heartbeat = interval
}
// SetRPCServer to be used
func SetRPCServer(s cluster.RPCServer) {
//TODO
app.rpcServer = s
if reflect.TypeOf(s) == reflect.TypeOf(&cluster.NatsRPCServer{}) {
// When using nats rpc server the server must start listening to messages
// destined to the userID that's binding
session.SetOnSessionBind(func(s *session.Session) error {
if app.server.Frontend {
subs, err := app.rpcServer.(*cluster.NatsRPCServer).SubscribeToUserMessages(s.UID())
if err != nil {
return err
}
s.Subscription = subs
}
return nil
})
}
}
// SetRPCClient to be used
func SetRPCClient(s cluster.RPCClient) {
app.rpcClient = s
}
// SetServiceDiscoveryClient to be used
func SetServiceDiscoveryClient(s cluster.ServiceDiscovery) {
app.serviceDiscovery = s
}
// SetSerializer customize application serializer, which automatically Marshal
// and UnMarshal handler payload
func SetSerializer(seri serialize.Serializer) {
app.serializer = seri
}
// SetServerType sets the server type
// TODO need to specify in start
func SetServerType(t string) {
app.server.Type = t
}
// SetServerData sets the server data that will be broadcasted using service discovery to other servers
// TODO need to specify in start
func SetServerData(data map[string]string) {
app.server.Data = data
}
func startDefaultSD() {
// initialize default service discovery
// TODO remove this, force specifying
var err error
app.serviceDiscovery, err = cluster.NewEtcdServiceDiscovery(
[]string{"localhost:2379"},
time.Duration(5)*time.Second,
"pitaya/",
time.Duration(20)*time.Second,
time.Duration(60)*time.Second,
time.Duration(120)*time.Second,
app.server,
)
if err != nil {
log.Fatalf("error starting cluster service discovery component: %s", err.Error())
}
}
func startDefaultRPCServer() {
// initialize default rpc server
// TODO remove this, force specifying
var err error
SetRPCServer(cluster.NewNatsRPCServer(
"nats://localhost:4222",
app.server,
))
if err != nil {
log.Fatalf("error starting cluster rpc server component: %s", err.Error())
}
}
func startDefaultRPCClient() {
// initialize default rpc client
// TODO remove this, force specifying
var err error
app.rpcClient = cluster.NewNatsRPCClient(
"nats://localhost:4222",
app.server,
)
if err != nil {
log.Fatalf("error starting cluster rpc client component: %s", err.Error())
}
}
func initSysRemotes() {
gob.Register(&session.Data{})
sys := &remote.Sys{}
RegisterRemote(sys,
component.WithName("sys"),
component.WithNameFunc(strings.ToLower),
)
}
// Start starts the app
// TODO fix non cluster mode
func Start() {
if !app.configured {
log.Fatal("starting app without configuring it first! call pitaya.Configure()")
}
if !app.server.Frontend && len(app.acceptors) > 0 {
log.Fatal("acceptors are not allowed on backend servers")
}
if app.serverMode == Cluster {
if app.serviceDiscovery == nil {
log.Warn("creating default service discovery because cluster mode is enabled, if you want to specify yours, use pitaya.SetServiceDiscoveryClient")
startDefaultSD()
}
if app.rpcServer == nil {
log.Warn("creating default rpc server because cluster mode is enabled, if you want to specify yours, use pitaya.SetRPCServer")
startDefaultRPCServer()
}
if app.rpcClient == nil {
log.Warn("creating default rpc client because cluster mode is enabled, if you want to specify yours, use pitaya.SetRPCClient")
startDefaultRPCClient()
RegisterModule(app.serviceDiscovery, "serviceDiscovery")
RegisterModule(app.rpcServer, "rpcServer")
RegisterModule(app.rpcClient, "rpcClient")
}
remoteService = service.NewRemoteService(
app.rpcClient,
app.rpcServer,
app.serviceDiscovery,
app.packetEncoder,
app.serializer,
)
initSysRemotes()
}
handlerService = service.NewHandlerService(
app.dieChan,
app.packetDecoder,
app.packetEncoder,
app.serializer,
app.heartbeat,
app.server,
remoteService,
)
listen()
defer timer.GlobalTicker.Stop()
sg := make(chan os.Signal)
signal.Notify(sg, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL)
// stop server
select {
case <-app.dieChan:
log.Warn("The app will shutdown in a few seconds")
case s := <-sg:
log.Warn("got signal", s)
}
log.Warn("server is stopping...")
shutdownModules()
shutdownComponents()
}
func listen() {
startupComponents()
// create global ticker instance, timer precision could be customized
// by SetTimerPrecision
timer.GlobalTicker = time.NewTicker(timer.Precision)
log.Infof("starting server %s:%s", app.server.Type, app.server.ID)
go handlerService.Dispatch()
for _, acc := range app.acceptors {
a := acc
go func() {
for conn := range a.GetConnChan() {
go handlerService.Handle(conn)
}
}()
go func() {
a.ListenAndServe()
}()
log.Infof("listening with acceptor %s on addr %s", reflect.TypeOf(a), a.GetAddr())
}
startModules()
// this handles remote messages
// TODO probably this shouldnt be here :/
if app.rpcServer != nil {
// TODO config concurrency, should this be done this way?
processMsgConcurrency := 100
for i := 0; i < processMsgConcurrency; i++ {
go remoteService.ProcessRemoteMessages(i)
// TODO: use same parellelism?
go remoteService.ProcessUserPush()
}
}
}
// SetDictionary set routes map, TODO(warning): set dictionary in runtime would be a dangerous operation!!!!!!
func SetDictionary(dict map[string]uint16) {
message.SetDictionary(dict)
}
// Shutdown send a signal to let 'pitaya' shutdown itself.
func Shutdown() {
close(app.dieChan)
}