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 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/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 new file mode 100644 index 00000000..86c78ada --- /dev/null +++ b/lib/mux/sysGetsock_nowindows.go @@ -0,0 +1,46 @@ +// +build !windows + +package mux + +import ( + "errors" + "github.com/xtaci/kcp-go" + "net" + "os" + "syscall" +) + +func sysGetSock(fd *os.File) (bufferSize int, err error) { + 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) { + 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 + 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 new file mode 100644 index 00000000..5cf213f9 --- /dev/null +++ b/lib/mux/sysGetsock_windows.go @@ -0,0 +1,46 @@ +// +build windows + +package mux + +import ( + "errors" + "github.com/xtaci/kcp-go" + "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??? + // Todo + // 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 + 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 + } +}