@@ -546,18 +546,25 @@ func (s *UDPSession) notifyWriteEvent() {
546
546
}
547
547
548
548
func (s * UDPSession ) kcpInput (data []byte ) {
549
+ kcpInErrors := uint64 (0 )
550
+ fecErrs := uint64 (0 )
551
+ fecRecovered := uint64 (0 )
552
+
549
553
if s .fec != nil {
550
554
f := s .fec .decode (data )
555
+ s .mu .Lock ()
556
+ if f .flag == typeData {
557
+ if ret := s .kcp .Input (data [fecHeaderSizePlus2 :], true ); ret != 0 {
558
+ atomic .AddUint64 (& DefaultSnmp .KCPInErrors , 1 )
559
+ }
560
+ }
561
+
551
562
if f .flag == typeData || f .flag == typeFEC {
552
563
if f .flag == typeFEC {
553
564
atomic .AddUint64 (& DefaultSnmp .FECSegs , 1 )
554
565
}
555
566
556
567
if recovers := s .fec .input (f ); recovers != nil {
557
- s .mu .Lock ()
558
- kcpInErrors := uint64 (0 )
559
- fecErrs := uint64 (0 )
560
- fecRecovered := uint64 (0 )
561
568
for _ , r := range recovers {
562
569
if len (r ) >= 2 { // must be larger than 2bytes
563
570
sz := binary .LittleEndian .Uint16 (r )
@@ -574,38 +581,43 @@ func (s *UDPSession) kcpInput(data []byte) {
574
581
fecErrs ++
575
582
}
576
583
}
577
- s .mu .Unlock ()
578
- atomic .AddUint64 (& DefaultSnmp .KCPInErrors , kcpInErrors )
579
- atomic .AddUint64 (& DefaultSnmp .FECErrs , fecErrs )
580
- atomic .AddUint64 (& DefaultSnmp .FECRecovered , fecRecovered )
581
584
}
582
585
}
583
- if f .flag == typeData {
584
- s .mu .Lock ()
585
- if ret := s .kcp .Input (data [fecHeaderSizePlus2 :], true ); ret != 0 {
586
- atomic .AddUint64 (& DefaultSnmp .KCPInErrors , 1 )
587
- }
588
- s .mu .Unlock ()
586
+
587
+ // notify reader
588
+ if n := s .kcp .PeekSize (); n > 0 {
589
+ s .notifyReadEvent ()
590
+ }
591
+ if s .ackNoDelay {
592
+ s .kcp .flush ()
589
593
}
594
+ s .mu .Unlock ()
590
595
} else {
591
596
s .mu .Lock ()
592
597
if ret := s .kcp .Input (data , true ); ret != 0 {
593
598
atomic .AddUint64 (& DefaultSnmp .KCPInErrors , 1 )
594
599
}
600
+ // notify reader
601
+ if n := s .kcp .PeekSize (); n > 0 {
602
+ s .notifyReadEvent ()
603
+ }
604
+ if s .ackNoDelay {
605
+ s .kcp .flush ()
606
+ }
595
607
s .mu .Unlock ()
596
608
}
597
609
598
- // notify reader
599
- s .mu .Lock ()
600
- if n := s .kcp .PeekSize (); n > 0 {
601
- s .notifyReadEvent ()
602
- }
603
- if s .ackNoDelay {
604
- s .kcp .flush ()
605
- }
606
- s .mu .Unlock ()
607
610
atomic .AddUint64 (& DefaultSnmp .InSegs , 1 )
608
611
atomic .AddUint64 (& DefaultSnmp .InBytes , uint64 (len (data )))
612
+ if kcpInErrors > 0 {
613
+ atomic .AddUint64 (& DefaultSnmp .KCPInErrors , kcpInErrors )
614
+ }
615
+ if fecErrs > 0 {
616
+ atomic .AddUint64 (& DefaultSnmp .FECErrs , fecErrs )
617
+ }
618
+ if fecRecovered > 0 {
619
+ atomic .AddUint64 (& DefaultSnmp .FECRecovered , fecRecovered )
620
+ }
609
621
}
610
622
611
623
func (s * UDPSession ) receiver (ch chan []byte ) {
0 commit comments