Update README.md, add heartbeat packets to detect dead or hanging connections.
This commit is contained in:
@@ -25,7 +25,18 @@ namespace ColumnLynx::Net::TCP {
|
||||
Utils::LibSodiumWrapper* sodiumWrapper,
|
||||
std::array<uint8_t, 32>* aesKey,
|
||||
uint64_t* sessionIDRef)
|
||||
: mResolver(ioContext), mSocket(ioContext), mHost(host), mPort(port), mLibSodiumWrapper(sodiumWrapper), mGlobalKeyRef(aesKey), mSessionIDRef(sessionIDRef) {}
|
||||
:
|
||||
mResolver(ioContext),
|
||||
mSocket(ioContext),
|
||||
mHost(host),
|
||||
mPort(port),
|
||||
mLibSodiumWrapper(sodiumWrapper),
|
||||
mGlobalKeyRef(aesKey),
|
||||
mSessionIDRef(sessionIDRef),
|
||||
mHeartbeatTimer(mSocket.get_executor()),
|
||||
mLastHeartbeatReceived(std::chrono::steady_clock::now()),
|
||||
mLastHeartbeatSent(std::chrono::steady_clock::now())
|
||||
{}
|
||||
|
||||
void start() {
|
||||
auto self = shared_from_this();
|
||||
@@ -55,6 +66,8 @@ namespace ColumnLynx::Net::TCP {
|
||||
);
|
||||
|
||||
mHandler->sendMessage(ClientMessageType::HANDSHAKE_INIT, Utils::uint8ArrayToString(payload.data(), payload.size()));
|
||||
|
||||
mStartHeartbeat();
|
||||
} else {
|
||||
Utils::error("Client connect failed: " + ec.message());
|
||||
}
|
||||
@@ -85,6 +98,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
}
|
||||
|
||||
asio::error_code ec;
|
||||
mHeartbeatTimer.cancel();
|
||||
|
||||
mHandler->socket().shutdown(tcp::socket::shutdown_both, ec);
|
||||
if (ec) {
|
||||
@@ -110,6 +124,42 @@ namespace ColumnLynx::Net::TCP {
|
||||
}
|
||||
|
||||
private:
|
||||
void mStartHeartbeat() {
|
||||
auto self = shared_from_this();
|
||||
mHeartbeatTimer.expires_after(std::chrono::seconds(5));
|
||||
mHeartbeatTimer.async_wait([this, self](const asio::error_code& ec) {
|
||||
if (ec == asio::error::operation_aborted) {
|
||||
return; // Timer was cancelled
|
||||
}
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - self->mLastHeartbeatReceived).count();
|
||||
|
||||
if (elapsed >= 15) { // 3 missed heartbeats
|
||||
Utils::error("Missed 3 heartbeats. I think the other party might have died! Disconnecting.");
|
||||
|
||||
// Close sockets forcefully, server is dead
|
||||
asio::error_code ec;
|
||||
mHandler->socket().shutdown(tcp::socket::shutdown_both, ec);
|
||||
mHandler->socket().close(ec);
|
||||
mConnected = false;
|
||||
|
||||
mGlobalKeyRef = nullptr;
|
||||
if (mSessionIDRef) {
|
||||
*mSessionIDRef = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
self->sendMessage(ClientMessageType::HEARTBEAT);
|
||||
Utils::log("Sent HEARTBEAT to server.");
|
||||
self->mLastHeartbeatSent = std::chrono::steady_clock::now();
|
||||
|
||||
self->mStartHeartbeat(); // Recursive
|
||||
});
|
||||
}
|
||||
|
||||
void mHandleMessage(ServerMessageType type, const std::string& data) {
|
||||
switch (type) {
|
||||
case ServerMessageType::HANDSHAKE_IDENTIFY:
|
||||
@@ -198,6 +248,15 @@ namespace ColumnLynx::Net::TCP {
|
||||
mHandshakeComplete = true;
|
||||
}
|
||||
|
||||
break;
|
||||
case ServerMessageType::HEARTBEAT:
|
||||
Utils::log("Received HEARTBEAT from server.");
|
||||
mHandler->sendMessage(ClientMessageType::HEARTBEAT_ACK, ""); // Send ACK
|
||||
break;
|
||||
case ServerMessageType::HEARTBEAT_ACK:
|
||||
Utils::log("Received HEARTBEAT_ACK from server.");
|
||||
mLastHeartbeatReceived = std::chrono::steady_clock::now();
|
||||
mMissedHeartbeats = 0; // Reset missed heartbeat count
|
||||
break;
|
||||
case ServerMessageType::GRACEFUL_DISCONNECT:
|
||||
Utils::log("Server is disconnecting: " + data);
|
||||
@@ -224,5 +283,9 @@ namespace ColumnLynx::Net::TCP {
|
||||
SymmetricKey mConnectionAESKey;
|
||||
std::array<uint8_t, 32>* mGlobalKeyRef; // Reference to global AES key
|
||||
uint64_t* mSessionIDRef; // Reference to global Session ID
|
||||
asio::steady_timer mHeartbeatTimer;
|
||||
std::chrono::steady_clock::time_point mLastHeartbeatReceived;
|
||||
std::chrono::steady_clock::time_point mLastHeartbeatSent;
|
||||
int mMissedHeartbeats = 0;
|
||||
};
|
||||
}
|
||||
@@ -13,6 +13,9 @@ namespace ColumnLynx::Net::TCP {
|
||||
HANDSHAKE_CHALLENGE_RESPONSE = 0x04, // Response to client's challenge
|
||||
HANDSHAKE_EXCHANGE_KEY_CONFIRM = 0x06, // If accepted, send encrypted AES key and session ID
|
||||
|
||||
// Shared
|
||||
HEARTBEAT = 0xF0, // Keep-alive message
|
||||
HEARTBEAT_ACK = 0xF1, // Acknowledgement of keep-alive
|
||||
GRACEFUL_DISCONNECT = 0xFE, // Notify client of impending disconnection
|
||||
KILL_CONNECTION = 0xFF, // Forecefully terminate the connection (with cleanup if possible), reserved for unrecoverable errors
|
||||
};
|
||||
@@ -22,6 +25,9 @@ namespace ColumnLynx::Net::TCP {
|
||||
HANDSHAKE_CHALLENGE = 0xA3, // Challenge ownership of private key
|
||||
HANDSHAKE_EXCHANGE_KEY = 0xA5, // Accept or reject identity, can kill the connection, also sends the AES key
|
||||
|
||||
// Shared
|
||||
HEARTBEAT = 0xF0, // Keep-alive message
|
||||
HEARTBEAT_ACK = 0xF1, // Acknowledgement of keep-alive
|
||||
GRACEFUL_DISCONNECT = 0xFE, // Notify server of impending disconnection
|
||||
KILL_CONNECTION = 0xFF, // Forecefully terminate the connection (with cleanup if possible), reserved for unrecoverable errors
|
||||
};
|
||||
|
||||
@@ -43,6 +43,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
});
|
||||
|
||||
mHandler->start();
|
||||
mStartHeartbeat();
|
||||
|
||||
// Placeholder for message handling setup
|
||||
Utils::log("Client connected: " + mHandler->socket().remote_endpoint().address().to_string());
|
||||
@@ -62,7 +63,7 @@ namespace ColumnLynx::Net::TCP {
|
||||
std::string ip = mHandler->socket().remote_endpoint().address().to_string();
|
||||
|
||||
mHandler->sendMessage(ServerMessageType::GRACEFUL_DISCONNECT, "Server initiated disconnect.");
|
||||
|
||||
mHeartbeatTimer.cancel();
|
||||
asio::error_code ec;
|
||||
mHandler->socket().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||
mHandler->socket().close(ec);
|
||||
@@ -84,7 +85,45 @@ namespace ColumnLynx::Net::TCP {
|
||||
|
||||
private:
|
||||
TCPConnection(asio::ip::tcp::socket socket, Utils::LibSodiumWrapper* sodiumWrapper)
|
||||
: mHandler(std::make_shared<MessageHandler>(std::move(socket))), mLibSodiumWrapper(sodiumWrapper) {}
|
||||
:
|
||||
mHandler(std::make_shared<MessageHandler>(std::move(socket))),
|
||||
mLibSodiumWrapper(sodiumWrapper),
|
||||
mHeartbeatTimer(mHandler->socket().get_executor()),
|
||||
mLastHeartbeatReceived(std::chrono::steady_clock::now()),
|
||||
mLastHeartbeatSent(std::chrono::steady_clock::now())
|
||||
{}
|
||||
|
||||
void mStartHeartbeat() {
|
||||
auto self = shared_from_this();
|
||||
mHeartbeatTimer.expires_after(std::chrono::seconds(5));
|
||||
mHeartbeatTimer.async_wait([this, self](const asio::error_code& ec) {
|
||||
if (ec == asio::error::operation_aborted) {
|
||||
return; // Timer was cancelled
|
||||
}
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - self->mLastHeartbeatReceived).count();
|
||||
|
||||
if (elapsed >= 15) { // 3 missed heartbeats
|
||||
Utils::error("Missed 3 heartbeats. I think the other party (client " + std::to_string(self->mConnectionSessionID) + ") might have died! Disconnecting.");
|
||||
|
||||
// Remove socket forcefully, client is dead
|
||||
asio::error_code ec;
|
||||
mHandler->socket().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||
mHandler->socket().close(ec);
|
||||
|
||||
SessionRegistry::getInstance().erase(self->mConnectionSessionID);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
self->sendMessage(ServerMessageType::HEARTBEAT);
|
||||
Utils::log("Sent HEARTBEAT to client " + std::to_string(self->mConnectionSessionID));
|
||||
self->mLastHeartbeatSent = now;
|
||||
|
||||
self->mStartHeartbeat(); // Recursive
|
||||
});
|
||||
}
|
||||
|
||||
void mHandleMessage(ClientMessageType type, const std::string& data) {
|
||||
std::string reqAddr = mHandler->socket().remote_endpoint().address().to_string();
|
||||
@@ -191,6 +230,17 @@ namespace ColumnLynx::Net::TCP {
|
||||
|
||||
break;
|
||||
}
|
||||
case ClientMessageType::HEARTBEAT: {
|
||||
Utils::log("Received HEARTBEAT from " + reqAddr);
|
||||
mHandler->sendMessage(ServerMessageType::HEARTBEAT_ACK, ""); // Send ACK
|
||||
break;
|
||||
}
|
||||
case ClientMessageType::HEARTBEAT_ACK: {
|
||||
Utils::log("Received HEARTBEAT_ACK from " + reqAddr);
|
||||
mLastHeartbeatReceived = std::chrono::steady_clock::now();
|
||||
mMissedHeartbeats = 0; // Reset missed heartbeat count
|
||||
break;
|
||||
}
|
||||
case ClientMessageType::GRACEFUL_DISCONNECT: {
|
||||
Utils::log("Received GRACEFUL_DISCONNECT from " + reqAddr + ": " + data);
|
||||
disconnect();
|
||||
@@ -208,5 +258,9 @@ namespace ColumnLynx::Net::TCP {
|
||||
std::array<uint8_t, 32> mConnectionAESKey;
|
||||
uint64_t mConnectionSessionID;
|
||||
AsymPublicKey mConnectionPublicKey;
|
||||
asio::steady_timer mHeartbeatTimer;
|
||||
std::chrono::steady_clock::time_point mLastHeartbeatReceived;
|
||||
std::chrono::steady_clock::time_point mLastHeartbeatSent;
|
||||
int mMissedHeartbeats = 0;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user