Merge branch 'Network' of https://github.com/dean11/Danbias into Network

This commit is contained in:
Pontus Fransson 2013-12-04 12:41:37 +01:00
commit e81062b875
14 changed files with 93 additions and 94 deletions

View File

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

View File

@ -99,11 +99,11 @@ int Connection::Disconnect()
return WSAGetLastError(); return WSAGetLastError();
} }
int Connection::Send(OysterByte& bytes) int Connection::Send(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{ {
int nBytes; int nBytes;
nBytes = send(this->socket, bytes, bytes.GetSize(), 0); nBytes = send(this->socket, *bytes, bytes->GetSize(), 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
return WSAGetLastError(); return WSAGetLastError();
@ -112,19 +112,20 @@ int Connection::Send(OysterByte& bytes)
return 0; return 0;
} }
int Connection::Recieve(OysterByte& bytes) int Connection::Recieve(Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
{ {
int nBytes; int nBytes;
bytes.Resize(1000); bytes.Get()->Resize(1000);
nBytes = recv(this->socket, bytes, 500, 0); bytes->Resize(1000);
nBytes = recv(this->socket, *bytes , 500, 0);
if(nBytes == SOCKET_ERROR) if(nBytes == SOCKET_ERROR)
{ {
return WSAGetLastError(); return WSAGetLastError();
} }
else else
{ {
bytes.SetSize(nBytes); bytes->SetSize(nBytes);
} }
std::cout << "Size of the recieved data: " << nBytes << " bytes" << std::endl; 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 InitiateServer( unsigned short port );
virtual int InitiateClient(); virtual int InitiateClient();
virtual int Send( OysterByte& bytes ); virtual int Send( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Recieve( OysterByte& bytes ); virtual int Recieve( Utility::DynamicMemory::SmartPointer<OysterByte> &bytes );
virtual int Disconnect(); virtual int Disconnect();
virtual int Connect( unsigned short port , const char serverName[] ); virtual int Connect( unsigned short port , const char serverName[] );

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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