blob: 60ed273cb0e6e3961fc3f6d832575b360df515cb (
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <zencore/thread.h>
#include <spdlog/spdlog.h>
#include <atomic>
#include <filesystem>
class ZenServerEnvironment
{
public:
ZenServerEnvironment();
~ZenServerEnvironment();
void Initialize(std::filesystem::path ProgramBaseDir);
void InitializeForTest(std::filesystem::path ProgramBaseDir, std::filesystem::path TestBaseDir);
std::filesystem::path CreateNewTestDir();
std::filesystem::path ProgramBaseDir() const { return m_ProgramBaseDir; }
std::filesystem::path GetTestRootDir(std::string_view Path);
inline bool IsInitialized() const { return m_IsInitialized; }
inline bool IsTestEnvironment() const { return m_IsTestInstance; }
private:
std::filesystem::path m_ProgramBaseDir;
std::filesystem::path m_TestBaseDir;
bool m_IsInitialized = false;
bool m_IsTestInstance = false;
};
struct ZenServerInstance
{
ZenServerInstance(ZenServerEnvironment& TestEnvironment);
~ZenServerInstance();
void SignalShutdown() { m_ShutdownEvent.Set(); }
void WaitUntilReady();
[[nodiscard]] bool WaitUntilReady(int Timeout);
void EnableTermination() { m_Terminate = true; }
void EnableMesh() { m_MeshEnabled = true; }
void SetTestDir(std::filesystem::path TestDir)
{
ZEN_ASSERT(!m_Process.IsValid());
m_TestDir = TestDir;
}
void SpawnServer(int BasePort = 0);
private:
ZenServerEnvironment& m_Env;
zen::ProcessHandle m_Process;
zen::Event m_ReadyEvent;
zen::Event m_ShutdownEvent;
bool m_Terminate = false;
std::filesystem::path m_TestDir;
bool m_MeshEnabled = false;
};
/** Shared system state
*
* Used as a scratchpad to identify running instances etc
*
* The state lives in a memory-mapped file backed by the swapfile
*
*/
class ZenServerState
{
public:
ZenServerState();
~ZenServerState();
struct ZenServerEntry
{
std::atomic<uint32_t> Pid;
std::atomic<uint16_t> ListenPort;
uint16_t Flags;
uint8_t SessionId[12];
uint8_t Padding[12];
void Reset();
};
static_assert(sizeof(ZenServerEntry) == 32);
void Initialize();
[[nodiscard]] bool InitializeReadOnly();
ZenServerEntry* Lookup(int ListenPort);
ZenServerEntry* Register(int ListenPort);
void Sweep();
void Snapshot(std::function<void(const ZenServerEntry&)>&& Callback);
private:
void* m_hMapFile = nullptr;
ZenServerEntry* m_Data;
int m_MaxEntryCount = 4096 / sizeof(ZenServerEntry);
ZenServerEntry* m_OurEntry = nullptr;
};
|