Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (136 loc) · 5.47 KB
/
Copy pathmain.cpp
File metadata and controls
162 lines (136 loc) · 5.47 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
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
#include <atomic>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <signal.h>
#include <thread>
#include <vector>
#include <asio.hpp>
#include "version.hpp"
#include "xtrpg/config/ConfigManager.hpp"
#include "xtrpg/interface/Observer.hpp"
#include "xtrpg/network/SocketConnectionListener.hpp"
#include "xtrpg/network/TlsConfig.hpp"
#include "xtrpg/network/TlsConfigProvider.hpp"
#include "xtrpg/xml/tokenizer/XmlStreamTokenizer.hpp"
#include "xtrpg/xmpp/ClientConnectionManager.hpp"
#include "xtrpg/xmpp/session/ClientSession.hpp"
#ifdef _WIN32
#define _WINSOCKAPI_
#include <windows.h>
#endif
// Global flag for signal handling
std::atomic<bool> g_shouldShutdown{false};
// Signal handler for graceful shutdown
void signalHandler(int signal) {
std::cout << "\nReceived signal " << signal
<< ", initiating graceful shutdown..." << std::endl;
g_shouldShutdown = true;
}
int main(int argc, char *argv[]) {
#ifdef _WIN32
// Set console codepages to UTF-8 (65001)
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
try {
// Output preamble information about the application and its configuration
// system.
std::cout << "XTRPG: A XMPP Server" << std::endl
<< "Version: " << xtrpg::VERSION << std::endl
<< "Copyright (C) 2026 XTRPG Contributors" << std::endl
<< "License: MIT" << std::endl
<< " "
"<https://github.com/xtrpg/xtrpg-cpp-server/blob/main/LICENSE>"
<< std::endl
<< std::endl
<< "This is free and open-source software." << std::endl
<< "You are free to use, modify, and redistribute it under the "
"terms of the MIT License."
<< std::endl
<< "There is NO WARRANTY for this software." << std::endl
<< std::endl;
// Discover and register all modules' configuration schemas, then load the
// configuration file and parse command-line arguments.
xtrpg::config::ConfigManager configManager;
configManager.registerAllDiscoveredModules();
std::ifstream configFile("./config.toml");
if (configFile) {
configManager.loadTomlFile(configFile);
}
configManager.parseCLI(argc, argv);
// Validate TLS certificate and key files exist before starting server
auto certPath = configManager.get<std::string>("tls", "cert_path")
.value_or("./server.crt");
auto keyPath = configManager.get<std::string>("tls", "key_path")
.value_or("./server.key");
if (!std::filesystem::exists(certPath)) {
std::cerr << "[ERROR] TLS certificate file not found: " << certPath
<< std::endl;
return 1;
}
if (!std::filesystem::exists(keyPath)) {
std::cerr << "[ERROR] TLS key file not found: " << keyPath << std::endl;
return 1;
}
std::cout << "[INFO] TLS configuration validated:" << std::endl
<< " Certificate: " << certPath << std::endl
<< " Key: " << keyPath << std::endl;
// Initialize the shared TLS settings object during startup.
xtrpg::network::initializeTlsSettings(
xtrpg::network::TlsSettings{.certPath = certPath, .keyPath = keyPath});
// Initialize Asio IO context for async I/O operations
asio::io_context ioContext;
// Get the port from configuration (default 5222 for XMPP C2S)
auto portValue = configManager.get<int64_t>("c2s", "port").value_or(5222);
uint16_t listeningPort = static_cast<uint16_t>(portValue);
// Create temporary connection handler to listen for incoming connections
// and create ClientSession instances
xtrpg::xmpp::ClientConnectionManager connectionManager(ioContext,
listeningPort);
// Set up signal handlers for graceful shutdown (SIGINT and SIGTERM)
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
#ifndef _WIN32
signal(SIGHUP, signalHandler);
#endif
std::cout << "[INFO] Server running. Press Ctrl+C to shutdown."
<< std::endl;
// Run IO context in a thread pool for handling async operations
std::vector<std::thread> ioThreads;
const size_t threadCount = std::thread::hardware_concurrency();
for (size_t i = 0; i < threadCount; ++i) {
ioThreads.emplace_back([&ioContext]() { ioContext.run(); });
}
// Main thread: wait for shutdown signal
int previousCount = -1;
while (!g_shouldShutdown) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (previousCount != connectionManager.countConnections()) {
std::cout << "[main] Connection Count: "
<< connectionManager.countConnections() << std::endl;
previousCount = connectionManager.countConnections();
}
}
std::cout << "[INFO] Shutting down IO context..." << std::endl;
ioContext.stop();
// Wait for all IO threads to complete
for (auto &thread : ioThreads) {
if (thread.joinable()) {
thread.join();
}
}
std::cout << "[INFO] Server shutdown complete." << std::endl;
} catch (const std::exception &e) {
std::cerr << "EXCEPTION OCCURRED" << std::endl
<< "Application closing die to \"" << e.what() << "\"."
<< std::endl;
return 1;
} catch (...) {
std::cerr << "UNEXPECTED EXCEPTION OCCURRED" << std::endl
<< "Application closing." << std::endl;
return 1;
}
return 0;
}