fix mux kcp connection

This commit is contained in:
ffdfgdfg 2019-12-28 20:40:00 +08:00
parent 813eae1216
commit 1f64715fab
3 changed files with 33 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/cnlh/nps/lib/common"
"github.com/cnlh/nps/lib/goroutine"
"github.com/xtaci/kcp-go"
"io"
"log"
"net"
@ -33,7 +34,8 @@ func TestNewMux(t *testing.T) {
//poolConnCopy, _ := ants.NewPoolWithFunc(200000, common.copyConn, ants.WithNonblocking(false))
time.Sleep(time.Second * 3)
go func() {
m2 := NewMux(conn2, "tcp")
//m2 := NewMux(conn2, "tcp")
m2 := NewMux(conn2, "kcp")
for {
//logs.Warn("npc starting accept")
c, err := m2.Accept()
@ -82,7 +84,8 @@ func TestNewMux(t *testing.T) {
}()
go func() {
m1 := NewMux(conn1, "tcp")
//m1 := NewMux(conn1, "tcp")
m1 := NewMux(conn1, "kcp")
l, err := net.Listen("tcp", "127.0.0.1:7777")
if err != nil {
logs.Warn(err)
@ -144,12 +147,14 @@ func TestNewMux(t *testing.T) {
func server() {
var err error
l, err := net.Listen("tcp", "127.0.0.1:9999")
//l, err := net.Listen("tcp", "127.0.0.1:9999")
l, err := kcp.Listen("127.0.0.1:9999")
if err != nil {
logs.Warn(err)
}
go func() {
conn1, err = l.Accept()
logs.Info("accept", conn1)
if err != nil {
logs.Warn(err)
}
@ -159,7 +164,9 @@ func server() {
func client() {
var err error
conn2, err = net.Dial("tcp", "127.0.0.1:9999")
//conn2, err = net.Dial("tcp", "127.0.0.1:9999")
logs.Warn("dial")
conn2, err = kcp.Dial("127.0.0.1:9999")
if err != nil {
logs.Warn(err)
}

View File

@ -4,13 +4,18 @@ package mux
import (
"errors"
"github.com/xtaci/kcp-go"
"net"
"os"
"syscall"
)
func sysGetSock(fd *os.File) (bufferSize int, err error) {
return syscall.GetsockoptInt(int(fd.Fd()), syscall.SOL_SOCKET, syscall.SO_RCVBUF)
if fd != nil {
return syscall.GetsockoptInt(int(fd.Fd()), syscall.SOL_SOCKET, syscall.SO_RCVBUF)
} else {
return 1400 * 320, nil
}
}
func getConnFd(c net.Conn) (fd *os.File, err error) {
@ -27,6 +32,13 @@ func getConnFd(c net.Conn) (fd *os.File, err error) {
return
}
return
case *kcp.UDPSession:
//fd, err = (*net.UDPConn)(unsafe.Pointer(c.(*kcp.UDPSession))).File()
//if err != nil {
// return
//}
// Todo
return
default:
err = errors.New("mux:unknown conn type, only tcp or kcp")
return

View File

@ -4,6 +4,7 @@ package mux
import (
"errors"
"github.com/xtaci/kcp-go"
"net"
"os"
)
@ -11,6 +12,7 @@ import (
func sysGetSock(fd *os.File) (bufferSize int, err error) {
// https://github.com/golang/sys/blob/master/windows/syscall_windows.go#L1184
// not support, WTF???
// Todo
// return syscall.GetsockoptInt((syscall.Handle)(unsafe.Pointer(fd.Fd())), syscall.SOL_SOCKET, syscall.SO_RCVBUF)
bufferSize = 10 * 1024 * 1024
return
@ -30,6 +32,13 @@ func getConnFd(c net.Conn) (fd *os.File, err error) {
// return
//}
return
case *kcp.UDPSession:
//fd, err = (*net.UDPConn)(unsafe.Pointer(c.(*kcp.UDPSession))).File()
//if err != nil {
// return
//}
// Todo
return
default:
err = errors.New("mux:unknown conn type, only tcp or kcp")
return