[*] Net: Globalize adapter cache

This commit is contained in:
Reece Wilson 2023-12-01 14:58:58 +00:00
parent e0f74d6160
commit aadca8058e
2 changed files with 16 additions and 19 deletions

View File

@ -13,41 +13,40 @@ namespace Aurora::IO::Net
{
const AuString &NetSrvInterfaces::GetHostname()
{
AU_LOCK_GUARD(this->lock);
if (this->bShouldUpdateCache)
AU_LOCK_GUARD(NetSrvInterfaces::gLock);
if (NetSrvInterfaces::bShouldUpdateCache)
{
UpdateCache();
}
return hostname;
return NetSrvInterfaces::gHostname;
}
void NetSrvInterfaces::ResetAdapterCache()
{
AU_LOCK_GUARD(this->lock);
this->bShouldUpdateCache = true;
AU_LOCK_GUARD(NetSrvInterfaces::gLock);
NetSrvInterfaces::bShouldUpdateCache = true;
}
void NetSrvInterfaces::UpdateCache()
{
this->hostname = NetAdapter::GetHostname();
NetSrvInterfaces::gHostname = NetAdapter::GetHostname();
auto ipv4s = NetAdapter::GetIPv4s();
auto ipv6s = NetAdapter::GetIPv6s();
this->adapters.clear();
this->adapters.insert(this->adapters.end(), ipv4s.begin(), ipv4s.end());
this->adapters.insert(this->adapters.end(), ipv6s.begin(), ipv6s.end());
NetSrvInterfaces::gAdapters.clear();
NetSrvInterfaces::gAdapters.insert(NetSrvInterfaces::gAdapters.end(), ipv4s.begin(), ipv4s.end());
NetSrvInterfaces::gAdapters.insert(NetSrvInterfaces::gAdapters.end(), ipv6s.begin(), ipv6s.end());
}
AuList<AuSPtr<INetAdapter>> NetSrvInterfaces::GetAdapters()
{
AU_LOCK_GUARD(this->lock);
AU_LOCK_GUARD(NetSrvInterfaces::gLock);
if (this->bShouldUpdateCache)
{
UpdateCache();
}
return this->adapters;
return this->NetSrvInterfaces::gAdapters;
}
bool NetSrvInterfaces::IsSupportedOnOS()

View File

@ -11,20 +11,18 @@ namespace Aurora::IO::Net
{
struct NetSrvInterfaces : INetSrvInterfaces
{
const AuString &GetHostname() override;
const AuString & GetHostname() override;
void ResetAdapterCache() override;
AuList<AuSPtr<INetAdapter>> GetAdapters() override;
bool IsSupportedOnOS();
private:
void UpdateCache();
AuThreadPrimitives::SpinLock lock;
AuString hostname;
bool bShouldUpdateCache { true };
AuList<AuSPtr<INetAdapter>> adapters;
cstatic AuMutex gLock;
cstatic AuString gHostname;
cstatic bool bShouldUpdateCache { true };
cstatic AuList<AuSPtr<INetAdapter>> gAdapters;
};
}