Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API on Dedicated Server Game Port #162

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Config/AccessTransformers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Friend=(Class="AFGBuildableFactory", FriendClass="UFRM_Factory")
Friend=(Class="AFGSchematicManager", FriendClass="UFRM_Factory")
Accessor=(Class="AFGCharacterPlayer", Property="mCachedPlayerName")

#Friend=(Class="/Script/FactoryDedicatedServer.FGServerSubsystem", FriendClass="AFicsitRemoteMonitoring")
#Friend=(Class="/Script/FactoryDedicatedServer.FGServerAPIManager", FriendClass="AFicsitRemoteMonitoring")
Friend=(Class="UFGServerSubsystem", FriendClass="AFicsitRemoteMonitoring")
Friend=(Class="UFGServerAPIManager", FriendClass="AFicsitRemoteMonitoring")

BlueprintReadWrite=(Class="/Script/FactoryGame.FGItemDescriptor", Property="mPersistentBigIcon")

Expand Down
10 changes: 9 additions & 1 deletion Source/FicsitRemoteMonitoring/FicsitRemoteMonitoring.build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ public FicsitRemoteMonitoring(ReadOnlyTargetRules Target) : base(Target)
// Add uWebSockets
LoaduWebSockets(Target);

PublicDependencyModuleNames.Add("FactoryDedicatedServer");

if (Target.Type == TargetType.Server)
{
PublicDependencyModuleNames.Add("FactoryDedicatedServer");
// Define WITH_DEDICATED_SERVER=1 only for server targets

PublicDefinitions.Add("WITH_DEDICATED_SERVER=1");
} else
{
// Explicitly set WITH_DEDICATED_SERVER=0 for non-server builds to avoid warnings
PublicDefinitions.Add("WITH_DEDICATED_SERVER=0");
}

PublicDefinitions.Add("UWS_STATICLIB"); // If you're using the static version of uWS
Expand Down
58 changes: 30 additions & 28 deletions Source/FicsitRemoteMonitoring/Private/FicsitRemoteMonitoring.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "FicsitRemoteMonitoring.h"
#include "Async/Async.h"
#include "FRM_Request.h"
#include "HttpServerRequest.h"
#include "IHttpRouter.h"

us_listen_socket_t* SocketListener;
bool SocketRunning = false;
Expand Down Expand Up @@ -737,36 +739,36 @@ void AFicsitRemoteMonitoring::RegisterEndpoint(const FString& Method, const FStr

UE_LOGFMT(LogHttpServer, Log, "Registered API Endpoint: {APIName} - Current number of endpoints registered: {1}", APIName, APIEndpoints.Num());

/*
// Store the APIName in a member variable for use in HandleCSSEndpoint
StoredAPIName = APIName;
#if WITH_DEDICATED_SERVER
const UFGServerSubsystem* ServerSubsystem = UFGServerSubsystem::Get(GetWorld());
const UFGServerAPIManager* APIManager = ServerSubsystem->GetServerAPIManager();
const TSharedPtr<IHttpRouter> Router = APIManager->mHTTPRouter;
const FString DataPath = "/api/" + APIName;

UFGServerSubsystem* ServerSubsystem = UFGServerSubsystem::Get(GetWorld());
if (IsValid(ServerSubsystem)) { return; }
EHttpServerRequestVerbs RequestVerb = EHttpServerRequestVerbs::VERB_GET;

UFGServerAPIManager* APIManager = ServerSubsystem->GetServerAPIManager();
if (IsValid(APIManager)) { return; }

if (!IsRunningDedicatedServer()) {

// Store the APIName in a member variable for use in HandleCSSEndpoint
StoredAPIName = APIName;

UFGServerSubsystem* ServerSubsystem = UFGServerSubsystem::Get(GetWorld());
if (IsValid(ServerSubsystem)) { return; }

UFGServerAPIManager* APIManager = ServerSubsystem->GetServerAPIManager();
if (IsValid(APIManager)) { return; }

FFGRequestHandlerRegistration HandleRegistration = FFGRequestHandlerRegistration();
HandleRegistration.HandlerObject = this;
HandleRegistration.HandlerFunction = this->FindFunction(FName("HandleCSSEndpoint"));
HandleRegistration.FunctionName = FName(*APIName);
HandleRegistration.PrivilegeLevel = EPrivilegeLevel::None;
APIManager->mRegisteredHandlers.Add(FString(APIName), HandleRegistration);

};
*/
if (Method == "POST")
{
RequestVerb = EHttpServerRequestVerbs::VERB_POST;
}

Router->BindRoute(DataPath, RequestVerb,
[this, APIName](const FHttpServerRequest& Request, const FHttpResultCallback& OnComplete)
{
bool bSuccess = false;
FRequestData RequestData;
RequestData.QueryParams = Request.QueryParams;
TUniquePtr<FHttpServerResponse> Response = FHttpServerResponse::Create(
AFicsitRemoteMonitoring::HandleEndpoint(this->GetWorld(), APIName, RequestData, bSuccess),
"application/json");
OnComplete(MoveTemp(Response));
if (!bSuccess)
{
UE_LOGFMT(LogHttpServer, Log, "Game Port - API Endpoint Not Found: {APIName}", APIName);
};
return true;
});
#endif
}

FCallEndpointResponse AFicsitRemoteMonitoring::CallEndpoint(UObject* WorldContext, FString InEndpoint, FRequestData RequestData, bool& bSuccess)
Expand Down
18 changes: 13 additions & 5 deletions Source/FicsitRemoteMonitoring/Public/FicsitRemoteMonitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "UObject/NoExportTypes.h"
#include "Async/Async.h"
#include "Templates/Function.h" // Required for function pointers
//#include "FactoryDedicatedServer/Public/FGServerSubsystem.h"
//#include "FactoryDedicatedServer/Public/Networking/FGServerAPIManager.h"
#include "FRM_Drones.h"
#include "FRM_Factory.h"
#include "FRM_Player.h"
Expand Down Expand Up @@ -56,6 +54,14 @@ THIRD_PARTY_INCLUDES_START
#include "ThirdParty/uWebSockets/App.h"
THIRD_PARTY_INCLUDES_END

#if WITH_DEDICATED_SERVER
#include "FactoryDedicatedServer/Public/FGServerSubsystem.h"
#include "FactoryDedicatedServer/Public/Networking/FGServerAPIManager.h"
#include "JsonUtilities.h"
#include "HttpServerRequest.h"
#include "IHttpRouter.h"
#endif

#include "FicsitRemoteMonitoring.generated.h"

struct FWebSocketUserData {
Expand Down Expand Up @@ -119,11 +125,13 @@ class FICSITREMOTEMONITORING_API AFicsitRemoteMonitoring : public AModSubsystem

public:

AFicsitRemoteMonitoring();
virtual ~AFicsitRemoteMonitoring();

#if WITH_DEDICATED_SERVER
friend class UFGServerSubsystem;
friend class UFGServerAPIManager;
#endif

AFicsitRemoteMonitoring();
virtual ~AFicsitRemoteMonitoring();

/** Get the subsystem in the current world, can be nullptr, e.g. on game ending (destroy) or game startup. */
static AFicsitRemoteMonitoring* Get(UWorld* world);
Expand Down
Loading