From cdd6fc7c1ed5df7102e2b167fdecd918b1de95ac Mon Sep 17 00:00:00 2001 From: John Date: Wed, 19 Dec 2018 16:17:54 +0800 Subject: [PATCH] extend pid length from 16bit to 24bit in process communication of gproc package --- g/os/gproc/gproc_comm_receive.go | 18 +++++++++--------- g/os/gproc/gproc_comm_send.go | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/g/os/gproc/gproc_comm_receive.go b/g/os/gproc/gproc_comm_receive.go index e3fa94dd5..9407fb8be 100644 --- a/g/os/gproc/gproc_comm_receive.go +++ b/g/os/gproc/gproc_comm_receive.go @@ -100,32 +100,32 @@ func tcpServiceHandler(conn *gtcp.Conn) { } // 数据解包,防止黏包 -// 数据格式:总长度(24bit)|发送进程PID(16bit)|接收进程PID(16bit)|分组长度(8bit)|分组名称(变长)|校验(32bit)|参数(变长) +// 数据格式:总长度(24bit)|发送进程PID(24bit)|接收进程PID(24bit)|分组长度(8bit)|分组名称(变长)|校验(32bit)|参数(变长) func bufferToMsgs(buffer []byte) []*Msg { s := 0 msgs := make([]*Msg, 0) for s < len(buffer) { // 长度解析及校验 length := gbinary.DecodeToInt(buffer[s : s + 3]) - if length < 12 || length > len(buffer) { + if length < 14 || length > len(buffer) { s++ continue } // 分组信息解析 - groupLen := gbinary.DecodeToInt(buffer[s + 7 : s + 8]) + groupLen := gbinary.DecodeToInt(buffer[s + 9 : s + 10]) // checksum校验(仅对参数做校验,提高校验效率) - checksum1 := gbinary.DecodeToUint32(buffer[s + 8 + groupLen : s + 8 + groupLen + 4]) - checksum2 := gtcp.Checksum(buffer[s + 8 + groupLen + 4 : s + length]) + checksum1 := gbinary.DecodeToUint32(buffer[s + 10 + groupLen : s + 10 + groupLen + 4]) + checksum2 := gtcp.Checksum(buffer[s + 10 + groupLen + 4 : s + length]) if checksum1 != checksum2 { s++ continue } // 接收进程PID校验 - if Pid() == gbinary.DecodeToInt(buffer[s + 5 : s + 7]) { + if Pid() == gbinary.DecodeToInt(buffer[s + 6 : s + 9]) { msgs = append(msgs, &Msg { - Pid : gbinary.DecodeToInt(buffer[s + 3 : s + 5]), - Data : buffer[s + 8 + groupLen + 4 : s + length], - Group : string(buffer[s + 8 : s + 8 + groupLen]), + Pid : gbinary.DecodeToInt(buffer[s + 3 : s + 6]), + Data : buffer[s + 10 + groupLen + 4 : s + length], + Group : string(buffer[s + 10 : s + 10 + groupLen]), }) } s += length diff --git a/g/os/gproc/gproc_comm_send.go b/g/os/gproc/gproc_comm_send.go index 94ed30f14..334e2de92 100644 --- a/g/os/gproc/gproc_comm_send.go +++ b/g/os/gproc/gproc_comm_send.go @@ -27,16 +27,16 @@ const ( ) // 向指定gproc进程发送数据 -// 数据格式:总长度(24bit)|发送进程PID(16bit)|接收进程PID(16bit)|分组长度(8bit)|分组名称(变长)|校验(32bit)|参数(变长) +// 数据格式:总长度(24bit)|发送进程PID(24bit)|接收进程PID(24bit)|分组长度(8bit)|分组名称(变长)|校验(32bit)|参数(变长) func Send(pid int, data []byte, group...string) error { groupName := gPROC_COMM_DEAFULT_GRUOP_NAME if len(group) > 0 { groupName = group[0] } buffer := make([]byte, 0) - buffer = append(buffer, gbinary.EncodeByLength(3, len(groupName) + len(data) + 12)...) - buffer = append(buffer, gbinary.EncodeByLength(2, Pid())...) - buffer = append(buffer, gbinary.EncodeByLength(2, pid)...) + buffer = append(buffer, gbinary.EncodeByLength(3, len(groupName) + len(data) + 14)...) + buffer = append(buffer, gbinary.EncodeByLength(3, Pid())...) + buffer = append(buffer, gbinary.EncodeByLength(3, pid)...) buffer = append(buffer, gbinary.EncodeByLength(1, len(groupName))...) buffer = append(buffer, []byte(groupName)...) buffer = append(buffer, gbinary.EncodeUint32(gtcp.Checksum(data))...)