5
5
"fmt"
6
6
"net"
7
7
"net/url"
8
+ "sync"
8
9
"time"
9
10
10
11
"github.com/aceld/zinx/zdecoder"
@@ -26,6 +27,8 @@ type Client struct {
26
27
version string
27
28
// Connection instance 连接实例
28
29
conn ziface.IConnection
30
+ // Connection instance 连接实例的锁,保证可见性
31
+ connMux sync.Mutex
29
32
// Hook function called on connection start 该client的连接创建时Hook函数
30
33
onConnStart func (conn ziface.IConnection )
31
34
// Hook function called on connection stop 该client的连接断开时的Hook函数
@@ -121,6 +124,7 @@ func (c *Client) Restart() {
121
124
}
122
125
123
126
// Create a raw socket and get net.Conn (创建原始Socket,得到net.Conn)
127
+ var connect ziface.IConnection
124
128
switch c .version {
125
129
case "websocket" :
126
130
wsAddr := fmt .Sprintf ("ws://%s:%d" , c .Ip , c .Port )
@@ -137,7 +141,7 @@ func (c *Client) Restart() {
137
141
return
138
142
}
139
143
// Create Connection object
140
- c . conn = newWsClientConn (c , wsConn )
144
+ connect = newWsClientConn (c , wsConn )
141
145
142
146
default :
143
147
var conn net.Conn
@@ -166,19 +170,19 @@ func (c *Client) Restart() {
166
170
}
167
171
}
168
172
// Create Connection object
169
- c . conn = newClientConn (c , conn )
173
+ connect = newClientConn (c , conn )
170
174
}
171
175
172
- zlog .Ins ().InfoF ("[START] Zinx Client LocalAddr: %s, RemoteAddr: %s\n " , c . conn . LocalAddr (), c . conn .RemoteAddr ())
176
+ zlog .Ins ().InfoF ("[START] Zinx Client LocalAddr: %s, RemoteAddr: %s\n " , connect . LocalAddr (), connect .RemoteAddr ())
173
177
// HeartBeat detection
174
178
if c .hc != nil {
175
179
// Bind connection and heartbeat detector after connection is successfully established
176
180
// (创建连接成功,绑定连接与心跳检测器)
177
- c .hc .BindConn (c . conn )
181
+ c .hc .BindConn (connect )
178
182
}
179
183
180
184
// Start connection
181
- go c . conn .Start ()
185
+ go connect .Start ()
182
186
183
187
select {
184
188
case <- c .exitChan :
@@ -237,8 +241,11 @@ func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *ziface
237
241
}
238
242
239
243
func (c * Client ) Stop () {
240
- zlog .Ins ().InfoF ("[STOP] Zinx Client LocalAddr: %s, RemoteAddr: %s\n " , c .conn .LocalAddr (), c .conn .RemoteAddr ())
241
- c .conn .Stop ()
244
+ con := c .Conn ()
245
+ if con != nil {
246
+ zlog .Ins ().InfoF ("[STOP] Zinx Client LocalAddr: %s, RemoteAddr: %s\n " , con .LocalAddr (), con .RemoteAddr ())
247
+ con .Stop ()
248
+ }
242
249
c .exitChan <- struct {}{}
243
250
close (c .exitChan )
244
251
close (c .ErrChan )
@@ -249,9 +256,17 @@ func (c *Client) AddRouter(msgID uint32, router ziface.IRouter) {
249
256
}
250
257
251
258
func (c * Client ) Conn () ziface.IConnection {
259
+ c .connMux .Lock ()
260
+ defer c .connMux .Unlock ()
252
261
return c .conn
253
262
}
254
263
264
+ func (c * Client ) setConn (con ziface.IConnection ) {
265
+ c .connMux .Lock ()
266
+ defer c .connMux .Unlock ()
267
+ c .conn = con
268
+ }
269
+
255
270
func (c * Client ) SetOnConnStart (hookFunc func (ziface.IConnection )) {
256
271
c .onConnStart = hookFunc
257
272
}
0 commit comments