Use std::unique_ptr for ENetHost

This commit is contained in:
phosit
2026-04-16 18:34:08 +02:00
parent 74255b49c0
commit 0642153abc
7 changed files with 27 additions and 28 deletions
+6 -11
View File
@@ -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()
+1 -1
View File
@@ -124,7 +124,7 @@ private:
std::atomic<bool> m_LoopRunning{false};
std::atomic<bool> m_ShouldShutdown{false};
ENetHost* m_Host{nullptr};
std::unique_ptr<ENetHost, DestroyHost> m_Host;
ENetPeer* m_Server{nullptr};
CNetStatsTable* m_Stats{nullptr};
};
+9 -1
View File
@@ -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:
+3 -7
View File
@@ -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);
+1 -1
View File
@@ -263,7 +263,7 @@ private:
*/
const bool m_LobbyAuth;
ENetHost* m_Host{nullptr};
std::unique_ptr<ENetHost, DestroyHost> m_Host;
std::vector<CNetServerSession*> m_Sessions;
CNetStatsTable* m_Stats{nullptr};
+5 -5
View File
@@ -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<std::mutex> 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);
+2 -2
View File
@@ -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;