@@ -6,42 +6,73 @@ import (
66 "sync/atomic"
77 "time"
88
9+ "github.com/dobyte/due/v2/core/buffer"
910 "github.com/dobyte/due/v2/errors"
1011 "github.com/dobyte/due/v2/log"
1112 "github.com/dobyte/due/v2/network"
1213 "github.com/dobyte/due/v2/packet"
1314 "github.com/dobyte/due/v2/utils/xcall"
1415 "github.com/dobyte/due/v2/utils/xnet"
1516 "github.com/dobyte/due/v2/utils/xtime"
17+ "github.com/xtaci/kcp-go/v5"
1618)
1719
1820type clientConn struct {
1921 rw sync.RWMutex
20- id int64 // 连接ID
21- uid int64 // 用户ID
22- attr * attr // 连接属性
23- conn net. Conn // TCP源连接
24- state int32 // 连接状态
25- client * client // 客户端
26- chWrite chan chWrite // 写入队列
27- done chan struct {} // 写入完成信号
28- close chan struct {} // 关闭信号
29- lastHeartbeatTime int64 // 上次心跳时间
22+ id int64 // 连接ID
23+ uid int64 // 用户ID
24+ attr * attr // 连接属性
25+ conn * kcp. UDPSession // UDP源连接
26+ state atomic. Int32 // 连接状态
27+ client * client // 客户端
28+ chWrite chan chWrite // 写入队列
29+ done chan struct {} // 写入完成信号
30+ close chan struct {} // 关闭信号
31+ lastHeartbeatTime atomic. Int64 // 上次心跳时间
3032}
3133
3234var _ network.Conn = & clientConn {}
3335
34- func newClientConn (client * client , id int64 , conn net. Conn ) network.Conn {
36+ func newClientConn (client * client , id int64 , conn * kcp. UDPSession ) network.Conn {
3537 c := & clientConn {
36- id : id ,
37- attr : & attr {},
38- conn : conn ,
39- state : int32 (network .ConnOpened ),
40- client : client ,
41- chWrite : make (chan chWrite , 4096 ),
42- done : make (chan struct {}),
43- close : make (chan struct {}),
44- lastHeartbeatTime : xtime .Now ().UnixNano (),
38+ id : id ,
39+ attr : & attr {},
40+ conn : conn ,
41+ client : client ,
42+ chWrite : make (chan chWrite , 4096 ),
43+ done : make (chan struct {}),
44+ close : make (chan struct {}),
45+ }
46+
47+ c .state .Store (int32 (network .ConnOpened ))
48+ c .lastHeartbeatTime .Store (xtime .Now ().UnixNano ())
49+
50+ if c .client .opts .mtu > 0 {
51+ conn .SetMtu (c .client .opts .mtu )
52+ }
53+
54+ if len (c .client .opts .noDelay ) == 4 {
55+ conn .SetNoDelay (c .client .opts .noDelay [0 ], c .client .opts .noDelay [1 ], c .client .opts .noDelay [2 ], c .client .opts .noDelay [3 ])
56+ }
57+
58+ if c .client .opts .ackNoDelay {
59+ conn .SetACKNoDelay (c .client .opts .ackNoDelay )
60+ }
61+
62+ if c .client .opts .writeDelay {
63+ conn .SetWriteDelay (c .client .opts .writeDelay )
64+ }
65+
66+ if len (c .client .opts .windowSize ) == 2 {
67+ conn .SetWindowSize (c .client .opts .windowSize [0 ], c .client .opts .windowSize [1 ])
68+ }
69+
70+ if c .client .opts .readBuffer > 0 {
71+ conn .SetReadBuffer (c .client .opts .readBuffer )
72+ }
73+
74+ if c .client .opts .writeBuffer > 0 {
75+ conn .SetWriteBuffer (c .client .opts .writeBuffer )
4576 }
4677
4778 xcall .Go (c .read )
@@ -99,21 +130,26 @@ func (c *clientConn) Send(msg []byte) error {
99130}
100131
101132// Push 发送消息(异步)
102- func (c * clientConn ) Push (msg []byte ) ( err error ) {
103- if err = c .checkState (); err != nil {
104- return
133+ func (c * clientConn ) Push (msg []byte ) error {
134+ if err : = c .checkState (); err != nil {
135+ return err
105136 }
106137
107138 c .rw .RLock ()
139+ defer c .rw .RUnlock ()
140+
141+ if c .conn == nil {
142+ return errors .ErrConnectionClosed
143+ }
144+
108145 c .chWrite <- chWrite {typ : dataPacket , msg : msg }
109- c .rw .RUnlock ()
110146
111- return
147+ return nil
112148}
113149
114150// State 获取连接状态
115151func (c * clientConn ) State () network.ConnState {
116- return network .ConnState (atomic . LoadInt32 ( & c .state ))
152+ return network .ConnState (c .state . Load ( ))
117153}
118154
119155// Close 关闭连接
@@ -181,7 +217,7 @@ func (c *clientConn) RemoteAddr() (net.Addr, error) {
181217
182218// 检测连接状态
183219func (c * clientConn ) checkState () error {
184- switch network . ConnState ( atomic . LoadInt32 ( & c . state ) ) {
220+ switch c . State ( ) {
185221 case network .ConnHanged :
186222 return errors .ErrConnectionHanged
187223 case network .ConnClosed :
@@ -193,46 +229,47 @@ func (c *clientConn) checkState() error {
193229
194230// 优雅关闭
195231func (c * clientConn ) graceClose () error {
196- if ! atomic . CompareAndSwapInt32 ( & c .state , int32 (network .ConnOpened ), int32 (network .ConnHanged )) {
232+ if ! c .state . CompareAndSwap ( int32 (network .ConnOpened ), int32 (network .ConnHanged )) {
197233 return errors .ErrConnectionNotOpened
198234 }
199235
200236 c .rw .RLock ()
237+ if c .conn == nil {
238+ c .rw .RUnlock ()
239+ return errors .ErrConnectionClosed
240+ }
201241 c .chWrite <- chWrite {typ : closeSig }
202242 c .rw .RUnlock ()
203243
204244 <- c .done
205245
206- if ! atomic . CompareAndSwapInt32 ( & c .state , int32 (network .ConnHanged ), int32 (network .ConnClosed )) {
246+ if ! c .state . CompareAndSwap ( int32 (network .ConnHanged ), int32 (network .ConnClosed )) {
207247 return errors .ErrConnectionNotHanged
208248 }
209249
210- c .rw .Lock ()
211- close (c .chWrite )
212- close (c .close )
213- close (c .done )
214- conn := c .conn
215- c .conn = nil
216- c .rw .Unlock ()
217-
218- err := conn .Close ()
219-
220- if c .client .disconnectHandler != nil {
221- c .client .disconnectHandler (c )
222- }
223-
224- return err
250+ return c .doClose ()
225251}
226252
227253// 强制关闭
228254func (c * clientConn ) forceClose () error {
229- if ! atomic . CompareAndSwapInt32 ( & c .state , int32 (network .ConnOpened ), int32 (network .ConnClosed )) {
230- if ! atomic . CompareAndSwapInt32 ( & c .state , int32 (network .ConnHanged ), int32 (network .ConnClosed )) {
255+ if ! c .state . CompareAndSwap ( int32 (network .ConnOpened ), int32 (network .ConnClosed )) {
256+ if ! c .state . CompareAndSwap ( int32 (network .ConnHanged ), int32 (network .ConnClosed )) {
231257 return errors .ErrConnectionClosed
232258 }
233259 }
234260
261+ return c .doClose ()
262+ }
263+
264+ // 执行关闭操作
265+ func (c * clientConn ) doClose () error {
235266 c .rw .Lock ()
267+
268+ if c .conn == nil {
269+ c .rw .Unlock ()
270+ return errors .ErrConnectionClosed
271+ }
272+
236273 close (c .chWrite )
237274 close (c .close )
238275 close (c .done )
@@ -265,7 +302,7 @@ func (c *clientConn) read() {
265302 }
266303
267304 if c .client .opts .heartbeatInterval > 0 {
268- atomic . StoreInt64 ( & c .lastHeartbeatTime , xtime .Now ().UnixNano ())
305+ c .lastHeartbeatTime . Store ( xtime .Now ().UnixNano ())
269306 }
270307
271308 switch c .State () {
@@ -277,6 +314,11 @@ func (c *clientConn) read() {
277314 // ignore
278315 }
279316
317+ // ignore empty packet
318+ if len (msg ) == 0 {
319+ continue
320+ }
321+
280322 isHeartbeat , err := packet .CheckHeartbeat (msg )
281323 if err != nil {
282324 log .Errorf ("check heartbeat message error: %v" , err )
@@ -288,13 +330,8 @@ func (c *clientConn) read() {
288330 continue
289331 }
290332
291- // ignore empty packet
292- if len (msg ) == 0 {
293- continue
294- }
295-
296333 if c .client .receiveHandler != nil {
297- c .client .receiveHandler (c , msg )
334+ c .client .receiveHandler (c , buffer . NewBytes ( msg ) )
298335 }
299336 }
300337 }
@@ -335,9 +372,14 @@ func (c *clientConn) write() {
335372 if _ , err := conn .Write (r .msg ); err != nil {
336373 log .Errorf ("write data message error: %v" , err )
337374 }
338- case <- ticker .C :
339- deadline := xtime .Now ().Add (- 2 * c .client .opts .heartbeatInterval ).UnixNano ()
340- if atomic .LoadInt64 (& c .lastHeartbeatTime ) < deadline {
375+ case t , ok := <- ticker .C :
376+ if ! ok {
377+ return
378+ }
379+
380+ deadline := t .Add (- 2 * c .client .opts .heartbeatInterval ).UnixNano ()
381+
382+ if c .lastHeartbeatTime .Load () < deadline {
341383 log .Debugf ("connection heartbeat timeout" )
342384 _ = c .forceClose ()
343385 return
@@ -361,5 +403,5 @@ func (c *clientConn) write() {
361403
362404// 是否已关闭
363405func (c * clientConn ) isClosed () bool {
364- return network .ConnState (atomic . LoadInt32 ( & c .state )) == network .ConnClosed
406+ return network .ConnState (c .state . Load ( )) == network .ConnClosed
365407}
0 commit comments