-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
84 lines (75 loc) · 2.1 KB
/
connect.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
/**
*
* @author liangjf
* @create on 2020/5/21
* @version 1.0
*/
package connect
import (
"net"
"github.com/liangjfblue/gpusher/common/codec"
"github.com/liangjfblue/gpusher/gateway/common"
"github.com/liangjfblue/gpusher/common/logger/log"
)
//Connection 客户端连接抽象层
type Connection struct {
conn net.Conn
proto string
msgChan chan []byte
}
//NewConnect 创建客户端连接抽象
func NewConnect(conn net.Conn, proto string) *Connection {
return &Connection{
conn: conn,
proto: proto,
msgChan: make(chan []byte),
}
}
//HandleWriteMsg2Connect 客户端推送消息通道监听和写客户端
func (c *Connection) HandleWriteMsg2Connect(appId int, uuid string) {
go func() {
var (
n int
err error
)
for msg := range c.msgChan {
switch c.proto {
case common.TcpProtocol:
//tcp自定义协议
var resp []byte
cc := codec.GetCodec(codec.Default)
resp, err = cc.Encode(
&codec.FrameHeader{
MsgType: codec.GeneralMsg,
StreamID: 1, //TODO 消息序号生成器 redis
},
msg,
)
if err != nil {
log.GetLogger(common.GatewayLog).Error("codec Encode data err:%s", err.Error())
return
}
n, err = c.conn.Write(resp)
case common.WsProtocol:
n, err = c.conn.Write(msg)
default:
log.GetLogger(common.GatewayLog).Error("not support proto type")
}
log.GetLogger(common.GatewayLog).Debug("appId:%d, uuid:%s, write msg n:%d", appId, uuid, n)
if err != nil {
log.GetLogger(common.GatewayLog).Error("appId:%d, uuid:%s, write msg err:%s", appId, uuid, err.Error())
}
}
log.GetLogger(common.GatewayLog).Debug("appId:%d, uuid:%s, conn goroutine closed", appId, uuid)
}()
}
//WriteMsg2Connect 对外暴露, 用于推送消息到chan的转换
func (c *Connection) WriteMsg2Connect(appId int, uuid string, msg []byte) {
select {
case c.msgChan <- msg:
log.GetLogger(common.GatewayLog).Debug("appId:%d,uuid:%s write msg:%s", appId, uuid, string(msg))
default:
c.conn.Close()
log.GetLogger(common.GatewayLog).Error("appId:%d,uuid:%s write msg:%s; close conn", appId, uuid, string(msg))
}
}