From 9b29bd64bd23c46d491d65a6d16e7fadc888e45c Mon Sep 17 00:00:00 2001 From: ffdfgdfg Date: Fri, 27 Dec 2019 23:24:19 +0800 Subject: [PATCH] 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 ab02323..254945e 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 0000000..cc1ce48 --- /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 0000000..58a5b6d --- /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 + } +}