Skip to content

Commit 150e0c8

Browse files
authored
Merge pull request #219 from aceld/translate_english
translate english
2 parents 8948f58 + 438204f commit 150e0c8

File tree

7 files changed

+384
-219
lines changed

7 files changed

+384
-219
lines changed

zdecoder/htlvcrcdecoder.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (hcd *HtlvCrcDecoder) decode(data []byte) *HtlvCrcDecoder {
104104

105105
// CRC
106106
if !CheckCRC(data[:datasize-2], htlvData.Crc) {
107-
zlog.Ins().DebugF("crc校验失败 %s %s\n", hex.EncodeToString(data), hex.EncodeToString(htlvData.Crc))
107+
zlog.Ins().DebugF("crc check error %s %s\n", hex.EncodeToString(data), hex.EncodeToString(htlvData.Crc))
108108
return nil
109109
}
110110

@@ -118,25 +118,28 @@ func (hcd *HtlvCrcDecoder) Intercept(chain ziface.IChain) ziface.IcResp {
118118
//1. Get the IMessage of zinx
119119
iMessage := chain.GetIMessage()
120120
if iMessage == nil {
121-
//进入责任链下一层
121+
// Go to the next layer in the chain of responsibility
122122
return chain.ProceedWithIMessage(iMessage, nil)
123123
}
124124

125125
//2. Get Data
126126
data := iMessage.GetData()
127127
//zlog.Ins().DebugF("HTLVCRC-RawData size:%d data:%s\n", len(data), hex.EncodeToString(data))
128128

129-
//3. 读取的数据不超过包头,直接进入下一层
129+
//3. If the amount of data read is less than the length of the header, proceed to the next layer directly.
130+
// (读取的数据不超过包头,直接进入下一层)
130131
if len(data) < HEADER_SIZE {
131132
return chain.ProceedWithIMessage(iMessage, nil)
132133
}
133134

134135
//4. HTLV+CRC Decode
135136
htlvData := hcd.decode(data)
136137

137-
//5. 将解码后的数据重新设置到IMessage中, Zinx的Router需要MsgID来寻址
138+
//5. Set the decoded data back to the IMessage, the Zinx Router needs MsgID for addressing
139+
// (将解码后的数据重新设置到IMessage中, Zinx的Router需要MsgID来寻址)
138140
iMessage.SetMsgID(uint32(htlvData.Funcode))
139141

140-
//6. 将解码后的数据进入下一层
142+
//6. Pass the decoded data to the next layer.
143+
// (将解码后的数据进入下一层)
141144
return chain.ProceedWithIMessage(iMessage, *htlvData)
142145
}

zdecoder/ltvdecoder_little.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
// LTV, which stands for Length-Tag(Type)-Value, is a simple and practical data transmission scheme.
2+
// In the definition of LTV, there are three fields: Tag field, Length field, and Value field.
3+
// The value of the Length field is actually the length of the Value field.
4+
//
5+
// Before decoding (20 bytes) After decoding (20 bytes)
6+
//+------------+------------+-----------------+ +------------+------------+-----------------+
7+
//| Length | Tag | Value |----->| Length | Tag | Value |
8+
//| 0x0000000C | 0x00000001 | "HELLO, WORLD" | | 0x0000000C | 0x00000001 | "HELLO, WORLD" |
9+
//+------------+------------+-----------------+ +------------+------------+-----------------+
10+
// Length: uint32 type, 4 bytes, indicating the length of Value is 12 (hex: 0x0000000C).
11+
// Tag: uint32 type, 4 bytes, serves as MsgId, temporarily set to 1.
12+
// Value: 12 characters in total, 12 bytes in length.
13+
//
14+
// Explanation:
15+
// lengthFieldOffset = 0 (The index of Length byte is 0) Length field offset.
16+
// lengthFieldLength = 4 (Length takes up 4 bytes) Length field length.
17+
// lengthAdjustment = 4 (Length only represents the length of Value. The program will only read Length bytes and end.
18+
// If there is a crc of 2 bytes after Value, then it is 2 here. If Length represents the total
19+
// length of Tag+Length+Value, then it is -8 here.)
20+
// initialBytesToStrip = 0 (0 means returning the complete protocol content Tag+Length+Value. If you only want to
21+
// return the Value content and remove the 4 bytes of Tag and 4 bytes of Length, it is 8 here.)
22+
// The number of bytes removed from the decoded frame for the first time.
23+
// maxFrameLength = 2^32 + 4 + 4 (Since Length is of uint type, 2^32 represents the maximum length of Value.
24+
// In addition, Tag and Length each take up 4 bytes.)
25+
26+
// [简体中文]
127
// LTV,即Length-Tag(Type)-Value,是一种简单实用的数据传输方案。
228
//在LTV的定义中,可以知道它包括三个域,分别为:标签域(Tag),长度域(Length),内容域(Value)。这里的长度域的值实际上就是内容域的长度。
329
//
@@ -13,8 +39,11 @@
1339
// 说明:
1440
// lengthFieldOffset = 0 (Length的字节位索引下标是0) 长度字段的偏差
1541
// lengthFieldLength = 4 (Length是4个byte) 长度字段占的字节数
16-
// lengthAdjustment = 4 (Length只表示Value长度,程序只会读取Length个字节就结束,后面没有来,故为0,若Value后面还有crc占2字节的话,那么此处就是2。若Length标记的是Tag+Length+Value总长度,那么此处是-8)
17-
// initialBytesToStrip = 0 (这个0表示返回完整的协议内容Tag+Length+Value,如果只想返回Value内容,去掉Tag的4字节和Length的4字节,此处就是8) 从解码帧中第一次去除的字节数
42+
// lengthAdjustment = 4 (Length只表示Value长度,程序只会读取Length个字节就结束,后面没有来,故为0,
43+
// 若Value后面还有crc占2字节的话,那么此处就是2。若Length标记的是Tag+Length+Value总长度,
44+
// 那么此处是-8)
45+
// initialBytesToStrip = 0 (这个0表示返回完整的协议内容Tag+Length+Value,如果只想返回Value内容,去掉Tag的4字节和Length
46+
// 的4字节,此处就是8) 从解码帧中第一次去除的字节数
1847
// maxFrameLength = 2^32 + 4 + 4 (Length为uint类型,故2^32次方表示Value最大长度,此外Tag和Length各占4字节)
1948

2049
package zdecoder
@@ -29,9 +58,9 @@ import (
2958
const LTV_HEADER_SIZE = 8 //表示TLV空包长度
3059

3160
type LTV_Little_Decoder struct {
32-
Length uint32 //消息长度
33-
Tag uint32 //消息类型
34-
Value []byte //消息内容
61+
Length uint32 //L
62+
Tag uint32 //T
63+
Value []byte //V
3564
}
3665

3766
func NewLTV_Little_Decoder() ziface.IDecoder {
@@ -66,45 +95,47 @@ func (ltv *LTV_Little_Decoder) GetLengthField() *ziface.LengthField {
6695
func (ltv *LTV_Little_Decoder) decode(data []byte) *LTV_Little_Decoder {
6796
ltvData := LTV_Little_Decoder{}
6897

69-
//获取L
98+
//Get L
7099
ltvData.Length = binary.LittleEndian.Uint32(data[0:4])
71-
//获取T
100+
//Get T
72101
ltvData.Tag = binary.LittleEndian.Uint32(data[4:8])
73-
//确定V的长度
102+
//Determine the length of V. (确定V的长度)
74103
ltvData.Value = make([]byte, ltvData.Length)
75104

76-
//5. 获取V
105+
//5. Get V
77106
binary.Read(bytes.NewBuffer(data[8:8+ltvData.Length]), binary.LittleEndian, ltvData.Value)
78107

79108
return &ltvData
80109
}
81110

82111
func (ltv *LTV_Little_Decoder) Intercept(chain ziface.IChain) ziface.IcResp {
83-
//1.获取zinx的IMessage
112+
//1. Get the IMessage of zinx
84113
iMessage := chain.GetIMessage()
85114
if iMessage == nil {
86-
//进入责任链下一层
115+
// Go to the next layer in the chain of responsibility
87116
return chain.ProceedWithIMessage(iMessage, nil)
88117
}
89118

90-
//2. 获取数据
119+
//2. Get Data
91120
data := iMessage.GetData()
92121
//zlog.Ins().DebugF("LTV-RawData size:%d data:%s\n", len(data), hex.EncodeToString(data))
93122

94-
//3. 读取的数据不超过包头,直接进入下一层
123+
//3. If the amount of data read is less than the length of the header, proceed to the next layer directly.
124+
// (读取的数据不超过包头,直接进入下一层)
95125
if len(data) < LTV_HEADER_SIZE {
96-
//进入责任链下一层
97126
return chain.ProceedWithIMessage(iMessage, nil)
98127
}
99128

100-
//4. ltv解码
129+
//4. LTV Decode
101130
ltvData := ltv.decode(data)
102131

103-
//5. 将解码后的数据重新设置到IMessage中, Zinx的Router需要MsgID来寻址
132+
//5. Set the decoded data back to the IMessage, the Zinx Router needs MsgID for addressing
133+
// (将解码后的数据重新设置到IMessage中, Zinx的Router需要MsgID来寻址)
104134
iMessage.SetDataLen(ltvData.Length)
105135
iMessage.SetMsgID(ltvData.Tag)
106136
iMessage.SetData(ltvData.Value)
107137

108-
//6. 将解码后的数据进入下一层
138+
//6. Pass the decoded data to the next layer.
139+
// (将解码后的数据进入下一层)
109140
return chain.ProceedWithIMessage(iMessage, *ltvData)
110141
}

zdecoder/tlvdecoder.go

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
// TLV, which stands for Tag(Type)-Length-Value, is a simple and practical data transmission scheme.
2+
// In the definition of TLV, it can be seen that it consists of three fields: the tag field (Tag), the length
3+
// field (Length), and the value field (Value).
4+
// The value of the length field is actually the length of the content field.
5+
//
6+
// Before decoding (20 bytes) After decoding (20 bytes)
7+
// +------------+------------+-----------------+ +------------+------------+-----------------+
8+
// | Tag | Length | Value |-----> | Tag | Length | Value |
9+
// | 0x00000001 | 0x0000000C | "HELLO, WORLD" | | 0x00000001 | 0x0000000C | "HELLO, WORLD" |
10+
// +------------+------------+-----------------+ +------------+------------+-----------------+
11+
// Tag: uint32 type, occupies 4 bytes, Tag is set as MsgId, temporarily set to 1
12+
// Length: uint32 type, occupies 4 bytes, Length marks the length of Value, which is 12(hex:0x0000000C)
13+
// Value: 12 characters in total, occupies 12 bytes
14+
//
15+
// Explanation:
16+
// lengthFieldOffset = 4 (The byte index of Length is 4) Length field offset
17+
// lengthFieldLength = 4 (Length is 4 bytes) Length field length in bytes
18+
// lengthAdjustment = 0 (Length only represents the length of Value. The program will read only Length bytes and end.
19+
// If there is a crc of 2 bytes after Value, then this is 2. If Length marks the total length of
20+
// Tag+Length+Value, then this is -8)
21+
// initialBytesToStrip = 0 (This 0 means that the complete protocol content Tag+Length+Value is returned. If you only
22+
// want to return the Value content, remove the 4 bytes of Tag and 4 bytes of Length, and
23+
// this is 8) Number of bytes to strip from the decoded frame
24+
// maxFrameLength = 2^32 + 4 + 4 (Since Length is of uint type, 2^32 represents the maximum length of Value. In addition,
25+
// Tag and Length each occupy 4 bytes.)
26+
27+
// [简体中文]
128
// TLV,即Tag(Type)—Length—Value,是一种简单实用的数据传输方案。
229
//在TLV的定义中,可以知道它包括三个域,分别为:标签域(Tag),长度域(Length),内容域(Value)。这里的长度域的值实际上就是内容域的长度。
330
//
@@ -13,8 +40,11 @@
1340
// 说明:
1441
// lengthFieldOffset = 4 (Length的字节位索引下标是4) 长度字段的偏差
1542
// lengthFieldLength = 4 (Length是4个byte) 长度字段占的字节数
16-
// lengthAdjustment = 0 (Length只表示Value长度,程序只会读取Length个字节就结束,后面没有来,故为0,若Value后面还有crc占2字节的话,那么此处就是2。若Length标记的是Tag+Length+Value总长度,那么此处是-8)
17-
// initialBytesToStrip = 0 (这个0表示返回完整的协议内容Tag+Length+Value,如果只想返回Value内容,去掉Tag的4字节和Length的4字节,此处就是8) 从解码帧中第一次去除的字节数
43+
// lengthAdjustment = 0 (Length只表示Value长度,程序只会读取Length个字节就结束,后面没有来,故为0,
44+
// 若Value后面还有crc占2字节的话,那么此处就是2。若Length标记的是Tag+Length+Value总长度,
45+
// 那么此处是-8)
46+
// initialBytesToStrip = 0 (这个0表示返回完整的协议内容Tag+Length+Value,如果只想返回Value内容,去掉Tag的4字节和Length
47+
// 的4字节,此处就是8) 从解码帧中第一次去除的字节数
1848
// maxFrameLength = 2^32 + 4 + 4 (Length为uint类型,故2^32次方表示Value最大长度,此外Tag和Length各占4字节)
1949

2050
package zdecoder
@@ -29,9 +59,9 @@ import (
2959
const TLV_HEADER_SIZE = 8 //表示TLV空包长度
3060

3161
type TLVDecoder struct {
32-
Tag uint32 //消息类型
33-
Length uint32 //消息长度
34-
Value []byte //消息内容
62+
Tag uint32 //T
63+
Length uint32 //L
64+
Value []byte //V
3565
}
3666

3767
func NewTLVDecoder() ziface.IDecoder {
@@ -65,14 +95,14 @@ func (tlv *TLVDecoder) GetLengthField() *ziface.LengthField {
6595

6696
func (tlv *TLVDecoder) decode(data []byte) *TLVDecoder {
6797
tlvData := TLVDecoder{}
68-
//获取T
98+
//Get T
6999
tlvData.Tag = binary.BigEndian.Uint32(data[0:4])
70-
//获取L
100+
//Get L
71101
tlvData.Length = binary.BigEndian.Uint32(data[4:8])
72-
//确定V的长度
102+
//Determine the length of V. (确定V的长度)
73103
tlvData.Value = make([]byte, tlvData.Length)
74104

75-
//5.获取V
105+
//Get V
76106
binary.Read(bytes.NewBuffer(data[8:8+tlvData.Length]), binary.BigEndian, tlvData.Value)
77107

78108
//zlog.Ins().DebugF("TLV-DecodeData size:%d data:%+v\n", unsafe.Sizeof(data), tlvData)
@@ -81,30 +111,33 @@ func (tlv *TLVDecoder) decode(data []byte) *TLVDecoder {
81111

82112
func (tlv *TLVDecoder) Intercept(chain ziface.IChain) ziface.IcResp {
83113

84-
//1. 获取zinx的IMessage
114+
//1. Get the IMessage of zinx
85115
iMessage := chain.GetIMessage()
86116
if iMessage == nil {
87-
//进入责任链下一层
117+
// Go to the next layer in the chain of responsibility
88118
return chain.ProceedWithIMessage(iMessage, nil)
89119
}
90120

91-
//2. 获取数据
121+
//2. Get Data
92122
data := iMessage.GetData()
93123
//zlog.Ins().DebugF("TLV-RawData size:%d data:%s\n", len(data), hex.EncodeToString(data))
94124

95-
//3. 读取的数据不超过包头,直接进入下一层
125+
//3. If the amount of data read is less than the length of the header, proceed to the next layer directly.
126+
// (读取的数据不超过包头,直接进入下一层)
96127
if len(data) < TLV_HEADER_SIZE {
97128
return chain.ProceedWithIMessage(iMessage, nil)
98129
}
99130

100-
//4. TLV解码
131+
//4. TLV Decode
101132
tlvData := tlv.decode(data)
102133

103-
//5. 将解码后的数据重新设置到IMessage中, Zinx的Router需要MsgID来寻址
134+
//5. Set the decoded data back to the IMessage, the Zinx Router needs MsgID for addressing
135+
// (将解码后的数据重新设置到IMessage中, Zinx的Router需要MsgID来寻址)
104136
iMessage.SetMsgID(tlvData.Tag)
105137
iMessage.SetData(tlvData.Value)
106138
iMessage.SetDataLen(tlvData.Length)
107139

108-
//6. 将解码后的数据进入下一层
140+
//6. Pass the decoded data to the next layer.
141+
// (将解码后的数据进入下一层)
109142
return chain.ProceedWithIMessage(iMessage, *tlvData)
110143
}

ziface/iclient.go

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @Title iclient.go
2-
// @Description 提供Client抽象层全部接口声明
2+
// @Description Provides all interface declarations for the Client abstraction layer.
33
// @Author Aceld - 2023-2-28
44
package ziface
55

@@ -11,19 +11,58 @@ type IClient interface {
1111
Stop()
1212
AddRouter(msgID uint32, router IRouter)
1313
Conn() IConnection
14-
SetOnConnStart(func(IConnection)) //设置该Client的连接创建时Hook函数
15-
SetOnConnStop(func(IConnection)) //设置该Client的连接断开时的Hook函数
16-
GetOnConnStart() func(IConnection) //获取该Client的连接创建时Hook函数
17-
GetOnConnStop() func(IConnection) //设置该Client的连接断开时的Hook函数
18-
GetPacket() IDataPack //获取Client绑定的数据协议封包方式
19-
SetPacket(IDataPack) //设置Client绑定的数据协议封包方式
20-
GetMsgHandler() IMsgHandle //获取Client绑定的消息处理模块
21-
StartHeartBeat(time.Duration) //启动心跳检测
22-
StartHeartBeatWithOption(time.Duration, *HeartBeatOption) //启动心跳检测(自定义回调)
14+
15+
// SetOnConnStart Set the Hook function to be called when a connection is created for this Client
16+
// (设置该Client的连接创建时Hook函数)
17+
SetOnConnStart(func(IConnection))
18+
19+
// SetOnConnStop Set the Hook function to be called when a connection is closed for this Client
20+
// (设置该Client的连接断开时的Hook函数)
21+
SetOnConnStop(func(IConnection))
22+
23+
// GetOnConnStart Get the Hook function that is called when a connection is created for this Client
24+
// (获取该Client的连接创建时Hook函数)
25+
GetOnConnStart() func(IConnection)
26+
27+
// GetOnConnStop Get the Hook function that is called when a connection is closed for this Client
28+
// (设置该Client的连接断开时的Hook函数)
29+
GetOnConnStop() func(IConnection)
30+
31+
// GetPacket Get the data protocol packet binding method for this Client
32+
// (获取Client绑定的数据协议封包方式)
33+
GetPacket() IDataPack
34+
35+
// SetPacket Set the data protocol packet binding method for this Client
36+
// (设置Client绑定的数据协议封包方式)
37+
SetPacket(IDataPack)
38+
39+
// GetMsgHandler Get the message handling module bound to this Client
40+
// (获取Client绑定的消息处理模块)
41+
GetMsgHandler() IMsgHandle
42+
43+
// StartHeartBeat Start heartbeat detection(启动心跳检测)
44+
StartHeartBeat(time.Duration)
45+
46+
// StartHeartBeatWithOption Start heartbeat detection with custom callbacks 启动心跳检测(自定义回调)
47+
StartHeartBeatWithOption(time.Duration, *HeartBeatOption)
48+
49+
// GetLengthField Get the length field of this Client
2350
GetLengthField() *LengthField
51+
52+
// SetDecoder Set the decoder for this Client 设置解码器
2453
SetDecoder(IDecoder)
54+
55+
// AddInterceptor Add an interceptor for this Client 添加拦截器
2556
AddInterceptor(IInterceptor)
26-
GetErrChan() chan error // 获取客户端错误管道
57+
58+
// Get the error channel for this Client 获取客户端错误管道
59+
GetErrChan() chan error
60+
61+
// Set the name of this Clien
62+
// 设置客户端Client名称
2763
SetName(string)
64+
65+
// Get the name of this Client
66+
// 获取客户端Client名称
2867
GetName() string
2968
}

0 commit comments

Comments
 (0)