优化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

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