Relatively graceful shutdowns

This commit is contained in:
2025-11-09 01:35:19 +01:00
parent 5312c4205c
commit 098afeb4d8
11 changed files with 154 additions and 30 deletions

View File

@@ -69,9 +69,11 @@ namespace ColumnLynx::Net::TCP {
}
}
void disconnect() {
void disconnect(bool echo = true) {
if (mConnected && mHandler) {
mHandler->sendMessage(ClientMessageType::GRACEFUL_DISCONNECT, "Goodbye");
if (echo) {
mHandler->sendMessage(ClientMessageType::GRACEFUL_DISCONNECT, "Goodbye");
}
asio::error_code ec;
@@ -94,6 +96,10 @@ namespace ColumnLynx::Net::TCP {
return mHandshakeComplete;
}
bool isConnected() const {
return mConnected;
}
private:
void mHandleMessage(ServerMessageType type, const std::string& data) {
switch (type) {
@@ -187,7 +193,7 @@ namespace ColumnLynx::Net::TCP {
case ServerMessageType::GRACEFUL_DISCONNECT:
Utils::log("Server is disconnecting: " + data);
if (mConnected) { // Prevent Recursion
disconnect();
disconnect(false);
}
break;
default:

View File

@@ -61,6 +61,8 @@ namespace ColumnLynx::Net::TCP {
void disconnect() {
std::string ip = mHandler->socket().remote_endpoint().address().to_string();
mHandler->sendMessage(ServerMessageType::GRACEFUL_DISCONNECT, "Server initiated disconnect.");
asio::error_code ec;
mHandler->socket().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
mHandler->socket().close(ec);

View File

@@ -21,19 +21,22 @@ namespace ColumnLynx::Net::TCP {
class TCPServer {
public:
TCPServer(asio::io_context& ioContext, uint16_t port, Utils::LibSodiumWrapper* sodiumWrapper)
: mIoContext(ioContext), mAcceptor(ioContext, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)), mSodiumWrapper(sodiumWrapper)
TCPServer(asio::io_context& ioContext, uint16_t port, Utils::LibSodiumWrapper* sodiumWrapper, bool* hostRunning)
: mIoContext(ioContext), mAcceptor(ioContext, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)), mSodiumWrapper(sodiumWrapper), mHostRunning(hostRunning)
{
Utils::log("Started TCP server on port " + std::to_string(port));
mStartAccept();
}
void stop();
private:
void mStartAccept();
asio::io_context &mIoContext;
asio::ip::tcp::acceptor mAcceptor;
std::unordered_set<TCPConnection::pointer> mClients;
Utils::LibSodiumWrapper *mSodiumWrapper;
bool* mHostRunning;
};
}

View File

@@ -12,13 +12,15 @@
namespace ColumnLynx::Net::UDP {
class UDPServer {
public:
UDPServer(asio::io_context& ioContext, uint16_t port)
: mSocket(ioContext, asio::ip::udp::endpoint(asio::ip::udp::v4(), port))
UDPServer(asio::io_context& ioContext, uint16_t port, bool* hostRunning)
: mSocket(ioContext, asio::ip::udp::endpoint(asio::ip::udp::v4(), port)), mHostRunning(hostRunning)
{
Utils::log("Started UDP server on port " + std::to_string(port));
mStartReceive();
}
void stop();
private:
void mStartReceive();
void mHandlePacket(std::size_t bytes);
@@ -26,5 +28,6 @@ namespace ColumnLynx::Net::UDP {
asio::ip::udp::socket mSocket;
asio::ip::udp::endpoint mRemoteEndpoint;
std::array<uint8_t, 2048> mRecvBuffer; // Adjust size as needed
bool* mHostRunning;
};
}