-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessage.cpp
181 lines (164 loc) · 4.84 KB
/
Message.cpp
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
#include "Message.h"
namespace redis {
std::string Message::Encode() const {
std::string buf;
buf.push_back('*');
buf.append(std::to_string(_vals.size()));
buf.append("\r\n");
for (auto& p : _vals) {
buf.push_back('$');
buf.append(std::to_string(p.size()));
buf.append("\r\n");
buf.append(p);
buf.append("\r\n");
}
return buf;
}
int Message::Decode(const std::string& buf) {
return Decode(buf.data(), buf.size());
}
static int parse_plain(const char* data, int len, std::vector<std::string>* ret);
static int parse(const char* data, int len, std::vector<std::string>* ret);
int Message::Decode(const char* data, int len) {
return parse(data, len, &_vals);
}
static int parse(const char* data, int len, std::vector<std::string>* ret) {
ret->clear();
const int BULK = 0;
const int ARRAY = 1;
const int PLAIN = 2;
const int INIT = 0;
const int MARK = 1;
const int SIZE = 2;
const int DATA = 3;
int type = 0;
int bulk = 0;
int status = INIT;
int size = 0;
int s = 0;
int e = 0;
while (1) {
if (e == len) {
return 0;
}
if (status == INIT) {
char c = data[e];
if (c == '*') {
bulk = 0;
type = ARRAY;
status = SIZE;
} else if (c == '$') {
bulk = 1;
type = BULK;
status = SIZE;
} else if (isalpha(c)) {
type = PLAIN;
status = DATA;
} else if (isspace(c)) {
s++;
} else {
// error
return -1;
}
e++;
continue;
}
if (type == PLAIN) {
int n = parse_plain(data + s, len - s, ret);
if (n == 0) {
return 0;
}
return s + n;
} else {
if (status == MARK) {
int c = data[e];
if (c != '$') {
printf("%d error\n", __LINE__);
return -1;
}
e++;
size = 0;
status = SIZE;
} else if (status == SIZE) {
int c = data[e];
if (c == '\r') {
if (e > s && data[e - 1] == '\r') {
printf("%d error\n", __LINE__);
return -1;
}
} else if (c == '\n') {
if (type == ARRAY) {
bulk = size;
type = BULK;
status = MARK;
} else {
status = DATA;
}
s = e + 1;
} else if (isdigit(c)) {
if (e > s && data[e - 1] == '\r') {
printf("%d error\n", __LINE__);
return -1;
}
size = size * 10 + (c - '0');
} else {
printf("%d char: %d, error\n", __LINE__, c);
return -1;
}
e++;
} else {
int end = s + size;
if (end >= len) {
e = (int)len;
} else {
char c = data[end];
if (c == '\n') {
//
} else if (end < (int)len - 1 && c == '\r' && data[end + 1] == '\n') {
end += 1;
} else {
e = len;
if (e - s > size + 2) { // \r\n = 2
printf("%d error\n", __LINE__);
return -1;
}
continue;
}
std::string p(data + s, size);
ret->push_back(p);
status = MARK;
bulk--;
s = end + 1;
e = s;
if (bulk == 0) {
return e;
}
}
}
}
}
return 0;
}
static int parse_plain(const char* data, int len, std::vector<std::string>* ret) {
int s = 0;
for (int e = 0; e < len; e++) {
char c = data[e];
if (c == ' ') {
int size = e - s;
std::string p(data + s, size);
ret->push_back(p);
s = e + 1;
} else if (c == '\n') {
int size = e - s;
if (size > 0 && data[e - 1] == '\r') {
size -= 1;
}
std::string p(data + s, size);
ret->push_back(p);
s = e + 1;
return s;
}
}
return 0;
}
}; // namespace redis