Cuberite
A lightweight, fast and extensible game server for Minecraft
main.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "main.h"
5 #include "BuildInfo.h"
6 #include "Logger.h"
8 #include "Root.h"
9 #include "tclap/CmdLine.h"
10 
16 
17 
18 
19 
20 
24 
25 
26 
27 
28 
30 // ParseArguments - Read the startup arguments and store into a settings object
31 
32 static void ParseArguments(int argc, char ** argv, cMemorySettingsRepository & a_Settings)
33 {
34  // Parse the comand line args:
35  TCLAP::CmdLine cmd("Cuberite");
36  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);
37  TCLAP::ValueArg<AString> confArg ("c", "config-file", "Config file to use", false, "settings.ini", "string", cmd);
38  TCLAP::MultiArg<int> portsArg ("p", "port", "The port number the server should listen to", false, "port", cmd);
39  TCLAP::SwitchArg commLogArg ("", "log-comm", "Log server client communications to file", cmd);
40  TCLAP::SwitchArg commLogInArg ("", "log-comm-in", "Log inbound server client communications to file", cmd);
41  TCLAP::SwitchArg commLogOutArg ("", "log-comm-out", "Log outbound server client communications to file", cmd);
42  TCLAP::SwitchArg crashDumpFull ("", "crash-dump-full", "Crashdumps created by the server will contain full server memory", cmd);
43  TCLAP::SwitchArg crashDumpGlobals("", "crash-dump-globals", "Crashdumps created by the server will contain the global variables' values", cmd);
44  TCLAP::SwitchArg noBufArg ("", "no-output-buffering", "Disable output buffering", cmd);
45  TCLAP::SwitchArg noFileLogArg ("", "no-log-file", "Disable logging to file", cmd);
46  TCLAP::SwitchArg runAsServiceArg ("d", "service", "Run as a service on Windows, or daemon on UNIX like systems", cmd);
47  cmd.parse(argc, argv);
48 
49  // Copy the parsed args' values into a settings repository:
50  if (confArg.isSet())
51  {
52  AString conf_file = confArg.getValue();
53  a_Settings.AddValue("Server", "ConfigFile", conf_file);
54  }
55  if (slotsArg.isSet())
56  {
57  int slots = slotsArg.getValue();
58  a_Settings.AddValue("Server", "MaxPlayers", static_cast<Int64>(slots));
59  }
60  if (portsArg.isSet())
61  {
62  for (auto port: portsArg.getValue())
63  {
64  a_Settings.AddValue("Server", "Ports", std::to_string(port));
65  }
66  }
67  if (noFileLogArg.getValue())
68  {
69  a_Settings.AddValue("Server", "DisableLogFile", true);
70  }
71  if (commLogArg.getValue())
72  {
73  g_ShouldLogCommIn = true;
74  g_ShouldLogCommOut = true;
75  }
76  else
77  {
78  g_ShouldLogCommIn = commLogInArg.getValue();
79  g_ShouldLogCommOut = commLogOutArg.getValue();
80  }
81  if (noBufArg.getValue())
82  {
83  setvbuf(stdout, nullptr, _IONBF, 0);
84  }
85  a_Settings.SetReadOnly();
86 
87  if (runAsServiceArg.getValue())
88  {
89  g_RunAsService = true;
90  }
91 
92  // Apply the CrashDump flags for platforms that support them:
93  if (crashDumpGlobals.getValue())
94  {
96  }
97  if (crashDumpFull.getValue())
98  {
100  }
101 }
102 
103 
104 
105 
106 
108 // UniversalMain - Main startup logic for both standard running and as a service
109 
110 static int UniversalMain(int argc, char * argv[], const bool a_RunningAsService)
111 {
112  const struct MiniDumpWriterRAII
113  {
114  MiniDumpWriterRAII()
115  {
116  // Registers a last chance exception handler to write a minidump on crash:
118  }
119 
120  ~MiniDumpWriterRAII()
121  {
123  }
124  } MiniDumpWriter;
125 
126  const struct SleepResolutionBoosterRAII
127  {
128  SleepResolutionBoosterRAII()
129  {
130  // Boost timer resolution to keep TPS high:
132  }
133 
134  ~SleepResolutionBoosterRAII()
135  {
137  }
139 
140  // Register signal handlers, enabling graceful shutdown from the terminal:
142 
143  // Initialize logging subsystem:
145 
146  try
147  {
148  cMemorySettingsRepository Settings;
149  ParseArguments(argc, argv, Settings); // Make sure g_RunAsService is set correctly before checking its value.
150 
151  // Attempt to run as a service:
152  if (g_RunAsService && !a_RunningAsService)
153  {
154  // This will either fork or call UniversalMain again:
155  if (StartAsService::MakeIntoService<&UniversalMain>())
156  {
157  return EXIT_SUCCESS;
158  }
159  }
160 
161  while (true)
162  {
163  const struct NetworkRAII
164  {
165  NetworkRAII()
166  {
167  // Initialize LibEvent:
169  }
170 
171  ~NetworkRAII()
172  {
173  // Shutdown all of LibEvent:
175  }
176  } LibEvent;
177 
178  cRoot Root;
179  if (!Root.Run(Settings))
180  {
181  break;
182  }
183  }
184 
185  return EXIT_SUCCESS;
186  }
187  catch (const fmt::format_error & Oops)
188  {
189  std::cerr << "Formatting exception: " << Oops.what() << '\n';
190  }
191  catch (const TCLAP::ArgException & Oops)
192  {
193  std::cerr << fmt::sprintf("Error reading command line {} for argument {}\n", Oops.error(), Oops.argId());
194  }
195  catch (const std::exception & Oops)
196  {
197  std::cerr << "Standard exception: " << Oops.what() << '\n';
198  }
199  catch (...)
200  {
201  std::cerr << "Unknown exception!\n";
202  }
203 
204  return EXIT_FAILURE;
205 }
206 
207 
208 
209 
210 
211 int main(int argc, char ** argv)
212 {
213 #if !defined(NDEBUG) && defined(_MSC_VER)
214  _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
215 
216  // _X: The simple built-in CRT leak finder - simply break when allocating the Nth block ({N} is listed in the leak output)
217  // Only useful when the leak is in the same sequence all the time
218  // _CrtSetBreakAlloc(85950);
219 
220 #endif // !NDEBUG && _MSC_VER
221 
222  return UniversalMain(argc, argv, false);
223 }
signed long long Int64
Definition: Globals.h:151
bool g_ShouldLogCommOut
If set to true, the protocols will log each player's outgoing (S->C) communication to a per-connectio...
Definition: main.cpp:22
int main(int argc, char **argv)
Definition: main.cpp:211
static void ParseArguments(int argc, char **argv, cMemorySettingsRepository &a_Settings)
Definition: main.cpp:32
bool g_RunAsService
If set to true, binary will attempt to run as a service.
Definition: main.cpp:23
bool g_ShouldLogCommIn
If set to true, the protocols will log each player's incoming (C->S) communication to a per-connectio...
Definition: main.cpp:21
static int UniversalMain(int argc, char *argv[], const bool a_RunningAsService)
Definition: main.cpp:110
std::string AString
Definition: StringUtils.h:11
static void AddDumpFlags(const MiniDumpFlags)
static void Register()
static void Unregister()
static void InitiateMultithreading()
Definition: Logger.cpp:53
virtual void AddValue(const AString &a_KeyName, const AString &a_ValueName, const AString &a_Value) override
Adds a new value to the specified key.
void Terminate(void)
Terminates all network-related threads.
static cNetworkSingleton & Get(void)
Returns the singleton instance of this class.
void Initialise(void)
Initialises all network-related threads.
The root of the object hierarchy.
Definition: Root.h:50
bool Run(cSettingsRepositoryInterface &a_OverridesRepo)
Run the server.
Definition: Root.cpp:103