2021-08-04 14:56:02 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-10-12 21:21:22 -04:00
|
|
|
#include "HAL/Platform.h"
|
2021-11-18 14:37:34 -05:00
|
|
|
#include "Containers/StringView.h"
|
|
|
|
|
#include "Containers/UnrealString.h"
|
|
|
|
|
#include "Dom/JsonObject.h"
|
2022-02-14 12:19:59 -05:00
|
|
|
#include "Misc/Optional.h"
|
2021-11-18 14:37:34 -05:00
|
|
|
#include "Misc/TVariant.h"
|
|
|
|
|
#include "Policies/PrettyJsonPrintPolicy.h"
|
|
|
|
|
#include "Serialization/JsonWriter.h"
|
2022-03-02 22:10:10 -05:00
|
|
|
#include "Templates/PimplPtr.h"
|
2021-11-18 14:37:34 -05:00
|
|
|
#include "Templates/UniquePtr.h"
|
2022-02-14 12:19:59 -05:00
|
|
|
#include "ZenGlobals.h"
|
2022-03-01 07:20:31 -05:00
|
|
|
#include "Async/Future.h"
|
2021-08-04 14:56:02 -04:00
|
|
|
|
2022-03-01 05:06:05 -05:00
|
|
|
#if UE_WITH_ZEN
|
|
|
|
|
# include "ZenStatistics.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-10-25 20:05:28 -04:00
|
|
|
#define UE_API ZEN_API
|
2021-09-22 13:00:17 -04:00
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
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();
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-01 05:06:05 -05:00
|
|
|
}
|
2021-11-18 14:37:34 -05:00
|
|
|
|
|
|
|
|
#if UE_WITH_ZEN
|
|
|
|
|
|
|
|
|
|
namespace UE::Zen
|
|
|
|
|
{
|
2021-08-04 14:56:02 -04:00
|
|
|
|
2021-09-22 13:00:17 -04:00
|
|
|
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:
|
2021-10-25 20:05:28 -04:00
|
|
|
UE_API FScopeZenService();
|
2021-09-22 13:00:17 -04:00
|
|
|
UE_API FScopeZenService(FStringView InstanceURL);
|
2021-11-18 14:37:34 -05:00
|
|
|
UE_API FScopeZenService(FServiceSettings&& InSettings);
|
2021-10-25 20:05:28 -04:00
|
|
|
UE_API ~FScopeZenService();
|
|
|
|
|
|
2021-09-22 13:00:17 -04:00
|
|
|
|
|
|
|
|
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();
|
2021-11-18 14:37:34 -05:00
|
|
|
UE_API bool IsDefaultServicePresent();
|
2021-09-22 13:00:17 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A representation of a Zen service instance. Generally not accessed directly, but via FScopeZenService.
|
|
|
|
|
*/
|
2021-08-04 14:56:02 -04:00
|
|
|
class FZenServiceInstance
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-10-12 21:21:22 -04:00
|
|
|
|
2021-10-25 20:05:28 -04:00
|
|
|
UE_API FZenServiceInstance();
|
2021-11-18 14:37:34 -05:00
|
|
|
UE_API FZenServiceInstance(FStringView InstanceURL);
|
|
|
|
|
UE_API FZenServiceInstance(FServiceSettings&& InSettings);
|
2021-10-25 20:05:28 -04:00
|
|
|
UE_API ~FZenServiceInstance();
|
2021-08-04 14:56:02 -04:00
|
|
|
|
2021-10-25 20:05:28 -04:00
|
|
|
inline const TCHAR* GetURL() const { return *URL; }
|
|
|
|
|
inline const TCHAR* GetHostName() const { return *HostName; }
|
|
|
|
|
inline uint16 GetPort() const { return Port; }
|
2021-11-18 14:37:34 -05:00
|
|
|
inline const FServiceSettings& GetServiceSettings() const { return Settings; }
|
2022-03-01 07:20:31 -05:00
|
|
|
UE_API bool GetStats(FZenStats& Stats);
|
2021-10-25 20:05:28 -04:00
|
|
|
UE_API bool IsServiceRunning();
|
|
|
|
|
UE_API bool IsServiceReady();
|
2022-01-21 04:24:08 -05:00
|
|
|
UE_API bool IsServiceRunningLocally() const { return bIsRunningLocally; }
|
2021-08-04 14:56:02 -04:00
|
|
|
|
2022-02-10 03:30:55 -05:00
|
|
|
static UE_API uint16 GetAutoLaunchedPort();
|
2021-11-18 14:37:34 -05:00
|
|
|
|
2021-08-04 14:56:02 -04:00
|
|
|
private:
|
2021-09-22 13:00:17 -04:00
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
void Initialize();
|
2021-09-22 13:00:17 -04:00
|
|
|
void PromptUserToStopRunningServerInstance(const FString& ServerFilePath);
|
|
|
|
|
FString ConditionalUpdateLocalInstall();
|
2021-11-18 14:37:34 -05:00
|
|
|
static bool AutoLaunch(const FServiceAutoLaunchSettings& InSettings, FString&& ExecutablePath, FString& OutHostName, uint16& OutPort);
|
2021-09-22 13:00:17 -04:00
|
|
|
|
2022-03-02 22:10:10 -05:00
|
|
|
mutable TPimplPtr<class FZenHttpRequest> StatsHttpRequest;
|
2022-03-01 07:20:31 -05:00
|
|
|
mutable TFuture<FZenStats> StatsRequest;
|
2022-02-14 12:19:59 -05:00
|
|
|
mutable FZenStats LastStats;
|
|
|
|
|
mutable uint64 LastStatsTime = 0;
|
|
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
FServiceSettings Settings;
|
2021-09-22 13:00:17 -04:00
|
|
|
FString URL;
|
|
|
|
|
FString HostName;
|
|
|
|
|
uint16 Port;
|
2022-01-26 10:54:48 -05:00
|
|
|
static uint16 AutoLaunchedPort;
|
2021-09-22 13:00:17 -04:00
|
|
|
bool bHasLaunchedLocal = false;
|
2022-01-21 04:24:08 -05:00
|
|
|
bool bIsRunningLocally = true;
|
2021-08-04 14:56:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace UE::Zen
|
|
|
|
|
|
|
|
|
|
#endif // UE_WITH_ZEN
|
2021-11-18 14:37:34 -05:00
|
|
|
|
|
|
|
|
#undef UE_API
|