From 30fe49880ffd278df8b927869d60c5238ab1d241 Mon Sep 17 00:00:00 2001 From: Ayndpa Date: Wed, 19 Nov 2025 21:00:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=BD=91=E7=BB=9C=E7=AE=A1?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E4=BC=98=E5=8C=96=E6=88=BF=E9=97=B4=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E7=A1=AE=E4=BF=9D=E8=B5=84=E6=BA=90=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- online_game_tool.cpp | 36 +++++++++++++----------------- steam/steam_networking_manager.cpp | 34 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/online_game_tool.cpp b/online_game_tool.cpp index 82559e9..574d533 100644 --- a/online_game_tool.cpp +++ b/online_game_tool.cpp @@ -208,20 +208,11 @@ int main() if ((steamManager.isHost() || steamManager.isConnected()) && roomManager.getCurrentLobby().IsValid()) { ImGui::Begin("房间状态"); - if (!steamManager.isHost()) - { - ImGui::Text("与主机延迟: %d ms", steamManager.getHostPing()); - } - ImGui::Separator(); ImGui::Text("用户列表:"); - int columnCount = steamManager.isHost() ? 2 : 1; - if (ImGui::BeginTable("UserTable", columnCount, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) + if (ImGui::BeginTable("UserTable", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { ImGui::TableSetupColumn("名称"); - if (steamManager.isHost()) - { - ImGui::TableSetupColumn("延迟 (ms)"); - } + ImGui::TableSetupColumn("延迟 (ms)"); ImGui::TableHeadersRow(); { std::vector members = roomManager.getLobbyMembers(); @@ -232,17 +223,17 @@ int main() ImGui::TableNextColumn(); const char *name = SteamFriends()->GetFriendPersonaName(memberID); ImGui::Text("%s", name); - if (steamManager.isHost()) + ImGui::TableNextColumn(); + if (memberID == mySteamID) { - ImGui::TableNextColumn(); - if (memberID == mySteamID) - { - ImGui::Text("-"); - } - else + ImGui::Text("-"); + } + else + { + int ping = 0; + if (steamManager.isHost()) { // Find connection for this member - int ping = 0; std::lock_guard lockConn(connectionsMutex); for (const auto &conn : steamManager.getConnections()) { @@ -256,8 +247,13 @@ int main() } } } - ImGui::Text("%d", ping); } + else + { + // Client shows ping to host + ping = steamManager.getHostPing(); + } + ImGui::Text("%d", ping); } } } diff --git a/steam/steam_networking_manager.cpp b/steam/steam_networking_manager.cpp index 67f38e1..0b7f5c6 100644 --- a/steam/steam_networking_manager.cpp +++ b/steam/steam_networking_manager.cpp @@ -129,6 +129,40 @@ bool SteamNetworkingManager::joinHost(uint64 hostID) } } +void SteamNetworkingManager::disconnect() +{ + std::lock_guard lock(connectionsMutex); + + // Close client connection + if (g_hConnection != k_HSteamNetConnection_Invalid) + { + m_pInterface->CloseConnection(g_hConnection, 0, nullptr, false); + g_hConnection = k_HSteamNetConnection_Invalid; + } + + // Close all host connections + for (auto conn : connections) + { + m_pInterface->CloseConnection(conn, 0, nullptr, false); + } + connections.clear(); + + // Close listen socket + if (hListenSock != k_HSteamListenSocket_Invalid) + { + m_pInterface->CloseListenSocket(hListenSock); + hListenSock = k_HSteamListenSocket_Invalid; + } + + // Reset state + g_isHost = false; + g_isClient = false; + g_isConnected = false; + hostPing_ = 0; + + std::cout << "Disconnected from network" << std::endl; +} + void SteamNetworkingManager::setMessageHandlerDependencies(boost::asio::io_context &io_context, std::unique_ptr &server, int &localPort) { io_context_ = &io_context;