Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
Main.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include <atomic>
7#include <chrono>
8#include <csignal>
9#include <ctime>
10#include <filesystem>
11#include <fstream>
12#include <iostream>
13#include <string>
14#include <thread>
15
16namespace
17{
18std::atomic<bool> running(true);
19
20void Stop(int)
21{
22 running = false;
23}
24
25void PrintUsage(char const* executable)
26{
27 std::cout
28 << "Usage: " << executable << " [control-file]\n"
29 << "\n"
30 << "Creates a packet log control file while this process is running.\n"
31 << "Configured servers write per-session packet logs when they see that file.\n"
32 << "\n"
33 << "Default control-file: packetlogserver.active\n";
34}
35
36bool WriteControlFile(std::filesystem::path const& controlFile)
37{
38 std::error_code error;
39 std::filesystem::path parent = controlFile.parent_path();
40 if (!parent.empty())
41 {
42 std::filesystem::create_directories(parent, error);
43 if (error)
44 {
45 std::cerr << "Failed to create control file directory " << parent.string()
46 << ": " << error.message() << "\n";
47 return false;
48 }
49 }
50
51 std::ofstream file(controlFile, std::ios::out | std::ios::trunc);
52 if (!file)
53 {
54 std::cerr << "Failed to create control file " << controlFile.string() << "\n";
55 return false;
56 }
57
58 file << "SkyFire packet log server active\n";
59 file << "StartedUnixTime=" << static_cast<long long>(std::time(nullptr)) << "\n";
60 return true;
61}
62}
63
64int main(int argc, char** argv)
65{
66 if (argc > 1)
67 {
68 std::string arg = argv[1];
69 if (arg == "-h" || arg == "--help" || arg == "/?")
70 {
71 PrintUsage(argv[0]);
72 return 0;
73 }
74 }
75
76 std::filesystem::path controlFile = argc > 1 ? argv[1] : "packetlogserver.active";
77
78 if (!WriteControlFile(controlFile))
79 return 1;
80
81 std::signal(SIGINT, Stop);
82 std::signal(SIGTERM, Stop);
83
84 std::cout << "SkyFire packet log server active.\n";
85 std::cout << "Control file: " << std::filesystem::absolute(controlFile).string() << "\n";
86 std::cout << "Press Ctrl+C to stop packet logging.\n";
87
88 while (running)
89 std::this_thread::sleep_for(std::chrono::milliseconds(250));
90
91 std::error_code error;
92 std::filesystem::remove(controlFile, error);
93 if (error)
94 {
95 std::cerr << "Failed to remove control file " << controlFile.string()
96 << ": " << error.message() << "\n";
97 return 1;
98 }
99
100 std::cout << "SkyFire packet log server stopped.\n";
101 return 0;
102}
int main(int argc, char **argv)
Launch the auth server.
Definition Main.cpp:207