From 10b82da4fd22c83c42f3401006d4b08cf59b8f59 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Tue, 9 Sep 2025 17:31:11 +0800 Subject: [PATCH] fix: asio::ip::tcp::acceptor need call listen() --- src/main.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.cc b/src/main.cc index d28028f..f126dba 100644 --- a/src/main.cc +++ b/src/main.cc @@ -20,7 +20,8 @@ initLog() class RedisServer { public: RedisServer(asio::ip::tcp::endpoint ep = asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 6379), int io_thread_num = 2) - : _io_context(), + : _running(false), + _io_context(), _io_context_guard(asio::make_work_guard(_io_context)), _io_threads(), _acceptor(_io_context, ep) @@ -30,10 +31,13 @@ public: } } - bool running() const { return true; } + ~RedisServer() { _io_threads.join_all(); } + + bool running() const { return _running; } bool startServer() { + _acceptor.listen(1024); LOGI("Listen {}:{}", _acceptor.local_endpoint().address().to_string(), _acceptor.local_endpoint().port()); startAccept(); return true; @@ -46,6 +50,7 @@ private: if (!ec) { auto remote_ep = new_socket.remote_endpoint(); LOGI("Connected to Server, From {}:{}", remote_ep.address().to_string(), remote_ep.port()); + _io_context.stop(); } else { LOGE("Accept failed. reason={}", ec.message()); } @@ -55,6 +60,7 @@ private: } private: + std::atomic _running; asio::io_context _io_context; asio::executor_work_guard _io_context_guard; thread_group _io_threads;