/
appbase_net.go
195 lines (155 loc) · 4.07 KB
/
appbase_net.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
package app
import (
"github.com/toophy/login/help"
"net"
)
func (this *AppBase) AddConn(c *ClientConn) {
this.Conns[this.ConnLast] = c
this.ConnLast++
}
func (this *AppBase) DelConn(id int) {
if _, ok := this.Conns[id]; ok {
if len(this.Conns[id].Name) > 0 {
delete(this.RemoteSvr, this.Conns[id].Name)
}
delete(this.Conns, id)
}
}
func (this *AppBase) GetConnById(id int) *ClientConn {
if v, ok := this.Conns[id]; ok {
return v
}
return nil
}
func (this *AppBase) GetConnByName(name string) *ClientConn {
if v, ok := this.RemoteSvr[name]; ok {
return v
}
return nil
}
func (this *AppBase) RegMsgFunc(id int, f MsgFunc) {
this.MsgProc[id] = f
if id > this.MsgProcCount {
this.MsgProcCount = id
}
}
func (this *AppBase) Listen(name, net_type, address string, onRet ConnRetFunc) {
if len(address) == 0 || len(address) == 0 || len(net_type) == 0 {
onRet("listen failed", name, 0, "listen failed")
return
}
// 打开本地TCP侦听
serverAddr, err := net.ResolveTCPAddr(net_type, address)
if err != nil {
onRet("listen failed", name, 0, "Listen Start : port failed: '"+address+"' "+err.Error())
return
}
listener, err := net.ListenTCP(net_type, serverAddr)
if err != nil {
onRet("listen failed", name, 0, "TcpSerer ListenTCP: "+err.Error())
return
}
ln := new(ListenConn)
ln.InitListen(name, net_type, address, listener)
this.Listener[name] = ln
onRet("listen ok", name, 0, "")
for {
conn, err := listener.AcceptTCP()
if err != nil {
if !onRet("accept failed", name, 0, "TcpSerer Accept: "+err.Error()) {
break
}
continue
}
c := new(ClientConn)
c.InitClient(this.ConnLast, conn)
this.AddConn(c)
onRet("accept ok", "", c.Id, "")
go this.ConnProcEx(c, onRet)
}
}
func (this *AppBase) Connect(name, net_type, address string, onRet ConnRetFunc) {
if len(address) == 0 || len(net_type) == 0 || len(name) == 0 {
onRet("connect failed", name, 0, "listen failed")
return
}
// 打开本地TCP侦听
remoteAddr, err := net.ResolveTCPAddr(net_type, address)
if err != nil {
onRet("connect failed", name, 0, "Connect Start : port failed: '"+address+"' "+err.Error())
return
}
conn, err := net.DialTCP(net_type, nil, remoteAddr)
if err != nil {
onRet("connect failed", name, 0, "Connect dialtcp failed: '"+address+"' "+err.Error())
} else {
c := new(ClientConn)
c.InitClient(this.ConnLast, conn)
c.Name = name
this.RemoteSvr[name] = c
this.AddConn(c)
onRet("connect ok", name, c.Id, "")
go this.ConnProcEx(c, onRet)
}
}
func (this *AppBase) ConnProc(c *ClientConn, onRet ConnRetFunc) {
for {
c.Stream.Seek(0)
err := c.Msg.ReadData(c.Conn)
if err == nil {
c.Stream.Seek(MaxHeader)
msg_code := c.Stream.ReadU2()
if msg_code >= 0 && msg_code <= this.MsgProcCount && this.MsgProc[msg_code] != nil {
this.MsgProc[msg_code](c)
}
} else {
onRet("read failed", c.Name, c.Id, err.Error())
break
}
}
this._closeConn(c, onRet)
}
func (this *AppBase) ConnProcEx(c *ClientConn, onRet ConnRetFunc) {
// 捕捉异常
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case error:
GetApp().LogWarn("ConnProcEx:" + r.(error).Error())
case string:
GetApp().LogWarn("ConnProcEx:" + r.(string))
}
}
this._closeConn(c, onRet)
}()
for {
data, ret := ReadConnData(c.Conn)
if ret == nil {
// 校验 data.Token, 拆包, 解密, 分别处理消息
var stream help.Stream
stream.Init(data.Data)
for i := uint32(0); i < data.Count; i++ {
msg_len_id := stream.ReadUint32()
// msg_len := int(msg_len_id >>16)
msg_id := int(msg_len_id & 0xFFFF)
if msg_id >= 0 && msg_id <= this.MsgProcCount && this.MsgProc[msg_id] != nil {
this.MsgProc[msg_id](c)
}
}
} else {
onRet("read failed", c.Name, c.Id, ret.Error())
break
}
}
this._closeConn(c, onRet)
}
func (this *AppBase) _closeConn(c *ClientConn, onRet ConnRetFunc) {
onRet("pre close", c.Name, c.Id, "")
err := c.Conn.Close()
if err != nil {
onRet("close failed", c.Name, c.Id, err.Error())
} else {
onRet("close ok", c.Name, c.Id, "")
}
GetApp().DelConn(c.Id)
}