-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (64 loc) · 1.57 KB
/
main.cpp
File metadata and controls
76 lines (64 loc) · 1.57 KB
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
#include <atomic>
#include <csignal>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <string_view>
#include "server.hpp"
namespace
{
std::atomic<bool> server_running = true;
void print_usage(std::string_view basename)
{
std::cout << "Usage: " << basename
<< " <ADMIN_USERNAME> <ADMIN_PASSWORD> <UDP_PORT>\n";
}
int parse_port(const char* const str)
{
const int port = std::stoi(str);
static const int maxPort = 65535;
if (port < 0 || port > maxPort)
{
throw std::out_of_range("Invalid port number");
}
return port;
}
} // namespace
int main(int argc, char* argv[])
{
if (argc == 0)
{
std::cerr << "argc is zero\n";
return EXIT_FAILURE;
}
if (argc == 2 && std::string_view(argv[1]) == "-h")
{
print_usage(argv[0]);
return EXIT_SUCCESS;
}
if (argc < 4)
{
std::cerr << "Not enough arguments!\n";
print_usage(argv[0]);
return EXIT_FAILURE;
}
try
{
const int port = parse_port(argv[3]);
auth::server::Server server{argv[1], argv[2], port, server_running};
auto stopServer = [](int)
{
std::cout << "Stopping server...\n";
server_running.store(false, std::memory_order_relaxed);
};
std::signal(SIGINT, stopServer);
std::signal(SIGTERM, stopServer);
server.Run();
return EXIT_SUCCESS;
}
catch (const std::exception& err)
{
std::cerr << "Auth server failure: " << err.what() << '\n';
}
return EXIT_FAILURE;
}