-
Notifications
You must be signed in to change notification settings - Fork 537
Expand file tree
/
Copy pathmain.go
More file actions
168 lines (145 loc) · 4.52 KB
/
Copy pathmain.go
File metadata and controls
168 lines (145 loc) · 4.52 KB
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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"strconv"
"strings"
"time"
"github.com/quic-go/quic-go"
"github.com/topfreegames/pitaya/v3/examples/demo/cluster_grpc/services"
pitaya "github.com/topfreegames/pitaya/v3/pkg"
"github.com/topfreegames/pitaya/v3/pkg/acceptor"
"github.com/topfreegames/pitaya/v3/pkg/cluster"
"github.com/topfreegames/pitaya/v3/pkg/component"
"github.com/topfreegames/pitaya/v3/pkg/config"
"github.com/topfreegames/pitaya/v3/pkg/constants"
"github.com/topfreegames/pitaya/v3/pkg/groups"
"github.com/topfreegames/pitaya/v3/pkg/modules"
"github.com/topfreegames/pitaya/v3/pkg/route"
)
var app pitaya.Pitaya
func configureBackend() {
room := services.NewRoom(app)
app.Register(room,
component.WithName("room"),
component.WithNameFunc(strings.ToLower),
)
app.RegisterRemote(room,
component.WithName("room"),
component.WithNameFunc(strings.ToLower),
)
}
func configureFrontend(port int) {
app.Register(services.NewConnector(app),
component.WithName("connector"),
component.WithNameFunc(strings.ToLower),
)
app.RegisterRemote(&services.ConnectorRemote{},
component.WithName("connectorremote"),
component.WithNameFunc(strings.ToLower),
)
err := app.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 = app.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())
}
}
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.Int("rpcsvport", 3434, "the port that grpc server will listen")
acceptorType := flag.String("acceptor", "tcp", "the type of acceptor to use (tcp or quic)")
flag.Parse()
meta := map[string]string{
constants.GRPCHostKey: "127.0.0.1",
constants.GRPCPortKey: strconv.Itoa(*rpcServerPort),
}
var bs *modules.ETCDBindingStorage
app, bs = createApp(*port, *isFrontend, *svType, meta, *rpcServerPort, *acceptorType)
defer app.Shutdown()
app.RegisterModule(bs, "bindingsStorage")
if !*isFrontend {
configureBackend()
} else {
configureFrontend(*port)
}
app.Start()
}
func createApp(port int, isFrontend bool, svType string, meta map[string]string, rpcServerPort int, acceptorType string) (pitaya.Pitaya, *modules.ETCDBindingStorage) {
builder := pitaya.NewDefaultBuilder(isFrontend, svType, pitaya.Cluster, meta, *config.NewDefaultPitayaConfig())
grpcServerConfig := builder.Config.Cluster.RPC.Server.Grpc
grpcServerConfig.Port = rpcServerPort
gs, err := cluster.NewGRPCServer(grpcServerConfig, builder.Server, builder.MetricsReporters)
if err != nil {
panic(err)
}
builder.RPCServer = gs
builder.Groups = groups.NewMemoryGroupService(builder.Config.Groups.Memory)
bs := modules.NewETCDBindingStorage(builder.Server, builder.SessionPool, builder.Config.Modules.BindingStorage.Etcd)
gc, err := cluster.NewGRPCClient(
builder.Config.Cluster.RPC.Client.Grpc,
builder.Server,
builder.MetricsReporters,
bs,
cluster.NewInfoRetriever(builder.Config.Cluster.Info),
)
if err != nil {
panic(err)
}
builder.RPCClient = gc
if isFrontend {
switch acceptorType {
case "quic":
tlsConf := &tls.Config{
Certificates: []tls.Certificate{
loadTLSCertificates(),
},
NextProtos: []string{"h3"},
}
quicConf := &quic.Config{
MaxIdleTimeout: 35 * time.Second,
EnableDatagrams: true,
}
quicAcceptor := acceptor.NewQuicAcceptor(fmt.Sprintf(":%d", port), tlsConf, quicConf)
builder.AddAcceptor(quicAcceptor)
case "tcp":
tcp := acceptor.NewTCPAcceptor(fmt.Sprintf(":%d", port))
builder.AddAcceptor(tcp)
default:
panic(fmt.Sprintf("Invalid acceptor type: %s. Use 'tcp' or 'quic'.", acceptorType))
}
}
return builder.Build(), bs
}
func loadTLSCertificates() tls.Certificate {
certPath := "../../../pkg/acceptor/fixtures/server.crt"
keyPath := "../../../pkg/acceptor/fixtures/server.key"
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
panic(fmt.Sprintf("Erro ao carregar certificados TLS: %v", err))
}
return cert
}