Refactor to use LockedWrapper guard

This commit is contained in:
2023-09-03 02:01:32 -04:00
parent 4073c88d08
commit e237c28382
7 changed files with 166 additions and 64 deletions

View File

@@ -41,10 +41,10 @@ uint32 AIntegrationGameModeBase::Run()
continue;
}
{
FScopeLock lk(&LuprexMutex);
Sockets->Update();
Luprex.play_invoke_event_update(&Luprex, EngineSeconds);
Sockets->Update();
FLockedWrapper lockedwrap(LockableWrapper);
Sockets->Update(lockedwrap);
lockedwrap->play_invoke_event_update(lockedwrap.Get(), EngineSeconds);
Sockets->Update(lockedwrap);
}
}
return 0;
@@ -52,6 +52,8 @@ uint32 AIntegrationGameModeBase::Run()
void AIntegrationGameModeBase::ResetToInitialState()
{
FLockedWrapper w(LockableWrapper);
// Shut down the thread and release the ThreadEvent
if (Thread != nullptr)
{
@@ -65,12 +67,16 @@ void AIntegrationGameModeBase::ResetToInitialState()
ThreadStopRequested = false;
// Release and close all sockets.
Sockets.Reset();
if (Sockets != nullptr)
{
Sockets->ForceCloseEverything(w);
Sockets.Reset();
}
// Delete the engine.
if (Luprex.release != nullptr)
if (w->release != nullptr)
{
Luprex.release(&Luprex);
w->release(w.Get());
}
// Reset the clocks.
@@ -79,15 +85,15 @@ void AIntegrationGameModeBase::ResetToInitialState()
}
void AIntegrationGameModeBase::HandleLuprexConsoleOutput()
void AIntegrationGameModeBase::HandleLuprexConsoleOutput(FLockedWrapper &w)
{
uint32_t ndata; const char* data;
Luprex.get_outgoing(&Luprex, 0, &ndata, &data);
w->get_outgoing(w.Get(), 0, &ndata, &data);
if (ndata == 0) return;
std::string_view src(data, ndata);
int consumed;
std::u16string cps = drvutil::utf8_to_ucs2(src, &consumed);
Luprex.play_sent_outgoing(&Luprex, 0, consumed);
w->play_sent_outgoing(w.Get(), 0, consumed);
FString fs(cps.size(), (const UCS2CHAR*)(&cps[0]));
ConsoleOutput.Append(fs);
}
@@ -96,11 +102,11 @@ void AIntegrationGameModeBase::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
{
FScopeLock lk(&LuprexMutex);
if (Luprex.engine != nullptr)
FLockedWrapper lockedwrap(LockableWrapper);
if (lockedwrap->engine != nullptr)
{
EngineSeconds += DeltaSeconds;
HandleLuprexConsoleOutput();
HandleLuprexConsoleOutput(lockedwrap);
}
}
TArray<FString> prints = engineutil::DPrintGetStored();
@@ -121,9 +127,9 @@ void AIntegrationGameModeBase::Tick(float DeltaSeconds)
void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs)
{
if (Luprex.engine != nullptr)
FLockedWrapper w(LockableWrapper);
if (w->engine != nullptr)
{
FScopeLock lk(&LuprexMutex);
const TCHAR* fstchar = *fs;
if (sizeof(TCHAR) == 2)
{
@@ -131,7 +137,7 @@ void AIntegrationGameModeBase::ConsoleSendInput(const FString& fs)
std::u16string_view fsview((const char16_t*)fstchar, fs.Len());
std::string utf8 = drvutil::utf16_to_utf8(fsview);
utf8 = utf8 + "\n";
Luprex.play_recv_incoming(&Luprex, 0, utf8.size(), utf8.c_str());
w->play_recv_incoming(w.Get(), 0, utf8.size(), utf8.c_str());
}
}
}
@@ -140,27 +146,26 @@ void AIntegrationGameModeBase::BeginPlay()
{
Super::BeginPlay();
FLockedWrapper w(LockableWrapper);
// Sanity checks.
checkf(Thread == nullptr, TEXT("There should be no thread here."));
checkf(Luprex.engine == nullptr, TEXT("There should be no engine here."));
checkf(w->engine == nullptr, TEXT("There should be no engine here."));
// Make sure we're starting from a clean slate.
ResetToInitialState();
// Try to initialize the wrapper.
if (Luprex.play_initialize == nullptr)
{
engineutil::init_wrapper(&Luprex);
}
w.InitWrapper();
// If we failed to initialize the wrapper, print an error message.
if (Luprex.play_initialize == nullptr)
if (w->play_initialize == nullptr)
{
engineutil::DPrint("Luprex wrapper initialization failed");
}
// If wrapper is initialized, try to initialize the luprex engine.
if (Luprex.play_initialize != nullptr)
if (w->play_initialize != nullptr)
{
drvutil::ostringstream srcpak;
std::string srcpakerr = drvutil::package_lua_source("c:\\Luprex", &srcpak);
@@ -171,10 +176,10 @@ void AIntegrationGameModeBase::BeginPlay()
std::string_view srcpakv = srcpak.view();
char* argv[1];
argv[0] = const_cast<char*>("lpxserver");
Luprex.play_initialize(&Luprex, 1, argv, srcpakv.size(), srcpakv.data(), "");
if (Luprex.error[0])
w->play_initialize(w.Get(), 1, argv, srcpakv.size(), srcpakv.data(), "");
if (w->error[0])
{
engineutil::DPrint(Luprex.error);
engineutil::DPrint(w->error);
}
else
{
@@ -183,9 +188,9 @@ void AIntegrationGameModeBase::BeginPlay()
}
// If we successfully created a luprex engine, create a socket system and a worker thread.
if (Luprex.engine != nullptr)
if (w->engine != nullptr)
{
Sockets.Reset(FLpxSockets::Create(&Luprex));
Sockets.Reset(FLpxSockets::Create(w));
std::string error = Sockets->GetError();
check(error.empty());
ThreadEvent = FPlatformProcess::GetSynchEventFromPool(false);