更新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
SteamAPI_RunCallbacks();
// Update Steam networking info
steamManager.update();
// Start ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();

View File

@@ -101,6 +101,9 @@ void SteamMessageHandler::pollMessages() {
}
}
// 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();
}

View File

@@ -117,7 +117,7 @@ bool SteamNetworkingManager::initialize() {
std::cout << "Cleared Rich Presence on startup" << std::endl;
// 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
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) {
io_context_ = &io_context;
clientMap_ = &clientMap;
clientMutex_ = &clientMutex;
server_ = &server;
localPort_ = &localPort;
messageHandler_ = new SteamMessageHandler(io_context, m_pInterface, connections, clientMap, clientMutex, connectionsMutex, server, g_isHost, localPort);
void SteamNetworkingManager::update() {
std::lock_guard<std::mutex> lock(connectionsMutex);
for (auto& pair : userMap) {
HSteamNetConnection conn = pair.first;
UserInfo& userInfo = pair.second;
SteamNetConnectionInfo_t info;
SteamNetConnectionRealTimeStatus_t status;
if (m_pInterface->GetConnectionInfo(conn, &info) && m_pInterface->GetConnectionRealTimeStatus(conn, &status, 0, nullptr)) {
userInfo.ping = status.m_nPing;
userInfo.isRelay = (info.m_idPOPRelay != 0);
}
void SteamNetworkingManager::startMessageHandler() {
if (messageHandler_) {
messageHandler_->start();
}
}
void SteamNetworkingManager::stopMessageHandler() {
if (messageHandler_) {
messageHandler_->stop();
}
}

View File

@@ -81,15 +81,8 @@ public:
HSteamNetConnection getConnection() const { return g_hConnection; }
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);
// Message handler
void startMessageHandler();
void stopMessageHandler();
// For callbacks
void setHostSteamID(CSteamID id) { g_hostSteamID = id; }
CSteamID getHostSteamID() const { return g_hostSteamID; }
// Update user info (ping, relay status)
void update();
friend class SteamFriendsCallbacks;
friend class SteamMatchmakingCallbacks;