AuroraRuntime/Source/IO/Net/AuNetWorker.cpp

89 lines
2.2 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuNetWorker.cpp
Date: 2022-8-16
Author: Reece
***/
#include "Networking.hpp"
#include "AuNetWorker.hpp"
#include "AuNetSrvWorkers.hpp"
namespace Aurora::IO::Net
{
NetWorker::NetWorker(NetSrvWorkers *pParent,
AuUInt8 workerIndex,
AuSPtr<IIOProcessor> pIOProcessor) :
pParent_(pParent),
workerIndex_(workerIndex),
pIOProcessor_(pIOProcessor)
{
this->pEvent_ = AuLoop::NewLSEventSlow(false, true);
SysAssert(bool(this->pEvent_));
}
bool NetWorker::IncrementIOEventTaskCounter()
{
if (AuAtomicAdd(&this->dwAtomicEventCounter_, 1u) != 1)
{
return true;
}
this->pWorkItem_ = this->pIOProcessor_->StartSimpleLSWatchEx(this->ToEvent(),
{},
false);
return bool(this->pWorkItem_);
}
void NetWorker::DecrementIOEventTaskCounter()
{
if (AuAtomicSub(&this->dwAtomicEventCounter_, 1u) == 0)
{
SysAssert(this->pWorkItem_->StopWatch());
}
}
void NetWorker::AddSocket(ISocket *pSocket)
{
SysAssert(AuTryInsert(this->childSockets, pSocket));
}
void NetWorker::RemoveSocket(ISocket *pSocket)
{
/*dont care*/AuTryRemove(this->childSockets, pSocket);
}
AuSPtr<AuLoop::ILSEvent> NetWorker::ToEvent()
{
return this->pEvent_;
}
bool NetWorker::IsOnThread()
{
return this->ToProcessor()->GetOwnedThreadId() == AuThreads::GetThreadId();
}
AuSPtr<IIOProcessor> NetWorker::ToProcessor()
{
return this->pIOProcessor_;
}
AuUInt8 NetWorker::GetWorkerIndex()
{
return this->workerIndex_;
}
void NetWorker::Destroy()
{
auto sockets = AuExchange(this->childSockets, {});
for (const auto &pSocket : sockets)
{
pSocket->Destroy();
}
if (auto pParent = AuExchange(this->pParent_, {}))
{
pParent->RemoveCache(this);
}
}
}