From df1594a43ba4814ccc999599b9095e1981f80c41 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Wed, 4 Dec 2013 14:58:15 +0100 Subject: [PATCH] Network - Fixed small errors --- .../NetworkDependencies/Connection.cpp | 3 +- Code/Network/NetworkDependencies/Listener.cpp | 9 ++++-- .../NetworkDependencies/OysterByte.cpp | 28 +++++++++++++++++++ Code/Network/NetworkDependencies/OysterByte.h | 3 ++ Code/Network/NetworkDependencies/PostBox.h | 2 +- .../NetworkDependencies/ThreadedClient.cpp | 4 +-- .../OysterNetworkClient/ClientMain.cpp | 3 +- .../OysterNetworkServer/ServerMain.cpp | 7 +++-- 8 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 169827ba..299f60d3 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -116,7 +116,6 @@ int Connection::Recieve(Utility::DynamicMemory::SmartPointer &bytes) { int nBytes; - bytes.Get()->Resize(1000); bytes->Resize(1000); nBytes = recv(this->socket, *bytes , 500, 0); if(nBytes == SOCKET_ERROR) @@ -138,7 +137,7 @@ int Connection::Listen() int clientSocket; if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) { - return WSAGetLastError(); + return INVALID_SOCKET;//WSAGetLastError(); } return clientSocket; diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index bebf4563..f00c282a 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -44,9 +44,12 @@ int Listener::Accept() SmartPointer clientSocket = SmartPointer(new int()); *clientSocket = connection->Listen(); - mutex.LockMutex(); - postBox->PostMessage(clientSocket); - mutex.UnlockMutex(); + if(*clientSocket != -1) + { + mutex.LockMutex(); + postBox->PostMessage(clientSocket); + mutex.UnlockMutex(); + } return clientSocket; } diff --git a/Code/Network/NetworkDependencies/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp index 8df493f1..0ae7bf96 100644 --- a/Code/Network/NetworkDependencies/OysterByte.cpp +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -16,6 +16,19 @@ OysterByte::OysterByte(int cap) byteArray = new unsigned char[capacity]; } +OysterByte::OysterByte(const OysterByte& obj) +{ + delete[] this->byteArray; + this->byteArray = new unsigned char[obj.capacity]; + + for(int i = 0; i < obj.size; i++) + { + this->byteArray[i] = obj.byteArray[i]; + } + this->size = obj.size; + this->capacity = obj.capacity; +} + OysterByte::~OysterByte() { delete[] byteArray; @@ -67,6 +80,21 @@ void OysterByte::SetSize(unsigned int size) this->size = size; } +OysterByte& OysterByte::operator =(const OysterByte& obj) +{ + delete[] this->byteArray; + this->byteArray = new unsigned char[obj.capacity]; + + for(int i = 0; i < obj.size; i++) + { + this->byteArray[i] = obj.byteArray[i]; + } + this->size = obj.size; + this->capacity = obj.capacity; + + return *this; +} + OysterByte::operator char*() { return (char*)byteArray; diff --git a/Code/Network/NetworkDependencies/OysterByte.h b/Code/Network/NetworkDependencies/OysterByte.h index 9a6bc75a..99629bfc 100644 --- a/Code/Network/NetworkDependencies/OysterByte.h +++ b/Code/Network/NetworkDependencies/OysterByte.h @@ -16,6 +16,7 @@ namespace Oyster public: OysterByte(); OysterByte(int cap); + OysterByte(const OysterByte& obj); virtual ~OysterByte(); void Clear(); //Resets size to 0 @@ -28,6 +29,8 @@ namespace Oyster void SetBytes(unsigned char* bytes); void SetSize(unsigned int size); //Only sets the private variable 'size' + OysterByte& operator =(const OysterByte& obj); + operator char*(); operator const char*(); operator unsigned char*(); diff --git a/Code/Network/NetworkDependencies/PostBox.h b/Code/Network/NetworkDependencies/PostBox.h index ca4d03c8..f6db2c44 100644 --- a/Code/Network/NetworkDependencies/PostBox.h +++ b/Code/Network/NetworkDependencies/PostBox.h @@ -53,7 +53,7 @@ namespace Oyster { if(IsFull()) { - message = messages.Pop(); + message = messages.Pop(); return true; } return false; diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp index 57154f70..64b8c098 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.cpp +++ b/Code/Network/NetworkDependencies/ThreadedClient.cpp @@ -74,8 +74,8 @@ int ThreadedClient::Send() int ThreadedClient::Recv() { int errorCode = 0; - - SmartPointer temp = SmartPointer(new OysterByte()); + + SmartPointer temp = new OysterByte(); errorCode = this->connection->Recieve(temp); if(errorCode == 0) diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 5a48598c..2d2d428f 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -59,7 +59,7 @@ void chat(ThreadedClient &client) client.setRecvPostBox(postBox); - SmartPointer msgRecv = NULL; + SmartPointer msgRecv = SmartPointer(new OysterByte()); SmartPointer msgSend = SmartPointer(new OysterByte()); ProtocolSet* set = new ProtocolSet; @@ -83,7 +83,6 @@ void chat(ThreadedClient &client) if(postBox->FetchMessage(msgRecv)) { t->Unpack(set, msgRecv); - delete msgRecv; PrintOutMessage(set); set->Release(); diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 0e6c0751..14c28da5 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -26,7 +26,7 @@ void PrintOutMessage(ProtocolSet* set); int main() { SmartPointer sendBuffer = SmartPointer(new OysterByte); - SmartPointer recvBuffer = NULL; + SmartPointer recvBuffer = SmartPointer(new OysterByte()); ProtocolSet* set = new ProtocolSet; IPostBox> *postBox = new PostBox>(); IPostBox> *recvPostBox = new PostBox>(); @@ -91,7 +91,6 @@ int main() if(recvPostBox->FetchMessage(recvBuffer)) { t.Unpack(set, recvBuffer); - delete recvBuffer; PrintOutMessage(set); set->Release(); @@ -100,9 +99,13 @@ int main() Sleep(1); } listener.Shutdown(); + Sleep(1000); system("pause"); + for(int i = 0; i < clients.size(); i++) + delete clients.at(i); + delete postBox; return 0; }