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:
@ -30,6 +30,7 @@
|
||||
<ClInclude Include="..\..\include\Utility.hpp" />
|
||||
<ClInclude Include="..\..\include\VoidNetClient.hpp" />
|
||||
<ClInclude Include="..\..\include\VoidNetServer.hpp" />
|
||||
<ClInclude Include="..\..\src\Tags.hpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\src\Config.cpp" />
|
||||
|
||||
@ -42,6 +42,9 @@
|
||||
<ClInclude Include="..\..\include\Callbacks.hpp">
|
||||
<Filter>include</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\Tags.hpp">
|
||||
<Filter>include</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\src\Init.cpp">
|
||||
|
||||
@ -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
|
||||
@ -12,7 +12,7 @@ struct NetworkBuffer
|
||||
NetworkBuffer();
|
||||
~NetworkBuffer();
|
||||
|
||||
int body_size;
|
||||
uint32 body_size;
|
||||
byte *body = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
|
||||
@ -7,12 +7,10 @@
|
||||
|
||||
#include "Defs.hpp"
|
||||
|
||||
#include <array>
|
||||
|
||||
class Serializer
|
||||
struct Serializer
|
||||
{
|
||||
template<typename T> std::array<byte, sizeof(T)> 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 std::vector<byte> &to_bytes(const T &object);
|
||||
template<typename T> static const T &from_bytes(byte *bytes, T &object);
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -25,7 +25,7 @@ public:
|
||||
|
||||
VoidCode Connect();
|
||||
|
||||
char *ReceiveDataArray();
|
||||
NetworkBuffer ReceiveDataArray();
|
||||
const NetworkMessage &ReceiveData();
|
||||
bool SendData(const NetworkMessage &message);
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include "NetworkMessage.hpp"
|
||||
#include "Utility.hpp"
|
||||
#include "Serializer.hpp"
|
||||
#include <vector>
|
||||
|
||||
NetworkMessage::NetworkMessage()
|
||||
{
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
#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 *end = begin + sizeof(T);
|
||||
@ -11,12 +13,13 @@ template<typename T> std::array<byte, sizeof(T)> Serializer::to_bytes(const T& o
|
||||
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");
|
||||
|
||||
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;
|
||||
}
|
||||
14
src/Tags.hpp
Normal file
14
src/Tags.hpp
Normal 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
|
||||
@ -2,6 +2,8 @@
|
||||
#include "Utility.hpp"
|
||||
#include "Config.hpp"
|
||||
#include "NetworkBuffer.hpp"
|
||||
#include "Callbacks.hpp"
|
||||
#include "Tags.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@ -95,27 +97,37 @@ VoidCode TcpClient::Connect()
|
||||
return VOID_COULDNT_CONNECT;
|
||||
}
|
||||
|
||||
char *TcpClient::ReceiveDataArray()
|
||||
NetworkBuffer TcpClient::ReceiveDataArray()
|
||||
{
|
||||
NetworkBuffer buffer;
|
||||
|
||||
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
|
||||
return nullptr;
|
||||
return NetworkBuffer();
|
||||
}
|
||||
|
||||
buffer.body = new byte[buffer.body_size]();
|
||||
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
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user