forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (105 loc) · 3 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"github.com/spf13/viper"
"strings"
"github.com/topfreegames/pitaya"
"github.com/topfreegames/pitaya/acceptor"
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/constants"
"github.com/topfreegames/pitaya/examples/demo/cluster_grpc/services"
"github.com/topfreegames/pitaya/modules"
"github.com/topfreegames/pitaya/route"
"github.com/topfreegames/pitaya/serialize/json"
)
func configureBackend() {
room := services.NewRoom()
pitaya.Register(room,
component.WithName("room"),
component.WithNameFunc(strings.ToLower),
)
pitaya.RegisterRemote(room,
component.WithName("room"),
component.WithNameFunc(strings.ToLower),
)
}
func configureFrontend(port int) {
tcp := acceptor.NewTCPAcceptor(fmt.Sprintf(":%d", port))
pitaya.Register(&services.Connector{},
component.WithName("connector"),
component.WithNameFunc(strings.ToLower),
)
pitaya.RegisterRemote(&services.ConnectorRemote{},
component.WithName("connectorremote"),
component.WithNameFunc(strings.ToLower),
)
err := pitaya.AddRoute("room", func(
ctx context.Context,
route *route.Route,
payload []byte,
servers map[string]*cluster.Server,
) (*cluster.Server, error) {
// will return the first server
for k := range servers {
return servers[k], nil
}
return nil, nil
})
if err != nil {
fmt.Printf("error adding route %s\n", err.Error())
}
err = pitaya.SetDictionary(map[string]uint16{
"connector.getsessiondata": 1,
"connector.setsessiondata": 2,
"room.room.getsessiondata": 3,
"onMessage": 4,
"onMembers": 5,
})
if err != nil {
fmt.Printf("error setting route dictionary %s\n", err.Error())
}
pitaya.AddAcceptor(tcp)
}
func main() {
port := flag.Int("port", 3250, "the port to listen")
svType := flag.String("type", "connector", "the server type")
isFrontend := flag.Bool("frontend", true, "if server is frontend")
rpcServerPort := flag.String("rpcsvport", "3434", "the port that grpc server will listen")
flag.Parse()
defer pitaya.Shutdown()
pitaya.SetSerializer(json.NewSerializer())
if !*isFrontend {
configureBackend()
} else {
configureFrontend(*port)
}
confs := viper.New()
confs.Set("pitaya.cluster.rpc.server.grpc.port", *rpcServerPort)
meta := map[string]string{
constants.GRPCHostKey: "127.0.0.1",
constants.GRPCPortKey: *rpcServerPort,
}
pitaya.Configure(*isFrontend, *svType, pitaya.Cluster, meta, confs)
gs, err := cluster.NewGRPCServer(pitaya.GetConfig(), pitaya.GetServer(), pitaya.GetMetricsReporters())
if err != nil {
panic(err)
}
bs := modules.NewETCDBindingStorage(pitaya.GetServer(), pitaya.GetConfig())
pitaya.RegisterModule(bs, "bindingsStorage")
gc, err := cluster.NewGRPCClient(
pitaya.GetConfig(),
pitaya.GetServer(),
pitaya.GetMetricsReporters(),
bs,
cluster.NewConfigInfoRetriever(pitaya.GetConfig()),
)
if err != nil {
panic(err)
}
pitaya.SetRPCServer(gs)
pitaya.SetRPCClient(gc)
pitaya.Start()
}