Added Tests project, Callback functions are in progress, NetworkBuffer is done, Network Message is WIP but already has a lot of code, Serializer is done (subject to change), Utility classes are being made, and TcpClientWindows is WIP.
Every single piece of code wrote today may or not change, but it will probably change because ill find a better way to do it xD
This commit is contained in:
11
src/NetworkBuffer.cpp
Normal file
11
src/NetworkBuffer.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include "NetworkBuffer.hpp"
|
||||
#include "Utility.hpp"
|
||||
|
||||
NetworkBuffer::NetworkBuffer()
|
||||
{
|
||||
}
|
||||
|
||||
NetworkBuffer::~NetworkBuffer()
|
||||
{
|
||||
Utility::DeleteArray(body);
|
||||
}
|
||||
53
src/NetworkMessage.cpp
Normal file
53
src/NetworkMessage.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "NetworkMessage.hpp"
|
||||
#include "Utility.hpp"
|
||||
#include "Serializer.hpp"
|
||||
|
||||
NetworkMessage::NetworkMessage()
|
||||
{
|
||||
}
|
||||
|
||||
NetworkMessage::NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer) :
|
||||
sender(sender), distribution_mode(distribution_mode), destination_id(destination_id), tag(tag), subject(subject), buffer(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
NetworkMessage::~NetworkMessage()
|
||||
{
|
||||
}
|
||||
|
||||
const NetworkMessage &NetworkMessage::EncodeMessage()
|
||||
{
|
||||
return NetworkMessage();
|
||||
}
|
||||
|
||||
const NetworkMessage & NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
|
||||
{
|
||||
NetworkMessage message = DecodeMessageHeader(buffer);
|
||||
message.data = DecodeMessageData(buffer);
|
||||
return message;
|
||||
}
|
||||
|
||||
void *NetworkMessage::DecodeMessageData(const NetworkBuffer &buffer)
|
||||
{
|
||||
if (buffer.body_size < 9)
|
||||
return nullptr;
|
||||
byte version = buffer.body[0];
|
||||
switch (version)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
//version nor supported
|
||||
return nullptr;// WORKING HERE!!!!!!!!!!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer)
|
||||
{
|
||||
return NetworkMessage(Utility::BitConverter::ToUint16(buffer.body, 1), buffer.body[3], Utility::BitConverter::ToUint16(buffer.body, 4),
|
||||
buffer.body[6], Utility::BitConverter::ToUint16(buffer.body, 7), buffer);
|
||||
}
|
||||
22
src/Serializer.cpp
Normal file
22
src/Serializer.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "Serializer.hpp"
|
||||
|
||||
template<typename T> std::array<byte, sizeof(T)> Serializer::to_bytes(const T& object)
|
||||
{
|
||||
std::array<byte, sizeof(T)> bytes;
|
||||
|
||||
const byte *begin = reinterpret_cast<const byte*>(std::addressof(object));
|
||||
const byte *end = begin + sizeof(T);
|
||||
std::copy(begin, end, std::begin(bytes));
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
template<typename T> T& Serializer::from_bytes(const std::array<byte, sizeof(T)> &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);
|
||||
|
||||
return object;
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
#include "TcpClient.hpp"
|
||||
#include "Utility.hpp"
|
||||
#include "Config.hpp"
|
||||
#include "NetworkBuffer.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@ -57,21 +58,21 @@ TcpClient::~TcpClient()
|
||||
Utility::Delete(ptr);
|
||||
}
|
||||
|
||||
const std::string & TcpClient::GetIP()
|
||||
const std::string &TcpClient::GetIP()
|
||||
{
|
||||
return ip;
|
||||
}
|
||||
|
||||
void TcpClient::SetIP(const std::string & ip)
|
||||
{
|
||||
this->ip = ip;
|
||||
}
|
||||
|
||||
uint16 TcpClient::GetPort()
|
||||
{
|
||||
return port;
|
||||
}
|
||||
|
||||
void TcpClient::SetIP(const std::string & ip)
|
||||
{
|
||||
this->ip = ip;
|
||||
}
|
||||
|
||||
void TcpClient::SetPort(uint16 port)
|
||||
{
|
||||
this->port = port;
|
||||
@ -96,19 +97,20 @@ VoidCode TcpClient::Connect()
|
||||
|
||||
char *TcpClient::ReceiveDataArray()
|
||||
{
|
||||
char *header = new char[4]();
|
||||
|
||||
int remote_buffer_size;
|
||||
do
|
||||
{
|
||||
remote_buffer_size = recv(socket, header, 4, 0);
|
||||
NetworkBuffer buffer;
|
||||
|
||||
if (WSAGetLastError() != 0)
|
||||
{
|
||||
// there was a problem receiving
|
||||
}
|
||||
}
|
||||
while (remote_buffer_size > 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
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
const NetworkMessage &TcpClient::ReceiveData()
|
||||
|
||||
@ -14,4 +14,32 @@ void Utility::DeleteArray(void *pointer)
|
||||
return;
|
||||
delete[] pointer;
|
||||
pointer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
byte *Utility::BitConverter::FromUint8(uint8 number)
|
||||
{
|
||||
if (number == 0)
|
||||
return nullptr;
|
||||
return reinterpret_cast<byte*>(number);
|
||||
}
|
||||
|
||||
uint8 Utility::BitConverter::ToUint8(byte *bytes, uint16 start_index)
|
||||
{
|
||||
if (bytes == nullptr)
|
||||
return 0;
|
||||
return reinterpret_cast<uint8>(&bytes[start_index]);
|
||||
}
|
||||
|
||||
byte *Utility::BitConverter::FromUint16(uint16 number)
|
||||
{
|
||||
if (number == 0)
|
||||
return nullptr;
|
||||
return reinterpret_cast<byte*>(number);
|
||||
}
|
||||
|
||||
uint16 Utility::BitConverter::ToUint16(byte *bytes, uint16 start_index)
|
||||
{
|
||||
if (bytes == nullptr)
|
||||
return 0;
|
||||
return reinterpret_cast<uint16>(&bytes[start_index]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user