From 6c3cebb8f1c6a79afefb2d6f71a67b0c6b370b4b Mon Sep 17 00:00:00 2001 From: mxlong <1450271751@qq.com> Date: Sun, 23 Apr 2023 20:17:04 +0800 Subject: [PATCH] fix the unstable socket connection error If notify_one() of the condition variable has been called before the function wait(), wait() blocks the connection until the next connection is established. So you get an socket connect Error. It will occur more frequently on the iOS platform. solution: Use FScopedEvent of UE. reference: https://en.cppreference.com/w/cpp/thread/condition_variable/notify_one https://docs.unrealengine.com/4.26/en-US/API/Runtime/Core/Misc/FScopedEvent/ --- .../Private/Dispatcher/CommandDispatcher.cpp | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/GAutomatorSdk/UnrealEngine/GAutomatorSdk/Plugins/GAutomator/Source/GAutomator/Private/Dispatcher/CommandDispatcher.cpp b/GAutomatorSdk/UnrealEngine/GAutomatorSdk/Plugins/GAutomator/Source/GAutomator/Private/Dispatcher/CommandDispatcher.cpp index 3d3e481..b9a74e8 100644 --- a/GAutomatorSdk/UnrealEngine/GAutomatorSdk/Plugins/GAutomator/Source/GAutomator/Private/Dispatcher/CommandDispatcher.cpp +++ b/GAutomatorSdk/UnrealEngine/GAutomatorSdk/Plugins/GAutomator/Source/GAutomator/Private/Dispatcher/CommandDispatcher.cpp @@ -335,26 +335,16 @@ namespace WeTestU3DAutomation { FCommandHandler CommandHandler(InValue); - std::mutex m; - std::condition_variable* cond_var= FCommandHandler::cond_var; + FScopedEvent FinishedEvent; + AsyncTask(ENamedThreads::GameThread, [&CommandHandler, &OutResponse, &FinishedEvent]() { - AsyncTask(ENamedThreads::GameThread, [&CommandHandler,&OutResponse,&m,cond_var]() { - - std::unique_lock lock(m); OutResponse =CommandHandler.HandleCommand(); UE_LOG(GALog, Log, TEXT("Response body : %s"), *OutResponse); - if(FCommandHandler::flag==0) - cond_var->notify_one(); + FinishedEvent.Trigger(); }); - std::unique_lock lock(m); - cond_var->wait(lock); - if (FCommandHandler::flag != 0) - { - OutResponse = CommandHandler.GetResponse(); - FCommandHandler::flag = 0; - } + return true; } @@ -392,4 +382,4 @@ namespace WeTestU3DAutomation std::condition_variable* FCommandHandler::cond_var = new std::condition_variable(); int FCommandHandler::flag = 0; -} \ No newline at end of file +}