重构SteamMessageHandler,移除多余线程,改用定时器实现异步轮询,优化消息接收逻辑
This commit is contained in:
@@ -2,12 +2,11 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <steam_api.h>
|
||||
#include <isteamnetworkingsockets.h>
|
||||
|
||||
SteamMessageHandler::SteamMessageHandler(boost::asio::io_context& io_context, ISteamNetworkingSockets* interface, std::vector<HSteamNetConnection>& 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<boost::asio::steady_timer>(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<MultiplexManager> SteamMessageHandler::getMultiplexManager(HStea
|
||||
return multiplexManagers_[conn];
|
||||
}
|
||||
|
||||
void SteamMessageHandler::run() {
|
||||
while (running_) {
|
||||
// Poll networking
|
||||
m_pInterface_->RunCallbacks();
|
||||
void SteamMessageHandler::startAsyncPoll() {
|
||||
if (!running_) return;
|
||||
|
||||
// Receive messages
|
||||
pollMessages();
|
||||
// Poll networking callbacks
|
||||
m_pInterface_->RunCallbacks();
|
||||
|
||||
// Sleep a bit to avoid busy loop
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
void SteamMessageHandler::pollMessages() {
|
||||
// Receive messages and check if any were received
|
||||
int totalMessages = 0;
|
||||
std::vector<HSteamNetConnection> currentConnections;
|
||||
{
|
||||
std::lock_guard<std::mutex> 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();
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -22,8 +22,7 @@ public:
|
||||
std::shared_ptr<MultiplexManager> 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<HSteamNetConnection, std::shared_ptr<MultiplexManager>> multiplexManagers_;
|
||||
|
||||
std::thread thread_;
|
||||
std::thread io_thread_;
|
||||
std::unique_ptr<boost::asio::steady_timer> timer_;
|
||||
bool running_;
|
||||
int currentPollInterval_; // 当前轮询间隔(毫秒)
|
||||
};
|
||||
|
||||
#endif // STEAM_MESSAGE_HANDLER_H
|
||||
Reference in New Issue
Block a user