Server can handle multiple dropped clients at once

This commit is contained in:
Kayne Ruse
2015-02-17 20:48:03 +11:00
parent 87af4f1a1e
commit 2cc7260552
3 changed files with 28 additions and 17 deletions
+8 -8
View File
@@ -21,11 +21,15 @@
*/
#include "client_manager.hpp"
#include "ip_operators.hpp"
#include "udp_network_utility.hpp"
#include <chrono>
int ClientManager::CheckConnections() {
std::list<int> ClientManager::CheckConnections() {
//list of clients to disconnect
std::list<int> returnList;
for (auto& it : elementMap) {
//3 seconds between beats
if (ClientData::Clock::now() - it.second.GetLastBeat() > std::chrono::seconds(3)) {
@@ -38,21 +42,17 @@ int ClientManager::CheckConnections() {
for (auto& it : elementMap) {
if (it.second.GetAttempts() > 2) {
int ret = it.first;
// elementMap.erase(it.first);
return ret;
returnList.push_back(it.first);
}
}
return -1;
return returnList;
}
void ClientManager::HandlePong(ServerPacket* const argPacket) {
//find and update the specified client
for (auto& it : elementMap) {
if (it.second.GetAddress().host == argPacket->srcAddress.host &&
it.second.GetAddress().port == argPacket->srcAddress.port
) {
if (it.second.GetAddress() == argPacket->srcAddress) {
it.second.ResetAttempts();
return;
}
+2 -1
View File
@@ -29,12 +29,13 @@
#include "SDL/SDL_net.h"
#include <functional>
#include <list>
#include <map>
class ClientManager: public Singleton<ClientManager> {
public:
//methods
int CheckConnections();
std::list<int> CheckConnections();
void HandlePong(ServerPacket* const argPacket);
//common public methods
+18 -8
View File
@@ -28,6 +28,7 @@
#include <stdexcept>
#include <chrono>
#include <iostream>
#include <list>
#include <sstream>
#include <string>
@@ -161,9 +162,16 @@ void ServerApplication::Init(int argc, char* argv[]) {
}
void ServerApplication::Proc() {
//network buffer
SerialPacket* packetBuffer = reinterpret_cast<SerialPacket*>(new char[MAX_PACKET_SIZE]);
memset(packetBuffer, 0, MAX_PACKET_SIZE); //zero the buffer
//time system
typedef std::chrono::steady_clock Clock;
Clock::time_point simTime = Clock::now();
Clock::time_point realTime;
while(running) {
//suck in the waiting packets & process them
while(network.Receive(packetBuffer)) {
@@ -173,18 +181,20 @@ void ServerApplication::Proc() {
catch(std::exception& e) {
std::cerr << "HandlePacket Error: " << e.what() << std::endl;
}
memset(packetBuffer, 0, MAX_PACKET_SIZE); //reset the buffer
//reset the buffer
memset(packetBuffer, 0, MAX_PACKET_SIZE);
}
//update the internals
//...
//Check connections
int disconnected = clientMgr.CheckConnections();
if (disconnected != -1) {
FullClientUnload(disconnected);
std::cerr << "Client dropped: " << disconnected << std::endl;
//Check client connections
std::list<int> disconnections = clientMgr.CheckConnections();
for(auto const& it : disconnections) {
FullClientUnload(it);
std::cerr << "Client dropped: " << it << std::endl;
}
//"tick" the internals
//...
//give the machine a break
SDL_Delay(10);
}