Console: read commands via localhost TCP

This commit is contained in:
SChernykh
2023-04-18 15:38:24 +02:00
parent ce192670a3
commit 196b27f3b2
4 changed files with 192 additions and 158 deletions

View File

@@ -18,43 +18,50 @@
#pragma once
#include "uv_util.h"
#include "tcp_server.h"
namespace p2pool {
class p2pool;
class ConsoleCommands : public nocopy_nomove
class ConsoleCommands : public TCPServer<256, 256>
{
public:
explicit ConsoleCommands(p2pool* pool);
~ConsoleCommands();
struct ConsoleClient : public Client
{
~ConsoleClient() {}
static Client* allocate() { return new ConsoleClient(); }
size_t size() const override { return sizeof(ConsoleClient); }
bool on_connect() override { return true; };
bool on_read(char* data, uint32_t size) override { static_cast<ConsoleCommands*>(m_owner)->process_input(m_command, data, size); return true; };
std::string m_command;
};
void on_shutdown() override;
private:
p2pool* m_pool;
uv_loop_t m_loop;
uv_async_t m_shutdownAsync;
uv_tty_t m_tty;
uv_pipe_t m_stdin_pipe;
uv_stream_t* m_stdin_handle;
uv_thread_t m_loopThread;
char m_readBuf[64];
bool m_readBufInUse;
std::string m_command;
static void loop(void* data);
static void on_shutdown(uv_async_t* async)
{
ConsoleCommands* pThis = reinterpret_cast<ConsoleCommands*>(async->data);
uv_close(reinterpret_cast<uv_handle_t*>(&pThis->m_shutdownAsync), nullptr);
uv_close(reinterpret_cast<uv_handle_t*>(pThis->m_stdin_handle), nullptr);
}
static void allocCallback(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
static void stdinReadCallback(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
void process_input(std::string& command, char* data, uint32_t size);
};
} // namespace p2pool