Skip to content

Commit 74f3194

Browse files
committed
修改: 封包大小使用常量
1 parent 49d234f commit 74f3194

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

doc/customproc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ https://github.com/davyxu/cellnet/blob/master/proc/tcp/transmitter.go
4747

4848
功能 | 类型 | 备注
4949
---|---|---
50-
包体大小 | uint16 | 只做UDP包完整性验证
50+
包体大小 | uint16 | 只做UDP包完整性验证,包含包体大小本身
5151
包体中的消息ID | uint16 | 包.消息名 的hash值(util.StringHash)
5252
包体中的用户消息数据 | []byte | 用户的消息大小,需要使用codec包解码
5353

proc/udp/recv.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ import (
55
"github.com/davyxu/cellnet/codec"
66
)
77

8-
const MTU = 1472
8+
const (
9+
MTU = 1472 // 最大传输单元
10+
packetLen = 2 // 包体大小字段
11+
msgIDLen = 2 // 消息ID字段
12+
13+
headerSize = msgIDLen + msgIDLen // 整个UDP包头部分
14+
)
915

1016
func recvPacket(pktData []byte) (msg interface{}, err error) {
1117

1218
// 小于包头,使用nc指令测试时,为1
13-
if len(pktData) < 2 {
19+
if len(pktData) < packetLen {
1420
return nil, nil
1521
}
1622

@@ -23,7 +29,7 @@ func recvPacket(pktData []byte) (msg interface{}, err error) {
2329
}
2430

2531
// 读取消息ID
26-
msgid := binary.LittleEndian.Uint16(pktData[2:])
32+
msgid := binary.LittleEndian.Uint16(pktData[packetLen:])
2733

2834
msgData := pktData[headerSize:]
2935

proc/udp/send.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"github.com/davyxu/cellnet/peer/udp"
88
)
99

10-
const headerSize = 2 + 2
11-
1210
func sendPacket(writer udp.DataWriter, ctx cellnet.ContextSet, msg interface{}) error {
1311

1412
// 将用户数据转换为字节数组和消息ID

tests/RunAll.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ function Cleanup()
2222
rm -rf examplebin
2323
}
2424

25-
trap CleanUp EXIT
25+
trap Cleanup EXIT

util/packet.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ var (
1515
)
1616

1717
const (
18-
packetSize = 2
18+
bodySize = 2 // 包体大小字段
19+
msgIDSize = 2 // 消息ID字段
1920
)
2021

2122
// 接收Length-Type-Value格式的封包流程
2223
func RecvLTVPacket(reader io.Reader, maxPacketSize int) (msg interface{}, err error) {
2324

2425
// Size为uint16,占2字节
25-
var sizeBuffer = make([]byte, packetSize)
26+
var sizeBuffer = make([]byte, bodySize)
2627

2728
// 持续读取Size直到读到为止
2829
_, err = io.ReadFull(reader, sizeBuffer)
@@ -32,7 +33,7 @@ func RecvLTVPacket(reader io.Reader, maxPacketSize int) (msg interface{}, err er
3233
return
3334
}
3435

35-
if len(sizeBuffer) < packetSize {
36+
if len(sizeBuffer) < bodySize {
3637
return nil, ErrMinPacket
3738
}
3839

@@ -54,13 +55,13 @@ func RecvLTVPacket(reader io.Reader, maxPacketSize int) (msg interface{}, err er
5455
return
5556
}
5657

57-
if len(sizeBuffer) < packetSize {
58+
if len(sizeBuffer) < bodySize {
5859
return nil, ErrShortMsgID
5960
}
6061

6162
msgid := binary.LittleEndian.Uint16(body)
6263

63-
msgData := body[2:]
64+
msgData := body[msgIDSize:]
6465

6566
// 将字节数组和消息ID用户解出消息
6667
msg, _, err = codec.DecodeMessage(int(msgid), msgData)
@@ -98,16 +99,16 @@ func SendLTVPacket(writer io.Writer, ctx cellnet.ContextSet, data interface{}) e
9899
msgID = meta.ID
99100
}
100101

101-
pkt := make([]byte, 2+2+len(msgData))
102+
pkt := make([]byte, bodySize+msgIDSize+len(msgData))
102103

103104
// Length
104-
binary.LittleEndian.PutUint16(pkt, uint16(2+len(msgData)))
105+
binary.LittleEndian.PutUint16(pkt, uint16(msgIDSize+len(msgData)))
105106

106107
// Type
107-
binary.LittleEndian.PutUint16(pkt[2:], uint16(msgID))
108+
binary.LittleEndian.PutUint16(pkt[msgIDSize:], uint16(msgID))
108109

109110
// Value
110-
copy(pkt[2+2:], msgData)
111+
copy(pkt[bodySize+msgIDSize:], msgData)
111112

112113
// 将数据写入Socket
113114
err := WriteFull(writer, pkt)

0 commit comments

Comments
 (0)