2013-12-03 13:04:53 +01:00
|
|
|
#include "ThreadedClient.h"
|
2013-12-08 23:56:17 +01:00
|
|
|
#include "OysterByte.h"
|
2013-12-03 13:04:53 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
using namespace Oyster::Network;
|
|
|
|
using namespace Oyster::Thread;
|
2013-12-04 12:40:49 +01:00
|
|
|
using namespace Utility::DynamicMemory;
|
2013-12-03 13:04:53 +01:00
|
|
|
|
|
|
|
ThreadedClient::ThreadedClient()
|
|
|
|
{
|
|
|
|
this->connection = new Connection();
|
2013-12-11 21:45:43 +01:00
|
|
|
this->sendPostBox = new PostBox<CustomNetProtocol*>;
|
2013-12-03 13:04:53 +01:00
|
|
|
this->recvPostBox = NULL;
|
2013-12-04 14:56:44 +01:00
|
|
|
|
|
|
|
connection->SetBlockingMode(false);
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadedClient::ThreadedClient(unsigned int socket)
|
|
|
|
{
|
|
|
|
this->connection = new Connection(socket);
|
2013-12-11 21:45:43 +01:00
|
|
|
this->sendPostBox = new PostBox<CustomNetProtocol*>;
|
2013-12-03 13:04:53 +01:00
|
|
|
this->recvPostBox = NULL;
|
2013-12-03 23:12:48 +01:00
|
|
|
|
|
|
|
connection->SetBlockingMode(false);
|
|
|
|
|
|
|
|
thread.Create(this, true);
|
|
|
|
}
|
|
|
|
|
2013-12-11 21:45:43 +01:00
|
|
|
ThreadedClient::ThreadedClient(IPostBox<CustomNetProtocol*>* postBox, unsigned int socket)
|
2013-12-03 23:12:48 +01:00
|
|
|
{
|
|
|
|
this->connection = new Connection(socket);
|
2013-12-11 21:45:43 +01:00
|
|
|
this->sendPostBox = new PostBox<CustomNetProtocol*>;
|
2013-12-03 23:12:48 +01:00
|
|
|
this->recvPostBox = postBox;
|
|
|
|
|
|
|
|
connection->SetBlockingMode(false);
|
|
|
|
|
|
|
|
thread.Create(this, true);
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadedClient::~ThreadedClient()
|
|
|
|
{
|
|
|
|
thread.Terminate();
|
|
|
|
delete this->connection;
|
|
|
|
this->connection = NULL;
|
|
|
|
this->recvPostBox = NULL;
|
|
|
|
|
|
|
|
if(sendPostBox != NULL)
|
|
|
|
{
|
|
|
|
delete sendPostBox;
|
|
|
|
this->sendPostBox = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 21:45:43 +01:00
|
|
|
void ThreadedClient::Send(CustomNetProtocol* protocol)
|
2013-12-03 13:04:53 +01:00
|
|
|
{
|
2013-12-11 21:45:43 +01:00
|
|
|
this->sendPostBox->PostMessage(protocol);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadedClient::IsConnected()
|
|
|
|
{
|
|
|
|
return connection->IsConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadedClient::Disconnect()
|
|
|
|
{
|
|
|
|
connection->Disconnect();
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int ThreadedClient::Send()
|
|
|
|
{
|
|
|
|
int errorCode = 0;
|
2013-12-08 23:56:17 +01:00
|
|
|
|
2013-12-03 23:12:48 +01:00
|
|
|
if(sendPostBox->IsFull())
|
2013-12-03 13:04:53 +01:00
|
|
|
{
|
2013-12-11 21:45:43 +01:00
|
|
|
OysterByte temp;
|
|
|
|
sendPostBox->FetchMessage();
|
|
|
|
//errorCode = this->connection->Send(temp);
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|
2013-12-08 23:56:17 +01:00
|
|
|
|
2013-12-03 13:04:53 +01:00
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ThreadedClient::Recv()
|
|
|
|
{
|
2013-12-09 10:48:43 +01:00
|
|
|
int errorCode = -1;
|
|
|
|
|
2013-12-08 23:56:17 +01:00
|
|
|
SmartPointer<OysterByte> temp = new OysterByte;
|
2013-12-04 12:40:49 +01:00
|
|
|
errorCode = this->connection->Recieve(temp);
|
2013-12-11 21:45:43 +01:00
|
|
|
|
2013-12-03 23:12:48 +01:00
|
|
|
if(errorCode == 0)
|
2013-12-03 13:04:53 +01:00
|
|
|
{
|
2013-12-09 10:48:43 +01:00
|
|
|
stdMutex.lock();
|
2013-12-11 21:45:43 +01:00
|
|
|
//recvPostBox->PostMessage(temp);
|
2013-12-09 10:48:43 +01:00
|
|
|
stdMutex.unlock();
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|
2013-12-08 23:56:17 +01:00
|
|
|
|
2013-12-03 13:04:53 +01:00
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadedClient::ThreadEntry()
|
|
|
|
{
|
2013-12-03 23:12:48 +01:00
|
|
|
std::cout<< "Client Thread started" << std::endl;
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadedClient::ThreadExit()
|
|
|
|
{
|
2013-12-03 23:12:48 +01:00
|
|
|
std::cout << "Client Thread exit" << std::endl;
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|
|
|
|
|
2013-12-09 10:48:43 +01:00
|
|
|
#include <Windows.h>
|
|
|
|
|
2013-12-03 13:04:53 +01:00
|
|
|
bool ThreadedClient::DoWork()
|
|
|
|
{
|
|
|
|
int errorCode;
|
|
|
|
errorCode = Send();
|
2013-12-03 23:12:48 +01:00
|
|
|
/*if(errorCode != 0)
|
2013-12-03 13:04:53 +01:00
|
|
|
{
|
|
|
|
return false;
|
2013-12-03 23:12:48 +01:00
|
|
|
}*/
|
2013-12-03 13:04:53 +01:00
|
|
|
|
|
|
|
errorCode = Recv();
|
2013-12-03 23:12:48 +01:00
|
|
|
/*if(errorCode != 0)
|
2013-12-03 13:04:53 +01:00
|
|
|
{
|
|
|
|
return false;
|
2013-12-03 23:12:48 +01:00
|
|
|
}*/
|
2013-12-09 10:48:43 +01:00
|
|
|
Sleep(1);
|
2013-12-03 13:04:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ThreadedClient::Connect(unsigned short port, const char serverName[])
|
|
|
|
{
|
|
|
|
int errorCode;
|
|
|
|
|
|
|
|
if((errorCode = connection->InitiateClient()) != 0)
|
|
|
|
{
|
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if((errorCode = connection->Connect(port, serverName)) != 0)
|
|
|
|
{
|
|
|
|
return errorCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread.Create(this, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-11 21:45:43 +01:00
|
|
|
void ThreadedClient::setRecvPostBox(IPostBox<CustomNetProtocol*> *postBox)
|
2013-12-03 13:04:53 +01:00
|
|
|
{
|
2013-12-09 10:48:43 +01:00
|
|
|
stdMutex.lock();
|
2013-12-03 13:04:53 +01:00
|
|
|
this->recvPostBox = postBox;
|
2013-12-09 10:48:43 +01:00
|
|
|
stdMutex.unlock();
|
2013-12-03 13:04:53 +01:00
|
|
|
}
|