Skip to content

Commit

Permalink
fix run on default and exit server stop
Browse files Browse the repository at this point in the history
  • Loading branch information
getnamo committed Oct 28, 2019
1 parent ada40cf commit 4453bff
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 56 deletions.
2 changes: 1 addition & 1 deletion NodeJs.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "0.4.1",
"VersionName": "0.4.2",
"FriendlyName": "NodeJs",
"Description": "Embed node.js as an unreal plugin.",
"Category": "Programming",
Expand Down
57 changes: 38 additions & 19 deletions Source/CommandLine/Private/NodeCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ FNodeCmd::FNodeCmd()
bUseRemoteMainScript = false;

NodeExe = TEXT("node.exe");
MainScriptRelativePath = TEXT("");
}

FNodeCmd::~FNodeCmd()
Expand All @@ -69,29 +70,30 @@ void FNodeCmd::StartupMainScriptIfNeeded()
}
}

void FNodeCmd::StopMainScriptSync()
{
if (Socket->bIsConnected)
{
Socket->Emit(TEXT("stopMainScript"), TEXT("ForceStop"));
Socket->SyncDisconnect();
}
bShouldMainRun = false;
}

void FNodeCmd::AddEventListener(TSharedPtr<FNodeEventListener> Listener)
{
Listeners.AddUnique(Listener);
StartupMainScriptIfNeeded();

if (bIsMainRunning)
{
Listener->OnMainScriptBegin(MainScriptRelativePath);
}
else
{
StartupMainScriptIfNeeded();
}
}

void FNodeCmd::RemoveEventListener(TSharedPtr<FNodeEventListener> Listener)
{
Listeners.Remove(Listener);

UE_LOG(LogTemp, Log, TEXT("Removed a listener, %d listeners left"), Listeners.Num());

//removed last listener? stop main script
if (bShouldStopMainScriptOnNoListeners && Listeners.Num() == 0)
{
UE_LOG(LogTemp, Log, TEXT("Stopping main script."));
StopMainScript();
}
}
Expand Down Expand Up @@ -148,14 +150,23 @@ bool FNodeCmd::RunMainScript(FString ScriptRelativePath, int32 Port)
}

UE_LOG(LogTemp, Log, TEXT("RunScriptStart"));
Socket->OnConnectedCallback = [&](const FString& InSessionId)
Socket->OnConnectedCallback = [&, ScriptRelativePath](const FString& InSessionId)
{
UE_LOG(LogTemp, Log, TEXT("Main script Connected."));

MainScriptRelativePath = ScriptRelativePath;

for (auto Listener : Listeners)
{
Listener->OnMainScriptBegin(ScriptRelativePath);
}
};
Socket->OnReconnectionCallback = [&](uint32 AttemptCount, uint32 DelayInMs)
{
UE_LOG(LogTemp, Error, TEXT("Main script connection error! Likely crash, stopping main script."));
bShouldMainRun = false;

MainScriptRelativePath = TEXT("");
};

//Mainscript console.log
Expand Down Expand Up @@ -340,18 +351,26 @@ void FNodeCmd::StopMainScript()
{
if (bIsMainRunning)
{
FCULambdaRunnable::RunLambdaOnBackGroundThreadPool([this]
FCULambdaRunnable::RunLambdaOnBackGroundThread([this]
{
if (Socket->bIsConnected)
{
Socket->Emit(TEXT("stopMainScript"), TEXT("ForceStop"));
Socket->SyncDisconnect();
}
bShouldMainRun = false;
StopMainScriptSync();
});
}
}

void FNodeCmd::StopMainScriptSync()
{
UE_LOG(LogTemp, Log, TEXT("StopMainScriptSync"));
if (Socket->bIsConnected)
{
UE_LOG(LogTemp, Log, TEXT("Socket->bIsConnected"));

Socket->Emit(TEXT("stopMainScript"), TEXT("ForceStop"));
Socket->SyncDisconnect();
}
bShouldMainRun = false;
}

void FNodeCmd::StopChildScript(int32 ProcessId)
{
if (bIsMainRunning && Socket->bIsConnected)
Expand Down
2 changes: 2 additions & 0 deletions Source/CommandLine/Public/NodeCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class COMMANDLINE_API FNodeEventListener
public:
TFunction<void(const FString& LogMsg)> OnConsoleLog;
TFunction<void(const FString& LogMsg)> OnScriptConsoleLog;
TFunction<void(const FString& ScriptRelativePath)> OnMainScriptBegin;
TFunction<void(const FString& ScriptRelativePath)> OnMainScriptEnd;
TFunction<void(int32 ProcessId)> OnChildScriptBegin;
TFunction<void(int32 ScriptId)> OnChildScriptEnd;
Expand Down Expand Up @@ -74,6 +75,7 @@ class COMMANDLINE_API FNodeCmd
FString ProcessDirectory;
FString PluginContentRelativePath;
FString NodeExe;
FString MainScriptRelativePath;

FThreadSafeBool bShouldMainRun;
FThreadSafeBool bIsMainRunning;
Expand Down
63 changes: 35 additions & 28 deletions Source/NodeJs/Private/NodeComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ UNodeComponent::UNodeComponent()
bReloadOnChange = true;
bIsRestartStop = false;
bAllowPreBinding = false;
bBeginPlayScriptHandled = false;

Cmd = INodeJsModule::Get().ValidSharedNativePointer(TEXT("main"));
Listener = MakeShareable(new FNodeEventListener());
Expand All @@ -40,34 +41,6 @@ void UNodeComponent::BeginPlay()
{
//Start the parent script which hosts all scripts
LinkAndStartWrapperScript();
if (bRunDefaultScriptOnBeginPlay)
{
RunDefaultScript();

//watch scripts?
if (bWatchFileOnBeginPlay)
{
Cmd->WatchScriptForChanges(DefaultScriptPath, [&](const FString& WatchedScriptPath)
{
if (bReloadOnChange)
{
//Restart
if (bScriptIsRunning)
{
//Stop and re-start script
bIsRestartStop = true;
StopScript();
}
//Just start
else
{
RunDefaultScript();
}
}
OnScriptChanged.Broadcast(WatchedScriptPath);
});
}
}
}
}

Expand All @@ -89,6 +62,40 @@ void UNodeComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)

void UNodeComponent::LinkAndStartWrapperScript()
{
Listener->OnMainScriptBegin = [this](const FString& ScriptRelativePath)
{
//We wait until after main script started to run our beginplay script
if (bRunDefaultScriptOnBeginPlay && !bBeginPlayScriptHandled)
{
bBeginPlayScriptHandled = true;
RunDefaultScript();

//watch scripts?
if (bWatchFileOnBeginPlay)
{
Cmd->WatchScriptForChanges(DefaultScriptPath, [&](const FString& WatchedScriptPath)
{
if (bReloadOnChange)
{
//Restart
if (bScriptIsRunning)
{
//Stop and re-start script
bIsRestartStop = true;
StopScript();
}
//Just start
else
{
RunDefaultScript();
}
}
OnScriptChanged.Broadcast(WatchedScriptPath);
});
}
}
};

Listener->OnChildScriptEnd = [this](int32 ProcessId)
{
OnScriptEnd.Broadcast(DefaultScriptPath);
Expand Down
11 changes: 3 additions & 8 deletions Source/NodeJs/Private/NodeJs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void FNodeJsModule::ReleaseNativePointer(TSharedPtr<FNodeCmd> PointerToRelease)
}
}

FCULambdaRunnable::RunLambdaOnBackGroundThreadPool([PointerToRelease, this]
FCULambdaRunnable::RunLambdaOnBackGroundThread([PointerToRelease, this]
{
if (PointerToRelease.IsValid())
{
Expand All @@ -74,11 +74,6 @@ void FNodeJsModule::ReleaseNativePointer(TSharedPtr<FNodeCmd> PointerToRelease)
{
FPlatformProcess::Sleep(0.01f);
}
//Disconnect, this can happen simultaneously
/*if (PointerToRelease->Socket && PointerToRelease->Socket->bIsConnected)
{
PointerToRelease->Socket->SyncDisconnect();
}*/

//Update our active status
bHasActiveNativePointers = PluginNativePointers.Num() > 0;
Expand Down Expand Up @@ -119,7 +114,7 @@ void FNodeJsModule::ShutdownModule()

float Elapsed = 0.f;
float SleepInc = 0.01f;
while (bHasActiveNativePointers)
/*while (bHasActiveNativePointers)
{
FPlatformProcess::Sleep(SleepInc);
Elapsed += SleepInc;
Expand All @@ -130,7 +125,7 @@ void FNodeJsModule::ShutdownModule()
UE_LOG(LogTemp, Warning, TEXT("FNodeJsModule::ShutdownModule force quit due to long wait to quit."));
break;
}
}
}*/
}

#undef LOCTEXT_NAMESPACE
Expand Down
1 change: 1 addition & 0 deletions Source/NodeJs/Public/NodeComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class NODEJS_API UNodeComponent : public UActorComponent

//to track specific type of restart
bool bIsRestartStop;
bool bBeginPlayScriptHandled;

//append process id for mux routing in main script
FString FullEventName(const FString& EventName);
Expand Down

0 comments on commit 4453bff

Please sign in to comment.