Files
kcp-go/readloop_linux.go
T
xtaci 582575ef06 Squashed commit of the following:
commit a8ae293a9ddee042c35e2501f93fa5dfa2425302
Merge: bdaf81e 1a84985
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 22:48:13 2020 +0800

    Merge branch 'master' into autotune

commit bdaf81e88e
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 22:38:31 2020 +0800

    add loc

commit ce66a98f95
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 19:53:42 2020 +0800

    lint

commit ad7383d8a3
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 19:05:35 2020 +0800

    fix typo

commit dc0fc2364c
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:47:14 2020 +0800

    adjust tests

commit ec45b72c39
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:45:17 2020 +0800

    remove obsolete decoder in listener

commit 827aa70e29
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:42:42 2020 +0800

    use hierarchical defensive receiving strategy

commit f5ca6b0e3a
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:34:30 2020 +0800

    optimize condition

commit ff34f7683f
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:18:00 2020 +0800

    fix test

commit a08b274651
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:13:20 2020 +0800

    solve compability issue with non FEC sender

commit ab7596783b
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:02:34 2020 +0800

    implements autotune in fec decoder

commit 260f61b3eb
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 14:34:48 2020 +0800

    hack 16-bit to identify FEC from packet

commit 105bfec567
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 2 21:38:47 2020 +0800

    add autotune
2020-10-09 22:52:11 +08:00

112 lines
2.4 KiB
Go

// +build linux
package kcp
import (
"net"
"os"
"sync/atomic"
"github.com/pkg/errors"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
// the read loop for a client session
func (s *UDPSession) readLoop() {
// default version
if s.xconn == nil {
s.defaultReadLoop()
return
}
// x/net version
var src string
msgs := make([]ipv4.Message, batchSize)
for k := range msgs {
msgs[k].Buffers = [][]byte{make([]byte, mtuLimit)}
}
for {
if count, err := s.xconn.ReadBatch(msgs, 0); err == nil {
for i := 0; i < count; i++ {
msg := &msgs[i]
// make sure the packet is from the same source
if src == "" { // set source address if nil
src = msg.Addr.String()
} else if msg.Addr.String() != src {
atomic.AddUint64(&DefaultSnmp.InErrs, 1)
continue
}
// source and size has validated
s.packetInput(msg.Buffers[0][:msg.N])
}
} else {
// compatibility issue:
// for linux kernel<=2.6.32, support for sendmmsg is not available
// an error of type os.SyscallError will be returned
if operr, ok := err.(*net.OpError); ok {
if se, ok := operr.Err.(*os.SyscallError); ok {
if se.Syscall == "recvmmsg" {
s.defaultReadLoop()
return
}
}
}
s.notifyReadError(errors.WithStack(err))
return
}
}
}
// monitor incoming data for all connections of server
func (l *Listener) monitor() {
var xconn batchConn
if _, ok := l.conn.(*net.UDPConn); ok {
addr, err := net.ResolveUDPAddr("udp", l.conn.LocalAddr().String())
if err == nil {
if addr.IP.To4() != nil {
xconn = ipv4.NewPacketConn(l.conn)
} else {
xconn = ipv6.NewPacketConn(l.conn)
}
}
}
// default version
if xconn == nil {
l.defaultMonitor()
return
}
// x/net version
msgs := make([]ipv4.Message, batchSize)
for k := range msgs {
msgs[k].Buffers = [][]byte{make([]byte, mtuLimit)}
}
for {
if count, err := xconn.ReadBatch(msgs, 0); err == nil {
for i := 0; i < count; i++ {
msg := &msgs[i]
l.packetInput(msg.Buffers[0][:msg.N], msg.Addr)
}
} else {
// compatibility issue:
// for linux kernel<=2.6.32, support for sendmmsg is not available
// an error of type os.SyscallError will be returned
if operr, ok := err.(*net.OpError); ok {
if se, ok := operr.Err.(*os.SyscallError); ok {
if se.Syscall == "recvmmsg" {
l.defaultMonitor()
return
}
}
}
l.notifyReadError(errors.WithStack(err))
return
}
}
}