Stub of FLpxSockets is now in place
This commit is contained in:
@@ -6,10 +6,6 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
|
||||
|
||||
EngineWrapper AIntegrationGameModeBase::Luprex;
|
||||
|
||||
//// Main loop.
|
||||
//while (!engw.get_stop_driver(&engw)) {
|
||||
// handle_lua_source();
|
||||
@@ -27,32 +23,17 @@ AIntegrationGameModeBase::AIntegrationGameModeBase()
|
||||
ThreadStopRequested = false;
|
||||
EngineSeconds = 0.0;
|
||||
NextThreadTrigger = 1.0;
|
||||
ThreadEvent = nullptr;
|
||||
//PrimaryActorTick.bCanEverTick = true; // Probably wrong
|
||||
//PrimaryActorTick.bTickEvenWhenPaused = true; // Probably wrong
|
||||
//PrimaryActorTick.TickGroup = TG_PrePhysics; // Probably wrong
|
||||
SetActorTickEnabled(true);
|
||||
SetActorTickInterval(0.05f);
|
||||
ThreadEvent = FPlatformProcess::GetSynchEventFromPool(false);
|
||||
}
|
||||
|
||||
AIntegrationGameModeBase::~AIntegrationGameModeBase()
|
||||
{
|
||||
WaitForThread();
|
||||
FPlatformProcess::ReturnSynchEventToPool(ThreadEvent);
|
||||
ThreadEvent = nullptr;
|
||||
}
|
||||
|
||||
void AIntegrationGameModeBase::HandleLuprexConsoleOutput()
|
||||
{
|
||||
uint32_t ndata; const char* data;
|
||||
Luprex.get_outgoing(&Luprex, 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);
|
||||
FString fs(cps.size(), (const UCS2CHAR*)(&cps[0]));
|
||||
ConsoleOutput.Append(fs);
|
||||
ResetToInitialState();
|
||||
}
|
||||
|
||||
// Run routine called by the worker thread.
|
||||
@@ -72,20 +53,53 @@ uint32 AIntegrationGameModeBase::Run()
|
||||
engineutil::DPrint("Thread triggered.");
|
||||
{
|
||||
FScopeLock lk(&LuprexMutex);
|
||||
Sockets->Update();
|
||||
Luprex.play_invoke_event_update(&Luprex, EngineSeconds);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#pragma optimize( "", off )
|
||||
void AIntegrationGameModeBase::WaitForThread()
|
||||
void AIntegrationGameModeBase::ResetToInitialState()
|
||||
{
|
||||
if (Thread == nullptr) return;
|
||||
ThreadStopRequested = true;
|
||||
ThreadEvent->Trigger();
|
||||
delete Thread; // This waits for the thread to complete.
|
||||
Thread = nullptr;
|
||||
// Shut down the thread and release the ThreadEvent
|
||||
if (Thread != nullptr)
|
||||
{
|
||||
ThreadStopRequested = true;
|
||||
ThreadEvent->Trigger();
|
||||
delete Thread; // This waits for the thread to complete.
|
||||
Thread = nullptr;
|
||||
FPlatformProcess::ReturnSynchEventToPool(ThreadEvent);
|
||||
ThreadEvent = nullptr;
|
||||
}
|
||||
ThreadStopRequested = false;
|
||||
|
||||
// Delete the engine.
|
||||
if (Luprex.release != nullptr)
|
||||
{
|
||||
Luprex.release(&Luprex);
|
||||
}
|
||||
|
||||
// Release and close all sockets.
|
||||
Sockets.Reset();
|
||||
|
||||
// Reset the clocks.
|
||||
EngineSeconds = 0;
|
||||
NextThreadTrigger = 1.0;
|
||||
}
|
||||
|
||||
|
||||
void AIntegrationGameModeBase::HandleLuprexConsoleOutput()
|
||||
{
|
||||
uint32_t ndata; const char* data;
|
||||
Luprex.get_outgoing(&Luprex, 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);
|
||||
FString fs(cps.size(), (const UCS2CHAR*)(&cps[0]));
|
||||
ConsoleOutput.Append(fs);
|
||||
}
|
||||
|
||||
void AIntegrationGameModeBase::Tick(float DeltaSeconds)
|
||||
@@ -93,8 +107,11 @@ void AIntegrationGameModeBase::Tick(float DeltaSeconds)
|
||||
Super::Tick(DeltaSeconds);
|
||||
{
|
||||
FScopeLock lk(&LuprexMutex);
|
||||
EngineSeconds += DeltaSeconds;
|
||||
HandleLuprexConsoleOutput();
|
||||
if (Luprex.engine != nullptr)
|
||||
{
|
||||
EngineSeconds += DeltaSeconds;
|
||||
HandleLuprexConsoleOutput();
|
||||
}
|
||||
}
|
||||
TArray<FString> prints = engineutil::DPrintGetStored();
|
||||
for (const FString& fs : prints) {
|
||||
@@ -133,31 +150,28 @@ void AIntegrationGameModeBase::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
// There should be no thread at this point.
|
||||
// Sanity checks.
|
||||
checkf(Thread == nullptr, TEXT("There should be no thread here."));
|
||||
checkf(Luprex.engine == nullptr, TEXT("There should be no engine here."));
|
||||
|
||||
// Reinitialize simple state.
|
||||
EngineSeconds = 0;
|
||||
NextThreadTrigger = 1.0;
|
||||
ThreadStopRequested = false;
|
||||
ThreadEvent->Wait(0); // Clear the event if set.
|
||||
// Make sure we're starting from a clean slate.
|
||||
ResetToInitialState();
|
||||
|
||||
// Try to initialize the wrapper.
|
||||
if (Luprex.play_initialize == nullptr)
|
||||
{
|
||||
engineutil::init_wrapper(&Luprex);
|
||||
if (Luprex.play_initialize == nullptr)
|
||||
{
|
||||
engineutil::DPrint("Luprex wrapper initialization failed");
|
||||
}
|
||||
}
|
||||
|
||||
// If we failed to initialize the wrapper, print an error message.
|
||||
if (Luprex.play_initialize == nullptr)
|
||||
{
|
||||
engineutil::DPrint("Luprex wrapper initialization failed");
|
||||
}
|
||||
|
||||
// If wrapper is initialized, try to initialize the luprex engine.
|
||||
if (Luprex.play_initialize != nullptr)
|
||||
{
|
||||
Luprex.release(&Luprex);
|
||||
Luprex.hook_dprint(engineutil::DPrintHook);
|
||||
|
||||
drvutil::ostringstream srcpak;
|
||||
std::string srcpakerr = drvutil::package_lua_source("c:\\Luprex", &srcpak);
|
||||
if (!srcpakerr.empty())
|
||||
@@ -178,16 +192,18 @@ void AIntegrationGameModeBase::BeginPlay()
|
||||
}
|
||||
}
|
||||
|
||||
// If we successfully created a Luprex engine, create a worker thread.
|
||||
// If we successfully created a luprex engine, create a socket system and a worker thread.
|
||||
if (Luprex.engine != nullptr)
|
||||
{
|
||||
Sockets.Reset(FLpxSockets::Create(&Luprex));
|
||||
ThreadEvent = FPlatformProcess::GetSynchEventFromPool(false);
|
||||
Thread = FRunnableThread::Create(this, TEXT("Worker Thread"));
|
||||
}
|
||||
}
|
||||
|
||||
void AIntegrationGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
|
||||
{
|
||||
WaitForThread();
|
||||
ResetToInitialState();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user