Massive update:

Changed all byte pointers to vectors
NetworkMessage: DecodeMessage and EncodeMessage are now static
TcpClient: Fixed OnMessage function
NetworkMessage: merged both private methods into 1 method
This commit is contained in:
xX-TheDoctor-Xx
2016-07-31 01:28:36 +01:00
parent 9e5cd40847
commit bb6a2a25d3
21 changed files with 86 additions and 109 deletions

4
TODO
View File

@ -10,4 +10,6 @@ implement close() in TcpClientWindows
implement ConfigReader methods implement ConfigReader methods
Fix NetworkBuffer and NetworkMessage code NetworkMessage EncodeMessage
Fix NetworkMessage line 50

View File

@ -0,0 +1 @@
<EFBFBD><EFBFBD>

Binary file not shown.

View File

@ -7,13 +7,15 @@
#include "Defs.hpp" #include "Defs.hpp"
#include <vector>
struct NetworkBuffer struct NetworkBuffer
{ {
NetworkBuffer(); NetworkBuffer();
~NetworkBuffer(); ~NetworkBuffer();
byte *header = nullptr; std::vector<byte> header; // contains the size of the body
byte *body = nullptr; std::vector<byte> body;
}; };
#endif #endif

View File

@ -17,7 +17,7 @@ struct NetworkMessage
static const NetworkBuffer &EncodeMessage(const NetworkMessage &message); static const NetworkBuffer &EncodeMessage(const NetworkMessage &message);
const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer); static const NetworkMessage &DecodeMessage(const NetworkBuffer &buffer);
uint16 sender = -2; uint16 sender = -2;
byte distribution_mode; byte distribution_mode;
@ -29,8 +29,6 @@ struct NetworkMessage
bool valid = false; bool valid = false;
private: private:
const NetworkMessage &decode_message_header(const NetworkBuffer &buffer);
void *decode_message_data(const NetworkBuffer &buffer);
NetworkBuffer buffer; NetworkBuffer buffer;
}; };

View File

@ -11,7 +11,7 @@
struct Serializer struct Serializer
{ {
template<typename T> static const std::vector<byte> &to_bytes(const 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); template<typename T> static const T& from_bytes(const std::vector<byte> &bytes, T& object);
}; };
#endif #endif

View File

@ -45,7 +45,7 @@ public:
std::function<void(uint16)> OnDisconnect; std::function<void(uint16)> OnDisconnect;
std::function<void(uint16)> OnConnect; std::function<void(uint16)> OnConnect;
std::function<void(uint16, uint16, uint16, void*)> OnMessage; std::function<void(uint16, byte, uint16, void*)> OnMessage;
private: private:
const NetworkBuffer &receive_data_array(); const NetworkBuffer &receive_data_array();

View File

@ -9,6 +9,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector>
struct Utility struct Utility
{ {
@ -17,29 +18,29 @@ struct Utility
struct BitConverter struct BitConverter
{ {
static byte *FromUint8(uint8 number); static const std::vector<byte> &FromUint8(uint8 number);
static uint8 ToUint8(byte *bytes, uint16 start_index); static uint8 ToUint8(const std::vector<byte> &bytes, uint16 start_index = 0);
static byte *FromUint16(uint16 number); static const std::vector<byte> &FromUint16(uint16 number);
static uint16 ToUint16(byte *bytes, uint16 start_index); static uint16 ToUint16(const std::vector<byte> &bytes, uint16 start_index = 0);
static byte *FromUint32(uint32 number); static const std::vector<byte> &FromUint32(uint32 number);
static uint32 *ToUint32(byte *bytes, uint16 start_index); static uint32 ToUint32(const std::vector<byte> &bytes, uint16 start_index = 0);
static byte *FromUint64(uint64 number); static const std::vector<byte> &FromUint64(uint64 number);
static uint64 ToUint64(byte *bytes, uint16 start_index); static uint64 ToUint64(const std::vector<byte> &bytes, uint16 start_index = 0);
static byte *FromInt8(int8 number); static const std::vector<byte> &FromInt8(int8 number);
static int8 ToInt8(byte *bytes, uint16 start_index); static int8 ToInt8(const std::vector<byte> &bytes, uint16 start_index = 0);
static byte *FromInt16(int16 number); static const std::vector<byte> &FromInt16(int16 number);
static int16 ToInt16(byte *bytes, uint16 start_index); static int16 ToInt16(const std::vector<byte> &bytes, uint16 start_index = 0);
static byte *FromInt32(int32 number); static const std::vector<byte> &FromInt32(int32 number);
static int32 ToInt32(byte *bytes, uint16 start_index); static int32 ToInt32(const std::vector<byte> &bytes, uint16 start_index = 0);
static byte *FromInt64(int64 number); static const std::vector<byte> &FromInt64(int64 number);
static int64 ToInt64(byte *bytes, uint16 start_index); static int64 ToInt64(const std::vector<byte> &bytes, uint16 start_index = 0);
}; };
struct StringConverter struct StringConverter
@ -54,7 +55,7 @@ struct Utility
static const std::string &ToString(int32 value); static const std::string &ToString(int32 value);
static const std::string &ToString(int64 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<byte> &bytes, uint16 start_index = 0, uint16 lenght = 0);
}; };
struct ConfigReader struct ConfigReader

View File

@ -7,5 +7,4 @@ NetworkBuffer::NetworkBuffer()
NetworkBuffer::~NetworkBuffer() NetworkBuffer::~NetworkBuffer()
{ {
Utility::DeleteArray(body);
} }

View File

@ -25,67 +25,36 @@ NetworkMessage::~NetworkMessage()
const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message) const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message)
{ {
NetworkBuffer buffer; return NetworkBuffer();
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<byte> serialized_data = Serializer::to_bytes(message.data);
encoded_data = &serialized_data[0];
buffer.body = encoded_message;
return buffer;
} }
const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer) const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
{ {
NetworkMessage message = decode_message_header(buffer); NetworkMessage message;
message.data = decode_message_data(buffer); message.sender = Utility::BitConverter::ToUint16(buffer.body, 1);
return message; 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 (Utility::BitConverter::ToUint32(buffer.header) < 9)
{ return message;
if (buffer.body_size < 9)
return nullptr;
byte version = buffer.body[0]; byte version = buffer.body[0];
switch (version) switch (version)
{ {
case 0: case 0:
{ {
void *object; void *object;
return Serializer::from_bytes(buffer.body + 9, object); object = Serializer::from_bytes(buffer.body, object);
} }
default: default:
{ {
//version nor supported //version not supported
throw std::runtime_error("NetworkMessage - Decoding version not supported"); throw std::runtime_error("NetworkMessage - Decoding version not supported");
} }
} }
}
const NetworkMessage &NetworkMessage::decode_message_header(const NetworkBuffer &buffer) return message;
{ }
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;
}

View File

@ -11,7 +11,7 @@ template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& obje
return bytes; return bytes;
} }
template<typename T> const T& Serializer::from_bytes(byte *bytes, T& object) template<typename T> const T& Serializer::from_bytes(const std::vector<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");

View File

@ -139,12 +139,17 @@ const NetworkBuffer &TcpClient::receive_data_array()
uint16 body_size; uint16 body_size;
if (DataAvailable(body_size)) if (DataAvailable(body_size))
buffer.body_size = body_size; {
else return NetworkBuffer(); if (!recv(tcp_socket, reinterpret_cast<char*>(&buffer.header[0]), 4, 0))
//invalid header
return NetworkBuffer();
}
else
return NetworkBuffer();
buffer.body = new byte[buffer.body_size](); uint32 body_size = Utility::BitConverter::ToUint32(buffer.header);
int32 body_received = recv(tcp_socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0); int32 body_received = recv(tcp_socket, reinterpret_cast<char*>(&buffer.body[0]), body_size, 0);
if (body_received == SOCKET_ERROR || body_received != buffer.body_size || WSAGetLastError() != 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 //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) void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *client)
{ {
NetworkBuffer buffer = message.EncodeMessage(message); NetworkBuffer buffer = NetworkMessage::EncodeMessage(message);
int32 bytes_sent = send(client->tcp_socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0); int32 bytes_sent = send(client->tcp_socket, reinterpret_cast<char*>(&buffer.body[0]), Utility::BitConverter::ToUint32(buffer.header), 0);
if (bytes_sent == SOCKET_ERROR || bytes_sent != buffer.body_size || WSAGetLastError() != 0) if (bytes_sent == SOCKET_ERROR || bytes_sent != Utility::BitConverter::ToUint32(buffer.header) || WSAGetLastError() != 0)
{ {
//something went wrong couldnt send anything/some data //something went wrong couldnt send anything/some data
} }

View File

@ -16,82 +16,82 @@ void Utility::DeleteArray(void *pointer)
pointer = nullptr; pointer = nullptr;
} }
byte *Utility::BitConverter::FromUint8(uint8 number) const std::vector<byte> &Utility::BitConverter::FromUint8(uint8 number)
{ {
return std::vector<byte>();
} }
uint8 Utility::BitConverter::ToUint8(byte *bytes, uint16 start_index) uint8 Utility::BitConverter::ToUint8(const std::vector<byte> &bytes, uint16 start_index)
{ {
return uint8();
} }
byte *Utility::BitConverter::FromUint16(uint16 number) const std::vector<byte> &Utility::BitConverter::FromUint16(uint16 number)
{ {
return std::vector<byte>();
} }
uint16 Utility::BitConverter::ToUint16(byte *bytes, uint16 start_index) uint16 Utility::BitConverter::ToUint16(const std::vector<byte> &bytes, uint16 start_index)
{ {
return uint16();
} }
byte * Utility::BitConverter::FromUint32(uint32 number) const std::vector<byte> & Utility::BitConverter::FromUint32(uint32 number)
{ {
return nullptr; return std::vector<byte>();
} }
uint32 * Utility::BitConverter::ToUint32(byte * bytes, uint16 start_index) uint32 Utility::BitConverter::ToUint32(const std::vector<byte> & bytes, uint16 start_index)
{ {
return nullptr; return uint32();
} }
byte * Utility::BitConverter::FromUint64(uint64 number) const std::vector<byte> & Utility::BitConverter::FromUint64(uint64 number)
{ {
return nullptr; return std::vector<byte>();
} }
uint64 Utility::BitConverter::ToUint64(byte * bytes, uint16 start_index) uint64 Utility::BitConverter::ToUint64(const std::vector<byte> & bytes, uint16 start_index)
{ {
return uint64(); return uint64();
} }
byte * Utility::BitConverter::FromInt8(int8 number) const std::vector<byte> & Utility::BitConverter::FromInt8(int8 number)
{ {
return nullptr; return std::vector<byte>();
} }
int8 Utility::BitConverter::ToInt8(byte * bytes, uint16 start_index) int8 Utility::BitConverter::ToInt8(const std::vector<byte> & bytes, uint16 start_index)
{ {
return int8(); return int8();
} }
byte * Utility::BitConverter::FromInt16(int16 number) const std::vector<byte> & Utility::BitConverter::FromInt16(int16 number)
{ {
return nullptr; return std::vector<byte>();
} }
int16 Utility::BitConverter::ToInt16(byte * bytes, uint16 start_index) int16 Utility::BitConverter::ToInt16(const std::vector<byte> & bytes, uint16 start_index)
{ {
return int16(); return int16();
} }
byte * Utility::BitConverter::FromInt32(int32 number) const std::vector<byte> & Utility::BitConverter::FromInt32(int32 number)
{ {
return nullptr; return std::vector<byte>();
} }
int32 Utility::BitConverter::ToInt32(byte * bytes, uint16 start_index) int32 Utility::BitConverter::ToInt32(const std::vector<byte> & bytes, uint16 start_index)
{ {
return int32(); return int32();
} }
byte * Utility::BitConverter::FromInt64(int64 number) const std::vector<byte> & Utility::BitConverter::FromInt64(int64 number)
{ {
return nullptr; return std::vector<byte>();
} }
int64 Utility::BitConverter::ToInt64(byte * bytes, uint16 start_index) int64 Utility::BitConverter::ToInt64(const std::vector<byte> & bytes, uint16 start_index)
{ {
return int64(); return int64();
} }
@ -141,7 +141,7 @@ const std::string & Utility::StringConverter::ToString(int64 value)
// TODO: insert return statement here // 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<byte> & bytes, uint16 start_index, uint16 lenght)
{ {
// TODO: insert return statement here // TODO: insert return statement here
} }