-
Notifications
You must be signed in to change notification settings - Fork 52
/
server.go
178 lines (157 loc) · 5.39 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
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
169
170
171
172
173
174
175
176
177
178
package gate
import (
"context"
"github.com/dobyte/due/transport/grpc/v2/internal/code"
"github.com/dobyte/due/transport/grpc/v2/internal/pb"
"github.com/dobyte/due/transport/grpc/v2/internal/server"
"github.com/dobyte/due/v2/errors"
"github.com/dobyte/due/v2/packet"
"github.com/dobyte/due/v2/session"
"github.com/dobyte/due/v2/transport"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func NewServer(provider transport.GateProvider, opts *server.Options) (*server.Server, error) {
s, err := server.NewServer(opts)
if err != nil {
return nil, err
}
_ = s.RegisterService(&pb.Gate_ServiceDesc, &endpoint{provider: provider})
return s, nil
}
type endpoint struct {
pb.UnimplementedGateServer
provider transport.GateProvider
}
// Bind 将用户与当前网关进行绑定
func (e *endpoint) Bind(ctx context.Context, req *pb.BindRequest) (*pb.BindReply, error) {
err := e.provider.Bind(ctx, req.CID, req.UID)
if err != nil {
switch err {
case errors.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
case errors.ErrInvalidArgument:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.BindReply{}, nil
}
// Unbind 将用户与当前网关进行解绑
func (e *endpoint) Unbind(ctx context.Context, req *pb.UnbindRequest) (*pb.UnbindReply, error) {
err := e.provider.Unbind(ctx, req.UID)
if err != nil {
switch err {
case errors.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
case errors.ErrInvalidArgument:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.UnbindReply{}, nil
}
// GetIP 获取客户端IP地址
func (e *endpoint) GetIP(ctx context.Context, req *pb.GetIPRequest) (*pb.GetIPReply, error) {
ip, err := e.provider.GetIP(ctx, session.Kind(req.Kind), req.Target)
if err != nil {
switch err {
case errors.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
case errors.ErrInvalidArgument:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.GetIPReply{IP: ip}, nil
}
// Push 推送消息给连接
func (e *endpoint) Push(ctx context.Context, req *pb.PushRequest) (*pb.PushReply, error) {
err := e.provider.Push(ctx, session.Kind(req.Kind), req.Target, &packet.Message{
Seq: req.Message.Seq,
Route: req.Message.Route,
Buffer: req.Message.Buffer,
})
if err != nil {
switch err {
case errors.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.PushReply{}, nil
}
// Multicast 推送组播消息
func (e *endpoint) Multicast(ctx context.Context, req *pb.MulticastRequest) (*pb.MulticastReply, error) {
total, err := e.provider.Multicast(ctx, session.Kind(req.Kind), req.Targets, &packet.Message{
Seq: req.Message.Seq,
Route: req.Message.Route,
Buffer: req.Message.Buffer,
})
if err != nil {
switch err {
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.MulticastReply{Total: total}, nil
}
// Broadcast 推送广播消息
func (e *endpoint) Broadcast(ctx context.Context, req *pb.BroadcastRequest) (*pb.BroadcastReply, error) {
total, err := e.provider.Broadcast(ctx, session.Kind(req.Kind), &packet.Message{
Seq: req.Message.Seq,
Route: req.Message.Route,
Buffer: req.Message.Buffer,
})
if err != nil {
switch err {
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.BroadcastReply{Total: total}, nil
}
// Stat 统计会话总数
func (e *endpoint) Stat(ctx context.Context, req *pb.StatRequest) (*pb.StatReply, error) {
total, err := e.provider.Stat(ctx, session.Kind(req.Kind))
if err != nil {
switch err {
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.StatReply{Total: total}, nil
}
// Disconnect 断开连接
func (e *endpoint) Disconnect(ctx context.Context, req *pb.DisconnectRequest) (*pb.DisconnectReply, error) {
err := e.provider.Disconnect(ctx, session.Kind(req.Kind), req.Target, req.IsForce)
if err != nil {
switch err {
case errors.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case errors.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.DisconnectReply{}, nil
}