mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
extend pid length from 16bit to 24bit in process communication of gproc package
This commit is contained in:
@ -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
|
||||
|
||||
@ -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))...)
|
||||
|
||||
Reference in New Issue
Block a user