server.go
3.27 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
package actions
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
"pro2d/conf"
"pro2d/models"
"pro2d/protos/pb"
"pro2d/utils"
)
type LoginServer struct{
pb.UnsafeLoginServer
*BasicServer
}
func NewAccountServer() *LoginServer {
return &LoginServer{
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 *LoginServer)Start() error {
lis, err := s.BasicServer.Start(conf.GlobalConf.AccountConf)
if err != nil {
return err
}
models.InitAccountServerModels()
var opts []grpc.ServerOption
//TLS
creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key")
if err != nil {
utils.Sugar.Errorf("Failed to generate credentials %v", err)
return err
}
opts = append(opts, grpc.Creds(creds))
//拦截器
opts = append(opts, grpc.UnaryInterceptor(AccountServerInterceptor))
//new一个grpc
s.GrpcServer = grpc.NewServer(opts...)
pb.RegisterLoginServer(s.GrpcServer, s)
reflection.Register(s.GrpcServer) //在给定的gRPC服务器上注册服务器反射服务
// Serve方法在lis上接受传入连接,为每个连接创建一个ServerTransport和server的goroutine。
// 该goroutine读取gRPC请求,然后调用已注册的处理程序来响应它们。
utils.Sugar.Debugf("Start LoginServer listening on %d with TLS", conf.GlobalConf.AccountConf.Port)
return s.GrpcServer.Serve(lis)
}
func (s *LoginServer)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)
acc := utils.CheckAuth(ctx)
if acc == nil {
return nil, fmt.Errorf("token error")
}
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()
var opts []grpc.ServerOption
//TLS
creds, err := credentials.NewServerTLSFromFile("keys/server.pem", "keys/server.key")
if err != nil {
utils.Sugar.Errorf("Failed to generate credentials %v", err)
return err
}
opts = append(opts, grpc.Creds(creds))
//拦截器
opts = append(opts, grpc.UnaryInterceptor(GameServerInterceptor))
//new一个grpc
s.GrpcServer = grpc.NewServer(opts...)
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 with TLS", conf.GlobalConf.GameConf.Port)
return s.GrpcServer.Serve(lis)
}
func (s *GameServer)Stop() {
s.BasicServer.Stop()
}