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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
// Copyright Epic Games, Inc. All Rights Reserved.
#include "up_cmd.h"
#include <zencore/filesystem.h>
#include <zencore/logging.h>
#include <zencore/process.h>
#include <zenutil/zenserverprocess.h>
#include <memory>
namespace zen {
UpCommand::UpCommand()
{
m_Options.add_option("lifetime",
"",
"owner-pid",
"Specify owning process id",
cxxopts::value(m_OwnerPid)->default_value("0"),
"<identifier>");
m_Options.add_options()("config", "Path to Lua config file", cxxopts::value(m_ConfigFile));
}
UpCommand::~UpCommand() = default;
int
UpCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions, argc, argv);
if (!ParseOptions(argc, argv))
{
return 0;
}
{
ZenServerState State;
if (State.InitializeReadOnly())
{
struct EntryInfo
{
uint32_t Pid = 0;
uint16_t DesiredPort = 0;
uint16_t EffectivePort = 0;
};
std::vector<EntryInfo> RunningEntries;
State.Snapshot([&RunningEntries](const zen::ZenServerState::ZenServerEntry& Entry) {
RunningEntries.push_back(EntryInfo{.Pid = Entry.Pid.load(),
.DesiredPort = Entry.DesiredListenPort.load(),
.EffectivePort = Entry.EffectiveListenPort.load()});
});
if (RunningEntries.size() > 0)
{
ZEN_CONSOLE("Zen server already running. First instance at port {}, pid {}",
RunningEntries[0].EffectivePort,
RunningEntries[0].Pid);
return 0;
}
}
}
std::filesystem::path ExePath = zen::GetRunningExecutablePath();
ZenServerEnvironment ServerEnvironment;
ServerEnvironment.Initialize(ExePath.parent_path());
ZenServerInstance Server(ServerEnvironment);
if (m_OwnerPid != 0)
{
Server.SetOwnerPid(m_OwnerPid);
}
std::string AdditionalArguments;
if (!m_ConfigFile.empty())
{
AdditionalArguments = fmt::format("--config {}", m_ConfigFile);
}
Server.SpawnServer(0, AdditionalArguments);
int Timeout = 10000;
if (!Server.WaitUntilReady(Timeout))
{
ZEN_ERROR("zen server launch failed (timed out)");
}
else
{
ZEN_CONSOLE("zen server up");
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
AttachCommand::AttachCommand()
{
m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("8558"), "<hostport>");
m_Options.add_option("lifetime", "", "owner-pid", "Specify owning process id", cxxopts::value(m_OwnerPid), "<identifier>");
}
AttachCommand::~AttachCommand() = default;
int
AttachCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions, argc, argv);
if (!ParseOptions(argc, argv))
{
return 0;
}
ZenServerState Instance;
Instance.Initialize();
ZenServerState::ZenServerEntry* Entry = Instance.Lookup(m_Port);
if (!Entry)
{
ZEN_WARN("no zen server instance to add sponsor process to");
return 1;
}
if (!Entry->AddSponsorProcess(m_OwnerPid))
{
ZEN_WARN("unable to add sponsor process to running zen server instance");
return 1;
}
ZEN_CONSOLE("added sponsor process {} to running instance {} on port {}", m_OwnerPid, Entry->Pid.load(), m_Port);
return 0;
}
//////////////////////////////////////////////////////////////////////////
DownCommand::DownCommand()
{
m_Options.add_option("", "p", "port", "Host port", cxxopts::value(m_Port)->default_value("8558"), "<hostport>");
}
DownCommand::~DownCommand() = default;
int
DownCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
{
ZEN_UNUSED(GlobalOptions);
if (!ParseOptions(argc, argv))
{
return 0;
}
// Discover executing instances
ZenServerState Instance;
Instance.Initialize();
ZenServerState::ZenServerEntry* Entry = Instance.Lookup(m_Port);
if (!Entry)
{
ZEN_WARN("no zen server to bring down");
return 0;
}
try
{
std::filesystem::path ExePath = zen::GetRunningExecutablePath();
ZenServerEnvironment ServerEnvironment;
ServerEnvironment.Initialize(ExePath.parent_path());
ZenServerInstance Server(ServerEnvironment);
Server.AttachToRunningServer(m_Port);
ZEN_CONSOLE("attached to server on port {}, requesting shutdown", m_Port);
Server.Shutdown();
ZEN_CONSOLE("shutdown complete");
return 0;
}
catch (const std::exception& Ex)
{
ZEN_DEBUG("Exception caught when requesting shutdown: {}", Ex.what());
}
// Since we cannot obtain a handle to the process we are unable to block on the process
// handle to determine when the server has shut down. Thus we signal that we would like
// a shutdown via the shutdown flag and then exit.
ZEN_CONSOLE("requesting shutdown of server on port {}", m_Port);
Entry->SignalShutdownRequest();
return 0;
}
} // namespace zen
|