diff --git a/src/server/shared/Network/NetworkThread.h b/src/server/shared/Network/NetworkThread.h index aec20a16a..a6d4ff352 100644 --- a/src/server/shared/Network/NetworkThread.h +++ b/src/server/shared/Network/NetworkThread.h @@ -38,8 +38,8 @@ template class NetworkThread { public: - NetworkThread() : _connections(0), _stopped(false), _thread(nullptr), _ioContext(1), - _acceptSocket(_ioContext), _updateTimer(_ioContext) { } + NetworkThread() : + _ioContext(1), _acceptSocket(_ioContext), _updateTimer(_ioContext) { } virtual ~NetworkThread() { @@ -48,7 +48,6 @@ public: if (_thread) { Wait(); - delete _thread; } } @@ -63,7 +62,7 @@ public: if (_thread) return false; - _thread = new std::thread(&NetworkThread::Run, this); + _thread = std::make_unique([this]() { NetworkThread::Run(); }); return true; } @@ -71,12 +70,15 @@ public: { ASSERT(_thread); - _thread->join(); - delete _thread; - _thread = nullptr; + if (_thread->joinable()) + { + _thread->join(); + } + + _thread.reset(); } - int32 GetConnectionCount() const + [[nodiscard]] int32 GetConnectionCount() const { return _connections; } @@ -86,7 +88,7 @@ public: std::lock_guard lock(_newSocketsLock); ++_connections; - _newSockets.push_back(sock); + _newSockets.emplace_back(sock); SocketAdded(sock); } @@ -111,7 +113,9 @@ protected: --_connections; } else - _sockets.push_back(sock); + { + _sockets.emplace_back(sock); + } } _newSockets.clear(); @@ -158,12 +162,12 @@ protected: } private: - typedef std::vector> SocketContainer; + using SocketContainer = std::vector>; - std::atomic _connections; - std::atomic _stopped; + std::atomic _connections{}; + std::atomic _stopped{}; - std::thread* _thread; + std::unique_ptr _thread; SocketContainer _sockets; diff --git a/src/server/shared/Network/SocketMgr.h b/src/server/shared/Network/SocketMgr.h index 0894f2bf4..19234fa71 100644 --- a/src/server/shared/Network/SocketMgr.h +++ b/src/server/shared/Network/SocketMgr.h @@ -39,10 +39,10 @@ public: { ASSERT(threadCount > 0); - AsyncAcceptor* acceptor = nullptr; + std::unique_ptr acceptor; try { - acceptor = new AsyncAcceptor(ioContext, bindIp, port); + acceptor = std::make_unique(ioContext, bindIp, port); } catch (boost::system::system_error const& err) { @@ -53,13 +53,12 @@ public: if (!acceptor->Bind()) { LOG_ERROR("network", "StartNetwork failed to bind socket acceptor"); - delete acceptor; return false; } - _acceptor = acceptor; + _acceptor = std::move(acceptor); _threadCount = threadCount; - _threads = CreateThreads(); + _threads = std::unique_ptr[]>(CreateThreads()); ASSERT(_threads); @@ -67,7 +66,6 @@ public: _threads[i].Start(); _acceptor->SetSocketFactory([this]() { return GetSocketForAccept(); }); - return true; } @@ -75,24 +73,20 @@ public: { _acceptor->Close(); - if (_threadCount != 0) - for (int32 i = 0; i < _threadCount; ++i) - _threads[i].Stop(); + for (int32 i = 0; i < _threadCount; ++i) + _threads[i].Stop(); Wait(); - delete _acceptor; - _acceptor = nullptr; - delete[] _threads; - _threads = nullptr; + _acceptor.reset(); + _threads.reset(); _threadCount = 0; } void Wait() { - if (_threadCount != 0) - for (int32 i = 0; i < _threadCount; ++i) - _threads[i].Wait(); + for (int32 i = 0; i < _threadCount; ++i) + _threads[i].Wait(); } virtual void OnSocketOpen(tcp::socket&& sock, uint32 threadIndex) @@ -126,18 +120,17 @@ public: std::pair GetSocketForAccept() { uint32 threadIndex = SelectThreadWithMinConnections(); - return std::make_pair(_threads[threadIndex].GetSocketForAccept(), threadIndex); + return { _threads[threadIndex].GetSocketForAccept(), threadIndex }; } protected: - SocketMgr() : - _acceptor(nullptr), _threads(nullptr), _threadCount(0) { } + SocketMgr() = default; virtual NetworkThread* CreateThreads() const = 0; - AsyncAcceptor* _acceptor; - NetworkThread* _threads; - int32 _threadCount; + std::unique_ptr _acceptor; + std::unique_ptr[]> _threads; + int32 _threadCount{}; }; #endif // SocketMgr_h__