-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
115 lines (99 loc) · 1.94 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
package uim
import (
"context"
"net"
"time"
)
const (
DefaultReadWait = time.Minute * 3
DefaultWriteWait = time.Second * 10
DefaultLoginWait = time.Second * 10
DefaultHeartbeat = time.Second * 55
)
type OpCode byte
const (
OpContinuation OpCode = 0x0
OpText OpCode = 0x1
OpBinary OpCode = 0x2
OpClose OpCode = 0x8
OpPing OpCode = 0x9
OpPong OpCode = 0xa
)
type Service interface {
ServiceID() string
ServiceName() string
GetMeta() map[string]string
}
type ServiceRegistration interface {
Service
PublicAddress() string
PublicPort() int
DialURL() string
GetTags() []string
GetProtocol() string
GetNamespace() string
String() string
}
type Server interface {
ServiceRegistration
SetAcceptor(Acceptor)
SetMessageListener(MessageListener)
SetStateListener(StateListener)
SetReadWait(time.Duration)
SetChannelMap(ChannelMap)
Start() error
Push(string, []byte) error
Shutdown(context.Context) error
}
type Acceptor interface {
Accept(Conn, time.Duration) (string, error)
}
type StateListener interface {
Disconnect(string) error
}
type MessageListener interface {
Receive(Agent, []byte)
}
type Frame interface {
SetOpCode(OpCode)
GetOpCode() OpCode
SetPayload([]byte)
GetPayload() []byte
}
// Conn Connection
type Conn interface {
net.Conn
ReadFrame() (Frame, error)
WriteFrame(OpCode, []byte) error
Flush() error
}
type Channel interface {
Conn
Agent
Close() error // 重写net.Conn中的方法
Readloop(lst MessageListener) error
SetWriteWait(time.Duration)
SetReadWait(time.Duration)
}
type Agent interface {
ID() string
Push([]byte) error
}
// Client is interface of client side.
type Client interface {
Service
Connect(string) error
SetDialer(Dialer)
Send([]byte) error
Read() (Frame, error)
Close()
}
type Dialer interface {
DialAndHandshake(DialerContext) (net.Conn, error)
}
type DialerContext struct {
Id string
Name string
Address string
Timeout time.Duration
}