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;