chore(Shared/Network): make all pointers to std::unique_ptr (#17787)

chore(Shared/Network): using smart pointers instead of native ptr's
This commit is contained in:
Winfidonarleyan
2023-11-26 01:23:52 +07:00
committed by GitHub
parent 23606bcba0
commit be147b2c71
2 changed files with 33 additions and 36 deletions

View File

@@ -38,8 +38,8 @@ template<class SocketType>
class NetworkThread class NetworkThread
{ {
public: public:
NetworkThread() : _connections(0), _stopped(false), _thread(nullptr), _ioContext(1), NetworkThread() :
_acceptSocket(_ioContext), _updateTimer(_ioContext) { } _ioContext(1), _acceptSocket(_ioContext), _updateTimer(_ioContext) { }
virtual ~NetworkThread() virtual ~NetworkThread()
{ {
@@ -48,7 +48,6 @@ public:
if (_thread) if (_thread)
{ {
Wait(); Wait();
delete _thread;
} }
} }
@@ -63,7 +62,7 @@ public:
if (_thread) if (_thread)
return false; return false;
_thread = new std::thread(&NetworkThread::Run, this); _thread = std::make_unique<std::thread>([this]() { NetworkThread::Run(); });
return true; return true;
} }
@@ -71,12 +70,15 @@ public:
{ {
ASSERT(_thread); ASSERT(_thread);
_thread->join(); if (_thread->joinable())
delete _thread; {
_thread = nullptr; _thread->join();
}
_thread.reset();
} }
int32 GetConnectionCount() const [[nodiscard]] int32 GetConnectionCount() const
{ {
return _connections; return _connections;
} }
@@ -86,7 +88,7 @@ public:
std::lock_guard<std::mutex> lock(_newSocketsLock); std::lock_guard<std::mutex> lock(_newSocketsLock);
++_connections; ++_connections;
_newSockets.push_back(sock); _newSockets.emplace_back(sock);
SocketAdded(sock); SocketAdded(sock);
} }
@@ -111,7 +113,9 @@ protected:
--_connections; --_connections;
} }
else else
_sockets.push_back(sock); {
_sockets.emplace_back(sock);
}
} }
_newSockets.clear(); _newSockets.clear();
@@ -158,12 +162,12 @@ protected:
} }
private: private:
typedef std::vector<std::shared_ptr<SocketType>> SocketContainer; using SocketContainer = std::vector<std::shared_ptr<SocketType>>;
std::atomic<int32> _connections; std::atomic<int32> _connections{};
std::atomic<bool> _stopped; std::atomic<bool> _stopped{};
std::thread* _thread; std::unique_ptr<std::thread> _thread;
SocketContainer _sockets; SocketContainer _sockets;

View File

@@ -39,10 +39,10 @@ public:
{ {
ASSERT(threadCount > 0); ASSERT(threadCount > 0);
AsyncAcceptor* acceptor = nullptr; std::unique_ptr<AsyncAcceptor> acceptor;
try try
{ {
acceptor = new AsyncAcceptor(ioContext, bindIp, port); acceptor = std::make_unique<AsyncAcceptor>(ioContext, bindIp, port);
} }
catch (boost::system::system_error const& err) catch (boost::system::system_error const& err)
{ {
@@ -53,13 +53,12 @@ public:
if (!acceptor->Bind()) if (!acceptor->Bind())
{ {
LOG_ERROR("network", "StartNetwork failed to bind socket acceptor"); LOG_ERROR("network", "StartNetwork failed to bind socket acceptor");
delete acceptor;
return false; return false;
} }
_acceptor = acceptor; _acceptor = std::move(acceptor);
_threadCount = threadCount; _threadCount = threadCount;
_threads = CreateThreads(); _threads = std::unique_ptr<NetworkThread<SocketType>[]>(CreateThreads());
ASSERT(_threads); ASSERT(_threads);
@@ -67,7 +66,6 @@ public:
_threads[i].Start(); _threads[i].Start();
_acceptor->SetSocketFactory([this]() { return GetSocketForAccept(); }); _acceptor->SetSocketFactory([this]() { return GetSocketForAccept(); });
return true; return true;
} }
@@ -75,24 +73,20 @@ public:
{ {
_acceptor->Close(); _acceptor->Close();
if (_threadCount != 0) for (int32 i = 0; i < _threadCount; ++i)
for (int32 i = 0; i < _threadCount; ++i) _threads[i].Stop();
_threads[i].Stop();
Wait(); Wait();
delete _acceptor; _acceptor.reset();
_acceptor = nullptr; _threads.reset();
delete[] _threads;
_threads = nullptr;
_threadCount = 0; _threadCount = 0;
} }
void Wait() void Wait()
{ {
if (_threadCount != 0) for (int32 i = 0; i < _threadCount; ++i)
for (int32 i = 0; i < _threadCount; ++i) _threads[i].Wait();
_threads[i].Wait();
} }
virtual void OnSocketOpen(tcp::socket&& sock, uint32 threadIndex) virtual void OnSocketOpen(tcp::socket&& sock, uint32 threadIndex)
@@ -126,18 +120,17 @@ public:
std::pair<tcp::socket*, uint32> GetSocketForAccept() std::pair<tcp::socket*, uint32> GetSocketForAccept()
{ {
uint32 threadIndex = SelectThreadWithMinConnections(); uint32 threadIndex = SelectThreadWithMinConnections();
return std::make_pair(_threads[threadIndex].GetSocketForAccept(), threadIndex); return { _threads[threadIndex].GetSocketForAccept(), threadIndex };
} }
protected: protected:
SocketMgr() : SocketMgr() = default;
_acceptor(nullptr), _threads(nullptr), _threadCount(0) { }
virtual NetworkThread<SocketType>* CreateThreads() const = 0; virtual NetworkThread<SocketType>* CreateThreads() const = 0;
AsyncAcceptor* _acceptor; std::unique_ptr<AsyncAcceptor> _acceptor;
NetworkThread<SocketType>* _threads; std::unique_ptr<NetworkThread<SocketType>[]> _threads;
int32 _threadCount; int32 _threadCount{};
}; };
#endif // SocketMgr_h__ #endif // SocketMgr_h__