From e8a326be532292838fe883df00ded9f05bc45430 Mon Sep 17 00:00:00 2001 From: Ayndpa Date: Wed, 19 Nov 2025 21:42:51 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84SteamMessageHandler=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=A4=9A=E4=BD=99=E7=BA=BF=E7=A8=8B=EF=BC=8C?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E5=AE=9A=E6=97=B6=E5=99=A8=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=BD=AE=E8=AF=A2=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=8E=A5=E6=94=B6=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- steam/steam_message_handler.cpp | 61 +++++++++++++++++---------------- steam/steam_message_handler.h | 7 ++-- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/steam/steam_message_handler.cpp b/steam/steam_message_handler.cpp index c59801c..89618d4 100644 --- a/steam/steam_message_handler.cpp +++ b/steam/steam_message_handler.cpp @@ -2,12 +2,11 @@ #include #include #include -#include #include #include SteamMessageHandler::SteamMessageHandler(boost::asio::io_context& io_context, ISteamNetworkingSockets* interface, std::vector& connections, std::mutex& connectionsMutex, bool& g_isHost, int& localPort) - : io_context_(io_context), m_pInterface_(interface), connections_(connections), connectionsMutex_(connectionsMutex), g_isHost_(g_isHost), localPort_(localPort), running_(false) {} + : io_context_(io_context), m_pInterface_(interface), connections_(connections), connectionsMutex_(connectionsMutex), g_isHost_(g_isHost), localPort_(localPort), running_(false), currentPollInterval_(0) {} SteamMessageHandler::~SteamMessageHandler() { stop(); @@ -16,23 +15,15 @@ SteamMessageHandler::~SteamMessageHandler() { void SteamMessageHandler::start() { if (running_) return; running_ = true; - thread_ = std::thread([this]() { run(); }); - // Start io_context in a separate thread - io_thread_ = std::thread([this]() { - auto work = boost::asio::make_work_guard(io_context_); - io_context_.run(); - }); + timer_ = std::make_unique(io_context_); + startAsyncPoll(); } void SteamMessageHandler::stop() { if (!running_) return; running_ = false; - io_context_.stop(); - if (thread_.joinable()) { - thread_.join(); - } - if (io_thread_.joinable()) { - io_thread_.join(); + if (timer_) { + timer_->cancel(); } } @@ -43,20 +34,14 @@ std::shared_ptr SteamMessageHandler::getMultiplexManager(HStea return multiplexManagers_[conn]; } -void SteamMessageHandler::run() { - while (running_) { - // Poll networking - m_pInterface_->RunCallbacks(); - - // Receive messages - pollMessages(); - - // Sleep a bit to avoid busy loop - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } -} - -void SteamMessageHandler::pollMessages() { +void SteamMessageHandler::startAsyncPoll() { + if (!running_) return; + + // Poll networking callbacks + m_pInterface_->RunCallbacks(); + + // Receive messages and check if any were received + int totalMessages = 0; std::vector currentConnections; { std::lock_guard lockConn(connectionsMutex_); @@ -65,6 +50,7 @@ void SteamMessageHandler::pollMessages() { for (auto conn : currentConnections) { ISteamNetworkingMessage* pIncomingMsgs[10]; int numMsgs = m_pInterface_->ReceiveMessagesOnConnection(conn, pIncomingMsgs, 10); + totalMessages += numMsgs; for (int i = 0; i < numMsgs; ++i) { ISteamNetworkingMessage* pIncomingMsg = pIncomingMsgs[i]; const char* data = (const char*)pIncomingMsg->m_pData; @@ -77,4 +63,21 @@ void SteamMessageHandler::pollMessages() { pIncomingMsg->Release(); } } -} \ No newline at end of file + + // Adaptive polling: if messages received, poll immediately; otherwise increase interval + if (totalMessages > 0) { + currentPollInterval_ = 0; // 有消息,立即轮询 + } else { + // 无消息,逐渐增加间隔,最大10ms + currentPollInterval_ = std::min(currentPollInterval_ + 1, 10); + } + + // Schedule next poll + timer_->expires_after(std::chrono::milliseconds(currentPollInterval_)); + timer_->async_wait([this](const boost::system::error_code& error) { + if (!error && running_) { + startAsyncPoll(); + } + }); +} + diff --git a/steam/steam_message_handler.h b/steam/steam_message_handler.h index 460ed84..91e5249 100644 --- a/steam/steam_message_handler.h +++ b/steam/steam_message_handler.h @@ -22,8 +22,7 @@ public: std::shared_ptr getMultiplexManager(HSteamNetConnection conn); private: - void run(); - void pollMessages(); + void startAsyncPoll(); boost::asio::io_context& io_context_; ISteamNetworkingSockets* m_pInterface_; @@ -34,9 +33,9 @@ private: std::map> multiplexManagers_; - std::thread thread_; - std::thread io_thread_; + std::unique_ptr timer_; bool running_; + int currentPollInterval_; // 当前轮询间隔(毫秒) }; #endif // STEAM_MESSAGE_HANDLER_H \ No newline at end of file