Hey all! I am confronted with a requirement to setup a REST server in an unreal project in order to send requests from some clients to this server which should modify the projects environment, behaviour, etc…
First shot i took was trying to include cpprestsdk, also known as Casablanca, into the StrategyGame example project. I linked the lib using the Build.cs and included the REST server code into the Module.cpp on Startup like this.
#include "StrategyGame.h"
#include "StrategyHUDSoundsWidgetStyle.h"
#include "StrategyHUDWidgetStyle.h"
#include "StrategyMenuWidgetStyle.h"
//-------------------------------------------------------
#include <cpprest/http_client.h>
#include <cpprest/http_listener.h>
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
using namespace utility;
//-------------------------------------------------------
class FStrategyGameModule : public FDefaultGameModuleImpl
{
virtual void StartupModule() override
{
//-------------------------------------------------------
http_listener* m_listener = nullptr;
{
string_t strScheme = U("http");
string_t strHost = U("localhost");
string_t strPort = U("7654");
uri_builder oBuilder;
oBuilder.set_scheme(strScheme);
oBuilder.set_host(strHost);
oBuilder.set_port(strPort);
m_listener = new http_listener(oBuilder.to_uri());
}
m_listener->open().wait();
m_listener->support([](http_request request)
{
http_response response;
response.set_status_code(status_codes::ExpectationFailed);
request.reply(response);
}
);
//-------------------------------------------------------
//Hot reload hack
FSlateStyleRegistry::UnRegisterSlateStyle(FStrategyStyle::GetStyleSetName());
FStrategyStyle::Initialize();
}
...
It does compile and run, though whenever a request is sent and handled, the app crashes most of the time with something like
Exception at 0x00007FFE2FA45328 (UE4Editor-Core.dll) in UE4Editor.exe: 0xC0000005: Access violation writing location 0x0000000000000010
I managed to break the problem down to something related to ending threads, as cpprestsdk creates a new thread for every incoming request. when responded the thread is most likely ended, but in this scenario it seems to force the unreal engine to crash. if i’m actively preving the thread from being ending with something very bad like
request.reply(response);
while (true)
{
FPlatformProcess::Sleep(120.0f);
UE_LOG(LogTemp, Log, TEXT("do not kill request thread"));
}
no crashes occur, but of course that’s no feasible solution at all!
Can anybody point me to the right direction or has another solution on adding a REST server to my unreal project?