mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化network包长度字段自动计算
This commit is contained in:
@@ -64,9 +64,7 @@ func (client *TCPClient) init() {
|
|||||||
if client.cons != nil {
|
if client.cons != nil {
|
||||||
log.SFatal("client is running")
|
log.SFatal("client is running")
|
||||||
}
|
}
|
||||||
if client.LenMsgLen == 0 {
|
|
||||||
client.LenMsgLen = Default_LenMsgLen
|
|
||||||
}
|
|
||||||
if client.MinMsgLen == 0 {
|
if client.MinMsgLen == 0 {
|
||||||
client.MinMsgLen = Default_MinMsgLen
|
client.MinMsgLen = Default_MinMsgLen
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// --------------
|
// --------------
|
||||||
@@ -21,29 +20,18 @@ type MsgParser struct {
|
|||||||
|
|
||||||
|
|
||||||
func (p *MsgParser) init(){
|
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()
|
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
|
// goroutine safe
|
||||||
func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
func (p *MsgParser) Read(conn *TCPConn) ([]byte, error) {
|
||||||
|
|||||||
@@ -9,12 +9,11 @@ import (
|
|||||||
|
|
||||||
const Default_ReadDeadline = time.Second*30 //30s
|
const Default_ReadDeadline = time.Second*30 //30s
|
||||||
const Default_WriteDeadline = 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_PendingWriteNum = 10000
|
||||||
const Default_LittleEndian = false
|
const Default_LittleEndian = false
|
||||||
const Default_MinMsgLen = 2
|
const Default_MinMsgLen = 2
|
||||||
const Default_MaxMsgLen = 65535
|
const Default_MaxMsgLen = 65535
|
||||||
const Default_LenMsgLen = 2
|
|
||||||
|
|
||||||
type TCPServer struct {
|
type TCPServer struct {
|
||||||
Addr string
|
Addr string
|
||||||
@@ -49,6 +48,7 @@ func (server *TCPServer) init() {
|
|||||||
server.MaxConnNum = Default_MaxConnNum
|
server.MaxConnNum = Default_MaxConnNum
|
||||||
log.SRelease("invalid MaxConnNum, reset to ", server.MaxConnNum)
|
log.SRelease("invalid MaxConnNum, reset to ", server.MaxConnNum)
|
||||||
}
|
}
|
||||||
|
|
||||||
if server.PendingWriteNum <= 0 {
|
if server.PendingWriteNum <= 0 {
|
||||||
server.PendingWriteNum = Default_PendingWriteNum
|
server.PendingWriteNum = Default_PendingWriteNum
|
||||||
log.SRelease("invalid PendingWriteNum, reset to ", server.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")
|
log.SRelease("invalid ReadDeadline, reset to ", server.ReadDeadline.Seconds(),"s")
|
||||||
}
|
}
|
||||||
|
|
||||||
if server.LenMsgLen == 0 {
|
|
||||||
server.LenMsgLen = Default_LenMsgLen
|
|
||||||
}
|
|
||||||
|
|
||||||
if server.NewAgent == nil {
|
if server.NewAgent == nil {
|
||||||
log.SFatal("NewAgent must not be nil")
|
log.SFatal("NewAgent must not be nil")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,6 @@ func (server *Server) Start(listenAddr string, maxRpcParamLen uint32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
server.rpcServer.Addr = ":" + splitAddr[1]
|
server.rpcServer.Addr = ":" + splitAddr[1]
|
||||||
server.rpcServer.LenMsgLen = 4 //uint16
|
|
||||||
server.rpcServer.MinMsgLen = 2
|
server.rpcServer.MinMsgLen = 2
|
||||||
if maxRpcParamLen > 0 {
|
if maxRpcParamLen > 0 {
|
||||||
server.rpcServer.MaxMsgLen = maxRpcParamLen
|
server.rpcServer.MaxMsgLen = maxRpcParamLen
|
||||||
|
|||||||
Reference in New Issue
Block a user