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:
@ -7,5 +7,4 @@ NetworkBuffer::NetworkBuffer()
|
||||
|
||||
NetworkBuffer::~NetworkBuffer()
|
||||
{
|
||||
Utility::DeleteArray(body);
|
||||
}
|
||||
@ -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<byte> 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;
|
||||
}
|
||||
@ -11,7 +11,7 @@ template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& obje
|
||||
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");
|
||||
|
||||
|
||||
@ -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<char*>(&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<char*>(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<char*>(&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<char*>(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<char*>(&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
|
||||
}
|
||||
|
||||
@ -16,82 +16,82 @@ void Utility::DeleteArray(void *pointer)
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
@ -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<byte> & bytes, uint16 start_index, uint16 lenght)
|
||||
{
|
||||
// TODO: insert return statement here
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user