Skip to content

Commit

Permalink
fix sendmmsg/recvmmsg compatibility issue for old linux kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Jun 11, 2019
1 parent 0934811 commit 3a4bcb9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions readloop_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package kcp

import (
"net"
"os"
"sync/atomic"

"github.com/pkg/errors"
Expand Down Expand Up @@ -47,6 +48,17 @@ func (s *UDPSession) readLoop() {
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
}
Expand Down Expand Up @@ -90,6 +102,17 @@ func (l *Listener) monitor() {
}
}
} 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
}
Expand Down
14 changes: 14 additions & 0 deletions tx_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package kcp

import (
"net"
"os"
"sync/atomic"

"github.com/pkg/errors"
Expand All @@ -28,6 +30,18 @@ func (s *UDPSession) tx(txqueue []ipv4.Message) {
npkts += n
txqueue = txqueue[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 == "sendmmsg" {
s.xconn = nil // set s.xconn to nil permanently
s.defaultTx(txqueue)
return
}
}
}
s.notifyWriteError(errors.WithStack(err))
break
}
Expand Down

0 comments on commit 3a4bcb9

Please sign in to comment.