forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedisCommand.cs
104 lines (84 loc) · 2.62 KB
/
RedisCommand.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CYQ.Data.Cache
{
/*
协议规范
redis允许客户端以TCP方式连接,默认6379端口。传输数据都以\r\n结尾。
请求格式
*<number of arguments>\r\n$<number of bytes of argument 1>\r\n<argument data>\r\n
例:*1\r\n$4\r\nINFO\r\n
响应格式
1:简单字符串,非二进制安全字符串,一般是状态回复。 +开头,例:+OK\r\n
2: 错误信息。 -开头, 例:-ERR unknown command 'mush'\r\n
3: 整型数字。 :开头, 例::1\r\n
4:大块回复值,最大512M。 $开头+数据长度。 例:$4\r\mush\r\n
5:多条回复。 *开头, 例:*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n
*/
/// <summary>
/// RedisCommand
/// </summary>
internal class RedisCommand : IDisposable
{
MSocket socket;
public RedisCommand(MSocket socket, int commandCount, string command)
{
this.socket = socket;
Write(string.Format("*{0}\r\n", commandCount));
WriteKey(command);
}
private void Write(string cmd)
{
WriteData(Encoding.UTF8.GetBytes(cmd));
}
private void WriteHeader(int bodyLen)
{
Write("$" + bodyLen + "\r\n");
}
public void WriteKey(string cmd)
{
byte[] data = Encoding.UTF8.GetBytes(cmd);
WriteHeader(data.Length);
WriteData(data);
Write("\r\n");
}
public void WriteValue(string value)
{
WriteKey(value);
}
public void WriteValue(byte[] data1)
{
WriteValue(data1, null);
}
public void WriteValue(byte[] data1, byte[] data2)
{
int len = data1.Length;
if (data2 != null)
{
len += data2.Length;
}
WriteHeader(len);
WriteData(data1);
if (data2 != null)
{
WriteData(data2);
}
Write("\r\n");
}
private void WriteData(byte[] cmd)
{
socket.Write(cmd);
}
public void Reset(int commandCount, string command)
{
Write(string.Format("*{0}\r\n", commandCount));
WriteKey(command);
}
public void Dispose()
{
}
}
}