AuroraRuntime/Source/IO/Net/AuNetInterface.cpp
Jamie Reece Wilson 2717178681 [+] Added INetworkInterface overloads
[+] INetInterface::SetWorkersService
[+] INetInterface::SetResolveService
[+] INetInterface::SetDatagramService
[+] INetInterface::SetInterfacesService
[*] Refactor: INetAdapter::GetStatus -> INetAdapter::GetAdapterStatus
[*] Refactor: const NetError &ISocketBase::GetError() -> AuOptional<const NetError &> ISocketBase::GetError()
[*] Refactor: INetAdapter to return const references of IPAddresses
2024-04-01 06:28:35 +01:00

154 lines
4.4 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuNetInterface.cpp
Date: 2022-8-16
Author: Reece
***/
#include "Networking.hpp"
#include "AuNetInterface.hpp"
#include "AuNetWorker.hpp"
namespace Aurora::IO::Net
{
NetInterface::NetInterface() :
netServiceResolve_(this),
netServiceSockets_(this),
netServiceDatagram_(this)
{
}
#define NET_INTERFACE_ADD_SERVICE(type, selfHosted, pOverload) \
AU_LOCK_GUARD(this->mutex_); \
\
if (this->pOverload) \
{ \
return this->pOverload; \
} \
else \
{ \
return AuSPtr<type>(AuSharedFromThis(), &this->selfHosted); \
}
AuSPtr<INetSrvWorkers> NetInterface::GetWorkersService()
{
NET_INTERFACE_ADD_SERVICE(INetSrvWorkers, netServiceWorkers_, pNetServiceOverloadWorkers_);
}
AuSPtr<INetSrvResolve> NetInterface::GetResolveService()
{
NET_INTERFACE_ADD_SERVICE(INetSrvResolve, netServiceResolve_, pNetServiceOverloadResolve_);
}
AuSPtr<INetSrvSockets> NetInterface::GetSocketService()
{
NET_INTERFACE_ADD_SERVICE(INetSrvSockets, netServiceSockets_, pNetServiceOverloadSockets_);
}
AuSPtr<INetSrvDatagram> NetInterface::GetDatagramService()
{
NET_INTERFACE_ADD_SERVICE(INetSrvDatagram, netServiceDatagram_, pNetServiceOverloadDatagram_);
}
AuSPtr<INetSrvInterfaces> NetInterface::GetInterfacesService()
{
AU_LOCK_GUARD(this->mutex_);
if (this->pNetServiceOverloadNetworkInterfaces_)
{
return this->pNetServiceOverloadNetworkInterfaces_;
}
if (!this->netServiceInterfaces_.IsSupportedOnOS())
{
return {};
}
return AuSPtr<INetSrvInterfaces>(AuSharedFromThis(), &this->netServiceInterfaces_);
}
void NetInterface::SetWorkersService(const AuSPtr<INetSrvWorkers> &pWorkers)
{
AU_LOCK_GUARD(this->mutex_);
this->pNetServiceOverloadWorkers_ = pWorkers;
}
void NetInterface::SetResolveService(const AuSPtr<INetSrvResolve> &pResolve)
{
AU_LOCK_GUARD(this->mutex_);
this->pNetServiceOverloadResolve_ = pResolve;
}
void NetInterface::SetSocketService(const AuSPtr<INetSrvSockets> &pSockets)
{
AU_LOCK_GUARD(this->mutex_);
this->pNetServiceOverloadSockets_ = pSockets;
}
void NetInterface::SetDatagramService(const AuSPtr<INetSrvDatagram> &pDatagrams)
{
AU_LOCK_GUARD(this->mutex_);
this->pNetServiceOverloadDatagram_ = pDatagrams;
}
void NetInterface::SetInterfacesService(const AuSPtr<INetSrvInterfaces> &pNetworkInterfaces)
{
AU_LOCK_GUARD(this->mutex_);
this->pNetServiceOverloadNetworkInterfaces_ = pNetworkInterfaces;
}
bool NetInterface::TrySchedule(const AuSPtr<IIOProcessorWorkUnit> &pWorkUnit)
{
auto workerId = AuAtomicAdd<AuUInt>(&this->atomicCounter_, 1u) - 1u;
auto worker = this->netServiceWorkers_.GetWorkerByIndex(workerId);
if (!worker)
{
SysPushErrorIO("No Worker");
return {};
}
return worker->ToProcessor()->SubmitIOWorkItem(pWorkUnit);
}
AuSPtr<NetWorker> NetInterface::TryScheduleEx()
{
AuSPtr<NetWorker> worker;
do
{
worker = this->netServiceWorkers_.GetWorkerByIndex(AuAtomicAdd<AuUInt>(&this->atomicCounter_, 1u) - 1u);
} while (!worker);
return worker;
}
void NetInterface::Shutdown()
{
}
void NetInterface::Destroy()
{
}
AUKN_SYM AuSPtr<INetInterface> NewNetworkInterface()
{
if (!IsNetReady())
{
SysPushErrorNet("Platform network service isn't available");
return {};
}
auto iface = AuMakeShared<NetInterface>();
if (!iface)
{
return iface;
}
// TODO: ...
return iface;
}
}