mirror of
https://github.com/xtaci/kcp-go.git
synced 2024-04-21 12:32:15 +00:00
commit a8ae293a9ddee042c35e2501f93fa5dfa2425302 Merge:bdaf81e1a84985Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 22:48:13 2020 +0800 Merge branch 'master' into autotune commitbdaf81e88eAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 22:38:31 2020 +0800 add loc commitce66a98f95Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 19:53:42 2020 +0800 lint commitad7383d8a3Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 19:05:35 2020 +0800 fix typo commitdc0fc2364cAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:47:14 2020 +0800 adjust tests commitec45b72c39Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:45:17 2020 +0800 remove obsolete decoder in listener commit827aa70e29Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:42:42 2020 +0800 use hierarchical defensive receiving strategy commitf5ca6b0e3aAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 16:34:30 2020 +0800 optimize condition commitff34f7683fAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 15:18:00 2020 +0800 fix test commita08b274651Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 15:13:20 2020 +0800 solve compability issue with non FEC sender commitab7596783bAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 15:02:34 2020 +0800 implements autotune in fec decoder commit260f61b3ebAuthor: xtaci <daniel820313@gmail.com> Date: Fri Oct 9 14:34:48 2020 +0800 hack 16-bit to identify FEC from packet commit105bfec567Author: xtaci <daniel820313@gmail.com> Date: Fri Oct 2 21:38:47 2020 +0800 add autotune
112 lines
2.4 KiB
Go
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
|
|
}
|
|
}
|
|
}
|