RedisHelper.cs
11.6 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using HslCommunication.Core;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
namespace HslCommunication.Enthernet.Redis
{
/// <summary>
/// 提供了redis辅助类的一些方法
/// </summary>
public class RedisHelper
{
#region Socket Helper
/// <summary>
/// 接收一行命令数据
/// </summary>
/// <param name="socket">网络套接字</param>
/// <returns>带有结果对象的数据信息</returns>
public static OperateResult<byte[]> ReceiveCommandLine(Socket socket)
{
return NetSupport.ReceiveCommandLineFromSocket(socket, (byte)'\n');
}
/// <summary>
/// 接收一行字符串的信息
/// </summary>
/// <param name="socket">网络套接字</param>
/// <param name="length">字符串的长度</param>
/// <returns>带有结果对象的数据信息</returns>
public static OperateResult<byte[]> ReceiveCommandString(Socket socket, int length)
{
try
{
List<byte> bufferArray = new List<byte>();
bufferArray.AddRange(NetSupport.ReadBytesFromSocket(socket, length));
OperateResult<byte[]> commandTail = ReceiveCommandLine(socket);
if (!commandTail.IsSuccess) return commandTail;
bufferArray.AddRange(commandTail.Content);
return OperateResult.CreateSuccessResult(bufferArray.ToArray());
}
catch (Exception ex)
{
return new OperateResult<byte[]>(ex.Message);
}
}
/// <summary>
/// 从网络接收一条redis消息
/// </summary>
/// <param name="socket">网络套接字</param>
/// <returns>接收的结果对象</returns>
public static OperateResult<byte[]> ReceiveCommand(Socket socket)
{
List<byte> bufferArray = new List<byte>();
OperateResult<byte[]> readCommandLine = ReceiveCommandLine(socket);
if (!readCommandLine.IsSuccess) return readCommandLine;
bufferArray.AddRange(readCommandLine.Content);
if (readCommandLine.Content[0] == '+' || readCommandLine.Content[0] == '-' || readCommandLine.Content[0] == ':')
{
// 状态回复,错误回复,整数回复
return OperateResult.CreateSuccessResult(bufferArray.ToArray());
}
else if (readCommandLine.Content[0] == '$')
{
// 批量回复,允许最大512M字节
OperateResult<int> lengthResult = GetNumberFromCommandLine(readCommandLine.Content);
if (!lengthResult.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(lengthResult);
if (lengthResult.Content < 0) return OperateResult.CreateSuccessResult(bufferArray.ToArray());
// 接收字符串信息
OperateResult<byte[]> receiveContent = ReceiveCommandString(socket, lengthResult.Content);
if (!receiveContent.IsSuccess) return receiveContent;
bufferArray.AddRange(receiveContent.Content);
return OperateResult.CreateSuccessResult(bufferArray.ToArray());
}
else if (readCommandLine.Content[0] == '*')
{
// 多参数的回复
OperateResult<int> lengthResult = GetNumberFromCommandLine(readCommandLine.Content);
if (!lengthResult.IsSuccess) return OperateResult.CreateFailedResult<byte[]>(lengthResult);
for (int i = 0; i < lengthResult.Content; i++)
{
OperateResult<byte[]> receiveCommand = ReceiveCommand(socket);
if (!receiveCommand.IsSuccess) return receiveCommand;
bufferArray.AddRange(receiveCommand.Content);
}
return OperateResult.CreateSuccessResult(bufferArray.ToArray());
}
else
{
return new OperateResult<byte[]>("Not Supported HeadCode: " + readCommandLine.Content[0]);
}
}
#endregion
#region Prase Helper
/// <summary>
/// 将字符串数组打包成一个redis的报文信息
/// </summary>
/// <param name="commands">字节数据信息</param>
/// <returns>结果报文信息</returns>
public static byte[] PackStringCommand(string[] commands)
{
StringBuilder sb = new StringBuilder();
sb.Append('*');
sb.Append(commands.Length.ToString());
sb.Append("\r\n");
for (int i = 0; i < commands.Length; i++)
{
sb.Append('$');
sb.Append(Encoding.UTF8.GetBytes(commands[i]).Length.ToString());
sb.Append("\r\n");
sb.Append(commands[i]);
sb.Append("\r\n");
}
return Encoding.UTF8.GetBytes(sb.ToString());
}
/// <summary>
/// 从原始的结果数据对象中提取出数字数据
/// </summary>
/// <param name="commandLine">原始的字节数据</param>
/// <returns>带有结果对象的数据信息</returns>
public static OperateResult<int> GetNumberFromCommandLine(byte[] commandLine)
{
try
{
string command = Encoding.UTF8.GetString(commandLine).TrimEnd('\r', '\n');
return OperateResult.CreateSuccessResult(Convert.ToInt32(command.Substring(1)));
}
catch (Exception ex)
{
return new OperateResult<int>(ex.Message);
}
}
/// <summary>
/// 从原始的结果数据对象中提取出数字数据
/// </summary>
/// <param name="commandLine">原始的字节数据</param>
/// <returns>带有结果对象的数据信息</returns>
public static OperateResult<long> GetLongNumberFromCommandLine(byte[] commandLine)
{
try
{
string command = Encoding.UTF8.GetString(commandLine).TrimEnd('\r', '\n');
return OperateResult.CreateSuccessResult(Convert.ToInt64(command.Substring(1)));
}
catch (Exception ex)
{
return new OperateResult<long>(ex.Message);
}
}
/// <summary>
/// 从结果的数据对象里提取字符串的信息
/// </summary>
/// <param name="commandLine">原始的字节数据</param>
/// <returns>带有结果对象的数据信息</returns>
public static OperateResult<string> GetStringFromCommandLine(byte[] commandLine)
{
try
{
if (commandLine[0] != '$') return new OperateResult<string>(Encoding.UTF8.GetString(commandLine));
// 先找到换行符
int index_start = -1;
int index_end = -1;
// 下面的判断兼容windows系统及linux系统
for (int i = 0; i < commandLine.Length; i++)
{
if (commandLine[i] == '\r' || commandLine[i] == '\n')
{
index_start = i;
}
if (commandLine[i] == '\n')
{
index_end = i;
break;
}
}
int length = Convert.ToInt32(Encoding.UTF8.GetString(commandLine, 1, index_start - 1));
if (length < 0) return new OperateResult<string>("(nil) None Value");
return OperateResult.CreateSuccessResult(Encoding.UTF8.GetString(commandLine, index_end + 1, length));
}
catch (Exception ex)
{
return new OperateResult<string>(ex.Message);
}
}
/// <summary>
/// 从redis的结果数据中分析出所有的字符串信息
/// </summary>
/// <param name="commandLine">结果数据</param>
/// <returns>带有结果对象的数据信息</returns>
public static OperateResult<string[]> GetStringsFromCommandLine(byte[] commandLine)
{
try
{
List<string> lists = new List<string>();
if (commandLine[0] != '*') return new OperateResult<string[]>(Encoding.UTF8.GetString(commandLine));
int index = 0;
for (int i = 0; i < commandLine.Length; i++)
{
if (commandLine[i] == '\r' || commandLine[i] == '\n')
{
index = i;
break;
}
}
int length = Convert.ToInt32(Encoding.UTF8.GetString(commandLine, 1, index - 1));
for (int i = 0; i < length; i++)
{
// 提取所有的字符串内容
int index_end = -1;
for (int j = index; j < commandLine.Length; j++)
{
if (commandLine[j] == '\n')
{
index_end = j;
break;
}
}
index = index_end + 1;
if (commandLine[index] == '$')
{
// 寻找子字符串
int index_start = -1;
for (int j = index; j < commandLine.Length; j++)
{
if (commandLine[j] == '\r' || commandLine[j] == '\n')
{
index_start = j;
break;
}
}
int stringLength = Convert.ToInt32(Encoding.UTF8.GetString(commandLine, index + 1, index_start - index - 1));
if (stringLength >= 0)
{
for (int j = index; j < commandLine.Length; j++)
{
if (commandLine[j] == '\n')
{
index_end = j;
break;
}
}
index = index_end + 1;
lists.Add(Encoding.UTF8.GetString(commandLine, index, stringLength));
index = index + stringLength;
}
else
{
lists.Add(null);
}
}
else
{
int index_start = -1;
for (int j = index; j < commandLine.Length; j++)
{
if (commandLine[j] == '\r' || commandLine[j] == '\n')
{
index_start = j;
break;
}
}
lists.Add(Encoding.UTF8.GetString(commandLine, index, index_start - index - 1));
}
}
return OperateResult.CreateSuccessResult(lists.ToArray());
}
catch (Exception ex)
{
return new OperateResult<string[]>(ex.Message);
}
}
#endregion
}
}