NetSimplifyClient.cs
11.1 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Text;
using HslCommunication.Core.Net;
using HslCommunication.Core.IMessage;
using HslCommunication.Core;
using System.Net.Sockets;
#if !NET35
using System.Threading.Tasks;
#endif
namespace HslCommunication.Enthernet
{
/// <summary>
/// 同步访问数据的客户端类,用于向服务器请求一些确定的数据信息
/// </summary>
/// <remarks>
/// 详细的使用说明,请参照博客<a href="http://www.cnblogs.com/dathlin/p/7697782.html">http://www.cnblogs.com/dathlin/p/7697782.html</a>
/// </remarks>
/// <example>
/// 此处贴上了Demo项目的服务器配置的示例代码
/// <code lang="cs" source="TestProject\HslCommunicationDemo\FormSimplifyNet.cs" region="FormSimplifyNet" title="FormSimplifyNet示例" />
/// </example>
public class NetSimplifyClient : NetworkDoubleBase<HslMessage, RegularByteTransform>
{
#region Constructor
/// <summary>
/// 实例化一个客户端的对象,用于和服务器通信
/// </summary>
/// <param name="ipAddress">服务器的ip地址</param>
/// <param name="port">服务器的端口号</param>
public NetSimplifyClient(string ipAddress, int port)
{
IpAddress = ipAddress;
Port = port;
}
/// <summary>
/// 实例化一个客户端对象,需要手动指定Ip地址和端口
/// </summary>
public NetSimplifyClient()
{
}
#endregion
#region Override NetworkDoubleBase
/// <summary>
/// 连接上服务器后需要进行的初始化操作,无论是否允许操作都要进行验证
/// </summary>
/// <param name="socket">网络套接字</param>
/// <returns>是否初始化成功,依据具体的协议进行重写</returns>
protected override OperateResult InitializationOnConnect(Socket socket)
{
if (isUseAccountCertificate)
{
return AccountCertificate(socket);
}
return OperateResult.CreateSuccessResult();
}
#endregion
/// <summary>
/// 客户端向服务器进行请求,请求字符串数据,忽略了自定义消息反馈
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public OperateResult<string> ReadFromServer(NetHandle customer, string send)
{
var read = ReadFromServerBase(HslProtocol.CommandBytes(customer, Token, send));
if (!read.IsSuccess) return OperateResult.CreateFailedResult<string>(read);
return OperateResult.CreateSuccessResult(Encoding.Unicode.GetString(read.Content));
}
/// <summary>
/// 客户端向服务器进行请求,请求字符串数组,忽略了自定义消息反馈
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public OperateResult<string[]> ReadFromServer(NetHandle customer, string[] send)
{
var read = ReadFromServerBase(HslProtocol.CommandBytes(customer, Token, send));
if (!read.IsSuccess) return OperateResult.CreateFailedResult<string[]>(read);
return OperateResult.CreateSuccessResult(HslProtocol.UnPackStringArrayFromByte(read.Content));
}
/// <summary>
/// 客户端向服务器进行请求,请求字节数据
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送的字节内容</param>
/// <returns>带返回消息的结果对象</returns>
public OperateResult<byte[]> ReadFromServer(NetHandle customer, byte[] send)
{
return ReadFromServerBase(HslProtocol.CommandBytes(customer, Token, send));
}
/// <summary>
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public OperateResult<NetHandle, string> ReadCustomerFromServer(NetHandle customer, string send)
{
var read = ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, Token, send));
if (!read.IsSuccess) return OperateResult.CreateFailedResult<NetHandle, string>(read);
return OperateResult.CreateSuccessResult(read.Content1, Encoding.Unicode.GetString(read.Content2));
}
/// <summary>
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public OperateResult<NetHandle, string[]> ReadCustomerFromServer(NetHandle customer, string[] send)
{
var read = ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, Token, send));
if (!read.IsSuccess) return OperateResult.CreateFailedResult<NetHandle, string[]>(read);
return OperateResult.CreateSuccessResult(read.Content1, HslProtocol.UnPackStringArrayFromByte(read.Content2));
}
/// <summary>
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public OperateResult<NetHandle, byte[]> ReadCustomerFromServer(NetHandle customer, byte[] send)
{
return ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, Token, send));
}
/// <summary>
/// 需要发送的底层数据
/// </summary>
/// <param name="send">需要发送的底层数据</param>
/// <returns>带返回消息的结果对象</returns>
private OperateResult<byte[]> ReadFromServerBase(byte[] send)
{
var read = ReadCustomerFromServerBase(send);
if (!read.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(read);
return OperateResult.CreateSuccessResult(read.Content2);
}
/// <summary>
/// 需要发送的底层数据
/// </summary>
/// <param name="send">需要发送的底层数据</param>
/// <returns>带返回消息的结果对象</returns>
private OperateResult<NetHandle, byte[]> ReadCustomerFromServerBase(byte[] send)
{
// 核心数据交互
var read = ReadFromCoreServer(send);
if (!read.IsSuccess) return OperateResult.CreateFailedResult<NetHandle, byte[]>(read);
// 提炼数据信息
byte[] headBytes = new byte[HslProtocol.HeadByteLength];
byte[] contentBytes = new byte[read.Content.Length - HslProtocol.HeadByteLength];
Array.Copy(read.Content, 0, headBytes, 0, HslProtocol.HeadByteLength);
if (contentBytes.Length > 0) Array.Copy(read.Content, HslProtocol.HeadByteLength, contentBytes, 0, read.Content.Length - HslProtocol.HeadByteLength);
if (BitConverter.ToInt32(headBytes, 0) == HslProtocol.ProtocolErrorMsg)
{
return new OperateResult<NetHandle, byte[]>(Encoding.ASCII.GetString(contentBytes));
}
int customer = BitConverter.ToInt32(headBytes, 4);
contentBytes = HslProtocol.CommandAnalysis(headBytes, contentBytes);
return OperateResult.CreateSuccessResult((NetHandle)customer, contentBytes);
}
#if !NET35
/// <summary>
/// 客户端向服务器进行异步请求,请求字符串数据
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
public Task<OperateResult<string>> ReadFromServerAsync(NetHandle customer, string send)
{
return Task.Run(() => ReadFromServer(customer, send));
}
/// <summary>
/// 客户端向服务器进行异步请求,请求字节数据
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送的字节内容</param>
/// <returns>带返回消息的结果对象</returns>
public Task<OperateResult<byte[]>> ReadFromServerAsync(NetHandle customer, byte[] send)
{
return Task.Run(() => ReadFromServer(customer, send));
}
/// <summary>
/// 客户端向服务器进行异步请求,请求字符串数据
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="sends">发送数据</param>
public Task<OperateResult<string[]>> ReadFromServerAsync(NetHandle customer, string[] sends)
{
return Task.Run(() => ReadFromServer(customer, sends));
}
/// <summary>
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public Task<OperateResult<NetHandle, string>> ReadCustomerFromServerAsync(NetHandle customer, string send)
{
return Task.Run(() => ReadCustomerFromServer(customer, send));
}
/// <summary>
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public Task<OperateResult<NetHandle, string[]>> ReadCustomerFromServerAsync(NetHandle customer, string[] send)
{
return Task.Run(() => ReadCustomerFromServer(customer, send));
}
/// <summary>
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
/// </summary>
/// <param name="customer">用户的指令头</param>
/// <param name="send">发送数据</param>
/// <returns>带返回消息的结果对象</returns>
public Task<OperateResult<NetHandle, byte[]>> ReadCustomerFromServerAsync(NetHandle customer, byte[] send)
{
return Task.Run(() => ReadCustomerFromServer(customer, send));
}
#endif
#region Object Override
/// <summary>
/// 获取本对象的字符串表示形式
/// </summary>
/// <returns>字符串信息</returns>
public override string ToString()
{
return $"NetSimplifyClient[{IpAddress}:{Port}]";
}
#endregion
}
}