From 0642153abcd5325e2b8b435e7d6ce5620f5a82a2 Mon Sep 17 00:00:00 2001 From: phosit Date: Thu, 16 Apr 2026 18:34:08 +0200 Subject: [PATCH] Use std::unique_ptr for ENetHost --- source/network/NetClientSession.cpp | 17 ++++++----------- source/network/NetClientSession.h | 2 +- source/network/NetHost.h | 10 +++++++++- source/network/NetServer.cpp | 10 +++------- source/network/NetServer.h | 2 +- source/network/NetStats.cpp | 10 +++++----- source/network/NetStats.h | 4 ++-- 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/source/network/NetClientSession.cpp b/source/network/NetClientSession.cpp index 0577981250..f7a85d878e 100644 --- a/source/network/NetClientSession.cpp +++ b/source/network/NetClientSession.cpp @@ -45,14 +45,10 @@ CNetClientSession::~CNetClientSession() delete m_Stats; - if (m_Host && m_Server) + if (m_Server) { // Disconnect immediately (we can't wait for acks) enet_peer_disconnect_now(m_Server, NDR_SERVER_SHUTDOWN); - enet_host_destroy(m_Host); - - m_Host = NULL; - m_Server = NULL; } } @@ -63,9 +59,9 @@ bool CNetClientSession::Connect(const CStr& server, const u16 port, ENetHost* en ENSURE(!m_Server); // Create ENet host if necessary. - ENetHost* host = enetClient != nullptr ? enetClient : PS::Enet::CreateHost(nullptr, 1, CHANNEL_COUNT); + m_Host.reset(enetClient != nullptr ? enetClient : PS::Enet::CreateHost(nullptr, 1, CHANNEL_COUNT)); - if (!host) + if (!m_Host) return false; // Bind to specified host @@ -75,11 +71,10 @@ bool CNetClientSession::Connect(const CStr& server, const u16 port, ENetHost* en return false; // Initiate connection to server - ENetPeer* peer = enet_host_connect(host, &addr, CHANNEL_COUNT, 0); + ENetPeer* peer = enet_host_connect(m_Host.get(), &addr, CHANNEL_COUNT, 0); if (!peer) return false; - m_Host = host; m_Server = peer; m_Stats = new CNetStatsTable(m_Server); @@ -124,7 +119,7 @@ void CNetClientSession::Poll() ENetEvent event; // Use the timeout to make the thread wait and save CPU time. - if (enet_host_service(m_Host, &event, NETCLIENT_POLL_TIMEOUT) <= 0) + if (enet_host_service(m_Host.get(), &event, NETCLIENT_POLL_TIMEOUT) <= 0) return; if (event.type == ENET_EVENT_TYPE_CONNECT) @@ -167,7 +162,7 @@ void CNetClientSession::Flush() LOGMESSAGE("NetClient: Failed to send packet to server"); } - enet_host_flush(m_Host); + enet_host_flush(m_Host.get()); } void CNetClientSession::ProcessPolledMessages() diff --git a/source/network/NetClientSession.h b/source/network/NetClientSession.h index d50b8a9de6..a6bda26387 100644 --- a/source/network/NetClientSession.h +++ b/source/network/NetClientSession.h @@ -124,7 +124,7 @@ private: std::atomic m_LoopRunning{false}; std::atomic m_ShouldShutdown{false}; - ENetHost* m_Host{nullptr}; + std::unique_ptr m_Host; ENetPeer* m_Server{nullptr}; CNetStatsTable* m_Stats{nullptr}; }; diff --git a/source/network/NetHost.h b/source/network/NetHost.h index f24f86e50a..49da8fb8cf 100644 --- a/source/network/NetHost.h +++ b/source/network/NetHost.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -84,6 +84,14 @@ enum NetDisconnectReason NDR_INCORRECT_SOFTWARE_VERSION }; +struct DestroyHost +{ + void operator()(ENetHost* host) const noexcept + { + enet_host_destroy(host); + } +}; + class CNetHost { public: diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index 6c826585e5..8c6ade9fd3 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -125,10 +125,9 @@ CNetServerWorker::CNetServerWorker(const bool continueSavedGame, std::uint16_t p addr.port = port; // Create ENet server - m_Host = PS::Enet::CreateHost(&addr, MAX_CLIENTS, CHANNEL_COUNT); + m_Host.reset(PS::Enet::CreateHost(&addr, MAX_CLIENTS, CHANNEL_COUNT)); if (!m_Host) { - enet_host_destroy(m_Host); LOGERROR("Net server: enet_host_create failed"); throw std::runtime_error{"Failed to start server"}; } @@ -174,9 +173,6 @@ CNetServerWorker::~CNetServerWorker() session->DisconnectNow(NDR_SERVER_SHUTDOWN); delete session; } - - if (m_Host) - enet_host_destroy(m_Host); } @@ -402,7 +398,7 @@ void CNetServerWorker::Run(const std::string& initAttributes) break; // Update profiler stats - m_Stats->LatchHostState(m_Host); + m_Stats->LatchHostState(*m_Host); } // Clear roots before deleting their context @@ -455,7 +451,7 @@ bool CNetServerWorker::RunStep() // Process network events: ENetEvent event; - int status = enet_host_service(m_Host, &event, HOST_SERVICE_TIMEOUT); + int status = enet_host_service(m_Host.get(), &event, HOST_SERVICE_TIMEOUT); if (status < 0) { LOGERROR("CNetServerWorker: enet_host_service failed (%d)", status); diff --git a/source/network/NetServer.h b/source/network/NetServer.h index 1fed5b9088..6af624a575 100644 --- a/source/network/NetServer.h +++ b/source/network/NetServer.h @@ -263,7 +263,7 @@ private: */ const bool m_LobbyAuth; - ENetHost* m_Host{nullptr}; + std::unique_ptr m_Host; std::vector m_Sessions; CNetStatsTable* m_Stats{nullptr}; diff --git a/source/network/NetStats.cpp b/source/network/NetStats.cpp index c9aa8c4fa8..d3572f9bdb 100644 --- a/source/network/NetStats.cpp +++ b/source/network/NetStats.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -124,17 +124,17 @@ AbstractProfileTable* CNetStatsTable::GetChild(size_t /*row*/) return 0; } -void CNetStatsTable::LatchHostState(const ENetHost* host) +void CNetStatsTable::LatchHostState(const ENetHost& host) { std::lock_guard lock(m_Mutex); #define ROW(id, title, member) \ - m_LatchedData[i].push_back(CStr::FromUInt(host->peers[i].member)); + m_LatchedData[i].push_back(CStr::FromUInt(host.peers[i].member)); m_LatchedData.clear(); - m_LatchedData.resize(host->peerCount); + m_LatchedData.resize(host.peerCount); - for (size_t i = 0; i < host->peerCount; ++i) + for (size_t i = 0; i < host.peerCount; ++i) { ROW(Row_InData, "incoming bytes", incomingDataTotal); ROW(Row_OutData, "outgoing bytes", outgoingDataTotal); diff --git a/source/network/NetStats.h b/source/network/NetStats.h index 1f5450938a..51b3c95c72 100644 --- a/source/network/NetStats.h +++ b/source/network/NetStats.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -53,7 +53,7 @@ public: CStr GetCellText(size_t row, size_t col) override; AbstractProfileTable* GetChild(size_t row) override; - void LatchHostState(const ENetHost* host); + void LatchHostState(const ENetHost& host); private: const ENetPeer* m_Peer;