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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "main.h"
#include "BuildInfo.h"
#include "Logger.h"
#include "MemorySettingsRepository.h"
#include "Root.h"
#include "tclap/CmdLine.h"
#include "OSSupport/ConsoleSignalHandler.h"
#include "OSSupport/NetworkSingleton.h"
#include "OSSupport/MiniDumpWriter.h"
#include "OSSupport/SleepResolutionBooster.h"
#include "OSSupport/StartAsService.h"
bool g_ShouldLogCommIn;
bool g_ShouldLogCommOut;
bool g_RunAsService;
////////////////////////////////////////////////////////////////////////////////
// ParseArguments - Read the startup arguments and store into a settings object
static void ParseArguments(int argc, char ** argv, cMemorySettingsRepository & a_Settings)
{
// Parse the comand line args:
TCLAP::CmdLine cmd("Cuberite");
TCLAP::ValueArg<int> slotsArg ("s", "max-players", "Maximum number of slots for the server to use, overrides setting in setting.ini", false, -1, "number", cmd);
TCLAP::ValueArg<AString> confArg ("c", "config-file", "Config file to use", false, "settings.ini", "string", cmd);
TCLAP::MultiArg<int> portsArg ("p", "port", "The port number the server should listen to", false, "port", cmd);
TCLAP::SwitchArg commLogArg ("", "log-comm", "Log server client communications to file", cmd);
TCLAP::SwitchArg commLogInArg ("", "log-comm-in", "Log inbound server client communications to file", cmd);
TCLAP::SwitchArg commLogOutArg ("", "log-comm-out", "Log outbound server client communications to file", cmd);
TCLAP::SwitchArg crashDumpFull ("", "crash-dump-full", "Crashdumps created by the server will contain full server memory", cmd);
TCLAP::SwitchArg crashDumpGlobals("", "crash-dump-globals", "Crashdumps created by the server will contain the global variables' values", cmd);
TCLAP::SwitchArg noBufArg ("", "no-output-buffering", "Disable output buffering", cmd);
TCLAP::SwitchArg noFileLogArg ("", "no-log-file", "Disable logging to file", cmd);
TCLAP::SwitchArg runAsServiceArg ("d", "service", "Run as a service on Windows, or daemon on UNIX like systems", cmd);
cmd.parse(argc, argv);
// Copy the parsed args' values into a settings repository:
if (confArg.isSet())
{
AString conf_file = confArg.getValue();
a_Settings.AddValue("Server", "ConfigFile", conf_file);
}
if (slotsArg.isSet())
{
int slots = slotsArg.getValue();
a_Settings.AddValue("Server", "MaxPlayers", static_cast<Int64>(slots));
}
if (portsArg.isSet())
{
for (auto port: portsArg.getValue())
{
a_Settings.AddValue("Server", "Ports", std::to_string(port));
}
}
if (noFileLogArg.getValue())
{
a_Settings.AddValue("Server", "DisableLogFile", true);
}
if (commLogArg.getValue())
{
g_ShouldLogCommIn = true;
g_ShouldLogCommOut = true;
}
else
{
g_ShouldLogCommIn = commLogInArg.getValue();
g_ShouldLogCommOut = commLogOutArg.getValue();
}
if (noBufArg.getValue())
{
setvbuf(stdout, nullptr, _IONBF, 0);
}
a_Settings.SetReadOnly();
if (runAsServiceArg.getValue())
{
g_RunAsService = true;
}
// Apply the CrashDump flags for platforms that support them:
if (crashDumpGlobals.getValue())
{
MiniDumpWriter::AddDumpFlags(MiniDumpFlags::WithDataSegments);
}
if (crashDumpFull.getValue())
{
MiniDumpWriter::AddDumpFlags(MiniDumpFlags::WithFullMemory);
}
}
////////////////////////////////////////////////////////////////////////////////
// UniversalMain - Main startup logic for both standard running and as a service
static int UniversalMain(int argc, char * argv[], const bool a_RunningAsService)
{
const struct MiniDumpWriterRAII
{
MiniDumpWriterRAII()
{
// Registers a last chance exception handler to write a minidump on crash:
MiniDumpWriter::Register();
}
~MiniDumpWriterRAII()
{
MiniDumpWriter::Unregister();
}
} MiniDumpWriter;
const struct SleepResolutionBoosterRAII
{
SleepResolutionBoosterRAII()
{
// Boost timer resolution to keep TPS high:
SleepResolutionBooster::Register();
}
~SleepResolutionBoosterRAII()
{
SleepResolutionBooster::Unregister();
}
} SleepResolutionBooster;
// Register signal handlers, enabling graceful shutdown from the terminal:
ConsoleSignalHandler::Register();
// Initialize logging subsystem:
cLogger::InitiateMultithreading();
try
{
cMemorySettingsRepository Settings;
ParseArguments(argc, argv, Settings); // Make sure g_RunAsService is set correctly before checking its value.
// Attempt to run as a service:
if (g_RunAsService && !a_RunningAsService)
{
// This will either fork or call UniversalMain again:
if (StartAsService::MakeIntoService<&UniversalMain>())
{
return EXIT_SUCCESS;
}
}
while (true)
{
const struct NetworkRAII
{
NetworkRAII()
{
// Initialize LibEvent:
cNetworkSingleton::Get().Initialise();
}
~NetworkRAII()
{
// Shutdown all of LibEvent:
cNetworkSingleton::Get().Terminate();
}
} LibEvent;
cRoot Root;
if (!Root.Run(Settings))
{
break;
}
}
return EXIT_SUCCESS;
}
catch (const fmt::format_error & Oops)
{
std::cerr << "Formatting exception: " << Oops.what() << '\n';
}
catch (const TCLAP::ArgException & Oops)
{
std::cerr << fmt::sprintf("Error reading command line {} for argument {}\n", Oops.error(), Oops.argId());
}
catch (const std::exception & Oops)
{
std::cerr << "Standard exception: " << Oops.what() << '\n';
}
catch (...)
{
std::cerr << "Unknown exception!\n";
}
return EXIT_FAILURE;
}
int main(int argc, char ** argv)
{
#if !defined(NDEBUG) && defined(_MSC_VER)
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
// _X: The simple built-in CRT leak finder - simply break when allocating the Nth block ({N} is listed in the leak output)
// Only useful when the leak is in the same sequence all the time
// _CrtSetBreakAlloc(85950);
#endif // !NDEBUG && _MSC_VER
return UniversalMain(argc, argv, false);
}
|