优化network包长度字段自动计算

This commit is contained in:
duanhf2012
2023-03-20 15:20:04 +08:00
parent f3ff09b90f
commit 95b153f8cf
4 changed files with 13 additions and 32 deletions

View File

@@ -64,9 +64,7 @@ func (client *TCPClient) init() {
if client.cons != nil {
log.SFatal("client is running")
}
if client.LenMsgLen == 0 {
client.LenMsgLen = Default_LenMsgLen
}
if client.MinMsgLen == 0 {
client.MinMsgLen = Default_MinMsgLen
}

View File

@@ -4,7 +4,6 @@ import (
"encoding/binary"
"errors"
"io"
"math"
)
// --------------
@@ -21,29 +20,18 @@ type MsgParser struct {
func (p *MsgParser) init(){
var max uint32
switch p.LenMsgLen {
case 1:
max = math.MaxUint8
case 2:
max = math.MaxUint16
case 4:
max = math.MaxUint32
default:
panic("LenMsgLen value must be 1 or 2 or 4")
}
if p.MinMsgLen > max {
p.MinMsgLen = max
}
if p.MaxMsgLen > max {
p.MaxMsgLen = max
}
p.INetMempool = NewMemAreaPool()
}
for i:=1;i<=4;i*=2 {
max := uint32(1<<(i*8)-1)
if p.MaxMsgLen <= max {
p.LenMsgLen = i
return
}
}
panic("MaxMsgLen value must be less than 4294967295")
}
// goroutine safe
func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {

View File

@@ -9,12 +9,11 @@ import (
const Default_ReadDeadline = time.Second*30 //30s
const Default_WriteDeadline = time.Second*30 //30s
const Default_MaxConnNum = 9000
const Default_MaxConnNum = 1000000
const Default_PendingWriteNum = 10000
const Default_LittleEndian = false
const Default_MinMsgLen = 2
const Default_MaxMsgLen = 65535
const Default_LenMsgLen = 2
type TCPServer struct {
Addr string
@@ -49,6 +48,7 @@ func (server *TCPServer) init() {
server.MaxConnNum = Default_MaxConnNum
log.SRelease("invalid MaxConnNum, reset to ", server.MaxConnNum)
}
if server.PendingWriteNum <= 0 {
server.PendingWriteNum = Default_PendingWriteNum
log.SRelease("invalid PendingWriteNum, reset to ", server.PendingWriteNum)
@@ -74,10 +74,6 @@ func (server *TCPServer) init() {
log.SRelease("invalid ReadDeadline, reset to ", server.ReadDeadline.Seconds(),"s")
}
if server.LenMsgLen == 0 {
server.LenMsgLen = Default_LenMsgLen
}
if server.NewAgent == nil {
log.SFatal("NewAgent must not be nil")
}