-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
142 lines (117 loc) · 2.74 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
package qim
import (
"context"
"net"
"time"
)
const (
DefaultReadwait = time.Minute * 3
DefaultWritewait = time.Second * 10
DefaultLoginwait = time.Second * 10
DefaultHeartbeat = time.Second * 55
)
const (
DefaultMessageReadPool = 5000
DefaultConnectionPool = 5000
)
// 基础服务的抽象接口
type Service interface {
ServiceID() string
ServiceName() string
GetMeta() map[string]string
}
// 服务注册的抽象接口
type ServiceRegistration interface {
Service
PublicAddress() string
PublicPort() uint16
DialURL() string
GetTags() []string
GetProtocol() string
GetNamespace() string
String() string
}
// 适用于不同底层协议(tcp、websocket)的通用服务端接口
type Server interface {
ServiceRegistration
SetAcceptor(acceptor Acceptor)
SetMessageListener(messageListener MessageListener)
SetStateListener(stateListener StateListener)
SetReadwait(readwait time.Duration)
SetChannelMap(channelMap ChannelMap)
// Start 用于在内部实现网络端口的监听和接收连接,
// 并完成一个Channel的初始化过程。
Start() error
// Push消息到指定的Channel中,
Push(channelID string, payload []byte) error
// 服务下线,关闭连接
Shutdown(ctx context.Context) error
}
// 连接接收器
type Acceptor interface {
// 返回有个握手完成的Channel对象或者一个error。
// 业务层需要处理不同协议和网络环境下的连接握手协议。
Accept(conn Conn, timeout time.Duration) (string, Meta, error)
}
// 监听消息
type MessageListener interface {
// 收到消息回调
Receive(agent Agent, payload []byte)
}
// 状态监听器
type StateListener interface {
// 连接断开回调
Disconnect(channelID string) error
}
type Meta map[string]string
type Agent interface {
ID() string
Push(payload []byte) error
GetMeta() Meta
}
type Conn interface {
net.Conn
ReadFrame() (Frame, error)
WriteFrame(opcode OpCode, payload []byte) error
Flush() error
}
type Channel interface {
Conn
Agent
Close() error
Readloop(lst MessageListener) error
SetWritewait(timeout time.Duration)
SetReadwait(timeout time.Duration)
}
type Client interface {
Service
Connect(addr string) error
SetDialer(dialer Dialer)
Send(payload []byte) error
Read() (Frame, error)
Close()
}
type Dialer interface {
DialAndHandshake(ctx DialerContext) (net.Conn, error)
}
type DialerContext struct {
ID string
Name string
Address string
Timeout time.Duration
}
type OpCode byte
const (
OpContinuation OpCode = 0x0
OpText OpCode = 0x1
OpBinary OpCode = 0x2
OpClose OpCode = 0x8
OpPing OpCode = 0x9
OpPong OpCode = 0xa
)
type Frame interface {
SetOpCode(opcode OpCode)
GetOpCode() OpCode
SetPayload(payload []byte)
GetPayload() []byte
}