From 72aa8ee0c43d5562550821de11a60e24a3068339 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Sun, 17 Jul 2016 19:59:22 +0100 Subject: [PATCH] Got tcp receiving for windows done Callbacks are done - maybe ill add a few more later - i dont know if they will work correctly yet Fixed the Serialization class Added internal tags - for now it only has CONNECT and DISCONNECT - maybe ill add more later Fixed some data type parameters in some functions --- VoidNetVS/VoidNetVS/VoidNetVS.vcxproj | 1 + VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters | 3 +++ include/Callbacks.hpp | 6 +++--- include/NetworkBuffer.hpp | 2 +- include/NetworkMessage.hpp | 3 ++- include/Serializer.hpp | 8 +++----- include/TcpClient.hpp | 2 +- src/NetworkMessage.cpp | 8 +++++--- src/Serializer.cpp | 11 ++++++---- src/Tags.hpp | 14 +++++++++++++ src/TcpClientWindows.cpp | 20 +++++++++++++++---- 11 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 src/Tags.hpp diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj index 75b4aaf..1314c09 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj @@ -30,6 +30,7 @@ + diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters index 9c8cac6..6e2ac58 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters @@ -42,6 +42,9 @@ include + + include + diff --git a/include/Callbacks.hpp b/include/Callbacks.hpp index 9a355de..bd3a456 100644 --- a/include/Callbacks.hpp +++ b/include/Callbacks.hpp @@ -8,10 +8,10 @@ #include "Defs.hpp" #include "Serializer.hpp" -void OnMessage(byte sender, byte tag, uint16 subject, byte *data); +void OnMessage(uint16 sender, uint16 tag, uint16 subject, void *data); -void OnDisconnect(byte id); +void OnDisconnect(uint16 id); -void OnConnect(byte id); +void OnConnect(uint16 id); #endif \ No newline at end of file diff --git a/include/NetworkBuffer.hpp b/include/NetworkBuffer.hpp index fb3a850..b85ec15 100644 --- a/include/NetworkBuffer.hpp +++ b/include/NetworkBuffer.hpp @@ -12,7 +12,7 @@ struct NetworkBuffer NetworkBuffer(); ~NetworkBuffer(); - int body_size; + uint32 body_size; byte *body = nullptr; }; diff --git a/include/NetworkMessage.hpp b/include/NetworkMessage.hpp index 47c9727..0f59eb2 100644 --- a/include/NetworkMessage.hpp +++ b/include/NetworkMessage.hpp @@ -11,6 +11,7 @@ struct NetworkMessage { NetworkMessage(); + NetworkMessage(const NetworkBuffer &buffer); NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer); ~NetworkMessage(); @@ -25,9 +26,9 @@ struct NetworkMessage byte distribution_mode; uint16 tag; uint16 subject; - void *data; +private: NetworkBuffer buffer; }; diff --git a/include/Serializer.hpp b/include/Serializer.hpp index f5cdf85..58a740a 100644 --- a/include/Serializer.hpp +++ b/include/Serializer.hpp @@ -7,12 +7,10 @@ #include "Defs.hpp" -#include - -class Serializer +struct Serializer { - template std::array to_bytes(const T& object); - template T& from_bytes(const std::array &bytes, T& object); + template static const std::vector &to_bytes(const T &object); + template static const T &from_bytes(byte *bytes, T &object); }; #endif \ No newline at end of file diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index d8a07a0..aff0048 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -25,7 +25,7 @@ public: VoidCode Connect(); - char *ReceiveDataArray(); + NetworkBuffer ReceiveDataArray(); const NetworkMessage &ReceiveData(); bool SendData(const NetworkMessage &message); diff --git a/src/NetworkMessage.cpp b/src/NetworkMessage.cpp index 1d283b9..6b79e2b 100644 --- a/src/NetworkMessage.cpp +++ b/src/NetworkMessage.cpp @@ -1,6 +1,7 @@ #include "NetworkMessage.hpp" #include "Utility.hpp" #include "Serializer.hpp" +#include NetworkMessage::NetworkMessage() { @@ -20,7 +21,7 @@ const NetworkMessage &NetworkMessage::EncodeMessage() return NetworkMessage(); } -const NetworkMessage & NetworkMessage::DecodeMessage(const NetworkBuffer &buffer) +const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer) { NetworkMessage message = DecodeMessageHeader(buffer); message.data = DecodeMessageData(buffer); @@ -36,12 +37,13 @@ void *NetworkMessage::DecodeMessageData(const NetworkBuffer &buffer) { case 0: { - break; + void *object; + return Serializer::from_bytes(buffer.body, object); } default: { //version nor supported - return nullptr;// WORKING HERE!!!!!!!!!!! + throw std::runtime_error("NetworkMessage - Decoding version not supported"); } } } diff --git a/src/Serializer.cpp b/src/Serializer.cpp index 9bb9951..7a877c6 100644 --- a/src/Serializer.cpp +++ b/src/Serializer.cpp @@ -1,8 +1,10 @@ #include "Serializer.hpp" -template std::array Serializer::to_bytes(const T& object) +#include + +template const std::vector &Serializer::to_bytes(const T& object) { - std::array bytes; + std::vector bytes; const byte *begin = reinterpret_cast(std::addressof(object)); const byte *end = begin + sizeof(T); @@ -11,12 +13,13 @@ template std::array Serializer::to_bytes(const T& o return bytes; } -template T& Serializer::from_bytes(const std::array &bytes, T& object) +template T& Serializer::from_bytes(byte *bytes, T& object) { static_assert(std::is_trivially_copyable::value, "not a TriviallyCopyable type"); byte *begin_object = reinterpret_cast(std::addressof(object)); - std::copy(std::begin(bytes), std::end(bytes), begin_object); + std::vector bytes_vector(&bytes); + std::copy(bytes_vector.begin(), bytes_vector.end(), begin_object); return object; } \ No newline at end of file diff --git a/src/Tags.hpp b/src/Tags.hpp new file mode 100644 index 0000000..154ef10 --- /dev/null +++ b/src/Tags.hpp @@ -0,0 +1,14 @@ +#ifndef TAGS_HPP +#define TAGS_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +enum InternalTags : uint16 +{ + CONNECT = 65534, + DISCONNECT = 65535, +}; + +#endif \ No newline at end of file diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index c3d2101..7dd1b33 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -2,6 +2,8 @@ #include "Utility.hpp" #include "Config.hpp" #include "NetworkBuffer.hpp" +#include "Callbacks.hpp" +#include "Tags.hpp" #include @@ -95,27 +97,37 @@ VoidCode TcpClient::Connect() return VOID_COULDNT_CONNECT; } -char *TcpClient::ReceiveDataArray() +NetworkBuffer TcpClient::ReceiveDataArray() { NetworkBuffer buffer; if (recv(socket, reinterpret_cast(buffer.body_size), 4, 0) != 4 || WSAGetLastError() != 0) { // there was a problem receiving the body size of the message - return nullptr; + return NetworkBuffer(); } buffer.body = new byte[buffer.body_size](); if (recv(socket, reinterpret_cast(buffer.body), buffer.body_size, 0) != buffer.body_size || WSAGetLastError() != 0) { //there was a problem receiving the body of the message - return nullptr; + return NetworkBuffer(); } + + return buffer; } const NetworkMessage &TcpClient::ReceiveData() { - + NetworkBuffer received_data = ReceiveDataArray(); + NetworkMessage message = NetworkMessage(received_data); + if (message.tag == CONNECT) + OnConnect(message.sender); + else if (message.tag == DISCONNECT) + OnDisconnect(message.sender); + else + OnMessage(message.sender, message.tag, message.subject, message.data); + return message; } bool TcpClient::SendData(const NetworkMessage &message)