From c1e39e20ca42b21eeac8b5068fa1f921bf9a070f Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 23 六月 2021 19:43:29 +0800 Subject: [PATCH] refactor, start tcp pub/sub. --- box/tcp_server.cpp | 47 +++++++++-------------------------------------- 1 files changed, 9 insertions(+), 38 deletions(-) diff --git a/box/tcp_server.cpp b/box/tcp_server.cpp index e4e229c..5cd8743 100644 --- a/box/tcp_server.cpp +++ b/box/tcp_server.cpp @@ -23,48 +23,19 @@ using namespace std::chrono_literals; -TcpServer::TcpServer(int port) : - run_(false), listener_(io_, tcp::endpoint(tcp::v6(), port)) -{ - Accept(); -} - -TcpServer::~TcpServer() { Stop(); } - -bool TcpServer::Start() -{ - Stop(); - bool cur = false; - if (run_.compare_exchange_strong(cur, true)) { - auto proc = [this]() { - while (run_) { - io_.run_one_for(100ms); - } - }; - std::thread(proc).swap(worker_); - } -} -void TcpServer::Stop() -{ - bool cur = true; - if (run_.compare_exchange_strong(cur, false)) { - io_.post([this]() { - listener_.close(); - }); - std::this_thread::sleep_for(1s); - if (worker_.joinable()) { - worker_.join(); - } - } -} - void TcpServer::Accept() { listener_.async_accept([this](bserror_t ec, tcp::socket sock) { if (!ec) { - LOG_INFO() << "server accept client"; - TcpReply1::Create(std::move(sock)); + LOG_TRACE() << "server accept client"; + TcpReply1::Create(std::move(sock), pscenter_); + Accept(); + } else { + // this is already destructed by now. + if (ec.value() != ECANCELED) { + LOG_WARNING() << "tcp server accept error: " << ec; + Accept(); + } } - Accept(); }); } \ No newline at end of file -- Gitblit v1.8.0