Skip to content

Commit 2001ed5

Browse files
committed
Merge branch 'master' of https://github.com/mlugg/GhostServer
2 parents dde683b + 099d4f9 commit 2001ed5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

GhostServer/networkmanager.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
#include <QVector>
1111

12-
#define HEARTBEAT_RATE 1000
12+
#define HEARTBEAT_RATE 5000
13+
#define HEARTBEAT_RATE_UDP 1000 // We don't actually respond to these, they're just to keep the connection alive
1314

1415
static std::chrono::time_point<std::chrono::steady_clock> lastHeartbeat;
16+
static std::chrono::time_point<std::chrono::steady_clock> lastHeartbeatUdp;
1517

1618
//DataGhost
1719

@@ -209,6 +211,7 @@ void NetworkManager::CheckConnection()
209211
client.currentMap = level_name;
210212
client.TCP_only = TCP_only;
211213
client.returnedHeartbeat = true; // Make sure they don't get immediately disconnected; their heartbeat starts on next beat
214+
client.missedLastHeartbeat = false;
212215

213216
this->selector.add(*client.tcpSocket);
214217

@@ -386,6 +389,17 @@ void NetworkManager::RunServer()
386389
lastHeartbeat = now;
387390
}
388391

392+
if (now > lastHeartbeatUdp + std::chrono::milliseconds(HEARTBEAT_RATE_UDP)) {
393+
for (auto &client : this->clients) {
394+
if (!client.TCP_only) {
395+
sf::Packet packet;
396+
packet << HEADER::HEART_BEAT << sf::Uint32(client.ID) << sf::Uint32(0);
397+
this->udpSocket.send(packet, client.IP, client.port);
398+
}
399+
}
400+
lastHeartbeatUdp = now;
401+
}
402+
389403
//UDP
390404
std::vector<sf::Packet> buffer;
391405
this->ReceiveUDPUpdates(buffer);
@@ -427,12 +441,13 @@ void NetworkManager::DoHeartbeats()
427441
std::queue<Client*> toDisconnect;
428442

429443
for (auto& client : this->clients) {
430-
if (!client.returnedHeartbeat) {
444+
if (!client.returnedHeartbeat && client.missedLastHeartbeat) {
431445
// Client didn't return heartbeat in time; sever connection
432446
toDisconnect.push(&client);
433447
} else {
434448
// Send a heartbeat
435449
client.heartbeatToken = rand();
450+
client.missedLastHeartbeat = !client.returnedHeartbeat;
436451
client.returnedHeartbeat = false;
437452
sf::Packet packet;
438453
packet << HEADER::HEART_BEAT << sf::Uint32(client.ID) << sf::Uint32(client.heartbeatToken);

GhostServer/networkmanager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct Client {
4949
bool TCP_only;
5050
uint32_t heartbeatToken;
5151
bool returnedHeartbeat;
52+
bool missedLastHeartbeat;
5253
};
5354

5455
class NetworkManager : public QObject

0 commit comments

Comments
 (0)