ClassNetHandle.cs
7.86 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*************************************************************************************
*
* 文件名:ClassNetHandle.cs
* 功能: 网络通信头,标识消息的内容
*
* 这个类公开在HslCommunication下面
*
*************************************************************************************/
namespace HslCommunication
{
/// <summary>
/// 用于网络传递的信息头,使用上等同于int
/// </summary>
/// <remarks>
/// 通常用于<see cref="Enthernet.NetComplexServer"/>和<see cref="Enthernet.NetComplexClient"/>之间的通信,以及<see cref="Enthernet.NetSimplifyServer"/>和<see cref="Enthernet.NetSimplifyClient"/>通讯
/// </remarks>
/// <example>
/// 使用上等同于int,只是本结构体允许将4字节的int拆分成3部分单独访问
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Core\NetHandle.cs" region="NetHandleExample" title="NetHandle示例" />
/// </example>
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
public struct NetHandle
{
#region Implicit Support
/// <summary>
/// 赋值操作,可以直接赋值int数据
/// </summary>
/// <param name="value">int数值</param>
/// <returns>等值的消息对象</returns>
public static implicit operator NetHandle(int value)
{
return new NetHandle(value);
}
/// <summary>
/// 也可以赋值给int数据
/// </summary>
/// <param name="netHandle">netHandle对象</param>
/// <returns>等值的消息对象</returns>
public static implicit operator int(NetHandle netHandle)
{
return netHandle.m_CodeValue;
}
#endregion
#region Operator Support
/// <summary>
/// 判断是否相等
/// </summary>
/// <param name="netHandle1">第一个数</param>
/// <param name="netHandle2">第二个数</param>
/// <returns>等于返回<c>True</c>,否则<c>False</c></returns>
public static bool operator ==(NetHandle netHandle1, NetHandle netHandle2)
{
return netHandle1.CodeValue == netHandle2.CodeValue;
}
/// <summary>
/// 判断是否不相等
/// </summary>
/// <param name="netHandle1">第一个对象</param>
/// <param name="netHandle2">第二个对象</param>
/// <returns>等于返回<c>False</c>,否则<c>True</c></returns>
public static bool operator !=(NetHandle netHandle1, NetHandle netHandle2)
{
return netHandle1.CodeValue != netHandle2.CodeValue;
}
/// <summary>
/// 两个数值相加
/// </summary>
/// <param name="netHandle1">第一个对象</param>
/// <param name="netHandle2">第二个对象</param>
/// <returns>返回两个指令的和</returns>
public static NetHandle operator +(NetHandle netHandle1, NetHandle netHandle2)
{
return new NetHandle(netHandle1.CodeValue + netHandle2.CodeValue);
}
/// <summary>
/// 两个数值相减
/// </summary>
/// <param name="netHandle1">第一个对象</param>
/// <param name="netHandle2">第二个对象</param>
/// <returns>返回两个指令的差</returns>
public static NetHandle operator -(NetHandle netHandle1, NetHandle netHandle2)
{
return new NetHandle(netHandle1.CodeValue - netHandle2.CodeValue);
}
/// <summary>
/// 判断是否小于另一个数值
/// </summary>
/// <param name="netHandle1">第一个对象</param>
/// <param name="netHandle2">第二个对象</param>
/// <returns>小于则返回<c>True</c>,否则返回<c>False</c></returns>
public static bool operator <(NetHandle netHandle1, NetHandle netHandle2)
{
return netHandle1.CodeValue < netHandle2.CodeValue;
}
/// <summary>
/// 判断是否大于另一个数值
/// </summary>
/// <param name="netHandle1">第一个对象</param>
/// <param name="netHandle2">第二个对象</param>
/// <returns>大于则返回<c>True</c>,否则返回<c>False</c></returns>
public static bool operator >(NetHandle netHandle1, NetHandle netHandle2)
{
return netHandle1.CodeValue > netHandle2.CodeValue;
}
#endregion
#region Constructor
/// <summary>
/// 初始化一个暗号对象
/// </summary>
/// <param name="value">使用一个默认的数值进行初始化</param>
public NetHandle(int value)
{
m_CodeMajor = 0;
m_CodeMinor = 0;
m_CodeIdentifier = 0;
m_CodeValue = value;
}
/// <summary>
/// 根据三个值来初始化暗号对象
/// </summary>
/// <param name="major">主暗号</param>
/// <param name="minor">次暗号</param>
/// <param name="identifier">暗号编号</param>
public NetHandle(byte major, byte minor, ushort identifier)
{
m_CodeValue = 0;
m_CodeMajor = major;
m_CodeMinor = minor;
m_CodeIdentifier = identifier;
}
#endregion
#region Private Members
/// <summary>
/// 完整的暗号值
/// </summary>
[System.Runtime.InteropServices.FieldOffset(0)]
private int m_CodeValue;
/// <summary>
/// 主暗号分类0-255
/// </summary>
[System.Runtime.InteropServices.FieldOffset(3)]
private byte m_CodeMajor;
/// <summary>
/// 次要的暗号分类0-255
/// </summary>
[System.Runtime.InteropServices.FieldOffset(2)]
private byte m_CodeMinor;
/// <summary>
/// 暗号的编号分类0-65535
/// </summary>
[System.Runtime.InteropServices.FieldOffset(0)]
private ushort m_CodeIdentifier;
#endregion
#region Public Members
/// <summary>
/// 完整的暗号值
/// </summary>
public int CodeValue { get => m_CodeValue; set => m_CodeValue = value; }
/// <summary>
/// 主暗号分类0-255
/// </summary>
public byte CodeMajor { get => m_CodeMajor; private set => m_CodeMajor = value; }
/// <summary>
/// 次要的暗号分类0-255
/// </summary>
public byte CodeMinor { get => m_CodeMinor; private set => m_CodeMinor = value; }
/// <summary>
/// 暗号的编号分类0-65535
/// </summary>
public ushort CodeIdentifier { get => m_CodeIdentifier; private set => m_CodeIdentifier = value; }
#endregion
#region Object Override
/// <summary>
/// 获取完整的暗号数据
/// </summary>
/// <returns>返回暗号的字符串表示形式</returns>
public override string ToString()
{
return m_CodeValue.ToString();
}
/// <summary>
/// 判断两个实例是否相同
/// </summary>
/// <param name="obj">对比的对象</param>
/// <returns>相同返回<c>True</c>,否则返回<c>False</c></returns>
public override bool Equals(object obj)
{
if (obj is NetHandle headCode)
{
return CodeValue.Equals(headCode.CodeValue);
}
else
{
return false;
}
}
/// <summary>
/// 获取哈希值
/// </summary>
/// <returns>返回当前对象的哈希值</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
#endregion
}
}