server.go
2.55 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
package actions
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"pro2d/conf"
"pro2d/models"
"pro2d/protos/pb"
"pro2d/utils"
)
type AccountServer struct{
pb.UnimplementedAccountServer
*BasicServer
}
func NewAccountServer() *AccountServer {
return &AccountServer{
BasicServer: NewServer(),
}
}
//拦截器
func AccountServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req)
resp, err := handler(ctx, req)
return resp, err
}
func (s *AccountServer)Start() error {
lis, err := s.BasicServer.Start(conf.GlobalConf.AccountConf)
if err != nil {
return err
}
models.InitAccountServerModels()
//new一个grpc
s.GrpcServer = grpc.NewServer(grpc.UnaryInterceptor(AccountServerInterceptor))
pb.RegisterAccountServer(s.GrpcServer, s)
reflection.Register(s.GrpcServer) //在给定的gRPC服务器上注册服务器反射服务
// Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。
// 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。
utils.Sugar.Debugf("Start AccountServer listening on %d", conf.GlobalConf.AccountConf.Port)
return s.GrpcServer.Serve(lis)
}
func (s *AccountServer)Stop() {
s.BasicServer.Stop()
}
type GameServer struct{
pb.UnimplementedGameServer
*BasicServer
}
func NewGameServer() *GameServer {
return &GameServer{
BasicServer: NewServer(),
}
}
//拦截器
func GameServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
utils.Sugar.Debugf("gRPC method: %s, %v", info.FullMethod, req)
resp, err := handler(ctx, req)
return resp, err
}
func (s *GameServer)Start() error {
lis, err := s.BasicServer.Start(conf.GlobalConf.GameConf)
if err != nil {
return err
}
models.InitGameServerModels()
//new一个grpc
s.GrpcServer = grpc.NewServer(grpc.UnaryInterceptor(GameServerInterceptor))
pb.RegisterGameServer(s.GrpcServer, s)
reflection.Register(s.GrpcServer) //在给定的gRPC服务器上注册服务器反射服务
// Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。
// 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。
utils.Sugar.Debugf("Start GameServer listening on %d", conf.GlobalConf.GameConf.Port)
return s.GrpcServer.Serve(lis)
}
func (s *GameServer)Stop() {
s.BasicServer.Stop()
}