Relatively graceful shutdowns
This commit is contained in:
@@ -16,11 +16,25 @@ using namespace ColumnLynx::Utils;
|
||||
using namespace ColumnLynx::Net::TCP;
|
||||
using namespace ColumnLynx::Net::UDP;
|
||||
|
||||
volatile sig_atomic_t done = 0;
|
||||
|
||||
/*void signalHandler(int signum) {
|
||||
if (signum == SIGINT || signum == SIGTERM) {
|
||||
log("Received termination signal. Shutting down server gracefully.");
|
||||
done = 1;
|
||||
}
|
||||
}*/
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
PanicHandler::init();
|
||||
|
||||
try {
|
||||
// TODO: Catch SIGINT and SIGTERM for graceful shutdown
|
||||
// Catch SIGINT and SIGTERM for graceful shutdown
|
||||
/*struct sigaction action;
|
||||
memset(&action, 0, sizeof(struct sigaction));
|
||||
action.sa_handler = signalHandler;
|
||||
sigaction(SIGINT, &action, nullptr);
|
||||
sigaction(SIGTERM, &action, nullptr);*/
|
||||
|
||||
log("ColumnLynx Server, Version " + getVersion());
|
||||
log("This software is licensed under the GPLv3. See LICENSE for details.");
|
||||
@@ -30,18 +44,48 @@ int main(int argc, char** argv) {
|
||||
log("Server public key: " + bytesToHexString(sodiumWrapper.getPublicKey(), crypto_sign_PUBLICKEYBYTES));
|
||||
log("Server private key: " + bytesToHexString(sodiumWrapper.getPrivateKey(), crypto_sign_SECRETKEYBYTES)); // TEMP, remove later
|
||||
|
||||
bool hostRunning = true;
|
||||
|
||||
asio::io_context io;
|
||||
auto server = std::make_shared<TCPServer>(io, serverPort(), &sodiumWrapper);
|
||||
auto udpServer = std::make_shared<UDPServer>(io, serverPort());
|
||||
|
||||
auto server = std::make_shared<TCPServer>(io, serverPort(), &sodiumWrapper, &hostRunning);
|
||||
auto udpServer = std::make_shared<UDPServer>(io, serverPort(), &hostRunning);
|
||||
|
||||
asio::signal_set signals(io, SIGINT, SIGTERM);
|
||||
signals.async_wait([&](const std::error_code&, int) {
|
||||
log("Received termination signal. Shutting down server gracefully.");
|
||||
done = 1;
|
||||
asio::post(io, [&]() {
|
||||
hostRunning = false;
|
||||
server->stop();
|
||||
udpServer->stop();
|
||||
});
|
||||
});
|
||||
|
||||
// Run the IO context in a separate thread
|
||||
std::thread ioThread([&io]() {
|
||||
io.run();
|
||||
});
|
||||
|
||||
ioThread.join();
|
||||
//ioThread.detach();
|
||||
|
||||
log("Server started on port " + std::to_string(serverPort()));
|
||||
|
||||
while (!done) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
log("Shutting down server...");
|
||||
/*hostRunning = false;
|
||||
server->stop();
|
||||
udpServer->stop();*/
|
||||
|
||||
io.stop();
|
||||
if (ioThread.joinable()) {
|
||||
ioThread.join();
|
||||
}
|
||||
|
||||
log("Server stopped.");
|
||||
} catch (const std::exception& e) {
|
||||
error("Server error: " + std::string(e.what()));
|
||||
}
|
||||
|
||||
@@ -20,27 +20,66 @@
|
||||
namespace ColumnLynx::Net::TCP {
|
||||
|
||||
void TCPServer::mStartAccept() {
|
||||
// A bit of a shotty implementation, might improve later
|
||||
/*std::cout << "Host running pointer: " << *mHostRunning << std::endl;
|
||||
|
||||
if (mHostRunning != nullptr && !(*mHostRunning)) {
|
||||
Utils::log("Server is stopping, not accepting new connections.");
|
||||
return;
|
||||
}*/
|
||||
|
||||
mAcceptor.async_accept(
|
||||
[this](asio::error_code ec, asio::ip::tcp::socket socket) {
|
||||
if (!NetHelper::isExpectedDisconnect(ec)) {
|
||||
auto client = TCPConnection::create(std::move(socket),
|
||||
mSodiumWrapper,
|
||||
[this](std::shared_ptr<TCPConnection> c) {
|
||||
mClients.erase(c);
|
||||
Utils::log("Client removed.");
|
||||
});
|
||||
|
||||
mClients.insert(client);
|
||||
client->start();
|
||||
|
||||
Utils::log("Accepted new client connection.");
|
||||
} else {
|
||||
if (ec) {
|
||||
if (ec == asio::error::operation_aborted) {
|
||||
// Acceptor was cancelled/closed during shutdown
|
||||
return;
|
||||
}
|
||||
Utils::error("Accept failed: " + ec.message());
|
||||
// Try again only if still running
|
||||
if (mHostRunning && *mHostRunning && mAcceptor.is_open())
|
||||
mStartAccept();
|
||||
return;
|
||||
}
|
||||
|
||||
TCPServer::mStartAccept(); // Accept next
|
||||
|
||||
auto client = TCPConnection::create(
|
||||
std::move(socket),
|
||||
mSodiumWrapper,
|
||||
[this](std::shared_ptr<TCPConnection> c) {
|
||||
mClients.erase(c);
|
||||
Utils::log("Client removed.");
|
||||
}
|
||||
);
|
||||
mClients.insert(client);
|
||||
client->start();
|
||||
Utils::log("Accepted new client connection.");
|
||||
|
||||
if (mHostRunning && *mHostRunning && mAcceptor.is_open())
|
||||
mStartAccept();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void TCPServer::stop() {
|
||||
// Stop accepting
|
||||
if (mAcceptor.is_open()) {
|
||||
asio::error_code ec;
|
||||
mAcceptor.cancel(ec);
|
||||
mAcceptor.close(ec);
|
||||
Utils::log("TCP Acceptor closed.");
|
||||
}
|
||||
|
||||
// Snapshot to avoid iterator invalidation while callbacks erase()
|
||||
std::vector<std::shared_ptr<TCPConnection>> snapshot(mClients.begin(), mClients.end());
|
||||
for (auto& client : snapshot) {
|
||||
try {
|
||||
client->disconnect(); // should shutdown+close the socket
|
||||
Utils::log("GRACEFUL_DISCONNECT sent to session: " + std::to_string(client->getSessionID()));
|
||||
} catch (const std::exception& e) {
|
||||
Utils::error(std::string("Error disconnecting client: ") + e.what());
|
||||
}
|
||||
}
|
||||
// Let the erase callback run as sockets close
|
||||
// Do NOT destroy server while io handlers may still reference it
|
||||
}
|
||||
}
|
||||
@@ -10,13 +10,23 @@
|
||||
|
||||
namespace ColumnLynx::Net::UDP {
|
||||
void UDPServer::mStartReceive() {
|
||||
// A bit of a shotty implementation, might improve later
|
||||
/*if (mHostRunning != nullptr && !(*mHostRunning)) {
|
||||
Utils::log("Server is stopping, not receiving new packets.");
|
||||
return;
|
||||
}*/
|
||||
|
||||
mSocket.async_receive_from(
|
||||
asio::buffer(mRecvBuffer), mRemoteEndpoint,
|
||||
[this](asio::error_code ec, std::size_t bytes) {
|
||||
if (!ec && bytes > 0) {
|
||||
mHandlePacket(bytes);
|
||||
if (ec) {
|
||||
if (ec == asio::error::operation_aborted) return; // Socket closed
|
||||
// Other recv error
|
||||
if (mHostRunning && *mHostRunning) mStartReceive();
|
||||
return;
|
||||
}
|
||||
mStartReceive(); // Continue receiving
|
||||
if (bytes > 0) mHandlePacket(bytes);
|
||||
if (mHostRunning && *mHostRunning) mStartReceive();
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -69,4 +79,13 @@ namespace ColumnLynx::Net::UDP {
|
||||
void UDPServer::mSendData(const uint64_t sessionID, const std::string& data) {
|
||||
// TODO: Implement
|
||||
}
|
||||
|
||||
void UDPServer::stop() {
|
||||
if (mSocket.is_open()) {
|
||||
asio::error_code ec;
|
||||
mSocket.cancel(ec);
|
||||
mSocket.close(ec);
|
||||
Utils::log("UDP Socket closed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user