From 58d904a328c0d849769b483e901a0be9426b8209 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期二, 20 七月 2021 20:20:44 +0800 Subject: [PATCH] 调整Request C.BHFree的位置 --- 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