Implementiere sanften Shutdown und verbessere Server-Stopp-Logik

- Füge Signalhandler in `main.cpp` hinzu, um einen sanften Shutdown bei SIGINT und SIGTERM zu ermöglichen.
- Aktualisiere die `Server`- und `SSLServer`-Klassen, um die Stopp-Logik zu verbessern und Threads ordnungsgemäß zu beenden.
- Optimiere die Timeout-Werte in der `run`-Methode für eine schnellere Reaktion auf Shutdown-Signale.
- Ergänze Logging für den Server-Stopp-Prozess zur besseren Nachverfolgbarkeit.
This commit is contained in:
Torsten Schulz (local)
2025-09-04 14:22:01 +02:00
parent c9235034b1
commit 8d0d1bc187
4 changed files with 118 additions and 17 deletions

View File

@@ -13,6 +13,8 @@
#include <arpa/inet.h>
#include <sstream>
#include <regex>
#include <errno.h>
#include <cstring>
namespace Yc {
namespace Lib {
@@ -63,13 +65,33 @@ namespace Yc {
}
}
Server::~Server() {
stop();
if (_socket >= 0) {
close(_socket);
_socket = -1;
}
}
void Server::stop() {
std::cout << "[YourChat] Stopping regular server..." << std::endl;
_stop = true;
// Stop all rooms
for (auto& room : _rooms) {
room->setStop();
}
std::cout << "[YourChat] Regular server stopped." << std::endl;
}
void Server::run() {
if (listen(_socket, 5) < 0) {
std::cout << "listen not possible" << std::endl;
exit(-1);
}
timeval origTv;
origTv.tv_sec = 5;
origTv.tv_sec = 1; // Shorter timeout for more responsive shutdown
origTv.tv_usec = 0;
int _maxSd = _socket;
std::set<int> activeSockets;
@@ -90,7 +112,8 @@ namespace Yc {
}
}
if (select(_maxSd + 1, &fd, NULL, NULL, &tv) > 0) {
int selectResult = select(_maxSd + 1, &fd, NULL, NULL, &tv);
if (selectResult > 0) {
// Neue Verbindung?
if (FD_ISSET(_socket, &fd)) {
std::thread(&Server::handleRequest, this).detach();
@@ -110,6 +133,13 @@ namespace Yc {
}
}
}
} else if (selectResult < 0) {
if (errno == EINTR) {
// Interrupted by signal, check if we should stop
continue;
}
std::cerr << "[YourChat] select() error: " << strerror(errno) << std::endl;
break;
}
// Aufgeräumte Sockets entfernen
@@ -125,6 +155,7 @@ namespace Yc {
}
}
}
std::cout << "[YourChat] Server run loop exiting" << std::endl;
}
std::vector<std::string> Server::roomList() {