forked from dobyte/due
/
server.go
321 lines (276 loc) · 8.81 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package gate
import (
"context"
"github.com/symsimmy/due/log"
"github.com/symsimmy/due/packet"
"github.com/symsimmy/due/session"
"github.com/symsimmy/due/transport"
"github.com/symsimmy/due/transport/grpc/code"
"github.com/symsimmy/due/transport/grpc/internal/pb"
"github.com/symsimmy/due/transport/grpc/internal/server"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io"
"time"
)
const defaultWriteChannelSize = 10240
func NewServer(provider transport.GateProvider, opts *server.Options) (*server.Server, error) {
s, err := server.NewServer(opts)
if err != nil {
return nil, err
}
endpoint := &endpoint{provider: provider}
endpoint.multicastCh = make(chan *pb.MulticastRequest, defaultWriteChannelSize)
endpoint.pushCh = make(chan *pb.PushRequest, defaultWriteChannelSize)
endpoint.broadcastCh = make(chan *pb.BroadcastRequest, defaultWriteChannelSize)
s.RegisterService(&pb.Gate_ServiceDesc, endpoint)
go endpoint.dispatch()
return s, nil
}
type endpoint struct {
pb.UnimplementedGateServer
provider transport.GateProvider
multicastCh chan *pb.MulticastRequest
pushCh chan *pb.PushRequest
broadcastCh chan *pb.BroadcastRequest
}
// 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 session.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case session.ErrInvalidSessionKind:
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 session.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case session.ErrInvalidSessionKind:
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 session.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case session.ErrInvalidSessionKind:
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
}
// 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 {
return nil, err
}
return &pb.StatReply{Total: total}, nil
}
// IsOnline 用户是否在线
func (e *endpoint) IsOnline(ctx context.Context, req *pb.IsOnlineRequest) (*pb.IsOnlineReply, error) {
isOnline, err := e.provider.IsOnline(ctx, session.Kind(req.Kind), req.Target)
if err != nil {
return nil, err
}
return &pb.IsOnlineReply{IsOnline: isOnline}, nil
}
// GetID 获取ID
func (e *endpoint) GetID(ctx context.Context, req *pb.GetIdRequest) (*pb.GetIdReply, error) {
id, err := e.provider.GetID(ctx, session.Kind(req.Kind), req.Target)
if err != nil {
return nil, err
}
return &pb.GetIdReply{Id: id}, nil
}
// Push 推送消息给连接
func (e *endpoint) Push(stream pb.Gate_PushServer) error {
for {
//从流中获取消息
req, err := stream.Recv()
if err == io.EOF {
//发送结果,并关闭
return stream.SendAndClose(&pb.PushReply{})
}
if err != nil {
log.Errorf("gate server receive push message failed.err:%v", err)
return err
}
e.pushCh <- req
}
}
// BatchPush 批量推送消息给连接
func (e *endpoint) BatchPush(ctx context.Context, req *pb.BatchPushRequest) (*pb.BatchPushReply, error) {
for _, request := range req.Request {
err := e.provider.Push(ctx, session.Kind(request.Kind), request.Target, &packet.Message{
Seq: request.Message.Seq,
Route: request.Message.Route,
Buffer: request.Message.Buffer,
})
if err != nil {
log.Warnf("method:[BatchPush] do request:[%+v] encountered error:[%v]", request, err.Error())
}
}
return &pb.BatchPushReply{}, nil
}
// Multicast 推送组播消息
func (e *endpoint) Multicast(stream pb.Gate_MulticastServer) error {
for {
// 从流中获取消息
req, err := stream.Recv()
if err == io.EOF {
// 发送结果,并关闭
return stream.SendAndClose(&pb.MulticastReply{Total: 0})
}
if err != nil {
log.Errorf("gate server receive multicast message failed.err:%v", err)
return err
}
e.multicastCh <- req
}
}
// BatchMulticast 批量推送组播消息
func (e *endpoint) BatchMulticast(ctx context.Context, req *pb.BatchMulticastRequest) (*pb.BatchMulticastReply, error) {
for _, request := range req.Request {
_, err := e.provider.Multicast(ctx, session.Kind(request.Kind), request.Targets, &packet.Message{
Seq: request.Message.Seq,
Route: request.Message.Route,
Buffer: request.Message.Buffer,
})
if err != nil {
log.Warnf("method:[BatchMulticast] do request:[%+v] encountered error:[%v]", request, err.Error())
}
}
return &pb.BatchMulticastReply{}, nil
}
// Broadcast 推送广播消息
func (e *endpoint) Broadcast(stream pb.Gate_BroadcastServer) error {
for {
// 从流中获取消息
req, err := stream.Recv()
if err == io.EOF {
// 发送结果,并关闭
return stream.SendAndClose(&pb.BroadcastReply{Total: 0})
}
if err != nil {
log.Errorf("gate server receive broadcast message failed.err:%v", err)
return err
}
e.broadcastCh <- req
}
}
// BatchBroadcast 批量推送广播消息
func (e *endpoint) BatchBroadcast(ctx context.Context, req *pb.BatchBroadcastRequest) (*pb.BatchBroadcastReply, error) {
for _, request := range req.Request {
_, err := e.provider.Broadcast(ctx, session.Kind(request.Kind), &packet.Message{
Seq: request.Message.Seq,
Route: request.Message.Route,
Buffer: request.Message.Buffer,
})
if err != nil {
log.Warnf("method:[BatchBroadcast] do request:[%+v] encountered error:[%v]", request, err.Error())
}
}
return &pb.BatchBroadcastReply{}, 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 session.ErrNotFoundSession:
return nil, status.New(code.NotFoundSession, err.Error()).Err()
case session.ErrInvalidSessionKind:
return nil, status.New(codes.InvalidArgument, err.Error()).Err()
default:
return nil, status.New(codes.Internal, err.Error()).Err()
}
}
return &pb.DisconnectReply{}, nil
}
func (e *endpoint) dispatch() {
defer func() {
if r := recover(); r != nil {
log.Errorf("dispatch multi panic: %v", r)
}
}()
writeChBacklogTicker := time.NewTicker(1 * time.Second)
defer writeChBacklogTicker.Stop()
for {
select {
case <-writeChBacklogTicker.C:
if len(e.multicastCh) >= defaultWriteChannelSize {
log.Warnf("multicastCh size:%+v", len(e.multicastCh))
}
if len(e.pushCh) >= defaultWriteChannelSize {
log.Warnf("pushCh size:%+v", len(e.pushCh))
}
if len(e.broadcastCh) >= defaultWriteChannelSize {
log.Warnf("broadcastCh size:%+v", len(e.broadcastCh))
}
case req, ok := <-e.multicastCh:
if !ok {
return
}
go func(req *pb.MulticastRequest) {
_, err := e.provider.Multicast(context.Background(), session.Kind(req.Kind), req.Targets, &packet.Message{
Seq: req.Message.Seq,
Route: req.Message.Route,
Buffer: req.Message.Buffer,
})
if err != nil {
log.Warnf("dispatch multicast message failed.err:%+v", err)
}
}(req)
case req, ok := <-e.pushCh:
if !ok {
return
}
err := e.provider.Push(context.Background(), session.Kind(req.Kind), req.Target, &packet.Message{
Seq: req.Message.Seq,
Route: req.Message.Route,
Buffer: req.Message.Buffer,
})
if err != nil {
log.Warnf("dispatch push message failed.err:%+v", err)
}
if len(e.pushCh) >= defaultWriteChannelSize {
log.Warnf("pushCh size:%+v", len(e.pushCh))
}
case req, ok := <-e.broadcastCh:
if !ok {
return
}
go func(req *pb.BroadcastRequest) {
_, err := e.provider.Broadcast(context.Background(), session.Kind(req.Kind), &packet.Message{
Seq: req.Message.Seq,
Route: req.Message.Route,
Buffer: req.Message.Buffer,
})
if err != nil {
log.Warnf("dispatch broadcast message failed.err:%+v", err)
}
}(req)
}
}
}