Fixed processing of long console commands

This commit is contained in:
SChernykh 2022-08-05 09:04:16 +02:00
parent be0e220fe4
commit 3dde71f1ff
2 changed files with 27 additions and 16 deletions

View file

@ -76,6 +76,8 @@ ConsoleCommands::ConsoleCommands(p2pool* pool)
LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err)); LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err));
throw std::exception(); throw std::exception();
} }
m_command.reserve(64);
} }
ConsoleCommands::~ConsoleCommands() ConsoleCommands::~ConsoleCommands()
@ -252,25 +254,32 @@ void ConsoleCommands::stdinReadCallback(uv_stream_t* stream, ssize_t nread, cons
ConsoleCommands* pThis = static_cast<ConsoleCommands*>(stream->data); ConsoleCommands* pThis = static_cast<ConsoleCommands*>(stream->data);
if (nread > 0) { if (nread > 0) {
for (size_t i = 0; i < static_cast<size_t>(nread); ++i) { std::string& command = pThis->m_command;
if ((buf->base[i] == '\r') || (buf->base[i] == '\n')) { command.append(buf->base, nread);
buf->base[i] = '\0';
cmd* c = cmds; do {
for (; c->name.len; ++c) { size_t k = command.find_first_of("\r\n");
if (!strncmp(buf->base, c->name.str, c->name.len)) { if (k == std::string::npos) {
const char* args = (c->name.len + 1 <= i) ? (buf->base + c->name.len + 1) : "";
c->func(pThis->m_pool, args);
break;
}
}
if (!c->name.len) {
LOGWARN(0, "Unknown command " << buf->base);
}
break; break;
} }
} command[k] = '\0';
cmd* c = cmds;
for (; c->name.len; ++c) {
if (!strncmp(command.c_str(), c->name.str, c->name.len)) {
const char* args = (c->name.len + 1 <= k) ? (command.c_str() + c->name.len + 1) : "";
c->func(pThis->m_pool, args);
break;
}
}
if (!c->name.len) {
LOGWARN(0, "Unknown command " << command);
}
k = command.find_first_not_of("\r\n", k + 1);
command.erase(0, k);
} while (true);
} }
else if (nread < 0) { else if (nread < 0) {
LOGWARN(4, "read error " << uv_err_name(static_cast<int>(nread))); LOGWARN(4, "read error " << uv_err_name(static_cast<int>(nread)));

View file

@ -40,6 +40,8 @@ private:
char m_readBuf[64]; char m_readBuf[64];
bool m_readBufInUse; bool m_readBufInUse;
std::string m_command;
static void loop(void* data); static void loop(void* data);
static void on_shutdown(uv_async_t* async) static void on_shutdown(uv_async_t* async)