-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
103 lines (89 loc) · 2.97 KB
/
server.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
package grpc
import (
"context"
"net"
"time"
pb "github.com/youminxue/euler/api/logic/grpc"
"github.com/youminxue/euler/internal/logic"
"github.com/youminxue/euler/internal/logic/conf"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
// use gzip decoder
_ "google.golang.org/grpc/encoding/gzip"
)
// New logic grpc server
func New(c *conf.RPCServer, l *logic.Logic) *grpc.Server {
keepParams := grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: time.Duration(c.IdleTimeout),
MaxConnectionAgeGrace: time.Duration(c.ForceCloseWait),
Time: time.Duration(c.KeepAliveInterval),
Timeout: time.Duration(c.KeepAliveTimeout),
MaxConnectionAge: time.Duration(c.MaxLifeTime),
})
srv := grpc.NewServer(keepParams)
pb.RegisterLogicServer(srv, &server{l})
lis, err := net.Listen(c.Network, c.Addr)
if err != nil {
panic(err)
}
go func() {
if err := srv.Serve(lis); err != nil {
panic(err)
}
}()
return srv
}
type server struct {
srv *logic.Logic
}
var _ pb.LogicServer = &server{}
// Ping Service
func (s *server) Ping(ctx context.Context, req *pb.PingReq) (*pb.PingReply, error) {
return &pb.PingReply{}, nil
}
// Close Service
func (s *server) Close(ctx context.Context, req *pb.CloseReq) (*pb.CloseReply, error) {
return &pb.CloseReply{}, nil
}
// Connect connect a conn.
func (s *server) Connect(ctx context.Context, req *pb.ConnectReq) (*pb.ConnectReply, error) {
mid, key, room, accepts, hb, err := s.srv.Connect(ctx, req.Server, req.Cookie, req.Token)
if err != nil {
return &pb.ConnectReply{}, err
}
return &pb.ConnectReply{Mid: mid, Key: key, RoomID: room, Accepts: accepts, Heartbeat: hb}, nil
}
// Disconnect disconnect a conn.
func (s *server) Disconnect(ctx context.Context, req *pb.DisconnectReq) (*pb.DisconnectReply, error) {
has, err := s.srv.Disconnect(ctx, req.Mid, req.Key, req.Server)
if err != nil {
return &pb.DisconnectReply{}, err
}
return &pb.DisconnectReply{Has: has}, nil
}
// Heartbeat beartbeat a conn.
func (s *server) Heartbeat(ctx context.Context, req *pb.HeartbeatReq) (*pb.HeartbeatReply, error) {
if err := s.srv.Heartbeat(ctx, req.Mid, req.Key, req.Server); err != nil {
return &pb.HeartbeatReply{}, err
}
return &pb.HeartbeatReply{}, nil
}
// RenewOnline renew server online.
func (s *server) RenewOnline(ctx context.Context, req *pb.OnlineReq) (*pb.OnlineReply, error) {
allRoomCount, err := s.srv.RenewOnline(ctx, req.Server, req.RoomCount)
if err != nil {
return &pb.OnlineReply{}, err
}
return &pb.OnlineReply{AllRoomCount: allRoomCount}, nil
}
// Receive receive a message.
func (s *server) Receive(ctx context.Context, req *pb.ReceiveReq) (*pb.ReceiveReply, error) {
if err := s.srv.Receive(ctx, req.Mid, req.Proto); err != nil {
return &pb.ReceiveReply{}, err
}
return &pb.ReceiveReply{}, nil
}
// nodes return nodes.
func (s *server) Nodes(ctx context.Context, req *pb.NodesReq) (*pb.NodesReply, error) {
return s.srv.NodesWeighted(ctx, req.Platform, req.ClientIP), nil
}