diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 57287669..fdd89ef4 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -81,13 +81,11 @@ void Connection::Disconnect() closesocket(this->socket); } -bool Connection::Send(const unsigned char message[]) +bool Connection::Send(OysterByte& bytes) { int nBytes; - unsigned long messageSize = strlen((char*)message); - messageSize = 1600; - nBytes = send(this->socket, (char*)message , messageSize, 0); + nBytes = send(this->socket, bytes, bytes.GetSize(), 0); if(nBytes == SOCKET_ERROR) { //Send failed! @@ -99,29 +97,31 @@ bool Connection::Send(const unsigned char message[]) return true; } -int Connection::Recieve(unsigned char message[]) +int Connection::Recieve(OysterByte& bytes) { int nBytes; - nBytes = recv(this->socket, (char*)message, 1600, 0); + bytes.Clear(1000); + nBytes = recv(this->socket, bytes, 500, 0); if(nBytes == SOCKET_ERROR) { //Recv failed return -1; } + else + { + bytes.SetSize(nBytes); + } - message[nBytes] = '\0'; + std::cout << "Size of the recieved data: " << nBytes << " bytes" << std::endl; + + //bytes.byteArray[nBytes] = '\0'; return 1; } int Connection::Listen() { - int optlen = sizeof(int); - int optval; - int Return = getsockopt(this->socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); - std::cout << optval << std::endl; - int clientSocket; if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) { diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index a0019a26..05b35070 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -6,6 +6,7 @@ ////////////////////////////////// #include "IConnection.h" +#include "../NetworkDependencies/OysterByte.h" namespace Oyster { @@ -22,8 +23,8 @@ namespace Oyster virtual bool InitiateServer( unsigned short port ); virtual bool InitiateClient(); - virtual bool Send(const unsigned char message[]); - virtual int Recieve(unsigned char message[]); + virtual bool Send( OysterByte& bytes ); + virtual int Recieve( OysterByte& bytes ); virtual void Disconnect(); virtual bool Connect( unsigned short port , const char serverName[] ); diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 51b7631d..85b63b77 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -9,13 +9,14 @@ namespace Oyster { namespace Network { + class OysterByte; class IConnection { public: virtual void Disconnect() = 0; - virtual bool Send( const unsigned char message[] ) = 0; - virtual int Recieve(unsigned char message[]) = 0; + virtual bool Send( OysterByte& bytes ) = 0; + virtual int Recieve( OysterByte& bytes) = 0; virtual bool InitiateServer( unsigned short port ) { return false; }; virtual bool InitiateClient() { return false; }; virtual int Listen() { return -1; }; diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index e14ad3f1..1143c716 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -9,12 +9,13 @@ namespace Oyster { namespace Network { + class OysterByte; class ITranslate { public: - virtual unsigned char* Pack (Protocols::ProtocolHeader &header ) = 0; - virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char message[] ) = 0; + virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0; + virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0; }; } diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index 66d9bbfc..97c1d12f 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -1,6 +1,9 @@ #include "MessageHeader.h" #include "../Packing.h" +#include +using namespace std; +using namespace Oyster::Network; using namespace Oyster::Network::Messages; using namespace Oyster::Network::Protocols; @@ -13,223 +16,239 @@ MessageHeader::~MessageHeader() { } -void MessageHeader::Pack(ProtocolHeader& header, unsigned char msg[] ) +void MessageHeader::Pack(ProtocolHeader& header, OysterByte& bytes) +{ + size = 0; + + PackInt(header.size, bytes); + PackInt(header.packageType, bytes); + PackInt(header.clientID, bytes); + SetSize(bytes); +} + +void MessageHeader::Unpack(OysterByte& bytes, ProtocolHeader& header) { size = 0; - PackInt(header.size, msg); - PackInt(header.packageType, msg); - PackInt(header.clientID, msg); - SetSize(msg); -} - -void MessageHeader::Unpack(unsigned char msg[], ProtocolHeader& header) -{ - size = 0; - - header.clientID = UnpackInt(msg); - header.packageType = UnpackInt(msg); - header.size = UnpackInt(msg); + header.size = UnpackInt(bytes); + header.packageType = UnpackInt(bytes); + header.clientID = UnpackInt(bytes); } /************************** Pack **************************/ -void MessageHeader::PackBool(bool i, unsigned char msg[]) +void MessageHeader::PackBool(bool i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(1); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 1; } -void MessageHeader::PackChar(char i, unsigned char msg[]) +void MessageHeader::PackChar(char i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(1); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 1; } -void MessageHeader::PackUnsignedChar(unsigned char i, unsigned char msg[]) +void MessageHeader::PackUnsignedChar(unsigned char i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(1); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 1; } -void MessageHeader::PackShort(short i, unsigned char msg[]) +void MessageHeader::PackShort(short i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(2); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 2; } -void MessageHeader::PackUnsignedShort(unsigned short i, unsigned char msg[]) +void MessageHeader::PackUnsignedShort(unsigned short i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(2); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 2; } -void MessageHeader::PackInt(int i, unsigned char msg[]) +void MessageHeader::PackInt(int i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(4); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 4; } -void MessageHeader::PackUnsignedInt(unsigned int i, unsigned char msg[]) +void MessageHeader::PackUnsignedInt(unsigned int i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(4); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 4; } -void MessageHeader::PackInt64(__int64 i, unsigned char msg[]) +void MessageHeader::PackInt64(__int64 i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(8); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 8; } -void MessageHeader::PackUnsignedInt64(unsigned __int64 i, unsigned char msg[]) +void MessageHeader::PackUnsignedInt64(unsigned __int64 i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(8); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 8; } -void MessageHeader::PackFloat(float i, unsigned char msg[]) +void MessageHeader::PackFloat(float i, OysterByte& bytes) { - Packing::Pack(&msg[size], i); + bytes.AddSize(4); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 4; } -void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[]) +void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes) { + bytes.AddSize(4); //Pack number of elements - PackUnsignedInt(elementCount, msg); + PackUnsignedInt(elementCount, bytes); //Pack all elements for(int j = 0; j < elementCount; j++) { - PackFloat(i[j], msg); + PackFloat(i[j], bytes); } } -void MessageHeader::PackDouble(double i, unsigned char msg[]) -{ - Packing::Pack(&msg[size], i); +void MessageHeader::PackDouble(double i, OysterByte& bytes) +{ + bytes.AddSize(8); + Packing::Pack(&bytes.GetByteArray()[size], i); size += 8; } -void MessageHeader::PackStr(char str[], unsigned char msg[]) +void MessageHeader::PackStr(char str[], OysterByte& bytes) { - Packing::Pack(&msg[size], str); - size += 2 + strlen(str); + int totalSize = 2 + strlen(str); + bytes.AddSize(totalSize); + Packing::Pack(&bytes.GetByteArray()[size], str); + size += totalSize; } -void MessageHeader::PackStr(std::string str, unsigned char msg[]) +void MessageHeader::PackStr(std::string str, OysterByte& bytes) { - Packing::Pack(&msg[size], str); - size += 2 + str.length(); + int totalSize = 2 + str.length(); + bytes.AddSize(totalSize); + Packing::Pack(&bytes.GetByteArray()[size], str); + size += totalSize; } /************************** Unpack **************************/ -bool MessageHeader::UnpackBool(unsigned char msg[]) +bool MessageHeader::UnpackBool(OysterByte& bytes) { - bool i = Packing::Unpackb(&msg[size]); + bool i = Packing::Unpackb(&bytes.GetByteArray()[size]); size += 1; return i; } -char MessageHeader::UnpackChar(unsigned char msg[]) +char MessageHeader::UnpackChar(OysterByte& bytes) { - char i = Packing::Unpackc(&msg[size]); + char i = Packing::Unpackc(&bytes.GetByteArray()[size]); size += 1; return i; } -unsigned char MessageHeader::UnpackUnsignedChar(unsigned char msg[]) +unsigned char MessageHeader::UnpackUnsignedChar(OysterByte& bytes) { - unsigned char i = Packing::UnpackC(&msg[size]); + unsigned char i = Packing::UnpackC(&bytes.GetByteArray()[size]); size += 1; return i; } -short MessageHeader::UnpackShort(unsigned char msg[]) +short MessageHeader::UnpackShort(OysterByte& bytes) { - short i = Packing::Unpacks(&msg[size]); + short i = Packing::Unpacks(&bytes.GetByteArray()[size]); size += 2; return i; } -unsigned short MessageHeader::UnpackUnsignedShort(unsigned char msg[]) +unsigned short MessageHeader::UnpackUnsignedShort(OysterByte& bytes) { - unsigned short i = Packing::UnpackS(&msg[size]); + unsigned short i = Packing::UnpackS(&bytes.GetByteArray()[size]); size += 2; return i; } -int MessageHeader::UnpackInt(unsigned char msg[]) +int MessageHeader::UnpackInt(OysterByte& bytes) { - int i = Packing::Unpacki(&msg[size]); + int i = Packing::Unpacki(&bytes.GetByteArray()[size]); size += 4; return i; } -unsigned int MessageHeader::UnpackUnsignedInt(unsigned char msg[]) +unsigned int MessageHeader::UnpackUnsignedInt(OysterByte& bytes) { - unsigned int i = Packing::UnpackI(&msg[size]); + unsigned int i = Packing::UnpackI(&bytes.GetByteArray()[size]); size += 4; return i; } -__int64 MessageHeader::UnpackInt64(unsigned char msg[]) +__int64 MessageHeader::UnpackInt64(OysterByte& bytes) { - __int64 i = Packing::Unpacki64(&msg[size]); + __int64 i = Packing::Unpacki64(&bytes.GetByteArray()[size]); size += 8; return i; } -unsigned __int64 MessageHeader::UnpackUnsignedInt64(unsigned char msg[]) +unsigned __int64 MessageHeader::UnpackUnsignedInt64(OysterByte& bytes) { - unsigned __int64 i = Packing::UnpackI64(&msg[size]); + unsigned __int64 i = Packing::UnpackI64(&bytes.GetByteArray()[size]); size += 8; return i; } -float MessageHeader::UnpackFloat(unsigned char msg[]) +float MessageHeader::UnpackFloat(OysterByte& bytes) { - float i = Packing::Unpackf(&msg[size]); + float i = Packing::Unpackf(&bytes.GetByteArray()[size]); size += 4; return i; } -float* MessageHeader::UnpackFloat(unsigned int& elementCount, unsigned char msg[]) +float* MessageHeader::UnpackFloat(unsigned int& elementCount, OysterByte& bytes) { float* i; - elementCount = UnpackUnsignedInt(msg); + elementCount = UnpackUnsignedInt(bytes); i = new float[elementCount]; for(int j = 0; j < elementCount; j++) { - i[j] = UnpackFloat(msg); + i[j] = UnpackFloat(bytes); } return i; } -double MessageHeader::UnpackDouble(unsigned char msg[]) +double MessageHeader::UnpackDouble(OysterByte& bytes) { - double i = Packing::Unpackd(&msg[size]); + double i = Packing::Unpackd(&bytes.GetByteArray()[size]); size += 8; return i; } -std::string MessageHeader::UnpackStr(unsigned char msg[]) +std::string MessageHeader::UnpackStr(OysterByte& bytes) { - std::string str = Packing::UnpackStr(&msg[size]); + std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]); size += 2 + str.length(); return str; } -void MessageHeader::SetSize(unsigned char msg[]) +void MessageHeader::SetSize(OysterByte& bytes) { - Packing::Pack(&msg[0], size); + Packing::Pack(bytes, size); } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.h b/Code/Network/NetworkDependencies/Messages/MessageHeader.h index 5852e239..095ebc1e 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.h +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.h @@ -7,6 +7,7 @@ #include #include "../Protocols.h" +#include "../OysterByte.h" namespace Oyster { @@ -20,64 +21,64 @@ namespace Oyster MessageHeader(); virtual ~MessageHeader(); - virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[] ); - virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header); + virtual void Pack(Protocols::ProtocolHeader& header, OysterByte& bytes ); + virtual void Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header); protected: //Pack variables to messages - void PackBool(bool i, unsigned char msg[]); + void PackBool(bool i, OysterByte& bytes); - void PackChar(char i, unsigned char msg[]); - void PackUnsignedChar(unsigned char i, unsigned char msg[]); + void PackChar(char i, OysterByte& bytes); + void PackUnsignedChar(unsigned char i, OysterByte& bytes); - void PackShort(short i, unsigned char msg[]); - void PackUnsignedShort(unsigned short i, unsigned char msg[]); + void PackShort(short i, OysterByte& bytes); + void PackUnsignedShort(unsigned short i, OysterByte& bytes); - void PackInt(int i, unsigned char msg[]); - void PackUnsignedInt(unsigned int i, unsigned char msg[]); + void PackInt(int i, OysterByte& bytes); + void PackUnsignedInt(unsigned int i, OysterByte& bytes); - void PackInt64(__int64 i, unsigned char msg[]); - void PackUnsignedInt64(unsigned __int64 i, unsigned char msg[]); + void PackInt64(__int64 i, OysterByte& bytes); + void PackUnsignedInt64(unsigned __int64 i, OysterByte& bytes); - void PackFloat(float i, unsigned char msg[]); - void PackFloat(float i[], unsigned int elementCount, unsigned char msg[]); - void PackDouble(double i, unsigned char msg[]); + void PackFloat(float i, OysterByte& bytes); + void PackFloat(float i[], unsigned int elementCount, OysterByte& bytes); + void PackDouble(double i, OysterByte& bytes); - void PackStr(char str[], unsigned char msg[]); - void PackStr(std::string str, unsigned char msg[]); + void PackStr(char str[], OysterByte& bytes); + void PackStr(std::string str, OysterByte& bytes); //TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc. //Unpack variables from message - bool UnpackBool(unsigned char msg[]); + bool UnpackBool(OysterByte& bytes); - char UnpackChar(unsigned char msg[]); - unsigned char UnpackUnsignedChar(unsigned char msg[]); + char UnpackChar(OysterByte& bytes); + unsigned char UnpackUnsignedChar(OysterByte& bytes); - short UnpackShort(unsigned char msg[]); - unsigned short UnpackUnsignedShort(unsigned char msg[]); + short UnpackShort(OysterByte& bytes); + unsigned short UnpackUnsignedShort(OysterByte& bytes); - int UnpackInt(unsigned char msg[]); - unsigned int UnpackUnsignedInt(unsigned char msg[]); + int UnpackInt(OysterByte& bytes); + unsigned int UnpackUnsignedInt(OysterByte& bytes); - __int64 UnpackInt64(unsigned char msg[]); - unsigned __int64 UnpackUnsignedInt64(unsigned char msg[]); + __int64 UnpackInt64(OysterByte& bytes); + unsigned __int64 UnpackUnsignedInt64(OysterByte& bytes); - float UnpackFloat(unsigned char msg[]); - float* UnpackFloat(unsigned int& elementCount, unsigned char msg[]); - double UnpackDouble(unsigned char msg[]); + float UnpackFloat(OysterByte& bytes); + float* UnpackFloat(unsigned int& elementCount, OysterByte& bytes); + double UnpackDouble(OysterByte& bytes); - std::string UnpackStr(unsigned char msg[]); + std::string UnpackStr(OysterByte& bytes); //TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc. - //Sets the this->size to first position in msg - void SetSize(unsigned char msg[]); + //Sets the this->size to the first position in msg + void SetSize(OysterByte& bytes); private: - int size; + unsigned int size; }; } diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.cpp b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp index ce5ca474..71f53223 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageTest.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp @@ -1,5 +1,6 @@ #include "MessageTest.h" +using namespace Oyster::Network; using namespace Oyster::Network::Messages; using namespace Oyster::Network::Protocols; @@ -11,19 +12,21 @@ MessageTest::~MessageTest() { } -void MessageTest::Pack(ProtocolHeader& header, unsigned char msg[]) +void MessageTest::Pack(ProtocolHeader& header, OysterByte& bytes) { - MessageHeader::Pack(header, msg); + MessageHeader::Pack(header, bytes); + unsigned char asd[1000]; + //strcpy_s(asd, bytes.GetSize(), bytes); - PackStr(static_cast(&header)->textMessage, msg); - PackFloat(static_cast(&header)->f, static_cast(&header)->numOfFloats, msg); - SetSize(msg); + PackStr(static_cast(&header)->textMessage, bytes); + PackFloat(static_cast(&header)->f, static_cast(&header)->numOfFloats, bytes); + SetSize(bytes); } -void MessageTest::Unpack(unsigned char msg[], ProtocolHeader& header) +void MessageTest::Unpack(OysterByte& bytes, ProtocolHeader& header) { - MessageHeader::Unpack(msg, header); + MessageHeader::Unpack(bytes, header); - static_cast(&header)->textMessage = UnpackStr(msg); - static_cast(&header)->f = UnpackFloat(static_cast(&header)->numOfFloats, msg); + static_cast(&header)->textMessage = UnpackStr(bytes); + static_cast(&header)->f = UnpackFloat(static_cast(&header)->numOfFloats, bytes); } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.h b/Code/Network/NetworkDependencies/Messages/MessageTest.h index 203fad23..ff636e56 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageTest.h +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.h @@ -19,8 +19,8 @@ namespace Oyster MessageTest(); virtual ~MessageTest(); - virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[]); - virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header); + virtual void Pack(Protocols::ProtocolHeader& header, OysterByte& bytes); + virtual void Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header); private: }; diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index 73eb03d0..5b660b13 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -164,6 +164,7 @@ + @@ -176,6 +177,7 @@ + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index babc0a34..dc3225ff 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -9,6 +9,7 @@ + @@ -23,5 +24,6 @@ + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp new file mode 100644 index 00000000..6f3ecb92 --- /dev/null +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -0,0 +1,94 @@ +#include "OysterByte.h" + +using namespace Oyster::Network; + +OysterByte::OysterByte() +{ + size = 0; + capacity = 10; + byteArray = new unsigned char[capacity]; +} + +OysterByte::OysterByte(int cap) +{ + size = 0; + capacity = cap; + byteArray = new unsigned char[capacity]; +} + +OysterByte::~OysterByte() +{ + delete[] byteArray; +} + +void OysterByte::Clear(unsigned int cap) +{ + delete[] byteArray; + byteArray = new unsigned char[cap]; + size = 0; +} + +int OysterByte::GetSize() +{ + return size; +} + +unsigned char* OysterByte::GetByteArray() +{ + return byteArray; +} + +void OysterByte::AddSize(unsigned int size) +{ + int oldSize = this->size; + this->size += size; + + if(this->size >= capacity) + { + IncreaseCapacity(oldSize); + } +} + +void OysterByte::SetBytes(unsigned char* bytes) +{ + delete[] byteArray; + byteArray = bytes; +} + +void OysterByte::SetSize(unsigned int size) +{ + this->size = size; +} + +OysterByte::operator char*() +{ + return (char*)byteArray; +} + +OysterByte::operator const char*() +{ + return (const char*)byteArray; +} + +OysterByte::operator unsigned char*() +{ + return byteArray; +} + +///////////// +// Private // +///////////// + +void OysterByte::IncreaseCapacity(unsigned int oldSize) +{ + capacity = size * 2; + unsigned char* temp = new unsigned char[capacity]; + + for(int i = 0; i < oldSize; i++) + { + temp[i] = byteArray[i]; + } + + delete[] byteArray; + byteArray = temp; +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/OysterByte.h b/Code/Network/NetworkDependencies/OysterByte.h new file mode 100644 index 00000000..390e4398 --- /dev/null +++ b/Code/Network/NetworkDependencies/OysterByte.h @@ -0,0 +1,49 @@ +#ifndef NETWORK_DEPENDENCIES_OYSTER_BYTE_H +#define NETWORK_DEPENDENCIES_OYSTER_BYTE_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#include + +namespace Oyster +{ + namespace Network + { + class OysterByte + { + public: + OysterByte(); + OysterByte(int cap); + virtual ~OysterByte(); + + void Clear(unsigned int cap); + + int GetSize(); + unsigned char* GetByteArray(); + + void AddSize(unsigned int size); + void SetBytes(unsigned char* bytes); + void SetSize(unsigned int size); //Only sets the private variable 'size' + + operator char*(); + operator const char*(); + operator unsigned char*(); + + //unsigned char& operator[](unsigned int id); + + private: + void IncreaseCapacity(unsigned int cap); //Expands the byteArray + + + private: + unsigned char* byteArray; + unsigned int size; + unsigned int capacity; + + }; + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index 8e187c36..fbd32df0 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; -unsigned char* Translator::Pack( ProtocolHeader &header ) +void Translator::Pack( ProtocolHeader &header, OysterByte& bytes ) { MessageHeader *message = NULL; @@ -21,21 +21,19 @@ unsigned char* Translator::Pack( ProtocolHeader &header ) if(message != NULL) { - message->Pack(header, this->msg); + message->Pack(header, bytes); delete message; message = NULL; } - - return msg; } -ProtocolSet* Translator::Unpack(ProtocolSet* set, unsigned char msg[] ) +ProtocolSet* Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) { ProtocolHeader *header = new ProtocolHeader(); MessageHeader *message = new MessageHeader(); - message->Unpack(msg, *header); + message->Unpack(bytes, *header); delete message; message = NULL; @@ -46,13 +44,13 @@ ProtocolSet* Translator::Unpack(ProtocolSet* set, unsigned char msg[] ) case package_type_header: message = new MessageHeader(); set->Protocol.pHeader = new ProtocolHeader; - message->Unpack(msg, *set->Protocol.pHeader); + message->Unpack(bytes, *set->Protocol.pHeader); break; case package_type_test: message = new MessageTest(); set->Protocol.pTest = new ProtocolTest; - message->Unpack(msg, *set->Protocol.pTest); + message->Unpack(bytes, *set->Protocol.pTest); break; } diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 581bb406..44ce39d2 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -8,6 +8,7 @@ #include "Messages/MessagesInclude.h" #include "Protocols.h" #include "ITranslate.h" +#include "OysterByte.h" namespace Oyster { @@ -16,14 +17,15 @@ namespace Oyster class Translator : public ITranslate { public: - Translator () { msg = new unsigned char[1601]; }; - ~Translator() { if(msg != NULL) { delete [] this->msg; }}; + Translator () { /*msg = new unsigned char[1601];*/ }; + ~Translator() { /*if(msg != NULL) { delete [] this->msg; }*/ }; - unsigned char* Pack (Protocols::ProtocolHeader &header ); - Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char msg[] ); + void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes ); + Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ); private: - unsigned char* msg; + //unsigned char* msg; + //OysterByte bytes; }; } diff --git a/Code/Network/OysterNetworkClient/Client.cpp b/Code/Network/OysterNetworkClient/Client.cpp index abe36d71..1c6357b9 100644 --- a/Code/Network/OysterNetworkClient/Client.cpp +++ b/Code/Network/OysterNetworkClient/Client.cpp @@ -21,12 +21,12 @@ bool Client::Connect(unsigned int port, char filename[]) return true; } -void Client::Send(unsigned char msg[]) +void Client::Send(Oyster::Network::OysterByte& bytes) { - connection->Send(msg); + connection->Send(bytes); } -void Client::Recv(unsigned char msg[]) +void Client::Recv(Oyster::Network::OysterByte& bytes) { - connection->Recieve(msg); + connection->Recieve(bytes); } \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/Client.h b/Code/Network/OysterNetworkClient/Client.h index 0929d721..0ecb2a32 100644 --- a/Code/Network/OysterNetworkClient/Client.h +++ b/Code/Network/OysterNetworkClient/Client.h @@ -6,6 +6,7 @@ ///////////////////////////////////// #include "../NetworkDependencies/Connection.h" +#include "../NetworkDependencies/OysterByte.h" namespace Oyster { @@ -21,8 +22,8 @@ namespace Oyster bool Connect(unsigned int port, char filename[]); - void Send(unsigned char msg[]); - void Recv(unsigned char msg[]); + void Send(OysterByte& bytes); + void Recv(OysterByte& bytes); private: ::Oyster::Network::Connection* connection; diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 719ca872..472ae10a 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -4,12 +4,13 @@ #include "../NetworkDependencies/WinsockFunctions.h" #include "..\NetworkDependencies\Translator.h" #include "..\NetworkDependencies\Protocols.h" +#include "../NetworkDependencies/OysterByte.h" #include "Client.h" #pragma comment(lib, "ws2_32.lib") using namespace std; -using namespace Oyster::Network::Protocols;; +using namespace Oyster::Network::Protocols; using namespace Oyster::Network::Client; void chat(Client &client); @@ -40,7 +41,7 @@ void chat(Client &client) { Oyster::Network::Translator *t = new Oyster::Network::Translator(); - unsigned char msgRecv[1601] = "\0"; + Oyster::Network::OysterByte msgRecv; string msgSend = ""; ProtocolSet* set = new ProtocolSet; @@ -70,13 +71,14 @@ void chat(Client &client) cout <<"Client 2: " << set->Protocol.pTest->textMessage <Protocol.pTest->numOfFloats; i++) { - //cout << set->Protocol.pTest->f[i] << ' ' ; + cout << set->Protocol.pTest->f[i] << ' ' ; } cout << endl; break; } set->Release(); + msgRecv.Clear(1000); std::getline(std::cin, msgSend); @@ -91,9 +93,9 @@ void chat(Client &client) test.textMessage = msgSend; - unsigned char *message = t->Pack(test); + t->Pack(test, msgRecv); - client.Send(message); + client.Send(msgRecv); } else diff --git a/Code/Network/OysterNetworkServer/Client.cpp b/Code/Network/OysterNetworkServer/Client.cpp index 8c2bdd0d..5cc15eec 100644 --- a/Code/Network/OysterNetworkServer/Client.cpp +++ b/Code/Network/OysterNetworkServer/Client.cpp @@ -1,5 +1,6 @@ #include "Client.h" +using namespace Oyster::Network; using namespace Oyster::Network::Server; Client::Client(unsigned int socket) @@ -12,12 +13,12 @@ Client::~Client() delete connection; } -void Client::Send(unsigned char buffer[]) +void Client::Send(OysterByte& bytes) { - connection->Send(buffer); + connection->Send(bytes); } -void Client::Recv(unsigned char buffer[]) +void Client::Recv(OysterByte& bytes) { - connection->Recieve(buffer); + connection->Recieve(bytes); } \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/Client.h b/Code/Network/OysterNetworkServer/Client.h index 0e3a6c29..2c5ba35f 100644 --- a/Code/Network/OysterNetworkServer/Client.h +++ b/Code/Network/OysterNetworkServer/Client.h @@ -6,6 +6,7 @@ ///////////////////////////////////// #include "../NetworkDependencies/Connection.h" +#include "../NetworkDependencies/OysterByte.h" namespace Oyster { @@ -19,8 +20,8 @@ namespace Oyster Client(unsigned int socket); ~Client(); - void Send(unsigned char buffer[]); - void Recv(unsigned char buffer[]); + void Send(OysterByte& bytes); + void Recv(OysterByte& bytes); private: ::Oyster::Network::Connection* connection; diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 80a59eca..94e854f5 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -5,6 +5,7 @@ #include "../NetworkDependencies/Listener.h" #include "../NetworkDependencies/Translator.h" #include "Client.h" +#include "../NetworkDependencies/OysterByte.h" #pragma comment(lib, "ws2_32.lib") @@ -15,7 +16,8 @@ using namespace ::Protocols; int main() { - unsigned char* recvBuffer = new unsigned char[1601]; + OysterByte recvBuffer; + cout << "Server" << endl; Translator t; @@ -42,8 +44,9 @@ int main() ProtocolSet* set = new ProtocolSet; ProtocolTest test; test.clientID = 0; + test.size = 2; test.textMessage = "hej"; - test.numOfFloats = 350; + test.numOfFloats = 35; test.f = new float[test.numOfFloats]; float temp = 395.456f; for(int i = 0; i < test.numOfFloats; i++) @@ -51,14 +54,15 @@ int main() test.f[i] = temp; temp--; } - recvBuffer = t.Pack(test); + + t.Pack(test, recvBuffer); client1.Send(recvBuffer); while(1) { client1.Recv(recvBuffer); - + t.Unpack(set, recvBuffer); cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; cout << "Client1: " << set->Protocol.pTest->textMessage << endl; @@ -85,7 +89,6 @@ int main() } ShutdownSockets(); - delete[] recvBuffer; delete set; system("pause");