From bf5cc69ff751cb7adb93a8a08a56995c7f151485 Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Wed, 18 Dec 2013 08:37:45 +0100 Subject: [PATCH] GameLogic - Added Postbox system to netsession --- .../ServerObjects/NetworkSession.cpp | 105 +++++++++++------- .../ServerObjects/NetworkSession.h | 6 +- 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp index 1e64642d..32def107 100644 --- a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp +++ b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp @@ -18,24 +18,24 @@ namespace DanBias void NetworkSession::AttachClient(Utility::DynamicMemory::SmartPointer client) { - while (!ClientListLock.try_lock()); //Possible Deadlock + ClientListLock.lock(); - int k = -1; - for (unsigned int i = 0; (k == -1) && i < this->clients.size(); i++) - { - if(!this->clients[i]) - k = i; - } + int k = -1; + for (unsigned int i = 0; (k == -1) && i < this->clients.size(); i++) + { + if(!this->clients[i]) + k = i; + } - if(k == -1) - { - this->clients.push_back(client); - this->clients[this->clients.size() - 1]->SetPostbox(&this->box); - } - else - { - this->clients[k]->SetPostbox(&this->box); - } + if(k == -1) + { + this->clients.push_back(client); + this->clients[this->clients.size() - 1]->SetPostbox(&this->box); + } + else + { + this->clients[k]->SetPostbox(&this->box); + } ClientListLock.unlock(); @@ -43,44 +43,67 @@ namespace DanBias void NetworkSession::DetachClient(Oyster::Network::NetworkClient* client) { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - if(this->clients[0]->NetClient_Object()->Id() == client->Id()) - this->clients[i] = 0; - } + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.size(); i++) + { + if(this->clients[0]->NetClient_Object()->Id() == client->Id()) + this->clients[i] = 0; + } + + ClientListLock.unlock(); } void NetworkSession::DetachClient(ClientObject* client) { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - if(this->clients[0]->NetClient_Object()->Id() == client->NetClient_Object()->Id()) - this->clients[i] = 0; - } + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.size(); i++) + { + if(this->clients[0]->NetClient_Object()->Id() == client->NetClient_Object()->Id()) + this->clients[i] = 0; + } + ClientListLock.unlock(); } void NetworkSession::DetachClient(short ID) { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - if(this->clients[0]->NetClient_Object()->Id() == ID) - this->clients[i] = 0; - } - + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.size(); i++) + { + if(this->clients[0]->NetClient_Object()->Id() == ID) + this->clients[i] = 0; + } + + ClientListLock.unlock(); } void NetworkSession::DetachClient() { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - this->clients[i] = 0; - } + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.size(); i++) + { + this->clients[i] = 0; + } + + ClientListLock.unlock(); } void NetworkSession::Kick() { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - this->clients[i]->NetClient_Object()->Disconnect(); - this->clients[i] = 0; - } + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.size(); i++) + { + this->clients[i]->NetClient_Object()->Disconnect(); + this->clients[i] = 0; + } + + ClientListLock.unlock(); + } + + void NetworkSession::SetPostbox(Oyster::IPostBox *box) + { + this->box = box; } }//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h index 9ff9b016..0228cbbd 100644 --- a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h +++ b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h @@ -6,7 +6,7 @@ #define NOMINMAX #include "Utilities.h" -#include +#include #include #include #include @@ -40,11 +40,11 @@ namespace DanBias void Send(Oyster::Network::CustomNetProtocol& protocol, int ID); //TODO: Do more lobby features - //virtual void + void SetPostbox(Oyster::IPostBox *box); protected: std::vector> clients; - Oyster::PostBox box; + Oyster::IPostBox *box; }; }//End namespace DanBias #endif // !DANBIASSERVER_NETWORK_SESSION_H