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"
|
2013-12-12 20:32:54 +01:00
|
|
|
#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()
|
2013-12-18 13:07:10 +01:00
|
|
|
{ }
|
|
|
|
NetworkSession::NetworkSession(const NetworkSession& orig)
|
2013-12-12 09:33:59 +01:00
|
|
|
{
|
2013-12-18 13:07:10 +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()
|
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
this->clients.Clear();
|
2013-12-12 09:33:59 +01:00
|
|
|
}
|
|
|
|
|
2013-12-18 13:07:10 +01:00
|
|
|
void NetworkSession::AttachClient(Utility::DynamicMemory::SmartPointer<ClientObject> client, Oyster::IPostBox<DanBias::NetworkSession::NetEvent> *box)
|
2013-12-12 09:33:59 +01:00
|
|
|
{
|
2013-12-18 08:37:45 +01:00
|
|
|
ClientListLock.lock();
|
|
|
|
|
|
|
|
int k = -1;
|
2013-12-18 13:07:10 +01:00
|
|
|
for (unsigned int i = 0; (k == -1) && i < this->clients.Size(); i++)
|
2013-12-18 08:37:45 +01:00
|
|
|
{
|
|
|
|
if(!this->clients[i])
|
|
|
|
k = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(k == -1)
|
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
this->clients.Push(client);
|
|
|
|
this->clients[this->clients.Size() - 1]->SetPostbox(box);
|
2013-12-18 08:37:45 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
this->clients[k] = client;
|
|
|
|
this->clients[k]->SetPostbox(box);
|
2013-12-18 08:37:45 +01:00
|
|
|
}
|
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
|
|
|
|
2013-12-18 13:07:10 +01:00
|
|
|
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::DetachClient(Oyster::Network::NetworkClient* client)
|
2013-12-16 09:00:11 +01:00
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
Utility::DynamicMemory::SmartPointer<ClientObject> val;
|
|
|
|
|
2013-12-18 08:37:45 +01:00
|
|
|
ClientListLock.lock();
|
|
|
|
|
2013-12-18 13:07:10 +01:00
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
2013-12-18 08:37:45 +01:00
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
if(this->clients[0]->GetClient()->Id() == client->Id())
|
2013-12-18 13:07:10 +01:00
|
|
|
{
|
|
|
|
val = this->clients[i];
|
2013-12-18 08:37:45 +01:00
|
|
|
this->clients[i] = 0;
|
2013-12-18 13:07:10 +01:00
|
|
|
}
|
2013-12-18 08:37:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientListLock.unlock();
|
2013-12-18 13:07:10 +01:00
|
|
|
|
|
|
|
return val;
|
2013-12-16 09:00:11 +01:00
|
|
|
}
|
2013-12-18 13:07:10 +01:00
|
|
|
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::DetachClient(ClientObject* client)
|
2013-12-16 09:00:11 +01:00
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
Utility::DynamicMemory::SmartPointer<ClientObject> val;
|
|
|
|
|
2013-12-18 08:37:45 +01:00
|
|
|
ClientListLock.lock();
|
|
|
|
|
2013-12-18 13:07:10 +01:00
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
2013-12-18 08:37:45 +01:00
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
if(this->clients[0]->GetClient()->Id() == client->GetClient()->Id())
|
2013-12-18 13:07:10 +01:00
|
|
|
{
|
|
|
|
val = this->clients[i];
|
2013-12-18 08:37:45 +01:00
|
|
|
this->clients[i] = 0;
|
2013-12-18 13:07:10 +01:00
|
|
|
}
|
2013-12-18 08:37:45 +01:00
|
|
|
}
|
2013-12-16 09:00:11 +01:00
|
|
|
|
2013-12-18 08:37:45 +01:00
|
|
|
ClientListLock.unlock();
|
2013-12-18 13:07:10 +01:00
|
|
|
|
|
|
|
return val;
|
2013-12-16 09:00:11 +01:00
|
|
|
}
|
2013-12-18 13:07:10 +01:00
|
|
|
Utility::DynamicMemory::SmartPointer<ClientObject> NetworkSession::DetachClient(short ID)
|
2013-12-12 09:33:59 +01:00
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
Utility::DynamicMemory::SmartPointer<ClientObject> val;
|
|
|
|
|
2013-12-18 08:37:45 +01:00
|
|
|
ClientListLock.lock();
|
|
|
|
|
2013-12-18 13:07:10 +01:00
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
2013-12-18 08:37:45 +01:00
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
if(this->clients[0]->GetClient()->Id() == ID)
|
2013-12-18 13:07:10 +01:00
|
|
|
{
|
|
|
|
val = this->clients[i];
|
2013-12-18 08:37:45 +01:00
|
|
|
this->clients[i] = 0;
|
2013-12-18 13:07:10 +01:00
|
|
|
}
|
2013-12-18 08:37:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientListLock.unlock();
|
2013-12-18 13:07:10 +01:00
|
|
|
|
|
|
|
return val;
|
2013-12-12 09:33:59 +01:00
|
|
|
}
|
2013-12-18 13:07:10 +01:00
|
|
|
|
|
|
|
void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol)
|
2013-12-12 09:33:59 +01:00
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
this->clients[i]->GetClient()->Send(&protocol);
|
2013-12-18 13:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
if(this->clients[i]->GetClient()->Id() == ID)
|
2013-12-18 08:37:45 +01:00
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
this->clients[i]->GetClient()->Send(&protocol);
|
2013-12-18 13:07:10 +01:00
|
|
|
break;
|
2013-12-18 08:37:45 +01:00
|
|
|
}
|
2013-12-18 13:07:10 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 08:37:45 +01:00
|
|
|
|
2013-12-18 13:07:10 +01:00
|
|
|
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
|
|
|
}
|
2013-12-18 13:07:10 +01:00
|
|
|
|
|
|
|
void NetworkSession::CloseSession(NetworkSession * owner)
|
2013-12-12 09:33:59 +01:00
|
|
|
{
|
2013-12-18 08:37:45 +01:00
|
|
|
ClientListLock.lock();
|
|
|
|
|
2013-12-18 13:07:10 +01:00
|
|
|
if(!owner)
|
2013-12-18 08:37:45 +01:00
|
|
|
{
|
2013-12-18 13:07:10 +01:00
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
this->clients[i]->GetClient()->Disconnect();
|
2013-12-18 13:07:10 +01:00
|
|
|
}
|
2013-12-18 08:37:45 +01:00
|
|
|
}
|
2013-12-18 13:07:10 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < this->clients.Size(); i++)
|
|
|
|
{
|
|
|
|
owner->AttachClient(this->clients[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->clients.Clear();
|
2013-12-18 08:37:45 +01:00
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
if(this->clients[i]->GetClient()->Id() == ID)
|
2013-12-19 12:32:23 +01:00
|
|
|
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++)
|
|
|
|
{
|
2013-12-20 09:42:02 +01:00
|
|
|
if(this->clients[i]->GetClient()->Id() == obj.GetClient()->Id())
|
2013-12-19 12:32:23 +01:00
|
|
|
return this->clients[i];
|
|
|
|
}
|
|
|
|
return Utility::DynamicMemory::SmartPointer<ClientObject>();
|
|
|
|
}
|
|
|
|
|
2013-12-12 09:33:59 +01:00
|
|
|
}//End namespace DanBias
|