github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/kcp-go/readloop.go (about)

     1  package kcp
     2  
     3  import (
     4  	"sync/atomic"
     5  
     6  	"github.com/pkg/errors"
     7  )
     8  
     9  func (s *UDPSession) defaultReadLoop() {
    10  	buf := make([]byte, mtuLimit)
    11  	var src string
    12  	for {
    13  		if n, addr, err := s.conn.ReadFrom(buf); err == nil {
    14  			// make sure the packet is from the same source
    15  			if src == "" { // set source address
    16  				src = addr.String()
    17  			} else if addr.String() != src {
    18  				atomic.AddUint64(&DefaultSnmp.InErrs, 1)
    19  				continue
    20  			}
    21  
    22  			if n >= s.headerSize()+IKCP_OVERHEAD {
    23  				s.packetInput(buf[:n])
    24  			} else {
    25  				atomic.AddUint64(&DefaultSnmp.InErrs, 1)
    26  			}
    27  		} else {
    28  			s.notifyReadError(errors.WithStack(err))
    29  			return
    30  		}
    31  	}
    32  }
    33  
    34  func (l *Listener) defaultMonitor() {
    35  	buf := make([]byte, mtuLimit)
    36  	for {
    37  		if n, from, err := l.conn.ReadFrom(buf); err == nil {
    38  			l.packetInput(buf[:n], from)
    39  		} else {
    40  			l.notifyReadError(errors.WithStack(err))
    41  			return
    42  		}
    43  	}
    44  }