更新Steam网络信息,添加用户信息更新功能以获取延迟和中继状态

This commit is contained in:
Ayndpa
2025-11-18 21:45:42 +08:00
parent e7a09f9d92
commit d1d26ad0f0
4 changed files with 20 additions and 28 deletions

View File

@@ -113,6 +113,9 @@ int main() {
// Run Steam callbacks // Run Steam callbacks
SteamAPI_RunCallbacks(); SteamAPI_RunCallbacks();
// Update Steam networking info
steamManager.update();
// Start ImGui frame // Start ImGui frame
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();

View File

@@ -101,6 +101,9 @@ void SteamMessageHandler::pollMessages() {
} }
} }
// Send to corresponding TCP client if exists (for host) // Send to corresponding TCP client if exists (for host)
if (clientMap_.count(conn)) {
clientMap_[conn]->send((const char*)pIncomingMsg->m_pData, pIncomingMsg->m_cbSize);
}
} }
pIncomingMsg->Release(); pIncomingMsg->Release();
} }

View File

@@ -117,7 +117,7 @@ bool SteamNetworkingManager::initialize() {
std::cout << "Cleared Rich Presence on startup" << std::endl; std::cout << "Cleared Rich Presence on startup" << std::endl;
// Check if callbacks are registered // Check if callbacks are registered
std::cout << "Steam API initialized, callbacks should be registered" << std::endl; std::cout << "Steam API initialized" << std::endl;
// Get friends list // Get friends list
int friendCount = SteamFriends()->GetFriendCount(k_EFriendFlagAll); int friendCount = SteamFriends()->GetFriendCount(k_EFriendFlagAll);
@@ -224,24 +224,17 @@ bool SteamNetworkingManager::joinHost(uint64 hostID) {
} }
} }
void SteamNetworkingManager::setMessageHandlerDependencies(boost::asio::io_context& io_context, std::map<HSteamNetConnection, std::shared_ptr<TCPClient>>& clientMap, std::mutex& clientMutex, std::unique_ptr<TCPServer>& server, int& localPort) { void SteamNetworkingManager::update() {
io_context_ = &io_context; std::lock_guard<std::mutex> lock(connectionsMutex);
clientMap_ = &clientMap; for (auto& pair : userMap) {
clientMutex_ = &clientMutex; HSteamNetConnection conn = pair.first;
server_ = &server; UserInfo& userInfo = pair.second;
localPort_ = &localPort; SteamNetConnectionInfo_t info;
messageHandler_ = new SteamMessageHandler(io_context, m_pInterface, connections, clientMap, clientMutex, connectionsMutex, server, g_isHost, localPort); SteamNetConnectionRealTimeStatus_t status;
} if (m_pInterface->GetConnectionInfo(conn, &info) && m_pInterface->GetConnectionRealTimeStatus(conn, &status, 0, nullptr)) {
userInfo.ping = status.m_nPing;
void SteamNetworkingManager::startMessageHandler() { userInfo.isRelay = (info.m_idPOPRelay != 0);
if (messageHandler_) {
messageHandler_->start();
} }
}
void SteamNetworkingManager::stopMessageHandler() {
if (messageHandler_) {
messageHandler_->stop();
} }
} }

View File

@@ -81,15 +81,8 @@ public:
HSteamNetConnection getConnection() const { return g_hConnection; } HSteamNetConnection getConnection() const { return g_hConnection; }
ISteamNetworkingSockets* getInterface() const { return m_pInterface; } ISteamNetworkingSockets* getInterface() const { return m_pInterface; }
void setMessageHandlerDependencies(boost::asio::io_context& io_context, std::map<HSteamNetConnection, std::shared_ptr<TCPClient>>& clientMap, std::mutex& clientMutex, std::unique_ptr<TCPServer>& server, int& localPort); // Update user info (ping, relay status)
void update();
// Message handler
void startMessageHandler();
void stopMessageHandler();
// For callbacks
void setHostSteamID(CSteamID id) { g_hostSteamID = id; }
CSteamID getHostSteamID() const { return g_hostSteamID; }
friend class SteamFriendsCallbacks; friend class SteamFriendsCallbacks;
friend class SteamMatchmakingCallbacks; friend class SteamMatchmakingCallbacks;