From 79cf9df1114e266e4b2887319363d40830768d73 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Wed, 4 Dec 2013 12:40:49 +0100 Subject: [PATCH] Network - changed from & to smartpoints --- Code/Misc/ThreadSafeQueue.h | 50 ++++++++----------- .../NetworkDependencies/Connection.cpp | 13 ++--- Code/Network/NetworkDependencies/Connection.h | 4 +- .../Network/NetworkDependencies/IConnection.h | 6 ++- Code/Network/NetworkDependencies/ITranslate.h | 6 ++- Code/Network/NetworkDependencies/Listener.cpp | 7 +-- Code/Network/NetworkDependencies/Listener.h | 5 +- Code/Network/NetworkDependencies/PostBox.h | 3 +- .../NetworkDependencies/ThreadedClient.cpp | 25 ++++------ .../NetworkDependencies/ThreadedClient.h | 11 ++-- .../NetworkDependencies/Translator.cpp | 14 +++--- Code/Network/NetworkDependencies/Translator.h | 4 +- .../OysterNetworkClient/ClientMain.cpp | 12 ++--- .../OysterNetworkServer/ServerMain.cpp | 27 +++++----- 14 files changed, 93 insertions(+), 94 deletions(-) diff --git a/Code/Misc/ThreadSafeQueue.h b/Code/Misc/ThreadSafeQueue.h index 19775f55..2e10b74c 100644 --- a/Code/Misc/ThreadSafeQueue.h +++ b/Code/Misc/ThreadSafeQueue.h @@ -118,62 +118,53 @@ namespace Oyster Type ThreadSafeQueue::Pop() { mutex.LockMutex(); - if(this->front != NULL) - { - Type item = this->front->item; - Node *destroyer = this->front; - this->front = front->next; - delete destroyer; - this->nrOfNodes--; + 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; + if(nrOfNodes == 0) + { + this->front = NULL; + this->back = NULL; } mutex.UnlockMutex(); - - return NULL; + return item; } template < typename Type > Type ThreadSafeQueue::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::Back() { mutex.LockMutex(); - if(back != NULL) - { - return this->back->item; - } + + return this->back->item; + mutex.UnlockMutex(); - return NULL; } template < typename Type > int ThreadSafeQueue::Size() { - //? behövs denna låsas? mutex.LockMutex(); + return this->nrOfNodes; + mutex.UnlockMutex(); } @@ -181,7 +172,7 @@ namespace Oyster bool ThreadSafeQueue::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; } diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 19556bbd..a4d6dcdc 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -79,11 +79,11 @@ int Connection::Disconnect() return WSAGetLastError(); } -int Connection::Send(OysterByte& bytes) +int Connection::Send(Utility::DynamicMemory::SmartPointer &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 &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; diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index 27933abf..a0c6913b 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -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 &bytes ); + virtual int Recieve( Utility::DynamicMemory::SmartPointer &bytes ); virtual int Disconnect(); virtual int Connect( unsigned short port , const char serverName[] ); diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 5f88932b..ecfe3869 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -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 &bytes ) = 0; + virtual int Recieve( Utility::DynamicMemory::SmartPointer &bytes) = 0; //initiates sockets and address for server and client virtual int InitiateServer( unsigned short port ) { return false; }; diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index 80edc8b1..77ceaa0f 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -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 &bytes) = 0; + virtual void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer &bytes ) = 0; }; } diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 05616aec..bebf4563 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -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* postBox) +void Listener::SetPostBox(Oyster::Network::IPostBox>* postBox) { mutex.LockMutex(); this->postBox = postBox; @@ -40,8 +41,8 @@ void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) int Listener::Accept() { - int clientSocket = 0; - clientSocket = connection->Listen(); + SmartPointer clientSocket = SmartPointer(new int()); + *clientSocket = connection->Listen(); mutex.LockMutex(); postBox->PostMessage(clientSocket); diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index 469e30c6..b9fdab21 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -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* postBox); + void SetPostBox(IPostBox>* postBox); private: //Thread functions @@ -43,7 +44,7 @@ namespace Oyster ::Oyster::Thread::OysterThread thread; OysterMutex mutex; - IPostBox* postBox; + IPostBox>* postBox; }; } diff --git a/Code/Network/NetworkDependencies/PostBox.h b/Code/Network/NetworkDependencies/PostBox.h index 960b6f34..ca4d03c8 100644 --- a/Code/Network/NetworkDependencies/PostBox.h +++ b/Code/Network/NetworkDependencies/PostBox.h @@ -53,8 +53,7 @@ namespace Oyster { if(IsFull()) { - message = messages.Front(); - messages.Pop(); + message = messages.Pop(); return true; } return false; diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp index dafe70b8..57154f70 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.cpp +++ b/Code/Network/NetworkDependencies/ThreadedClient.cpp @@ -3,18 +3,19 @@ #include using namespace Oyster::Network; using namespace Oyster::Thread; +using namespace Utility::DynamicMemory; ThreadedClient::ThreadedClient() { this->connection = new Connection(); - this->sendPostBox = new PostBox(); + this->sendPostBox = new PostBox>(); this->recvPostBox = NULL; } ThreadedClient::ThreadedClient(unsigned int socket) { this->connection = new Connection(socket); - this->sendPostBox = new PostBox(); + this->sendPostBox = new PostBox>(); this->recvPostBox = NULL; connection->SetBlockingMode(false); @@ -22,10 +23,10 @@ ThreadedClient::ThreadedClient(unsigned int socket) thread.Create(this, true); } -ThreadedClient::ThreadedClient(IPostBox* postBox, unsigned int socket) +ThreadedClient::ThreadedClient(IPostBox>* postBox, unsigned int socket) { this->connection = new Connection(socket); - this->sendPostBox = new PostBox; + this->sendPostBox = new PostBox>; this->recvPostBox = postBox; connection->SetBlockingMode(false); @@ -47,7 +48,7 @@ ThreadedClient::~ThreadedClient() } } -int ThreadedClient::Send(OysterByte* byte) +int ThreadedClient::Send(SmartPointer &byte) { mutex.LockMutex(); this->sendPostBox->PostMessage(byte); @@ -61,9 +62,9 @@ int ThreadedClient::Send() mutex.LockMutex(); if(sendPostBox->IsFull()) { - OysterByte *temp = NULL; + SmartPointer 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 temp = SmartPointer(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* postBox) +void ThreadedClient::setRecvPostBox(IPostBox> *postBox) { this->mutex.LockMutex(); this->recvPostBox = postBox; diff --git a/Code/Network/NetworkDependencies/ThreadedClient.h b/Code/Network/NetworkDependencies/ThreadedClient.h index dbf99347..2808f351 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.h +++ b/Code/Network/NetworkDependencies/ThreadedClient.h @@ -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* postBox, unsigned int socket); + ThreadedClient(IPostBox> *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* postBox); + void setRecvPostBox(IPostBox< Utility::DynamicMemory::SmartPointer< OysterByte >> *postBox); private: virtual int Send(); @@ -39,8 +40,8 @@ namespace Oyster private: Connection* connection; - IPostBox* sendPostBox; - IPostBox* recvPostBox; + IPostBox> *sendPostBox; + IPostBox> *recvPostBox; Oyster::Thread::OysterThread thread; OysterMutex mutex; diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index 746b00f2..94f66d62 100644 --- a/Code/Network/NetworkDependencies/Translator.cpp +++ b/Code/Network/NetworkDependencies/Translator.cpp @@ -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 &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 &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; } diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 56459f72..94c88457 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -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 &bytes ); + void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer &bytes ); private: diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 45861c96..5a48598c 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -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* postBox = new PostBox; + IPostBox< SmartPointer> *postBox = new PostBox< SmartPointer>; client.setRecvPostBox(postBox); - Oyster::Network::OysterByte* msgRecv = NULL; - Oyster::Network::OysterByte* msgSend = new OysterByte(); + SmartPointer msgRecv = NULL; + SmartPointer msgSend = SmartPointer(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; diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 95a07641..8e416f4e 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -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 sendBuffer = SmartPointer(new OysterByte); + SmartPointer recvBuffer = NULL; ProtocolSet* set = new ProtocolSet; - IPostBox* postBox = new PostBox(); - IPostBox* recvPostBox = new PostBox(); + IPostBox> *postBox = new PostBox>(); + IPostBox> *recvPostBox = new PostBox>(); cout << "Server" << endl; Translator t; - int errorCode; + int errorCode = 0; if(!InitWinSock()) { @@ -61,16 +64,16 @@ int main() WinTimer timer; vector clients; - int client = -1; + SmartPointer client = SmartPointer(); 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);