fix windows

This commit is contained in:
ffdfgdfg 2019-12-27 23:24:19 +08:00
parent 5e1b0be81c
commit 9b29bd64bd
3 changed files with 73 additions and 23 deletions

View File

@ -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)

View File

@ -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
}
}

View File

@ -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
}
}