forked from electric-sheep-co/arduino-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedisInternal.cpp
161 lines (136 loc) · 4.32 KB
/
RedisInternal.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
#include "RedisInternal.h"
#include <map>
#include <limits.h>
void RedisObject::init(Client &client)
{
data = client.readStringUntil('\r');
client.read(); // discard '\n'
}
String RedisSimpleString::RESP()
{
String emitStr((char)_type);
// Simple strings cannot contain CRLF, as they must terminate with CRLF
// https://redis.io/topics/protocol#resp-simple-strings
data.replace(CRLF, F(""));
emitStr += data;
emitStr += CRLF;
return emitStr;
}
void RedisBulkString::init(Client &client)
{
auto dLen = data.toInt();
// "Null Bulk String" -- https://redis.io/topics/protocol#resp-bulk-strings
if (dLen == -1)
{
data = (const char *)nullptr;
return;
}
auto charBuf = new char[dLen + 1];
bzero(charBuf, dLen + 1);
auto readB = client.readBytes(charBuf, dLen);
if ((int)readB != dLen)
{
Serial.printf("ERROR! Bad read (%ld ?= %ld)\n", (long)readB, (long)dLen);
exit(-1);
}
data = String(charBuf);
delete[] charBuf;
}
String RedisBulkString::RESP()
{
String emitStr((char)_type);
emitStr += String(data.length());
emitStr += CRLF;
emitStr += data;
emitStr += CRLF;
return emitStr;
}
void RedisArray::init(Client &client)
{
for (int i = 0; i < data.toInt(); i++)
add(RedisObject::parseType(client));
}
RedisArray::operator std::vector<String>() const
{
std::vector<String> rv;
for (auto ro : vec)
rv.push_back((String)*ro.get());
return rv;
}
String RedisArray::RESP()
{
String emitStr((char)_type);
emitStr += String(vec.size());
emitStr += CRLF;
for (auto rTypeInst : vec)
{
emitStr += rTypeInst->RESP();
}
return emitStr;
}
std::shared_ptr<RedisObject> RedisCommand::issue(Client &cmdClient)
{
if (!cmdClient.connected())
return std::shared_ptr<RedisObject>(new RedisInternalError(RedisInternalError::Disconnected));
auto cmdRespStr = RESP();
cmdClient.print(cmdRespStr);
auto ret = RedisObject::parseType(cmdClient);
if (ret && ret->type() == RedisObject::Type::InternalError)
_err = (String)*ret;
return ret;
}
template <>
int RedisCommand::issue_typed<int>(Client &cmdClient)
{
auto cmdRet = issue(cmdClient);
if (!cmdRet)
return INT_MAX - 0x0f;
if (cmdRet->type() != RedisObject::Type::Integer)
return INT_MAX - 0xf0;
return (int)*((RedisInteger *)cmdRet.get());
}
template <>
bool RedisCommand::issue_typed<bool>(Client &cmdClient)
{
auto cmdRet = issue(cmdClient);
if (cmdRet && cmdRet->type() == RedisObject::Type::Integer)
return (bool)*((RedisInteger *)cmdRet.get());
return false;
}
template <>
String RedisCommand::issue_typed<String>(Client &cmdClient)
{
return (String)*issue(cmdClient);
}
typedef std::map<RedisObject::Type, std::function<RedisObject *(Client &)>> TypeParseMap;
static TypeParseMap g_TypeParseMap{
{RedisObject::Type::SimpleString, [](Client &c) { return new RedisSimpleString(c); }},
{RedisObject::Type::BulkString, [](Client &c) { return new RedisBulkString(c); }},
{RedisObject::Type::Integer, [](Client &c) { return new RedisInteger(c); }},
{RedisObject::Type::Array, [](Client &c) { return new RedisArray(c); }},
{RedisObject::Type::Error, [](Client &c) { return new RedisError(c); }}};
std::shared_ptr<RedisObject> RedisObject::parseType(Client &client)
{
while (client.connected() && !client.available())
;
RedisObject::Type typeChar = RedisObject::Type::NoType;
do
{
if (!client.connected())
{
return std::shared_ptr<RedisObject>(new RedisInternalError(RedisInternalError::Disconnected));
}
typeChar = (RedisObject::Type)client.read();
} while (typeChar == -1 || typeChar == '\r' || typeChar == '\n');
if (g_TypeParseMap.find(typeChar) != g_TypeParseMap.end())
{
auto retVal = g_TypeParseMap[typeChar](client);
if (!retVal || retVal->type() == RedisObject::Type::Error)
{
String err = retVal ? (String)*retVal : "(nil)";
return std::shared_ptr<RedisObject>(new RedisInternalError(RedisInternalError::UnknownError, err));
}
return std::shared_ptr<RedisObject>(retVal);
}
return std::shared_ptr<RedisObject>(new RedisInternalError(RedisInternalError::UnknownType, String(typeChar)));
}