From 9b29bd64bd23c46d491d65a6d16e7fadc888e45c Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Fri, 27 Dec 2019 23:24:19 +0800 Subject: [PATCH 1/4] fix windows --- lib/mux/mux.go | 25 ++-------------------- lib/mux/sysGetsock_nowindows.go | 34 ++++++++++++++++++++++++++++++ lib/mux/sysGetsock_windows.go | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 23 deletions(-) create mode 100644 lib/mux/sysGetsock_nowindows.go create mode 100644 lib/mux/sysGetsock_windows.go diff --git a/lib/mux/mux.go b/lib/mux/mux.go index ab023235..254945e3 100644 --- a/lib/mux/mux.go +++ b/lib/mux/mux.go @@ -7,7 +7,6 @@ import ( "net" "os" "sync/atomic" - "syscall" "time" "github.com/astaxie/beego/logs" @@ -64,26 +63,6 @@ func NewMux(c net.Conn, connType string) *Mux { return m } -func getConnFd(c net.Conn) (fd *os.File, err error) { - switch c.(type) { - case *net.TCPConn: - fd, err = c.(*net.TCPConn).File() - if err != nil { - return - } - return - case *net.UDPConn: - fd, err = c.(*net.UDPConn).File() - if err != nil { - return - } - return - default: - err = errors.New("mux:unknown conn type, only tcp or kcp") - return - } -} - func (s *Mux) NewConn() (*conn, error) { if s.IsClose { return nil, errors.New("the mux has closed") @@ -442,7 +421,7 @@ func (Self *bandwidth) SetCopySize(n uint16) { func (Self *bandwidth) calcBandWidth() { t := Self.readStart.Sub(Self.lastReadStart) - bufferSize, err := syscall.GetsockoptInt(int(Self.fd.Fd()), syscall.SOL_SOCKET, syscall.SO_RCVBUF) + bufferSize, err := sysGetSock(Self.fd) //logs.Warn(bufferSize) if err != nil { logs.Warn(err) @@ -451,7 +430,7 @@ func (Self *bandwidth) calcBandWidth() { } if Self.bufLength >= uint32(bufferSize) { atomic.StoreUint64(&Self.readBandwidth, math.Float64bits(float64(Self.bufLength)/t.Seconds())) - // calculate the hole socket buffer, the time meaning to fill the buffer + // calculate the whole socket buffer, the time meaning to fill the buffer //logs.Warn(Self.Get()) } else { Self.calcThreshold = uint32(bufferSize) diff --git a/lib/mux/sysGetsock_nowindows.go b/lib/mux/sysGetsock_nowindows.go new file mode 100644 index 00000000..cc1ce480 --- /dev/null +++ b/lib/mux/sysGetsock_nowindows.go @@ -0,0 +1,34 @@ +// +build !windows + +package mux + +import ( + "errors" + "net" + "os" + "syscall" +) + +func sysGetSock(fd *os.File) (bufferSize int, err error) { + return syscall.GetsockoptInt(int(fd.Fd()), syscall.SOL_SOCKET, syscall.SO_RCVBUF) +} + +func getConnFd(c net.Conn) (fd *os.File, err error) { + switch c.(type) { + case *net.TCPConn: + fd, err = c.(*net.TCPConn).File() + if err != nil { + return + } + return + case *net.UDPConn: + fd, err = c.(*net.UDPConn).File() + if err != nil { + return + } + return + default: + err = errors.New("mux:unknown conn type, only tcp or kcp") + return + } +} diff --git a/lib/mux/sysGetsock_windows.go b/lib/mux/sysGetsock_windows.go new file mode 100644 index 00000000..58a5b6de --- /dev/null +++ b/lib/mux/sysGetsock_windows.go @@ -0,0 +1,37 @@ +// +build windows + +package mux + +import ( + "errors" + "net" + "os" +) + +func sysGetSock(fd *os.File) (bufferSize int, err error) { + // https://github.com/golang/sys/blob/master/windows/syscall_windows.go#L1184 + // not support, WTF??? + // return syscall.GetsockoptInt((syscall.Handle)(unsafe.Pointer(fd.Fd())), syscall.SOL_SOCKET, syscall.SO_RCVBUF) + bufferSize = 10 * 1024 * 1024 + return +} + +func getConnFd(c net.Conn) (fd *os.File, err error) { + switch c.(type) { + case *net.TCPConn: + //fd, err = c.(*net.TCPConn).File() + //if err != nil { + // return + //} + return + case *net.UDPConn: + //fd, err = c.(*net.UDPConn).File() + //if err != nil { + // return + //} + return + default: + err = errors.New("mux:unknown conn type, only tcp or kcp") + return + } +} From 1f64715fabb6fe675734071dd914c14d5ae22c4e Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Sat, 28 Dec 2019 20:40:00 +0800 Subject: [PATCH 2/4] fix mux kcp connection --- lib/mux/mux_test.go | 15 +++++++++++---- lib/mux/sysGetsock_nowindows.go | 14 +++++++++++++- lib/mux/sysGetsock_windows.go | 9 +++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/mux/mux_test.go b/lib/mux/mux_test.go index 151def1d..fb5adb2d 100644 --- a/lib/mux/mux_test.go +++ b/lib/mux/mux_test.go @@ -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) } diff --git a/lib/mux/sysGetsock_nowindows.go b/lib/mux/sysGetsock_nowindows.go index cc1ce480..86c78ada 100644 --- a/lib/mux/sysGetsock_nowindows.go +++ b/lib/mux/sysGetsock_nowindows.go @@ -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 diff --git a/lib/mux/sysGetsock_windows.go b/lib/mux/sysGetsock_windows.go index 58a5b6de..5cf213f9 100644 --- a/lib/mux/sysGetsock_windows.go +++ b/lib/mux/sysGetsock_windows.go @@ -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 From 86e0a52bb9cdb1d5814ec4936e50bb093acbccf8 Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Mon, 30 Dec 2019 00:22:50 +0800 Subject: [PATCH 3/4] fix android build --- build.android.sh | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/build.android.sh b/build.android.sh index 02c97fe5..d9f81416 100644 --- a/build.android.sh +++ b/build.android.sh @@ -4,18 +4,36 @@ #fyne-cross --targets=linux/amd64,windows/amd64,darwin/amd64 gui/npc/npc.go cd /go -go get -u fyne.io/fyne fyne.io/fyne/cmd/fyne - +apt-get install libegl1-mesa-dev libgles2-mesa-dev libx11-dev -y +#go get -u fyne.io/fyne/cmd/fyne fyne.io/fyne +mkdir -p /go/src/fyne.io +cd src/fyne.io +git clone https://github.com/fyne-io/fyne.git +cd fyne +git checkout v1.2.0 +go install -v ./cmd/fyne +#fyne package -os android fyne.io/fyne/cmd/hello +echo "fyne install success" mkdir -p /go/src/github.com/cnlh/nps cp -R /app/* /go/src/github.com/cnlh/nps cd /go/src/github.com/cnlh/nps +#go get -u fyne.io/fyne fyne.io/fyne/cmd/fyne +rm cmd/npc/sdk.go +#go get -u ./... +#go mod tidy +#rm -rf /go/src/golang.org/x/mobile +echo "tidy success" +cd /go/src/github.com/cnlh/nps go mod vendor cd vendor cp -R * /go/src cd .. rm -rf vendor #rm -rf ~/.cache/* +echo "vendor success" cd gui/npc +#rm -rf /go/src/golang.org/x/mobile +#go get -u fyne.io/fyne/cmd/fyne@v1.2.0 #export ANDROID_NDK_HOME=/usr/local/android_sdk/ndk-bundle fyne package -appID org.nps.client -os android -icon ../../docs/logo.png mv npc.apk /app/android_client.apk From fe1c6e10dd188a66a76e8a850cf492eaa544a4e8 Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Mon, 30 Dec 2019 00:25:36 +0800 Subject: [PATCH 4/4] master to dev (#341)