Network - changed from & to smartpoints

This commit is contained in:
Sam Mario Svensson 2013-12-04 12:40:49 +01:00
parent df1470eb2c
commit 79cf9df111
14 changed files with 93 additions and 94 deletions

View File

@ -118,62 +118,53 @@ namespace Oyster
Type ThreadSafeQueue<Type>::Pop()
{
mutex.LockMutex();
if(this->front != NULL)
Type item = this->front->item;
Node *destroyer = this->front;
this->front = front->next;
delete destroyer;
this->nrOfNodes--;
if(nrOfNodes == 0)
{
Type item = this->front->item;
Node *destroyer = this->front;
this->front = front->next;
delete destroyer;
this->nrOfNodes--;
if(nrOfNodes == 0)
{
this->front = NULL;
this->back = NULL;
}
mutex.UnlockMutex();
return item;
this->front = NULL;
this->back = NULL;
}
mutex.UnlockMutex();
return NULL;
return item;
}
template < typename Type >
Type ThreadSafeQueue<Type>::Front()
{
mutex.LockMutex();
if(front != NULL)
{
mutex.UnlockMutex();
return this->front->item;
}
return this->front->item;
mutex.UnlockMutex();
return NULL;
}
template < typename Type >
Type ThreadSafeQueue<Type>::Back()
{
mutex.LockMutex();
if(back != NULL)
{
return this->back->item;
}
return this->back->item;
mutex.UnlockMutex();
return NULL;
}
template < typename Type >
int ThreadSafeQueue<Type>::Size()
{
//? behövs denna låsas?
mutex.LockMutex();
return this->nrOfNodes;
mutex.UnlockMutex();
}
@ -181,7 +172,7 @@ namespace Oyster
bool ThreadSafeQueue<Type>::IsEmpty()
{
mutex.LockMutex();
if(nrOfNodes == 0 && this->front == NULL)
if(nrOfNodes == 0 || this->front == NULL)
{
mutex.UnlockMutex();
return true;
@ -191,6 +182,7 @@ namespace Oyster
{
mutex.UnlockMutex();
}
return false;
}

View File

@ -79,11 +79,11 @@ int Connection::Disconnect()
return WSAGetLastError();
}
int Connection::Send(OysterByte& bytes)
int Connection::Send(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{
int nBytes;
nBytes = send(this->socket, bytes, bytes.GetSize(), 0);
nBytes = send(this->socket, *bytes, bytes->GetSize(), 0);
if(nBytes == SOCKET_ERROR)
{
return WSAGetLastError();
@ -92,19 +92,20 @@ int Connection::Send(OysterByte& bytes)
return 0;
}
int Connection::Recieve(OysterByte& bytes)
int Connection::Recieve(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{
int nBytes;
bytes.Resize(1000);
nBytes = recv(this->socket, bytes, 500, 0);
bytes.Get()->Resize(1000);
bytes->Resize(1000);
nBytes = recv(this->socket, *bytes , 500, 0);
if(nBytes == SOCKET_ERROR)
{
return WSAGetLastError();
}
else
{
bytes.SetSize(nBytes);
bytes->SetSize(nBytes);
}
std::cout << "Size of the recieved data: " << nBytes << " bytes" << std::endl;

View File

@ -23,8 +23,8 @@ namespace Oyster
virtual int InitiateServer( unsigned short port );
virtual int InitiateClient();
virtual int Send( OysterByte& bytes );
virtual int Recieve( OysterByte& bytes );
virtual int Send( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Recieve( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Disconnect();
virtual int Connect( unsigned short port , const char serverName[] );

View File

@ -5,6 +5,8 @@
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "../../Misc/Utilities.h"
namespace Oyster
{
namespace Network
@ -17,8 +19,8 @@ namespace Oyster
//sends and recieve functions with bytearrays,
//will send to the users connection via socket
virtual int Send( OysterByte& bytes ) = 0;
virtual int Recieve( OysterByte& bytes) = 0;
virtual int Send( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes ) = 0;
virtual int Recieve( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes) = 0;
//initiates sockets and address for server and client
virtual int InitiateServer( unsigned short port ) { return false; };

View File

@ -5,6 +5,8 @@
// Created by Sam Svensson 2013 //
//////////////////////////////////
#include "../../Misc/Utilities.h"
namespace Oyster
{
namespace Network
@ -15,8 +17,8 @@ namespace Oyster
public:
//packs and unpacks packages for sending or recieving over the connection
virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0;
virtual void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0;
virtual void Pack (Protocols::ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes) = 0;
virtual void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes ) = 0;
};
}

View File

@ -1,6 +1,7 @@
#include "Listener.h"
using namespace Oyster::Network::Server;
using namespace Utility::DynamicMemory;
Listener::Listener()
{
@ -31,7 +32,7 @@ void Listener::Shutdown()
thread.Stop();
}
void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox)
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
{
mutex.LockMutex();
this->postBox = postBox;
@ -40,8 +41,8 @@ void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox)
int Listener::Accept()
{
int clientSocket = 0;
clientSocket = connection->Listen();
SmartPointer<int> clientSocket = SmartPointer<int>(new int());
*clientSocket = connection->Listen();
mutex.LockMutex();
postBox->PostMessage(clientSocket);

View File

@ -9,6 +9,7 @@
#include "../NetworkDependencies/Connection.h"
#include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h"
#include "../../Misc/Utilities.h"
#include "IPostBox.h"
namespace Oyster
@ -26,7 +27,7 @@ namespace Oyster
bool Init(unsigned int port);
void Shutdown();
void SetPostBox(IPostBox<int>* postBox);
void SetPostBox(IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox);
private:
//Thread functions
@ -43,7 +44,7 @@ namespace Oyster
::Oyster::Thread::OysterThread thread;
OysterMutex mutex;
IPostBox<int>* postBox;
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;
};
}

View File

@ -53,8 +53,7 @@ namespace Oyster
{
if(IsFull())
{
message = messages.Front();
messages.Pop();
message = messages.Pop();
return true;
}
return false;

View File

@ -3,18 +3,19 @@
#include <iostream>
using namespace Oyster::Network;
using namespace Oyster::Thread;
using namespace Utility::DynamicMemory;
ThreadedClient::ThreadedClient()
{
this->connection = new Connection();
this->sendPostBox = new PostBox<OysterByte*>();
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>();
this->recvPostBox = NULL;
}
ThreadedClient::ThreadedClient(unsigned int socket)
{
this->connection = new Connection(socket);
this->sendPostBox = new PostBox<OysterByte*>();
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>();
this->recvPostBox = NULL;
connection->SetBlockingMode(false);
@ -22,10 +23,10 @@ ThreadedClient::ThreadedClient(unsigned int socket)
thread.Create(this, true);
}
ThreadedClient::ThreadedClient(IPostBox<OysterByte*>* postBox, unsigned int socket)
ThreadedClient::ThreadedClient(IPostBox<SmartPointer<OysterByte>>* postBox, unsigned int socket)
{
this->connection = new Connection(socket);
this->sendPostBox = new PostBox<OysterByte*>;
this->sendPostBox = new PostBox<SmartPointer<OysterByte>>;
this->recvPostBox = postBox;
connection->SetBlockingMode(false);
@ -47,7 +48,7 @@ ThreadedClient::~ThreadedClient()
}
}
int ThreadedClient::Send(OysterByte* byte)
int ThreadedClient::Send(SmartPointer<OysterByte> &byte)
{
mutex.LockMutex();
this->sendPostBox->PostMessage(byte);
@ -61,9 +62,9 @@ int ThreadedClient::Send()
mutex.LockMutex();
if(sendPostBox->IsFull())
{
OysterByte *temp = NULL;
SmartPointer<OysterByte> temp = NULL;
sendPostBox->FetchMessage(temp);
errorCode = this->connection->Send(*temp);
errorCode = this->connection->Send(temp);
}
mutex.UnlockMutex();
@ -74,8 +75,8 @@ int ThreadedClient::Recv()
{
int errorCode = 0;
OysterByte *temp = new OysterByte();
errorCode = this->connection->Recieve(*temp);
SmartPointer<OysterByte> temp = SmartPointer<OysterByte>(new OysterByte());
errorCode = this->connection->Recieve(temp);
if(errorCode == 0)
{
@ -83,10 +84,6 @@ int ThreadedClient::Recv()
recvPostBox->PostMessage(temp);
mutex.UnlockMutex();
}
else
{
delete temp;
}
return errorCode;
}
@ -137,7 +134,7 @@ int ThreadedClient::Connect(unsigned short port, const char serverName[])
return 0;
}
void ThreadedClient::setRecvPostBox(IPostBox<OysterByte*>* postBox)
void ThreadedClient::setRecvPostBox(IPostBox<SmartPointer<OysterByte>> *postBox)
{
this->mutex.LockMutex();
this->recvPostBox = postBox;

View File

@ -10,6 +10,7 @@
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h"
#include "../../Misc/Utilities.h"
namespace Oyster
{
@ -20,14 +21,14 @@ namespace Oyster
public:
ThreadedClient();
ThreadedClient(unsigned int socket);
ThreadedClient(IPostBox<OysterByte*>* postBox, unsigned int socket);
ThreadedClient(IPostBox<Utility::DynamicMemory::SmartPointer< OysterByte >> *postBox, unsigned int socket);
virtual ~ThreadedClient();
int Send(OysterByte* byte);
int Send(Utility::DynamicMemory::SmartPointer< OysterByte > &byte);
int Connect(unsigned short port, const char serverName[]);
void setRecvPostBox(IPostBox<OysterByte*>* postBox);
void setRecvPostBox(IPostBox< Utility::DynamicMemory::SmartPointer< OysterByte >> *postBox);
private:
virtual int Send();
@ -39,8 +40,8 @@ namespace Oyster
private:
Connection* connection;
IPostBox<OysterByte*>* sendPostBox;
IPostBox<OysterByte*>* recvPostBox;
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *sendPostBox;
IPostBox<Utility::DynamicMemory::SmartPointer<OysterByte>> *recvPostBox;
Oyster::Thread::OysterThread thread;
OysterMutex mutex;

View File

@ -4,7 +4,7 @@ using namespace Oyster::Network;
using namespace ::Protocols;
using namespace ::Messages;
void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
void Translator::Pack( ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes )
{
MessageHeader *message = NULL;
@ -25,19 +25,19 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes )
if(message != NULL)
{
message->Pack(header, bytes);
message->Pack(header, *bytes);
delete message;
message = NULL;
}
}
void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
void Translator::Unpack(ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{
ProtocolHeader *header = new ProtocolHeader();
MessageHeader *message = new MessageHeader();
message->Unpack(bytes, *header);
message->Unpack(*bytes, *header);
delete message;
message = NULL;
@ -48,19 +48,19 @@ void Translator::Unpack(ProtocolSet* set, OysterByte& bytes )
case PackageType_header:
message = new MessageHeader();
set->Protocol.pHeader = new ProtocolHeader;
message->Unpack(bytes, *set->Protocol.pHeader);
message->Unpack(*bytes, *set->Protocol.pHeader);
break;
case PackageType_test:
message = new MessageTest();
set->Protocol.pTest = new ProtocolTest;
message->Unpack(bytes, *set->Protocol.pTest);
message->Unpack(*bytes, *set->Protocol.pTest);
break;
case PackageType_player_pos:
message = new MessagePlayerPos();
set->Protocol.pPlayerPos = new ProtocolPlayerPos;
message->Unpack(bytes, *set->Protocol.pPlayerPos);
message->Unpack(*bytes, *set->Protocol.pPlayerPos);
break;
}

View File

@ -20,8 +20,8 @@ namespace Oyster
Translator () { };
~Translator() { };
void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes );
void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes );
void Pack (Protocols::ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
private:

View File

@ -16,6 +16,7 @@ using namespace std;
using namespace Oyster::Network::Protocols;
using namespace Oyster::Network;
using namespace Utility;
using namespace Utility::DynamicMemory;
void chat(ThreadedClient &client);
void PrintOutMessage(ProtocolSet* set);
@ -54,12 +55,12 @@ int main()
void chat(ThreadedClient &client)
{
Oyster::Network::Translator *t = new Oyster::Network::Translator();
IPostBox<OysterByte*>* postBox = new PostBox<OysterByte*>;
IPostBox< SmartPointer<OysterByte >> *postBox = new PostBox< SmartPointer<OysterByte >>;
client.setRecvPostBox(postBox);
Oyster::Network::OysterByte* msgRecv = NULL;
Oyster::Network::OysterByte* msgSend = new OysterByte();
SmartPointer<OysterByte> msgRecv = NULL;
SmartPointer<OysterByte> msgSend = SmartPointer<OysterByte>(new OysterByte());
ProtocolSet* set = new ProtocolSet;
ProtocolPlayerPos test;
@ -72,7 +73,7 @@ void chat(ThreadedClient &client)
test.matrix[i] = temp;
temp++;
}
t->Pack(test, *msgSend);
t->Pack(test, msgSend);
WinTimer timer;
@ -81,7 +82,7 @@ void chat(ThreadedClient &client)
//Fetch new messages from the postbox
if(postBox->FetchMessage(msgRecv))
{
t->Unpack(set, *msgRecv);
t->Unpack(set, msgRecv);
delete msgRecv;
PrintOutMessage(set);
@ -97,7 +98,6 @@ void chat(ThreadedClient &client)
}
}
delete msgSend;
delete postBox;
delete t;
delete set;

View File

@ -9,6 +9,8 @@
#include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/WinTimer.h"
#include "../../Misc/Utilities.h"
#include "../../Misc/Utilities-Impl.h"
#pragma comment(lib, "ws2_32.lib")
@ -17,20 +19,21 @@ using namespace Oyster::Network::Server;
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace Utility;
using namespace ::Utility::DynamicMemory;
void PrintOutMessage(ProtocolSet* set);
int main()
{
OysterByte sendBuffer;
OysterByte* recvBuffer = NULL;
SmartPointer<OysterByte> sendBuffer = SmartPointer<OysterByte>(new OysterByte);
SmartPointer<OysterByte> recvBuffer = NULL;
ProtocolSet* set = new ProtocolSet;
IPostBox<int>* postBox = new PostBox<int>();
IPostBox<OysterByte*>* recvPostBox = new PostBox<OysterByte*>();
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<OysterByte>>();
cout << "Server" << endl;
Translator t;
int errorCode;
int errorCode = 0;
if(!InitWinSock())
{
@ -61,16 +64,16 @@ int main()
WinTimer timer;
vector<ThreadedClient*> clients;
int client = -1;
SmartPointer<int> client = SmartPointer<int>();
while(1)
{
//Fetch new clients from the postbox
if(postBox->FetchMessage(client))
{
cout << "Client connected: " << client << endl;
clients.push_back(new ThreadedClient(recvPostBox, client));
cout << "Client connected: " << *client << endl;
clients.push_back(new ThreadedClient(recvPostBox, *client));
clients.at(clients.size()-1)->Send(&sendBuffer);
clients.at(clients.size()-1)->Send(sendBuffer);
}
//Send a message every 1 secounds to all clients.
@ -80,14 +83,14 @@ int main()
timer.reset();
for(int i = 0; i < (int)clients.size(); i++)
{
clients.at(i)->Send(&sendBuffer);
clients.at(i)->Send(sendBuffer);
}
}
//Fetch messages
if(recvPostBox->FetchMessage(recvBuffer))
{
t.Unpack(set, *recvBuffer);
t.Unpack(set, recvBuffer);
delete recvBuffer;
PrintOutMessage(set);