2021-08-18 07:36:31 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "Pch.h"
|
|
|
|
|
#include "Asio.h"
|
|
|
|
|
#include "AsioContext.h"
|
2024-02-02 10:53:59 -05:00
|
|
|
#include "InstanceInfo.h"
|
|
|
|
|
#include "Lifetime.h"
|
2021-08-18 07:36:31 -04:00
|
|
|
#include "Recorder.h"
|
|
|
|
|
#include "Store.h"
|
|
|
|
|
#include "StoreCborServer.h"
|
|
|
|
|
#include "StoreService.h"
|
2023-03-28 09:47:42 -04:00
|
|
|
#include "StoreSettings.h"
|
2021-08-18 07:36:31 -04:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
struct FStoreServiceImpl
|
|
|
|
|
{
|
|
|
|
|
public:
|
2024-02-02 10:53:59 -05:00
|
|
|
FStoreServiceImpl(FStoreSettings* Settings, FInstanceInfo* InstanceInfo);
|
2021-08-18 07:36:31 -04:00
|
|
|
~FStoreServiceImpl();
|
|
|
|
|
FAsioContext Context;
|
|
|
|
|
FStore Store;
|
|
|
|
|
FRecorder Recorder;
|
|
|
|
|
FStoreCborServer CborServer;
|
2024-02-02 10:53:59 -05:00
|
|
|
FLifetime LifetimeManager;
|
2021-08-18 07:36:31 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2024-02-02 10:53:59 -05:00
|
|
|
FStoreServiceImpl::FStoreServiceImpl(FStoreSettings* Settings, FInstanceInfo* InstanceInfo)
|
2023-03-28 09:47:42 -04:00
|
|
|
: Context(Settings->ThreadCount)
|
|
|
|
|
, Store(Context.Get(), Settings)
|
2021-08-18 07:36:31 -04:00
|
|
|
, Recorder(Context.Get(), Store)
|
2023-03-28 09:47:42 -04:00
|
|
|
, CborServer(Context.Get(), Settings, Store, Recorder)
|
2024-02-02 10:53:59 -05:00
|
|
|
, LifetimeManager(Context.Get(), (FStoreService*)this, Settings, InstanceInfo)
|
2021-08-18 07:36:31 -04:00
|
|
|
{
|
2023-03-28 09:47:42 -04:00
|
|
|
if (Settings->RecorderPort >= 0)
|
|
|
|
|
{
|
|
|
|
|
Recorder.StartServer(Settings->RecorderPort);
|
|
|
|
|
}
|
|
|
|
|
Context.Start();
|
2021-08-18 07:36:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
FStoreServiceImpl::~FStoreServiceImpl()
|
|
|
|
|
{
|
|
|
|
|
asio::post(Context.Get(), [this] () {
|
2024-02-02 10:53:59 -05:00
|
|
|
LifetimeManager.StopTick();
|
2021-08-18 07:36:31 -04:00
|
|
|
CborServer.Close();
|
|
|
|
|
Recorder.Close();
|
|
|
|
|
Store.Close();
|
|
|
|
|
});
|
|
|
|
|
Context.Wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2024-02-02 10:53:59 -05:00
|
|
|
FStoreService* FStoreService::Create(FStoreSettings* Settings, FInstanceInfo* InstanceInfo)
|
2021-08-18 07:36:31 -04:00
|
|
|
{
|
2023-03-28 09:47:42 -04:00
|
|
|
if (Settings->ThreadCount <= 0)
|
2021-08-18 07:36:31 -04:00
|
|
|
{
|
2023-03-28 09:47:42 -04:00
|
|
|
Settings->ThreadCount = std::thread::hardware_concurrency();
|
2021-08-18 07:36:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: not thread safe yet
|
2023-03-28 09:47:42 -04:00
|
|
|
Settings->ThreadCount = 1;
|
2021-08-18 07:36:31 -04:00
|
|
|
|
2024-02-02 10:53:59 -05:00
|
|
|
return (FStoreService*) new FStoreServiceImpl(Settings, InstanceInfo);
|
2021-08-18 07:36:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
void FStoreService::operator delete (void* Addr)
|
|
|
|
|
{
|
|
|
|
|
auto* Self = (FStoreServiceImpl*)Addr;
|
|
|
|
|
delete Self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
uint32 FStoreService::GetPort() const
|
|
|
|
|
{
|
|
|
|
|
auto* Self = (FStoreServiceImpl*)this;
|
|
|
|
|
return Self->CborServer.GetPort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
uint32 FStoreService::GetRecorderPort() const
|
|
|
|
|
{
|
|
|
|
|
auto* Self = (FStoreServiceImpl*)this;
|
|
|
|
|
return Self->Recorder.GetPort();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-06 04:41:52 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
bool FStoreService::ShutdownIfNoConnections()
|
|
|
|
|
{
|
|
|
|
|
auto* Self = (FStoreServiceImpl*)this;
|
|
|
|
|
const uint32 ConnectionCount = Self->Recorder.GetSessionCount() + Self->CborServer.GetActivePeerCount();
|
|
|
|
|
if (!ConnectionCount)
|
|
|
|
|
{
|
|
|
|
|
// Close the recorder and store server so new connections are not
|
|
|
|
|
// accepted between returning and process exiting.
|
|
|
|
|
Self->Recorder.Close();
|
|
|
|
|
Self->CborServer.Close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 07:36:31 -04:00
|
|
|
/* vim: set noexpandtab : */
|