Files
UnrealEngineUWP/Engine/Source/Developer/Zen/Public/ZenServerInterface.h
zousar shaker 625a874551 Resolve compile issue in UE Zen client caused by including an Internal header from a Public header.
This is being resolved by cherrypicking 19196840 by mark.lintott from UE5/Main (see original changelist details at the end of this cl description).  On top of the cherrypick, there was a change made to re-use a single Http request when fetching stats.  This avoids the overhead of doing name resolution repeatedly if the zen instance has a name instead of IP address.

Tested by:
- Running ShooterGame editor & observing UnrealInsights for ZenHttp_CurlPerform timers and ensuring they are not happening on the game thread when the editor is sitting idle (they are on background threads instead)
-Terminating ZenServer.exe while the editor is running and observing whether the framerate of the editor is negatively affected

#rb matt.peters
[FYI] mark.lintott
#jira UE-132849
#jira UE-143182
#lockdown aurel.cordonnier
#preflight 6220038831454c90cc13a123

Original changelist:
============
Blocking Zen HTTP stats request now runs async using futures
#rb Andriy.Tylychko
#[fyi] Zousar.Shaker
#jira UE-132849
#preflight 621e0c71e15c51d8c5bd5967

#ushell-cherrypick of 19196840 by mark.lintott

#ROBOMERGE-OWNER: zousar.shaker
#ROBOMERGE-AUTHOR: zousar.shaker
#ROBOMERGE-SOURCE: CL 19234318 in //UE5/Release-5.0/... via CL 19236659
#ROBOMERGE-BOT: UE5 (Release-Engine-Staging -> Main) (v921-19075845)

[CL 19237357 by zousar shaker in ue5-main branch]
2022-03-02 22:10:10 -05:00

157 lines
4.9 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "HAL/Platform.h"
#include "Containers/StringView.h"
#include "Containers/UnrealString.h"
#include "Dom/JsonObject.h"
#include "Misc/Optional.h"
#include "Misc/TVariant.h"
#include "Policies/PrettyJsonPrintPolicy.h"
#include "Serialization/JsonWriter.h"
#include "Templates/PimplPtr.h"
#include "Templates/UniquePtr.h"
#include "ZenGlobals.h"
#include "Async/Future.h"
#if UE_WITH_ZEN
# include "ZenStatistics.h"
#endif
#define UE_API ZEN_API
namespace UE::Zen
{
struct FServiceConnectSettings
{
FString HostName;
uint16 Port = 1337;
};
struct FServiceAutoLaunchSettings
{
FString DataPath;
FString ExtraArgs;
uint16 DesiredPort = 1337;
bool bShowConsole = false;
bool bLimitProcessLifetime = false;
};
struct FServiceSettings
{
TVariant<FServiceAutoLaunchSettings, FServiceConnectSettings> SettingsVariant;
inline bool IsAutoLaunch() const { return SettingsVariant.IsType<FServiceAutoLaunchSettings>(); }
inline bool IsConnectExisting() const { return SettingsVariant.IsType<FServiceConnectSettings>(); }
UE_API void ReadFromConfig();
UE_API void ReadFromJson(FJsonObject& JsonObject);
UE_API void ReadFromURL(FStringView InstanceURL);
UE_API void WriteToJson(TJsonWriter<TCHAR, TPrettyJsonPrintPolicy<TCHAR>>& Writer) const;
private:
bool TryApplyAutoLaunchOverride();
};
}
#if UE_WITH_ZEN
namespace UE::Zen
{
class FZenServiceInstance;
/**
* Type used to declare usage of a Zen server instance whether the shared default instance or a unique non-default instance.
* Used to help manage launch, and optionally in the future, shutdown a shared default instance. Use the default constructor
* to reference the default instance (which may be launched on demand), or use the non-default constructors with a specific
* URL or HostName/Port pair which is required to pre-exist (will not be auto-launched).
*/
class FScopeZenService
{
public:
UE_API FScopeZenService();
UE_API FScopeZenService(FStringView InstanceURL);
UE_API FScopeZenService(FServiceSettings&& InSettings);
UE_API ~FScopeZenService();
UE_API const FZenServiceInstance& GetInstance() const { return *ServiceInstance; }
UE_API FZenServiceInstance& GetInstance() { return *ServiceInstance; }
private:
FZenServiceInstance* ServiceInstance;
TUniquePtr<UE::Zen::FZenServiceInstance> UniqueNonDefaultInstance;
};
/**
* Gets the default Zen service instance. The default instance is configured through ZenServiceInstance INI section.
* The default instance can (depending on configuration):
* - Auto-launched on demand (optionally elevated on Windows)
* - Be copied out of the workspace tree before execution
* - Shared between multiple tools running concurrently (implemented by launching multiple instances and expecting them to communicate and shutdown as needed)
* - Instigate self-shutdown when all processes that requested it have terminated
* - Use a subdirectory of the existing local DDC cache path as Zen's data path
* - Use an upstream Zen service
* - Be overridden by commandline arguments to reference an existing running instance instead of auto-launching a new instance.
* Note that no assumptions should be made about the hostname/port of the default instance. Calling code should instead expect
* that the instance is authoritative over the hostname/port that it will end up using and should query that information from the
* instance as needed.
*/
UE_API FZenServiceInstance& GetDefaultServiceInstance();
UE_API bool IsDefaultServicePresent();
/**
* A representation of a Zen service instance. Generally not accessed directly, but via FScopeZenService.
*/
class FZenServiceInstance
{
public:
UE_API FZenServiceInstance();
UE_API FZenServiceInstance(FStringView InstanceURL);
UE_API FZenServiceInstance(FServiceSettings&& InSettings);
UE_API ~FZenServiceInstance();
inline const TCHAR* GetURL() const { return *URL; }
inline const TCHAR* GetHostName() const { return *HostName; }
inline uint16 GetPort() const { return Port; }
inline const FServiceSettings& GetServiceSettings() const { return Settings; }
UE_API bool GetStats(FZenStats& Stats);
UE_API bool IsServiceRunning();
UE_API bool IsServiceReady();
UE_API bool IsServiceRunningLocally() const { return bIsRunningLocally; }
static UE_API uint16 GetAutoLaunchedPort();
private:
void Initialize();
void PromptUserToStopRunningServerInstance(const FString& ServerFilePath);
FString ConditionalUpdateLocalInstall();
static bool AutoLaunch(const FServiceAutoLaunchSettings& InSettings, FString&& ExecutablePath, FString& OutHostName, uint16& OutPort);
mutable TPimplPtr<class FZenHttpRequest> StatsHttpRequest;
mutable TFuture<FZenStats> StatsRequest;
mutable FZenStats LastStats;
mutable uint64 LastStatsTime = 0;
FServiceSettings Settings;
FString URL;
FString HostName;
uint16 Port;
static uint16 AutoLaunchedPort;
bool bHasLaunchedLocal = false;
bool bIsRunningLocally = true;
};
} // namespace UE::Zen
#endif // UE_WITH_ZEN
#undef UE_API