2 #include "RedisInternal.h" 6 data = client.readStringUntil(
'\r');
12 String emitStr((
char)_type);
15 data.replace(CRLF, F(
""));
25 auto dLen = String(data).toInt();
26 auto charBuf =
new char[dLen + 1];
27 bzero(charBuf, dLen + 1);
29 auto crlfCstr = String(CRLF).c_str();
30 if (client.readBytes(charBuf, dLen) != dLen || client.find(crlfCstr, 2)) {
31 Serial.printf(
"ERROR! Bad read\n");
35 data = String(charBuf);
41 String emitStr((
char)_type);
42 emitStr += String(data.length());
51 String emitStr((
char)_type);
52 emitStr += String(vec.size());
54 for (
auto rTypeInst : vec) {
55 emitStr += rTypeInst->RESP();
62 if (!cmdClient.connected())
63 return std::shared_ptr<RedisObject>(
new RedisInternalError(
"Client is not connected"));
65 auto cmdRespStr =
RESP();
66 #if ARDUINO_REDIS_SERIAL_TRACE 67 Serial.printf(
"----- CMD ----\n%s---- /CMD ----\n", cmdRespStr.c_str());
69 cmdClient.print(cmdRespStr);
70 auto ret = RedisObject::parseType(cmdClient);
71 if (ret && ret->type() == RedisObject::Type::InternalError)
77 int RedisCommand::issue_typed<int>(Client& cmdClient)
79 auto cmdRet = issue(cmdClient);
81 return INT_MAX - 0x0f;
82 if (cmdRet->type() != RedisObject::Type::Integer)
83 return INT_MAX - 0xf0;
88 bool RedisCommand::issue_typed<bool>(Client& cmdClient)
90 auto cmdRet = issue(cmdClient);
91 if (cmdRet && cmdRet->type() == RedisObject::Type::Integer)
97 String RedisCommand::issue_typed<String>(Client& cmdClient)
99 return (String)*issue(cmdClient);
102 typedef std::map<RedisObject::Type, std::function<RedisObject*(Client&)>> TypeParseMap;
104 static TypeParseMap g_TypeParseMap {
105 { RedisObject::Type::SimpleString, [](Client& c) {
return new RedisSimpleString(c); } },
106 { RedisObject::Type::BulkString, [](Client& c) {
return new RedisBulkString(c); } },
107 { RedisObject::Type::Integer, [](Client& c) {
return new RedisInteger(c); } },
108 { RedisObject::Type::Array, [](Client& c) {
return new RedisArray(c); } },
109 { RedisObject::Type::Error, [](Client& c) {
return new RedisError(c); } }
112 std::shared_ptr<RedisObject> RedisObject::parseType(Client& client)
114 if (client.connected()) {
115 while (!client.available()) {
121 #if ARDUINO_REDIS_SERIAL_TRACE 122 Serial.printf(
"Parsed type character '%c' (0x%x)\n", typeChar, typeChar);
125 if (g_TypeParseMap.find(typeChar) != g_TypeParseMap.end()) {
126 auto retVal = g_TypeParseMap[typeChar](client);
128 if (!retVal || retVal->type() == RedisObject::Type::Error) {
129 String err = retVal ? (String)*retVal :
"(nil)";
133 return std::shared_ptr<RedisObject>(retVal);
136 return std::shared_ptr<RedisObject>(
new RedisInternalError(
"Unable to find type: " + typeChar));
virtual String RESP() override
std::shared_ptr< RedisObject > issue(Client &cmdClient)
virtual String RESP() override
virtual void init(Client &client) override
virtual String RESP() override
virtual void init(Client &client)