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