forked from mirrors/0ad
Use std::unique_ptr for ENetHost
This commit is contained in:
@@ -45,14 +45,10 @@ CNetClientSession::~CNetClientSession()
|
|||||||
|
|
||||||
delete m_Stats;
|
delete m_Stats;
|
||||||
|
|
||||||
if (m_Host && m_Server)
|
if (m_Server)
|
||||||
{
|
{
|
||||||
// Disconnect immediately (we can't wait for acks)
|
// Disconnect immediately (we can't wait for acks)
|
||||||
enet_peer_disconnect_now(m_Server, NDR_SERVER_SHUTDOWN);
|
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);
|
ENSURE(!m_Server);
|
||||||
|
|
||||||
// Create ENet host if necessary.
|
// 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;
|
return false;
|
||||||
|
|
||||||
// Bind to specified host
|
// Bind to specified host
|
||||||
@@ -75,11 +71,10 @@ bool CNetClientSession::Connect(const CStr& server, const u16 port, ENetHost* en
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Initiate connection to server
|
// 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)
|
if (!peer)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_Host = host;
|
|
||||||
m_Server = peer;
|
m_Server = peer;
|
||||||
|
|
||||||
m_Stats = new CNetStatsTable(m_Server);
|
m_Stats = new CNetStatsTable(m_Server);
|
||||||
@@ -124,7 +119,7 @@ void CNetClientSession::Poll()
|
|||||||
ENetEvent event;
|
ENetEvent event;
|
||||||
|
|
||||||
// Use the timeout to make the thread wait and save CPU time.
|
// 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;
|
return;
|
||||||
|
|
||||||
if (event.type == ENET_EVENT_TYPE_CONNECT)
|
if (event.type == ENET_EVENT_TYPE_CONNECT)
|
||||||
@@ -167,7 +162,7 @@ void CNetClientSession::Flush()
|
|||||||
LOGMESSAGE("NetClient: Failed to send packet to server");
|
LOGMESSAGE("NetClient: Failed to send packet to server");
|
||||||
}
|
}
|
||||||
|
|
||||||
enet_host_flush(m_Host);
|
enet_host_flush(m_Host.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CNetClientSession::ProcessPolledMessages()
|
void CNetClientSession::ProcessPolledMessages()
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ private:
|
|||||||
std::atomic<bool> m_LoopRunning{false};
|
std::atomic<bool> m_LoopRunning{false};
|
||||||
std::atomic<bool> m_ShouldShutdown{false};
|
std::atomic<bool> m_ShouldShutdown{false};
|
||||||
|
|
||||||
ENetHost* m_Host{nullptr};
|
std::unique_ptr<ENetHost, DestroyHost> m_Host;
|
||||||
ENetPeer* m_Server{nullptr};
|
ENetPeer* m_Server{nullptr};
|
||||||
CNetStatsTable* m_Stats{nullptr};
|
CNetStatsTable* m_Stats{nullptr};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2025 Wildfire Games.
|
/* Copyright (C) 2026 Wildfire Games.
|
||||||
* This file is part of 0 A.D.
|
* This file is part of 0 A.D.
|
||||||
*
|
*
|
||||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||||
@@ -84,6 +84,14 @@ enum NetDisconnectReason
|
|||||||
NDR_INCORRECT_SOFTWARE_VERSION
|
NDR_INCORRECT_SOFTWARE_VERSION
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct DestroyHost
|
||||||
|
{
|
||||||
|
void operator()(ENetHost* host) const noexcept
|
||||||
|
{
|
||||||
|
enet_host_destroy(host);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class CNetHost
|
class CNetHost
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -125,10 +125,9 @@ CNetServerWorker::CNetServerWorker(const bool continueSavedGame, std::uint16_t p
|
|||||||
addr.port = port;
|
addr.port = port;
|
||||||
|
|
||||||
// Create ENet server
|
// 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)
|
if (!m_Host)
|
||||||
{
|
{
|
||||||
enet_host_destroy(m_Host);
|
|
||||||
LOGERROR("Net server: enet_host_create failed");
|
LOGERROR("Net server: enet_host_create failed");
|
||||||
throw std::runtime_error{"Failed to start server"};
|
throw std::runtime_error{"Failed to start server"};
|
||||||
}
|
}
|
||||||
@@ -174,9 +173,6 @@ CNetServerWorker::~CNetServerWorker()
|
|||||||
session->DisconnectNow(NDR_SERVER_SHUTDOWN);
|
session->DisconnectNow(NDR_SERVER_SHUTDOWN);
|
||||||
delete session;
|
delete session;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_Host)
|
|
||||||
enet_host_destroy(m_Host);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -402,7 +398,7 @@ void CNetServerWorker::Run(const std::string& initAttributes)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
// Update profiler stats
|
// Update profiler stats
|
||||||
m_Stats->LatchHostState(m_Host);
|
m_Stats->LatchHostState(*m_Host);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear roots before deleting their context
|
// Clear roots before deleting their context
|
||||||
@@ -455,7 +451,7 @@ bool CNetServerWorker::RunStep()
|
|||||||
// Process network events:
|
// Process network events:
|
||||||
|
|
||||||
ENetEvent event;
|
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)
|
if (status < 0)
|
||||||
{
|
{
|
||||||
LOGERROR("CNetServerWorker: enet_host_service failed (%d)", status);
|
LOGERROR("CNetServerWorker: enet_host_service failed (%d)", status);
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ private:
|
|||||||
*/
|
*/
|
||||||
const bool m_LobbyAuth;
|
const bool m_LobbyAuth;
|
||||||
|
|
||||||
ENetHost* m_Host{nullptr};
|
std::unique_ptr<ENetHost, DestroyHost> m_Host;
|
||||||
std::vector<CNetServerSession*> m_Sessions;
|
std::vector<CNetServerSession*> m_Sessions;
|
||||||
|
|
||||||
CNetStatsTable* m_Stats{nullptr};
|
CNetStatsTable* m_Stats{nullptr};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2025 Wildfire Games.
|
/* Copyright (C) 2026 Wildfire Games.
|
||||||
* This file is part of 0 A.D.
|
* This file is part of 0 A.D.
|
||||||
*
|
*
|
||||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
* 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CNetStatsTable::LatchHostState(const ENetHost* host)
|
void CNetStatsTable::LatchHostState(const ENetHost& host)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(m_Mutex);
|
std::lock_guard<std::mutex> lock(m_Mutex);
|
||||||
|
|
||||||
#define ROW(id, title, member) \
|
#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.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_InData, "incoming bytes", incomingDataTotal);
|
||||||
ROW(Row_OutData, "outgoing bytes", outgoingDataTotal);
|
ROW(Row_OutData, "outgoing bytes", outgoingDataTotal);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2025 Wildfire Games.
|
/* Copyright (C) 2026 Wildfire Games.
|
||||||
* This file is part of 0 A.D.
|
* This file is part of 0 A.D.
|
||||||
*
|
*
|
||||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
* 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;
|
CStr GetCellText(size_t row, size_t col) override;
|
||||||
AbstractProfileTable* GetChild(size_t row) override;
|
AbstractProfileTable* GetChild(size_t row) override;
|
||||||
|
|
||||||
void LatchHostState(const ENetHost* host);
|
void LatchHostState(const ENetHost& host);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const ENetPeer* m_Peer;
|
const ENetPeer* m_Peer;
|
||||||
|
|||||||
Reference in New Issue
Block a user