HslMessage.cs
2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
namespace HslCommunication.Core.IMessage
{
/// <summary>
/// 本组件系统使用的默认的消息规则,说明解析和反解析规则的
/// </summary>
public class HslMessage : INetMessage
{
/// <summary>
/// 本协议的消息头长度
/// </summary>
public int ProtocolHeadBytesLength
{
get { return 32; }
}
/// <summary>
/// 头子节信息
/// </summary>
public byte[] HeadBytes { get; set; }
/// <summary>
/// 内容字节信息
/// </summary>
public byte[] ContentBytes { get; set; }
/// <summary>
/// 检查接收的数据是否合法
/// </summary>
/// <param name="token">令牌</param>
/// <returns>是否合法</returns>
public bool CheckHeadBytesLegal(byte[] token)
{
if (HeadBytes == null) return false;
if (HeadBytes?.Length >= 32)
{
return BasicFramework.SoftBasic.IsTwoBytesEquel(HeadBytes, 12, token, 0, 16);
}
else
{
return false;
}
}
/// <summary>
/// 从头子节信息中解析出接下来需要接收的数据长度
/// </summary>
/// <returns>接下来的数据长度</returns>
public int GetContentLengthByHeadBytes()
{
if (HeadBytes?.Length >= 32)
{
return BitConverter.ToInt32(HeadBytes, 28);
}
else
{
return 0;
}
}
/// <summary>
/// 获取头子节里的特殊标识
/// </summary>
/// <returns>标识信息</returns>
public int GetHeadBytesIdentity()
{
if (HeadBytes?.Length >= 32)
{
return BitConverter.ToInt32(HeadBytes, 4);
}
else
{
return 0;
}
}
/// <summary>
/// 发送的字节信息
/// </summary>
public byte[] SendBytes { get; set; }
}
}