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
{
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<std::thread>([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<std::mutex> 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<std::shared_ptr<SocketType>> SocketContainer;
using SocketContainer = std::vector<std::shared_ptr<SocketType>>;
std::atomic<int32> _connections;
std::atomic<bool> _stopped;
std::atomic<int32> _connections{};
std::atomic<bool> _stopped{};
std::thread* _thread;
std::unique_ptr<std::thread> _thread;
SocketContainer _sockets;

View File

@@ -39,10 +39,10 @@ public:
{
ASSERT(threadCount > 0);
AsyncAcceptor* acceptor = nullptr;
std::unique_ptr<AsyncAcceptor> acceptor;
try
{
acceptor = new AsyncAcceptor(ioContext, bindIp, port);
acceptor = std::make_unique<AsyncAcceptor>(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<NetworkThread<SocketType>[]>(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<tcp::socket*, uint32> 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<SocketType>* CreateThreads() const = 0;
AsyncAcceptor* _acceptor;
NetworkThread<SocketType>* _threads;
int32 _threadCount;
std::unique_ptr<AsyncAcceptor> _acceptor;
std::unique_ptr<NetworkThread<SocketType>[]> _threads;
int32 _threadCount{};
};
#endif // SocketMgr_h__