diff --git a/TODO b/TODO index ac410b0..4f156fe 100644 --- a/TODO +++ b/TODO @@ -10,4 +10,6 @@ implement close() in TcpClientWindows implement ConfigReader methods -Fix NetworkBuffer and NetworkMessage code \ No newline at end of file +NetworkMessage EncodeMessage + +Fix NetworkMessage line 50 \ No newline at end of file diff --git a/TestsVS/Tests/Release/Tests.tlog/CL.read.1.tlog b/TestsVS/Tests/Release/Tests.tlog/CL.read.1.tlog index e3d9241..833e1ce 100644 Binary files a/TestsVS/Tests/Release/Tests.tlog/CL.read.1.tlog and b/TestsVS/Tests/Release/Tests.tlog/CL.read.1.tlog differ diff --git a/TestsVS/Tests/Release/Tests.tlog/link.command.1.tlog b/TestsVS/Tests/Release/Tests.tlog/link.command.1.tlog index 7d676d7..46b134b 100644 Binary files a/TestsVS/Tests/Release/Tests.tlog/link.command.1.tlog and b/TestsVS/Tests/Release/Tests.tlog/link.command.1.tlog differ diff --git a/TestsVS/Tests/Release/Tests.tlog/link.read.1.tlog b/TestsVS/Tests/Release/Tests.tlog/link.read.1.tlog index c0d510f..46b134b 100644 Binary files a/TestsVS/Tests/Release/Tests.tlog/link.read.1.tlog and b/TestsVS/Tests/Release/Tests.tlog/link.read.1.tlog differ diff --git a/TestsVS/Tests/Release/Tests.tlog/link.read.4.tlog b/TestsVS/Tests/Release/Tests.tlog/link.read.4.tlog new file mode 100644 index 0000000..46b134b --- /dev/null +++ b/TestsVS/Tests/Release/Tests.tlog/link.read.4.tlog @@ -0,0 +1 @@ +ÿþ \ No newline at end of file diff --git a/TestsVS/Tests/Release/Tests.tlog/link.write.1.tlog b/TestsVS/Tests/Release/Tests.tlog/link.write.1.tlog index 33fb381..46b134b 100644 Binary files a/TestsVS/Tests/Release/Tests.tlog/link.write.1.tlog and b/TestsVS/Tests/Release/Tests.tlog/link.write.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/unsuccessfulbuild b/TestsVS/Tests/Release/Tests.tlog/unsuccessfulbuild similarity index 100% rename from VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/unsuccessfulbuild rename to TestsVS/Tests/Release/Tests.tlog/unsuccessfulbuild diff --git a/VoidNetVS/Release/Tests.ipdb b/VoidNetVS/Release/Tests.ipdb index 10a1feb..6b09d0f 100644 Binary files a/VoidNetVS/Release/Tests.ipdb and b/VoidNetVS/Release/Tests.ipdb differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog index 9da15b6..8e4beea 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog index 895359d..99ef74b 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog index 19a0415..e74ead0 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog differ diff --git a/include/NetworkBuffer.hpp b/include/NetworkBuffer.hpp index 32e1c13..f25c8d7 100644 --- a/include/NetworkBuffer.hpp +++ b/include/NetworkBuffer.hpp @@ -7,13 +7,15 @@ #include "Defs.hpp" +#include + struct NetworkBuffer { NetworkBuffer(); ~NetworkBuffer(); - byte *header = nullptr; - byte *body = nullptr; + std::vector header; // contains the size of the body + std::vector body; }; #endif \ No newline at end of file diff --git a/include/NetworkMessage.hpp b/include/NetworkMessage.hpp index 5a1c9a8..bbc4b0f 100644 --- a/include/NetworkMessage.hpp +++ b/include/NetworkMessage.hpp @@ -17,7 +17,7 @@ struct NetworkMessage static const NetworkBuffer &EncodeMessage(const NetworkMessage &message); - const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer); + static const NetworkMessage &DecodeMessage(const NetworkBuffer &buffer); uint16 sender = -2; byte distribution_mode; @@ -29,8 +29,6 @@ struct NetworkMessage bool valid = false; private: - const NetworkMessage &decode_message_header(const NetworkBuffer &buffer); - void *decode_message_data(const NetworkBuffer &buffer); NetworkBuffer buffer; }; diff --git a/include/Serializer.hpp b/include/Serializer.hpp index 989470d..8bec1af 100644 --- a/include/Serializer.hpp +++ b/include/Serializer.hpp @@ -11,7 +11,7 @@ struct Serializer { template static const std::vector &to_bytes(const T &object); - template static const T& from_bytes(byte *bytes, T& object); + template static const T& from_bytes(const std::vector &bytes, T& object); }; #endif \ No newline at end of file diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 88cf530..be881d2 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -45,7 +45,7 @@ public: std::function OnDisconnect; std::function OnConnect; - std::function OnMessage; + std::function OnMessage; private: const NetworkBuffer &receive_data_array(); diff --git a/include/Utility.hpp b/include/Utility.hpp index 2f15b80..738a0c1 100644 --- a/include/Utility.hpp +++ b/include/Utility.hpp @@ -9,6 +9,7 @@ #include #include +#include struct Utility { @@ -17,29 +18,29 @@ struct Utility struct BitConverter { - static byte *FromUint8(uint8 number); - static uint8 ToUint8(byte *bytes, uint16 start_index); + static const std::vector &FromUint8(uint8 number); + static uint8 ToUint8(const std::vector &bytes, uint16 start_index = 0); - static byte *FromUint16(uint16 number); - static uint16 ToUint16(byte *bytes, uint16 start_index); + static const std::vector &FromUint16(uint16 number); + static uint16 ToUint16(const std::vector &bytes, uint16 start_index = 0); - static byte *FromUint32(uint32 number); - static uint32 *ToUint32(byte *bytes, uint16 start_index); + static const std::vector &FromUint32(uint32 number); + static uint32 ToUint32(const std::vector &bytes, uint16 start_index = 0); - static byte *FromUint64(uint64 number); - static uint64 ToUint64(byte *bytes, uint16 start_index); + static const std::vector &FromUint64(uint64 number); + static uint64 ToUint64(const std::vector &bytes, uint16 start_index = 0); - static byte *FromInt8(int8 number); - static int8 ToInt8(byte *bytes, uint16 start_index); + static const std::vector &FromInt8(int8 number); + static int8 ToInt8(const std::vector &bytes, uint16 start_index = 0); - static byte *FromInt16(int16 number); - static int16 ToInt16(byte *bytes, uint16 start_index); + static const std::vector &FromInt16(int16 number); + static int16 ToInt16(const std::vector &bytes, uint16 start_index = 0); - static byte *FromInt32(int32 number); - static int32 ToInt32(byte *bytes, uint16 start_index); + static const std::vector &FromInt32(int32 number); + static int32 ToInt32(const std::vector &bytes, uint16 start_index = 0); - static byte *FromInt64(int64 number); - static int64 ToInt64(byte *bytes, uint16 start_index); + static const std::vector &FromInt64(int64 number); + static int64 ToInt64(const std::vector &bytes, uint16 start_index = 0); }; struct StringConverter @@ -54,7 +55,7 @@ struct Utility static const std::string &ToString(int32 value); static const std::string &ToString(int64 value); - static const std::string &ToString(byte *bytes, uint16 start_index = 0, uint16 lenght = 0); + static const std::string &ToString(const std::vector &bytes, uint16 start_index = 0, uint16 lenght = 0); }; struct ConfigReader diff --git a/src/NetworkBuffer.cpp b/src/NetworkBuffer.cpp index a328b30..66049c0 100644 --- a/src/NetworkBuffer.cpp +++ b/src/NetworkBuffer.cpp @@ -7,5 +7,4 @@ NetworkBuffer::NetworkBuffer() NetworkBuffer::~NetworkBuffer() { - Utility::DeleteArray(body); } \ No newline at end of file diff --git a/src/NetworkMessage.cpp b/src/NetworkMessage.cpp index b76ee86..3e5dfc9 100644 --- a/src/NetworkMessage.cpp +++ b/src/NetworkMessage.cpp @@ -25,67 +25,36 @@ NetworkMessage::~NetworkMessage() const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message) { - NetworkBuffer buffer; - uint16 size = sizeof(buffer.body_size) + sizeof(buffer.body); - byte *encoded_message = new byte[size](); - buffer.body_size = size; - - byte *encoded_sender = &encoded_message[1]; - encoded_sender = Utility::BitConverter::FromUint16(message.sender); - - encoded_message[3] = message.distribution_mode; - - byte *encoded_destination_id = &encoded_message[4]; - encoded_destination_id = Utility::BitConverter::FromUint16(message.destination_id); - - encoded_message[6] = message.tag; - - byte *encoded_subject = &encoded_message[7]; - encoded_subject = Utility::BitConverter::FromUint16(message.subject); - - byte *encoded_data = &encoded_message[9]; - std::vector serialized_data = Serializer::to_bytes(message.data); - encoded_data = &serialized_data[0]; - - buffer.body = encoded_message; - return buffer; + return NetworkBuffer(); } const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer) { - NetworkMessage message = decode_message_header(buffer); - message.data = decode_message_data(buffer); - return message; -} + NetworkMessage message; + message.sender = Utility::BitConverter::ToUint16(buffer.body, 1); + message.distribution_mode = buffer.body[3]; + message.destination_id = Utility::BitConverter::ToUint16(buffer.body, 4); + message.tag = buffer.body[6]; + message.subject = Utility::BitConverter::ToUint16(buffer.body, 7); + message.buffer = buffer; + message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT; -void *NetworkMessage::decode_message_data(const NetworkBuffer &buffer) -{ - if (buffer.body_size < 9) - return nullptr; + if (Utility::BitConverter::ToUint32(buffer.header) < 9) + return message; byte version = buffer.body[0]; switch (version) { case 0: { void *object; - return Serializer::from_bytes(buffer.body + 9, object); + object = Serializer::from_bytes(buffer.body, object); } default: { - //version nor supported + //version not supported throw std::runtime_error("NetworkMessage - Decoding version not supported"); } } -} -const NetworkMessage &NetworkMessage::decode_message_header(const NetworkBuffer &buffer) -{ - sender = Utility::BitConverter::ToUint16(buffer.body, 1); - distribution_mode = buffer.body[3]; - destination_id = Utility::BitConverter::ToUint16(buffer.body, 4); - tag = buffer.body[6]; - subject = Utility::BitConverter::ToUint16(buffer.body, 7); - this->buffer = buffer; - valid = sender != -2 && tag != CONNECT && tag != DISCONNECT && distribution_mode > 0; - return *this; -} + return message; +} \ No newline at end of file diff --git a/src/Serializer.cpp b/src/Serializer.cpp index 4505087..02e5d9b 100644 --- a/src/Serializer.cpp +++ b/src/Serializer.cpp @@ -11,7 +11,7 @@ template const std::vector &Serializer::to_bytes(const T& obje return bytes; } -template const T& Serializer::from_bytes(byte *bytes, T& object) +template const T& Serializer::from_bytes(const std::vector &bytes, T& object) { static_assert(std::is_trivially_copyable::value, "not a TriviallyCopyable type"); diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index 2ff5755..b9ea324 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -139,12 +139,17 @@ const NetworkBuffer &TcpClient::receive_data_array() uint16 body_size; if (DataAvailable(body_size)) - buffer.body_size = body_size; - else return NetworkBuffer(); + { + if (!recv(tcp_socket, reinterpret_cast(&buffer.header[0]), 4, 0)) + //invalid header + return NetworkBuffer(); + } + else + return NetworkBuffer(); - buffer.body = new byte[buffer.body_size](); - int32 body_received = recv(tcp_socket, reinterpret_cast(buffer.body), buffer.body_size, 0); - if (body_received == SOCKET_ERROR || body_received != buffer.body_size || WSAGetLastError() != 0) + uint32 body_size = Utility::BitConverter::ToUint32(buffer.header); + int32 body_received = recv(tcp_socket, reinterpret_cast(&buffer.body[0]), body_size, 0); + if (body_received == SOCKET_ERROR || body_received != body_size || WSAGetLastError() != 0) { //there was a problem receiving the body of the message or theres no body to receive } @@ -181,9 +186,9 @@ const NetworkMessage & TcpClient::ReceiveMessage() void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *client) { - NetworkBuffer buffer = message.EncodeMessage(message); - int32 bytes_sent = send(client->tcp_socket, reinterpret_cast(buffer.body), buffer.body_size, 0); - if (bytes_sent == SOCKET_ERROR || bytes_sent != buffer.body_size || WSAGetLastError() != 0) + NetworkBuffer buffer = NetworkMessage::EncodeMessage(message); + int32 bytes_sent = send(client->tcp_socket, reinterpret_cast(&buffer.body[0]), Utility::BitConverter::ToUint32(buffer.header), 0); + if (bytes_sent == SOCKET_ERROR || bytes_sent != Utility::BitConverter::ToUint32(buffer.header) || WSAGetLastError() != 0) { //something went wrong couldnt send anything/some data } diff --git a/src/Utility.cpp b/src/Utility.cpp index 3108a28..1dbe2a1 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -16,82 +16,82 @@ void Utility::DeleteArray(void *pointer) pointer = nullptr; } -byte *Utility::BitConverter::FromUint8(uint8 number) +const std::vector &Utility::BitConverter::FromUint8(uint8 number) { - + return std::vector(); } -uint8 Utility::BitConverter::ToUint8(byte *bytes, uint16 start_index) +uint8 Utility::BitConverter::ToUint8(const std::vector &bytes, uint16 start_index) { - + return uint8(); } -byte *Utility::BitConverter::FromUint16(uint16 number) +const std::vector &Utility::BitConverter::FromUint16(uint16 number) { - + return std::vector(); } -uint16 Utility::BitConverter::ToUint16(byte *bytes, uint16 start_index) +uint16 Utility::BitConverter::ToUint16(const std::vector &bytes, uint16 start_index) { - + return uint16(); } -byte * Utility::BitConverter::FromUint32(uint32 number) +const std::vector & Utility::BitConverter::FromUint32(uint32 number) { - return nullptr; + return std::vector(); } -uint32 * Utility::BitConverter::ToUint32(byte * bytes, uint16 start_index) +uint32 Utility::BitConverter::ToUint32(const std::vector & bytes, uint16 start_index) { - return nullptr; + return uint32(); } -byte * Utility::BitConverter::FromUint64(uint64 number) +const std::vector & Utility::BitConverter::FromUint64(uint64 number) { - return nullptr; + return std::vector(); } -uint64 Utility::BitConverter::ToUint64(byte * bytes, uint16 start_index) +uint64 Utility::BitConverter::ToUint64(const std::vector & bytes, uint16 start_index) { return uint64(); } -byte * Utility::BitConverter::FromInt8(int8 number) +const std::vector & Utility::BitConverter::FromInt8(int8 number) { - return nullptr; + return std::vector(); } -int8 Utility::BitConverter::ToInt8(byte * bytes, uint16 start_index) +int8 Utility::BitConverter::ToInt8(const std::vector & bytes, uint16 start_index) { return int8(); } -byte * Utility::BitConverter::FromInt16(int16 number) +const std::vector & Utility::BitConverter::FromInt16(int16 number) { - return nullptr; + return std::vector(); } -int16 Utility::BitConverter::ToInt16(byte * bytes, uint16 start_index) +int16 Utility::BitConverter::ToInt16(const std::vector & bytes, uint16 start_index) { return int16(); } -byte * Utility::BitConverter::FromInt32(int32 number) +const std::vector & Utility::BitConverter::FromInt32(int32 number) { - return nullptr; + return std::vector(); } -int32 Utility::BitConverter::ToInt32(byte * bytes, uint16 start_index) +int32 Utility::BitConverter::ToInt32(const std::vector & bytes, uint16 start_index) { return int32(); } -byte * Utility::BitConverter::FromInt64(int64 number) +const std::vector & Utility::BitConverter::FromInt64(int64 number) { - return nullptr; + return std::vector(); } -int64 Utility::BitConverter::ToInt64(byte * bytes, uint16 start_index) +int64 Utility::BitConverter::ToInt64(const std::vector & bytes, uint16 start_index) { return int64(); } @@ -141,7 +141,7 @@ const std::string & Utility::StringConverter::ToString(int64 value) // TODO: insert return statement here } -const std::string & Utility::StringConverter::ToString(byte * bytes, uint16 start_index, uint16 lenght) +const std::string & Utility::StringConverter::ToString(const std::vector & bytes, uint16 start_index, uint16 lenght) { // TODO: insert return statement here }