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
This commit is contained in:
xX-TheDoctor-Xx
2016-07-17 19:59:22 +01:00
parent 16ed3619b3
commit 72aa8ee0c4
11 changed files with 56 additions and 22 deletions

View File

@ -30,6 +30,7 @@
<ClInclude Include="..\..\include\Utility.hpp" /> <ClInclude Include="..\..\include\Utility.hpp" />
<ClInclude Include="..\..\include\VoidNetClient.hpp" /> <ClInclude Include="..\..\include\VoidNetClient.hpp" />
<ClInclude Include="..\..\include\VoidNetServer.hpp" /> <ClInclude Include="..\..\include\VoidNetServer.hpp" />
<ClInclude Include="..\..\src\Tags.hpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Config.cpp" /> <ClCompile Include="..\..\src\Config.cpp" />

View File

@ -42,6 +42,9 @@
<ClInclude Include="..\..\include\Callbacks.hpp"> <ClInclude Include="..\..\include\Callbacks.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Tags.hpp">
<Filter>include</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Init.cpp"> <ClCompile Include="..\..\src\Init.cpp">

View File

@ -8,10 +8,10 @@
#include "Defs.hpp" #include "Defs.hpp"
#include "Serializer.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 #endif

View File

@ -12,7 +12,7 @@ struct NetworkBuffer
NetworkBuffer(); NetworkBuffer();
~NetworkBuffer(); ~NetworkBuffer();
int body_size; uint32 body_size;
byte *body = nullptr; byte *body = nullptr;
}; };

View File

@ -11,6 +11,7 @@
struct NetworkMessage struct NetworkMessage
{ {
NetworkMessage(); NetworkMessage();
NetworkMessage(const NetworkBuffer &buffer);
NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer); NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer);
~NetworkMessage(); ~NetworkMessage();
@ -25,9 +26,9 @@ struct NetworkMessage
byte distribution_mode; byte distribution_mode;
uint16 tag; uint16 tag;
uint16 subject; uint16 subject;
void *data; void *data;
private:
NetworkBuffer buffer; NetworkBuffer buffer;
}; };

View File

@ -7,12 +7,10 @@
#include "Defs.hpp" #include "Defs.hpp"
#include <array> struct Serializer
class Serializer
{ {
template<typename T> std::array<byte, sizeof(T)> to_bytes(const T& object); template<typename T> static const std::vector<byte> &to_bytes(const T &object);
template<typename T> T& from_bytes(const std::array<byte, sizeof(T)> &bytes, T& object); template<typename T> static const T &from_bytes(byte *bytes, T &object);
}; };
#endif #endif

View File

@ -25,7 +25,7 @@ public:
VoidCode Connect(); VoidCode Connect();
char *ReceiveDataArray(); NetworkBuffer ReceiveDataArray();
const NetworkMessage &ReceiveData(); const NetworkMessage &ReceiveData();
bool SendData(const NetworkMessage &message); bool SendData(const NetworkMessage &message);

View File

@ -1,6 +1,7 @@
#include "NetworkMessage.hpp" #include "NetworkMessage.hpp"
#include "Utility.hpp" #include "Utility.hpp"
#include "Serializer.hpp" #include "Serializer.hpp"
#include <vector>
NetworkMessage::NetworkMessage() NetworkMessage::NetworkMessage()
{ {
@ -36,12 +37,13 @@ void *NetworkMessage::DecodeMessageData(const NetworkBuffer &buffer)
{ {
case 0: case 0:
{ {
break; void *object;
return Serializer::from_bytes(buffer.body, object);
} }
default: default:
{ {
//version nor supported //version nor supported
return nullptr;// WORKING HERE!!!!!!!!!!! throw std::runtime_error("NetworkMessage - Decoding version not supported");
} }
} }
} }

View File

@ -1,8 +1,10 @@
#include "Serializer.hpp" #include "Serializer.hpp"
template<typename T> std::array<byte, sizeof(T)> Serializer::to_bytes(const T& object) #include <vector>
template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& object)
{ {
std::array<byte, sizeof(T)> bytes; std::vector<byte> bytes;
const byte *begin = reinterpret_cast<const byte*>(std::addressof(object)); const byte *begin = reinterpret_cast<const byte*>(std::addressof(object));
const byte *end = begin + sizeof(T); const byte *end = begin + sizeof(T);
@ -11,12 +13,13 @@ template<typename T> std::array<byte, sizeof(T)> Serializer::to_bytes(const T& o
return bytes; return bytes;
} }
template<typename T> T& Serializer::from_bytes(const std::array<byte, sizeof(T)> &bytes, T& object) template<typename T> T& Serializer::from_bytes(byte *bytes, T& object)
{ {
static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type"); static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type");
byte *begin_object = reinterpret_cast<byte*>(std::addressof(object)); byte *begin_object = reinterpret_cast<byte*>(std::addressof(object));
std::copy(std::begin(bytes), std::end(bytes), begin_object); std::vector<byte> bytes_vector(&bytes);
std::copy(bytes_vector.begin(), bytes_vector.end(), begin_object);
return object; return object;
} }

14
src/Tags.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef TAGS_HPP
#define TAGS_HPP
#ifdef _MSC_VER
#pragma once
#endif
enum InternalTags : uint16
{
CONNECT = 65534,
DISCONNECT = 65535,
};
#endif

View File

@ -2,6 +2,8 @@
#include "Utility.hpp" #include "Utility.hpp"
#include "Config.hpp" #include "Config.hpp"
#include "NetworkBuffer.hpp" #include "NetworkBuffer.hpp"
#include "Callbacks.hpp"
#include "Tags.hpp"
#include <iostream> #include <iostream>
@ -95,27 +97,37 @@ VoidCode TcpClient::Connect()
return VOID_COULDNT_CONNECT; return VOID_COULDNT_CONNECT;
} }
char *TcpClient::ReceiveDataArray() NetworkBuffer TcpClient::ReceiveDataArray()
{ {
NetworkBuffer buffer; NetworkBuffer buffer;
if (recv(socket, reinterpret_cast<char*>(buffer.body_size), 4, 0) != 4 || WSAGetLastError() != 0) if (recv(socket, reinterpret_cast<char*>(buffer.body_size), 4, 0) != 4 || WSAGetLastError() != 0)
{ {
// there was a problem receiving the body size of the message // there was a problem receiving the body size of the message
return nullptr; return NetworkBuffer();
} }
buffer.body = new byte[buffer.body_size](); buffer.body = new byte[buffer.body_size]();
if (recv(socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0) != buffer.body_size || WSAGetLastError() != 0) if (recv(socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0) != buffer.body_size || WSAGetLastError() != 0)
{ {
//there was a problem receiving the body of the message //there was a problem receiving the body of the message
return nullptr; return NetworkBuffer();
} }
return buffer;
} }
const NetworkMessage &TcpClient::ReceiveData() 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) bool TcpClient::SendData(const NetworkMessage &message)