use jthread

This commit is contained in:
2024-08-07 11:50:07 +02:00
parent af3ac8d37c
commit adb6dce08a
4 changed files with 12 additions and 10 deletions

View File

@@ -26,7 +26,7 @@ class EnetClient : private NonCopyable {
EnetConnection m_Connection;
Nz::ENetHost m_Host;
Nz::ENetPeer* m_Peer;
std::thread m_Thread;
std::jthread m_Thread;
bool m_Running;
void Update();

View File

@@ -33,7 +33,7 @@ class EnetServer : private NonCopyable {
Nz::ENetHost m_Host;
bool m_Running;
std::thread m_Thread;
std::jthread m_Thread;
std::map<std::uint16_t, std::unique_ptr<EnetConnection>> m_Connections;
};

View File

@@ -6,16 +6,16 @@ namespace network {
EnetClient::EnetClient(const Nz::IpAddress& address) : m_Running(true) {
m_Host.Create(Nz::IpAddress::LoopbackIpV4, 1);
m_Peer = m_Host.Connect(address);
m_Thread = std::thread(&EnetClient::WorkerThread, this);
m_Thread = std::jthread(&EnetClient::WorkerThread, this);
m_Connection.SetPeer(m_Peer);
}
EnetClient::~EnetClient() {
if (m_Peer->IsConnected())
Disconnect();
m_Host.Destroy();
m_Running = false;
m_Thread.join();
m_Thread.request_stop();
m_Host.Destroy();
}
void EnetClient::Disconnect() {

View File

@@ -5,10 +5,12 @@
namespace blitz {
namespace network {
EnetServer::EnetServer(std::uint16_t port) : m_Running(true) {
m_Host.Create(Nz::NetProtocol::Any, port, 80);
EnetServer::EnetServer(std::uint16_t a_Port) : m_Running(true) {
m_Running = m_Host.Create(Nz::NetProtocol::Any, a_Port, 80);
if (m_Running) {
m_Host.AllowsIncomingConnections(true);
m_Thread = std::thread(&EnetServer::WorkerThread, this);
m_Thread = std::jthread(&EnetServer::WorkerThread, this);
}
}
void EnetServer::WorkerThread() {
@@ -96,7 +98,7 @@ void EnetServer::RemoveConnection(std::uint16_t a_PeerId) {
void EnetServer::Destroy() {
m_Running = false;
m_Thread.join();
m_Thread.request_stop();
m_Host.Destroy();
}