aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/hub/storageserverinstance.h
blob: a2f3d25d72f0e3eb79321c473da6435100e018b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "resourcemetrics.h"

#include <zenutil/zenserverprocess.h>

#include <atomic>
#include <filesystem>

namespace zen {

/**
 * Storage Server Instance
 *
 * This class manages the lifecycle of a storage server instance, and
 * provides functions to query its state. There should be one instance
 * per module ID.
 */
class StorageServerInstance
{
public:
	StorageServerInstance(ZenServerEnvironment& RunEnvironment,
						  std::string_view		ModuleId,
						  std::filesystem::path FileHydrationPath,
						  std::filesystem::path HydrationTempPath);
	~StorageServerInstance();

	void Provision();
	void Deprovision();

	void Hibernate();
	void Wake();

	const ResourceMetrics& GetResourceMetrics() const { return m_ResourceMetrics; }

	inline std::string_view GetModuleId() const { return m_ModuleId; }
	inline bool				IsProvisioned() const { return m_IsProvisioned.load(); }

	inline uint16_t GetBasePort() const { return m_ServerInstance.GetBasePort(); }

#if ZEN_PLATFORM_WINDOWS
	void SetJobObject(JobObject* InJobObject) { m_JobObject = InJobObject; }
#endif

private:
	void				  WakeLocked();
	RwLock				  m_Lock;
	std::string			  m_ModuleId;
	std::atomic<bool>	  m_IsProvisioned{false};
	std::atomic<bool>	  m_IsHibernated{false};
	ZenServerInstance	  m_ServerInstance;
	std::filesystem::path m_BaseDir;
	std::filesystem::path m_TempDir;
	std::filesystem::path m_HydrationPath;
	ResourceMetrics		  m_ResourceMetrics;
#if ZEN_PLATFORM_WINDOWS
	JobObject* m_JobObject = nullptr;
#endif

	void SpawnServerProcess();

	void Hydrate();
	void Dehydrate();
};

}  // namespace zen