重构网络管理,优化房间状态显示逻辑,添加断开连接功能,确保资源正确释放
This commit is contained in:
@@ -208,20 +208,11 @@ int main()
|
|||||||
if ((steamManager.isHost() || steamManager.isConnected()) && roomManager.getCurrentLobby().IsValid())
|
if ((steamManager.isHost() || steamManager.isConnected()) && roomManager.getCurrentLobby().IsValid())
|
||||||
{
|
{
|
||||||
ImGui::Begin("房间状态");
|
ImGui::Begin("房间状态");
|
||||||
if (!steamManager.isHost())
|
|
||||||
{
|
|
||||||
ImGui::Text("与主机延迟: %d ms", steamManager.getHostPing());
|
|
||||||
}
|
|
||||||
ImGui::Separator();
|
|
||||||
ImGui::Text("用户列表:");
|
ImGui::Text("用户列表:");
|
||||||
int columnCount = steamManager.isHost() ? 2 : 1;
|
if (ImGui::BeginTable("UserTable", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
|
||||||
if (ImGui::BeginTable("UserTable", columnCount, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg))
|
|
||||||
{
|
{
|
||||||
ImGui::TableSetupColumn("名称");
|
ImGui::TableSetupColumn("名称");
|
||||||
if (steamManager.isHost())
|
ImGui::TableSetupColumn("延迟 (ms)");
|
||||||
{
|
|
||||||
ImGui::TableSetupColumn("延迟 (ms)");
|
|
||||||
}
|
|
||||||
ImGui::TableHeadersRow();
|
ImGui::TableHeadersRow();
|
||||||
{
|
{
|
||||||
std::vector<CSteamID> members = roomManager.getLobbyMembers();
|
std::vector<CSteamID> members = roomManager.getLobbyMembers();
|
||||||
@@ -232,17 +223,17 @@ int main()
|
|||||||
ImGui::TableNextColumn();
|
ImGui::TableNextColumn();
|
||||||
const char *name = SteamFriends()->GetFriendPersonaName(memberID);
|
const char *name = SteamFriends()->GetFriendPersonaName(memberID);
|
||||||
ImGui::Text("%s", name);
|
ImGui::Text("%s", name);
|
||||||
if (steamManager.isHost())
|
ImGui::TableNextColumn();
|
||||||
|
if (memberID == mySteamID)
|
||||||
{
|
{
|
||||||
ImGui::TableNextColumn();
|
ImGui::Text("-");
|
||||||
if (memberID == mySteamID)
|
}
|
||||||
{
|
else
|
||||||
ImGui::Text("-");
|
{
|
||||||
}
|
int ping = 0;
|
||||||
else
|
if (steamManager.isHost())
|
||||||
{
|
{
|
||||||
// Find connection for this member
|
// Find connection for this member
|
||||||
int ping = 0;
|
|
||||||
std::lock_guard<std::mutex> lockConn(connectionsMutex);
|
std::lock_guard<std::mutex> lockConn(connectionsMutex);
|
||||||
for (const auto &conn : steamManager.getConnections())
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,6 +129,40 @@ bool SteamNetworkingManager::joinHost(uint64 hostID)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SteamNetworkingManager::disconnect()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> 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<TCPServer> &server, int &localPort)
|
void SteamNetworkingManager::setMessageHandlerDependencies(boost::asio::io_context &io_context, std::unique_ptr<TCPServer> &server, int &localPort)
|
||||||
{
|
{
|
||||||
io_context_ = &io_context;
|
io_context_ = &io_context;
|
||||||
|
|||||||
Reference in New Issue
Block a user