-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathasync_server.go
191 lines (163 loc) · 3.98 KB
/
async_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
package tcp
import (
"errors"
"log"
"net"
"os"
"time"
)
const (
//tcp conn max packet size
defaultMaxPacketSize = 1024 << 10 //1MB
readChanSize = 100
writeChanSize = 100
)
var (
logger *log.Logger
)
func init() {
logger = log.New(os.Stdout, "", log.Lshortfile)
}
//AsyncTCPServer 结构定义
type AsyncTCPServer struct {
//TCP address to listen on
tcpAddr string
//the listener
listener *net.TCPListener
//callback is an interface
//it's used to process the connect establish, close and data receive
callback CallBack
protocol Protocol
//if srv is shutdown, close the channel used to inform all session to exit.
exitChan chan struct{}
readDeadline, writeDeadline time.Duration
bucket *TCPConnBucket
}
//NewAsyncTCPServer 返回一个AsyncTCPServer实例
func NewAsyncTCPServer(tcpAddr string, callback CallBack, protocol Protocol) *AsyncTCPServer {
return &AsyncTCPServer{
tcpAddr: tcpAddr,
callback: callback,
protocol: protocol,
bucket: NewTCPConnBucket(),
exitChan: make(chan struct{}),
}
}
//ListenAndServe 使用AsyncTCPServer的tcpAddr创建TCPListner并调用Server()方法开启监听
func (srv *AsyncTCPServer) ListenAndServe() error {
tcpAddr, err := net.ResolveTCPAddr("tcp4", srv.tcpAddr)
if err != nil {
return err
}
ln, err := net.ListenTCP("tcp4", tcpAddr)
if err != nil {
return err
}
go srv.Serve(ln)
return nil
}
//Serve 使用指定的TCPListener开启监听
func (srv *AsyncTCPServer) Serve(l *net.TCPListener) error {
srv.listener = l
defer func() {
if r := recover(); r != nil {
log.Println("Serve error", r)
}
srv.listener.Close()
}()
//清理无效连接
go func() {
for {
srv.removeClosedTCPConn()
time.Sleep(time.Millisecond * 10)
}
}()
var tempDelay time.Duration
for {
select {
case <-srv.exitChan:
return errors.New("TCPServer Closed")
default:
}
conn, err := srv.listener.AcceptTCP()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
time.Sleep(tempDelay)
continue
}
log.Println("ln error:", err.Error())
return err
}
tempDelay = 0
tcpConn := srv.newTCPConn(conn, srv.callback, srv.protocol)
tcpConn.setReadDeadline(srv.readDeadline)
tcpConn.setWriteDeadline(srv.writeDeadline)
srv.bucket.Put(tcpConn.GetRemoteAddr().String(), tcpConn)
}
}
func (srv *AsyncTCPServer) newTCPConn(conn *net.TCPConn, callback CallBack, protocol Protocol) *TCPConn {
if callback == nil {
// if the handler is nil, use srv handler
callback = srv.callback
}
if protocol == nil {
protocol = srv.protocol
}
c := NewTCPConn(conn, callback, protocol)
c.Serve()
return c
}
//Connect 使用指定的callback和protocol连接其他TCPServer,返回TCPConn
func (srv *AsyncTCPServer) Connect(ip string, callback CallBack, protocol Protocol) (*TCPConn, error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", ip)
if err != nil {
return nil, err
}
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
return nil, err
}
tcpConn := srv.newTCPConn(conn, callback, protocol)
return tcpConn, nil
}
//Close 首先关闭所有连接,然后关闭TCPServer
func (srv *AsyncTCPServer) Close() {
defer srv.listener.Close()
for _, c := range srv.bucket.GetAll() {
if !c.IsClosed() {
c.Close()
}
}
}
func (srv *AsyncTCPServer) removeClosedTCPConn() {
select {
case <-srv.exitChan:
return
default:
srv.bucket.removeClosedTCPConn()
}
}
func (srv *AsyncTCPServer) GetAllTCPConn() []*TCPConn {
conns := []*TCPConn{}
for _, conn := range srv.bucket.GetAll() {
conns = append(conns, conn)
}
return conns
}
func (srv *AsyncTCPServer) GetTCPConn(key string) *TCPConn {
return srv.bucket.Get(key)
}
func (srv *AsyncTCPServer) SetReadDeadline(t time.Duration) {
srv.readDeadline = t
}
func (srv *AsyncTCPServer) SetWriteDeadline(t time.Duration) {
srv.writeDeadline = t
}