-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessage.h
88 lines (80 loc) · 1.85 KB
/
Message.h
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
#include <vector>
#include <string>
#ifndef NET_MESSAGE_
#define NET_MESSAGE_
namespace redis {
class Message {
public:
Message() {
_client_id = -1;
}
Message(int client_id) {
_client_id = client_id;
}
Message(const std::vector<std::string>& vals) {
_vals = vals;
}
Message(int client_id, const std::vector<std::string>& vals) {
_client_id = client_id;
_vals = vals;
}
int ClientId() const {
return _client_id;
}
void SetClientId(int client_id) {
_client_id = client_id;
}
std::string Cmd() const {
if (_vals.size() > 0) {
return _vals[0];
}
return "";
}
void SetCmd(const std::string cmd) {
if (_vals.empty()) {
_vals.resize(1);
}
_vals[0] = cmd;
}
std::string Key() const {
if (_vals.size() > 1) {
return _vals[1];
}
return "";
}
std::string Val() const {
if (_vals.size() > 2) {
return _vals[2];
}
return "";
}
std::vector<std::string> Array() const {
return _vals;
}
std::vector<std::string> Args() const {
return Args(0);
}
std::vector<std::string> Args(size_t offset) const {
std::vector<std::string> ret;
if (_vals.size() > offset) {
ret.assign(_vals.begin() + offset + 1, _vals.end());
}
return ret;
}
std::string Arg(int idx) const {
idx += 1;
if ((int)_vals.size() > idx) {
return _vals[idx];
}
return "";
}
std::string Encode() const;
// 返回解析了多少字节
int Decode(const std::string& buf);
int Decode(const char* data, int len);
private:
int _client_id = -1;
std::vector<std::string> _vals;
};
}; // namespace redis
#endif