Danbias/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp

180 lines
4.1 KiB
C++
Raw Normal View History

2013-12-16 09:00:11 +01:00
2013-12-13 23:47:16 +01:00
#include "ClientObject.h"
2013-12-12 09:33:59 +01:00
#include "NetworkSession.h"
#include <mutex>
2013-12-12 09:33:59 +01:00
2013-12-17 14:15:20 +01:00
static std::mutex ClientListLock;
2013-12-12 09:33:59 +01:00
namespace DanBias
{
NetworkSession::NetworkSession()
{ }
NetworkSession::NetworkSession(const NetworkSession& orig)
2013-12-12 09:33:59 +01:00
{
this->clients = orig.clients;
}
const NetworkSession& NetworkSession::operator=(const NetworkSession& orig)
{
this->clients = orig.clients;
return *this;
2013-12-12 09:33:59 +01:00
}
NetworkSession::~NetworkSession()
{
this->clients.Clear();
2013-12-12 09:33:59 +01:00
}
void NetworkSession::AttachClient(Utility::DynamicMemory::SmartPointer<ClientObject> client, Oyster::IPostBox<DanBias::NetworkSession::NetEvent> *box)
2013-12-12 09:33:59 +01:00
{
ClientListLock.lock();
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(client);
this->clients[this->clients.Size() - 1]->SetPostbox(box);
}
else
{
this->clients[k] = client;
this->clients[k]->SetPostbox(box);
}
2013-12-17 14:15:20 +01:00
ClientListLock.unlock();
2013-12-12 09:33:59 +01:00
}
2013-12-16 09:00:11 +01:00
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::DetachClient(Oyster::Network::NetworkClient* client)
2013-12-16 09:00:11 +01:00
{
Utility::DynamicMemory::SmartPointer<ClientObject> val;
ClientListLock.lock();
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[0]->NetClient_Object()->Id() == client->Id())
{
val = this->clients[i];
this->clients[i] = 0;
}
}
ClientListLock.unlock();
return val;
2013-12-16 09:00:11 +01:00
}
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::DetachClient(ClientObject* client)
2013-12-16 09:00:11 +01:00
{
Utility::DynamicMemory::SmartPointer<ClientObject> val;
ClientListLock.lock();
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[0]->NetClient_Object()->Id() == client->NetClient_Object()->Id())
{
val = this->clients[i];
this->clients[i] = 0;
}
}
2013-12-16 09:00:11 +01:00
ClientListLock.unlock();
return val;
2013-12-16 09:00:11 +01:00
}
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::DetachClient(short ID)
2013-12-12 09:33:59 +01:00
{
Utility::DynamicMemory::SmartPointer<ClientObject> val;
ClientListLock.lock();
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[0]->NetClient_Object()->Id() == ID)
{
val = this->clients[i];
this->clients[i] = 0;
}
}
ClientListLock.unlock();
return val;
2013-12-12 09:33:59 +01:00
}
void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol)
2013-12-12 09:33:59 +01:00
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
this->clients[i]->NetClient_Object()->Send(&protocol);
}
}
void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID)
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[i]->NetClient_Object()->Id() == ID)
{
this->clients[i]->NetClient_Object()->Send(&protocol);
break;
}
}
}
void NetworkSession::SetPostbox(Oyster::IPostBox<DanBias::NetworkSession::NetEvent> *box)
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
this->clients[i]->SetPostbox(box);
}
2013-12-12 09:33:59 +01:00
}
void NetworkSession::CloseSession(NetworkSession * owner)
2013-12-12 09:33:59 +01:00
{
ClientListLock.lock();
if(!owner)
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
this->clients[i]->NetClient_Object()->Disconnect();
}
}
else
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
owner->AttachClient(this->clients[i]);
}
}
this->clients.Clear();
ClientListLock.unlock();
}
2013-12-19 12:32:23 +01:00
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::FindClient(int ID)
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[i]->NetClient_Object()->Id() == ID)
return this->clients[i];
}
return Utility::DynamicMemory::SmartPointer<ClientObject>();
}
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::FindClient(ClientObject& obj)
{
for (unsigned int i = 0; i < this->clients.Size(); i++)
{
if(this->clients[i]->NetClient_Object()->Id() == obj.NetClient_Object()->Id())
return this->clients[i];
}
return Utility::DynamicMemory::SmartPointer<ClientObject>();
}
2013-12-12 09:33:59 +01:00
}//End namespace DanBias