@@ -65,8 +65,8 @@ type ProxyConn struct {
6565 established bool
6666 sendch * common.Channel // *ProxyFrame
6767 recvch * common.Channel // *ProxyFrame
68- actived int
69- pinged int
68+ actived int32
69+ pinged int32
7070 id string
7171 needclose bool
7272}
@@ -213,7 +213,7 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms
213213 }
214214
215215 msglen := binary .LittleEndian .Uint32 (bs )
216- if msglen > uint32 (maxmsgsize )+ MAX_PROTO_PACK_SIZE || msglen < = 0 {
216+ if msglen > uint32 (maxmsgsize )+ MAX_PROTO_PACK_SIZE || msglen = = 0 {
217217 loggo .Error ("recvFrom len fail: %s %d" , conn .Info (), msglen )
218218 return errors .New ("msg len fail " + strconv .Itoa (int (msglen )))
219219 }
@@ -233,11 +233,6 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms
233233 return err
234234 }
235235
236- if loggo .IsDebug () {
237- loggo .Debug ("recvFrom start Write %s" , conn .Info ())
238- }
239- recvch .Write (f )
240-
241236 if f .Type != FRAME_TYPE_PING && f .Type != FRAME_TYPE_PONG && loggo .IsDebug () {
242237 loggo .Debug ("recvFrom %s %s" , conn .Info (), f .Type .String ())
243238 if f .Type == FRAME_TYPE_DATA {
@@ -248,6 +243,11 @@ func recvFrom(wg *thread.Group, recvch *common.Channel, conn network.Conn, maxms
248243 }
249244 }
250245
246+ if loggo .IsDebug () {
247+ loggo .Debug ("recvFrom start Write %s" , conn .Info ())
248+ }
249+ recvch .Write (f )
250+
251251 atomic .AddInt32 (& gState .MainRecvNum , 1 )
252252 atomic .AddInt64 (& gState .MainRecvSize , int64 (msglen )+ 4 )
253253 }
@@ -266,14 +266,14 @@ func sendTo(wg *thread.Group, sendch *common.Channel, conn network.Conn, compres
266266
267267 for ! wg .IsExit () {
268268 var f * ProxyFrame
269- if * pingflag > 0 {
270- * pingflag = 0
269+ if atomic . LoadInt32 ( pingflag ) > 0 {
270+ atomic . StoreInt32 ( pingflag , 0 )
271271 f = & ProxyFrame {}
272272 f .Type = FRAME_TYPE_PING
273273 f .PingFrame = & PingFrame {}
274274 f .PingFrame .Time = time .Now ().UnixNano ()
275- } else if * pongflag > 0 {
276- * pongflag = 0
275+ } else if atomic . LoadInt32 ( pongflag ) > 0 {
276+ atomic . StoreInt32 ( pongflag , 0 )
277277 f = & ProxyFrame {}
278278 f .Type = FRAME_TYPE_PONG
279279 f .PongFrame = & PongFrame {}
@@ -304,7 +304,7 @@ func sendTo(wg *thread.Group, sendch *common.Channel, conn network.Conn, compres
304304 }
305305
306306 msglen := uint32 (len (mb ))
307- if msglen > uint32 (maxmsgsize )+ MAX_PROTO_PACK_SIZE || msglen < = 0 {
307+ if msglen > uint32 (maxmsgsize )+ MAX_PROTO_PACK_SIZE || msglen = = 0 {
308308 loggo .Error ("sendTo len fail: %s %d" , conn .Info (), msglen )
309309 return errors .New ("msg len fail " + strconv .Itoa (int (msglen )))
310310 }
@@ -501,14 +501,14 @@ func checkPingActive(wg *thread.Group, sendch *common.Channel, recvch *common.Ch
501501 // 2. 定时触发 Ping 逻辑
502502 case <- pingTicker .C :
503503 // 检查心跳超时逻辑
504- if proxyconn .pinged > pingintertimeout {
504+ if atomic . LoadInt32 ( & proxyconn .pinged ) > int32 ( pingintertimeout ) {
505505 loggo .Info ("checkPingActive ping pong timeout %s" , proxyconn .conn .Info ())
506506 return errors .New ("ping pong timeout" )
507507 }
508508
509509 // 发送心跳逻辑
510510 atomic .AddInt32 (pingflag , 1 )
511- proxyconn .pinged ++
511+ atomic . AddInt32 ( & proxyconn .pinged , 1 )
512512 if showping {
513513 loggo .Info ("ping %s" , proxyconn .conn .Info ())
514514 }
@@ -556,7 +556,7 @@ func processPing(f *ProxyFrame, sendch *common.Channel, proxyconn *ProxyConn, po
556556
557557func processPong (f * ProxyFrame , sendch * common.Channel , proxyconn * ProxyConn , showping bool ) {
558558 elapse := time .Duration (time .Now ().UnixNano () - f .PongFrame .Time )
559- proxyconn .pinged = 0
559+ atomic . StoreInt32 ( & proxyconn .pinged , 0 )
560560 if showping {
561561 loggo .Info ("pong %s %s" , proxyconn .conn .Info (), elapse .String ())
562562 }
@@ -597,11 +597,11 @@ func checkSonnyActive(wg *thread.Group, proxyconn *ProxyConn, estimeout int, tim
597597
598598 // 2. 定时触发 Ping 逻辑
599599 case <- activedTicker .C :
600- if proxyconn .actived == 0 {
600+ if atomic . LoadInt32 ( & proxyconn .actived ) == 0 {
601601 loggo .Error ("checkSonnyActive timeout %s" , proxyconn .conn .Info ())
602602 return errors .New ("conn timeout" )
603603 }
604- proxyconn .actived = 0
604+ atomic . StoreInt32 ( & proxyconn .actived , 0 )
605605 }
606606 }
607607
@@ -633,7 +633,7 @@ func copySonnyRecv(wg *thread.Group, recvch *common.Channel, proxyConn *ProxyCon
633633 }
634634 }
635635 f .DataFrame .Id = proxyConn .id
636- proxyConn .actived ++
636+ atomic . AddInt32 ( & proxyConn .actived , 1 )
637637
638638 father .sendch .Write (f )
639639
0 commit comments