-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_connection.go
292 lines (247 loc) · 7.1 KB
/
tcp_connection.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
// @Title
// @Description
// @Author Wangwengang 2021/8/17 下午4:45
// @Update Wangwengang 2021/8/17 下午4:45
package impl
import (
"context"
"errors"
"fmt"
"io"
"net"
"sync"
"github.com/gorilla/websocket"
"github.com/wwengg/arsenal/anet"
"github.com/wwengg/arsenal/config"
)
type TcpConnection struct {
//当前Conn属于哪个Server
TCPServer anet.Server
//当前连接的socket TCP套接字
Conn *net.TCPConn
//当前连接的ID 也可以称作为SessionID,ID全局唯一
ConnID uint64
//消息管理MsgID和对应处理方法的消息管理模块
MsgHandler anet.MsgHandle
//告知该链接已经退出/停止的channel
ctx context.Context
cancel context.CancelFunc
//无缓冲管道,用于读、写两个goroutine之间的消息通信
msgChan chan []byte
//有缓冲管道,用于读、写两个goroutine之间的消息通信
msgBuffChan chan []byte
sync.RWMutex
//链接属性
property map[string]interface{}
////保护当前property的锁
propertyLock sync.Mutex
//当前连接的关闭状态
isClosed bool
}
//NewConnection 创建连接的方法
func NewConnection(server anet.Server, conn *net.TCPConn, connID uint64, msgHandler anet.MsgHandle) anet.Connection {
//初始化Conn属性
c := &TcpConnection{
TCPServer: server,
Conn: conn,
ConnID: connID,
isClosed: false,
MsgHandler: msgHandler,
msgChan: make(chan []byte),
msgBuffChan: make(chan []byte, config.ConfigHub.TcpConfig.MaxMsgChanLen),
property: nil,
}
//将新创建的Conn添加到链接管理中
c.TCPServer.GetConnMgr().Add(c)
return c
}
//StartWriter 写消息Goroutine, 用户将数据发送给客户端
func (c *TcpConnection) StartWriter() {
fmt.Println("[Writer Goroutine is running]")
defer fmt.Println(c.RemoteAddr().String(), "[conn Writer exit!]")
for {
select {
case data := <-c.msgChan:
//有数据要写给客户端
if _, err := c.Conn.Write(data); err != nil {
fmt.Println("Send Data error:, ", err, " Conn Writer exit")
return
}
//fmt.Printf("Send data succ! data = %+v\n", data)
case data, ok := <-c.msgBuffChan:
if ok {
//有数据要写给客户端
if _, err := c.Conn.Write(data); err != nil {
fmt.Println("Send Buff Data error:, ", err, " Conn Writer exit")
return
}
} else {
fmt.Println("msgBuffChan is Closed")
break
}
case <-c.ctx.Done():
return
}
}
}
//StartReader 读消息Goroutine,用于从客户端中读取数据
func (c *TcpConnection) StartReader() {
fmt.Println("[Reader Goroutine is running]")
defer fmt.Println(c.RemoteAddr().String(), "[conn Reader exit!]")
defer c.Stop()
// 创建拆包解包的对象
for {
select {
case <-c.ctx.Done():
return
default:
//读取客户端的Msg head
headData := make([]byte, c.TCPServer.Packet().GetHeadLen())
if _, err := io.ReadFull(c.Conn, headData); err != nil {
fmt.Println("read msg head error ", err)
return
}
//fmt.Printf("read headData %+v\n", headData)
//拆包,得到msgID 和 datalen 放在msg中
msg, err := c.TCPServer.Packet().Unpack(headData)
if err != nil {
fmt.Println("unpack error ", err)
return
}
//根据 dataLen 读取 data,放在msg.Data中
var data []byte
if msg.GetDataLen() > 0 {
data = make([]byte, msg.GetDataLen())
if _, err := io.ReadFull(c.Conn, data); err != nil {
fmt.Println("read msg data error ", err)
return
}
}
msg.SetData(data)
//得到当前客户端请求的Request数据
req := Request{
conn: c,
msg: msg,
}
if config.ConfigHub.TcpConfig.WorkerPoolSize > 0 {
//已经启动工作池机制,将消息交给Worker处理
c.MsgHandler.SendMsgToTaskQueue(&req)
} else {
//从绑定好的消息和对应的处理方法中执行对应的Handle方法
go c.MsgHandler.DoMsgHandler(&req)
}
}
}
}
//Start 启动连接,让当前连接开始工作
func (c *TcpConnection) Start() {
c.ctx, c.cancel = context.WithCancel(context.Background())
//1 开启用户从客户端读取数据流程的Goroutine
go c.StartReader()
//2 开启用于写回客户端数据流程的Goroutine
go c.StartWriter()
//按照用户传递进来的创建连接时需要处理的业务,执行钩子方法
c.TCPServer.CallOnConnStart(c)
}
//Stop 停止连接,结束当前连接状态M
func (c *TcpConnection) Stop() {
//如果用户注册了该链接的关闭回调业务,那么在此刻应该显示调用
c.TCPServer.CallOnConnStop(c)
c.Lock()
defer c.Unlock()
//如果当前链接已经关闭
if c.isClosed == true {
return
}
fmt.Println("Conn Stop()...ConnID = ", c.ConnID)
// 关闭socket链接
c.Conn.Close()
//关闭Writer
c.cancel()
//将链接从连接管理器中删除
c.TCPServer.GetConnMgr().Remove(c)
//关闭该链接全部管道
close(c.msgBuffChan)
//设置标志位
c.isClosed = true
}
//GetTCPConnection 从当前连接获取原始的socket TCPConn
func (c *TcpConnection) GetTcpConnection() *net.TCPConn {
return c.Conn
}
//GetTCPConnection 从当前连接获取原始的socket TCPConn
func (c *TcpConnection) GetWsConnection() *websocket.Conn {
return nil
}
//GetConnID 获取当前连接ID
func (c *TcpConnection) GetConnID() uint64 {
return c.ConnID
}
//RemoteAddr 获取远程客户端地址信息
func (c *TcpConnection) RemoteAddr() net.Addr {
return c.Conn.RemoteAddr()
}
//SendMsg 直接将Message数据发送数据给远程的TCP客户端
func (c *TcpConnection) SendMsg(msgID uint32, data []byte) error {
c.RLock()
defer c.RUnlock()
if c.isClosed == true {
return errors.New("connection closed when send msg")
}
//将data封包,并且发送
dp := c.TCPServer.Packet()
msg, err := dp.Pack(NewMsgPackage(msgID, data))
if err != nil {
fmt.Println("Pack error msg ID = ", msgID)
return errors.New("Pack error msg ")
}
//写回客户端
c.msgChan <- msg
return nil
}
//SendBuffMsg 发生BuffMsg
func (c *TcpConnection) SendBuffMsg(msgID uint32, data []byte) error {
c.RLock()
defer c.RUnlock()
if c.isClosed == true {
return errors.New("Connection closed when send buff msg")
}
//将data封包,并且发送
dp := c.TCPServer.Packet()
msg, err := dp.Pack(NewMsgPackage(msgID, data))
if err != nil {
fmt.Println("Pack error msg ID = ", msgID)
return errors.New("Pack error msg ")
}
//写回客户端
c.msgBuffChan <- msg
return nil
}
//SetProperty 设置链接属性
func (c *TcpConnection) SetProperty(key string, value interface{}) {
c.propertyLock.Lock()
defer c.propertyLock.Unlock()
if c.property == nil {
c.property = make(map[string]interface{})
}
c.property[key] = value
}
//GetProperty 获取链接属性
func (c *TcpConnection) GetProperty(key string) (interface{}, error) {
c.propertyLock.Lock()
defer c.propertyLock.Unlock()
if value, ok := c.property[key]; ok {
return value, nil
}
return nil, errors.New("no property found")
}
//RemoveProperty 移除链接属性
func (c *TcpConnection) RemoveProperty(key string) {
c.propertyLock.Lock()
defer c.propertyLock.Unlock()
delete(c.property, key)
}
//返回ctx,用于用户自定义的go程获取连接退出状态
func (c *TcpConnection) Context() context.Context {
return c.ctx
}