Fixed console commands startup

This commit is contained in:
SChernykh 2023-08-31 17:11:12 +02:00
parent 8e0f28ee30
commit 7986321a52
3 changed files with 60 additions and 39 deletions

View file

@ -47,8 +47,42 @@ ConsoleCommands::ConsoleCommands(p2pool* pool)
LOGINFO(3, "uv_guess_handle returned " << static_cast<int>(stdin_type)); LOGINFO(3, "uv_guess_handle returned " << static_cast<int>(stdin_type));
if (stdin_type != UV_TTY && stdin_type != UV_NAMED_PIPE) { if (stdin_type != UV_TTY && stdin_type != UV_NAMED_PIPE) {
LOGERR(1, "tty or named pipe is not available"); LOGERR(1, "tty or named pipe is not available");
}
int err;
if (stdin_type == UV_TTY) {
LOGINFO(3, "processing stdin as UV_TTY");
err = uv_tty_init(&m_loop, &m_tty, 0, 1);
if (err) {
LOGERR(1, "uv_tty_init failed, error " << uv_err_name(err));
throw std::exception(); throw std::exception();
} }
m_stdin_handle = reinterpret_cast<uv_stream_t*>(&m_tty);
}
else if (stdin_type == UV_NAMED_PIPE) {
LOGINFO(3, "processing stdin as UV_NAMED_PIPE");
err = uv_pipe_init(&m_loop, &m_stdin_pipe, 0);
if (err) {
LOGERR(1, "uv_pipe_init failed, error " << uv_err_name(err));
throw std::exception();
}
m_stdin_handle = reinterpret_cast<uv_stream_t*>(&m_stdin_pipe);
err = uv_pipe_open(&m_stdin_pipe, 0);
if (err) {
LOGERR(1, "uv_pipe_open failed, error " << uv_err_name(err));
throw std::exception();
}
}
if (m_stdin_handle) {
m_stdin_handle->data = this;
err = uv_read_start(m_stdin_handle, allocCallback, stdinReadCallback);
if (err) {
LOGERR(1, "uv_read_start failed, error " << uv_err_name(err));
throw std::exception();
}
}
std::random_device rd; std::random_device rd;
@ -67,43 +101,20 @@ ConsoleCommands::ConsoleCommands(p2pool* pool)
m_pool->api()->set(p2pool_api::Category::LOCAL, "console", m_pool->api()->set(p2pool_api::Category::LOCAL, "console",
[stdin_type, this](log::Stream& s) [stdin_type, this](log::Stream& s)
{ {
s << "{\"mode\":" << ((stdin_type == UV_TTY) ? "\"tty\"" : "\"pipe\"") s << "{\"mode\":\"";
<< ",\"tcp_port\":" << m_listenPort
<< "}";
});
}
int err;
if (stdin_type == UV_TTY) { if (stdin_type == UV_TTY) {
LOGINFO(3, "processing stdin as UV_TTY"); s << "tty";
err = uv_tty_init(&m_loop, &m_tty, 0, 1);
if (err) {
LOGERR(1, "uv_tty_init failed, error " << uv_err_name(err));
throw std::exception();
} }
m_stdin_handle = reinterpret_cast<uv_stream_t*>(&m_tty); else if (stdin_type == UV_NAMED_PIPE) {
s << "pipe";
} }
else { else {
LOGINFO(3, "processing stdin as UV_NAMED_PIPE"); s << static_cast<int>(stdin_type);
err = uv_pipe_init(&m_loop, &m_stdin_pipe, 0);
if (err) {
LOGERR(1, "uv_pipe_init failed, error " << uv_err_name(err));
throw std::exception();
} }
m_stdin_handle = reinterpret_cast<uv_stream_t*>(&m_stdin_pipe);
err = uv_pipe_open(&m_stdin_pipe, 0);
if (err) {
LOGERR(1, "uv_pipe_open failed, error " << uv_err_name(err));
throw std::exception();
}
}
m_stdin_handle->data = this;
err = uv_read_start(m_stdin_handle, allocCallback, stdinReadCallback); s << "\",\"tcp_port\":" << m_listenPort << '}';
if (err) { });
LOGERR(1, "uv_read_start failed, error " << uv_err_name(err));
throw std::exception();
} }
err = uv_thread_create(&m_loopThread, loop, this); err = uv_thread_create(&m_loopThread, loop, this);
@ -111,6 +122,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_loopThreadCreated = true;
} }
ConsoleCommands::~ConsoleCommands() ConsoleCommands::~ConsoleCommands()
@ -120,8 +133,10 @@ ConsoleCommands::~ConsoleCommands()
void ConsoleCommands::on_shutdown() void ConsoleCommands::on_shutdown()
{ {
if (m_stdin_handle) {
uv_close(reinterpret_cast<uv_handle_t*>(m_stdin_handle), nullptr); uv_close(reinterpret_cast<uv_handle_t*>(m_stdin_handle), nullptr);
} }
}
const char* ConsoleCommands::get_log_category() const const char* ConsoleCommands::get_log_category() const
{ {

View file

@ -27,6 +27,7 @@ TCPServer::TCPServer(int default_backlog, allocate_client_callback allocate_new_
: m_allocateNewClient(allocate_new_client) : m_allocateNewClient(allocate_new_client)
, m_defaultBacklog(default_backlog) , m_defaultBacklog(default_backlog)
, m_loopThread{} , m_loopThread{}
, m_loopThreadCreated(false)
#ifdef WITH_UPNP #ifdef WITH_UPNP
, m_portMapping(0) , m_portMapping(0)
#endif #endif
@ -250,6 +251,8 @@ void TCPServer::start_listening(const std::string& listen_addresses, bool upnp)
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));
PANIC_STOP(); PANIC_STOP();
} }
m_loopThreadCreated = true;
} }
bool TCPServer::connect_to_peer(bool is_v6, const char* ip, int port) bool TCPServer::connect_to_peer(bool is_v6, const char* ip, int port)
@ -473,7 +476,9 @@ void TCPServer::shutdown_tcp()
} }
#endif #endif
if (m_loopThreadCreated) {
uv_thread_join(&m_loopThread); uv_thread_join(&m_loopThread);
}
uv_mutex_destroy(&m_bansLock); uv_mutex_destroy(&m_bansLock);

View file

@ -149,6 +149,7 @@ protected:
int m_defaultBacklog; int m_defaultBacklog;
uv_thread_t m_loopThread; uv_thread_t m_loopThread;
std::atomic<bool> m_loopThreadCreated;
static void loop(void* data); static void loop(void* data);