Updated TODO list

Added IS_HANDSHAKE macro to check if a NetworkMessage is a handshake easier
Utility: All From... methods are now ToBytes
replaced emplace_back to insert since it was not compiling correcly
This commit is contained in:
xX-TheDoctor-Xx
2016-08-18 22:23:43 +01:00
parent 9d1c518610
commit 996b88c0f6
16 changed files with 59 additions and 48 deletions

View File

@ -21,12 +21,13 @@ const std::vector<byte>& Handshake::EncodeHandshake(const Handshake & handshake)
{
std::vector<byte> handshake_bytes;
std::vector<byte> id = Utility::BitConverter::FromUint16(handshake.id);
std::vector<byte> con_mode = Utility::BitConverter::FromUint8(handshake.con_code);
handshake_bytes.emplace_back(Utility::BitConverter::FromUint8(1));
handshake_bytes.emplace_back(id.begin(), id.end());
handshake_bytes.emplace_back(con_mode.begin(), con_mode.end());
std::vector<byte> id = Utility::BitConverter::ToBytes(handshake.id);
std::vector<byte> con_mode = Utility::BitConverter::ToBytes(handshake.con_code);
std::vector<byte> type = Utility::BitConverter::ToBytes(static_cast<uint8>(1));
handshake_bytes.insert(handshake_bytes.begin(), type.begin(), type.end());
handshake_bytes.insert(handshake_bytes.begin(), id.begin(), id.end());
handshake_bytes.insert(handshake_bytes.begin(), con_mode.begin(), con_mode.end());
return handshake_bytes;
}

View File

@ -29,33 +29,36 @@ const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message
{
NetworkBuffer net_buffer;
if (message.subject != 1)
if (!IS_HANDSHAKE(message))
{
std::vector<byte> sender = Utility::BitConverter::FromUint16(message.sender);
std::vector<byte> distribution_mode = Utility::BitConverter::FromUint8(message.distribution_mode);
std::vector<byte> destination_id = Utility::BitConverter::FromUint16(message.destination_id);
std::vector<byte> tag = Utility::BitConverter::FromUint8(message.tag);
std::vector<byte> subject = Utility::BitConverter::FromUint16(message.subject);
std::vector<byte> sender = Utility::BitConverter::ToBytes(message.sender);
std::vector<byte> distribution_mode = Utility::BitConverter::ToBytes(message.distribution_mode);
std::vector<byte> destination_id = Utility::BitConverter::ToBytes(message.destination_id);
std::vector<byte> tag = Utility::BitConverter::ToBytes(message.tag);
std::vector<byte> subject = Utility::BitConverter::ToBytes(message.subject);
std::vector<byte> data;
if (message.data != nullptr)
data = Serializer::to_bytes(message.data);
std::vector<byte> type = Utility::BitConverter::ToBytes(0);
net_buffer.body.emplace_back(Utility::BitConverter::FromInt8(0));
net_buffer.body.emplace_back(sender.begin(), sender.end());
net_buffer.body.emplace_back(distribution_mode.begin(), distribution_mode.end());
net_buffer.body.emplace_back(destination_id.begin(), destination_id.end());
net_buffer.body.emplace_back(tag.begin(), tag.end());
net_buffer.body.emplace_back(subject.begin(), subject.end());
net_buffer.body.insert(net_buffer.body.begin(), type.begin(), type.end());
net_buffer.body.insert(net_buffer.body.begin(), sender.begin(), sender.end());
net_buffer.body.insert(net_buffer.body.begin(), distribution_mode.begin(), distribution_mode.end());
net_buffer.body.insert(net_buffer.body.begin(), destination_id.begin(), destination_id.end());
net_buffer.body.insert(net_buffer.body.begin(), tag.begin(), tag.end());
net_buffer.body.insert(net_buffer.body.begin(), subject.begin(), subject.end());
if (message.data != nullptr && data.size() > 0)
net_buffer.body.emplace_back(data.begin(), data.end());
net_buffer.header = Utility::BitConverter::FromInt32(sender.size() + distribution_mode.size() + destination_id.size() +
net_buffer.body.insert(net_buffer.body.begin(), data.begin(), data.end());
net_buffer.header = Utility::BitConverter::ToBytes(sender.size() + distribution_mode.size() + destination_id.size() +
tag.size() + subject.size() + data.size());
net_buffer.valid = true;
}
else
{
std::vector<byte> handshake_bytes = Handshake::EncodeHandshake(Handshake::NetworkMessageToHandshake(message));
net_buffer.header = Utility::BitConverter::FromInt32(handshake_bytes.size());
std::vector<byte> type = Utility::BitConverter::ToBytes(static_cast<uint8>(1));
handshake_bytes.insert(handshake_bytes.begin(), type.begin(), type.end());
net_buffer.header = Utility::BitConverter::ToBytes(handshake_bytes.size());
net_buffer.body = handshake_bytes;
}
return net_buffer;

View File

@ -56,7 +56,7 @@ void TcpClient::receive_data(TcpClient *client)
NetworkMessage message(client->ReceiveMessage());
if (message.valid)
{
if (message.subject == 1) // its a handshake
if (IS_HANDSHAKE(message))
{
if (message.tag == CONNECT) // some user has connected - not us, never
std::async(std::launch::async, client->OnConnect, message.sender);

View File

@ -80,7 +80,7 @@ bool TcpClient::Connect()
return false;
NetworkMessage message(receive_data_array());
if (message.valid && message.subject == 1)
if (IS_HANDSHAKE(message))
{
if (message.tag == ConnectionCode::Accept)
{

View File

@ -82,7 +82,7 @@ void TcpServer::accept_connections(TcpServer *server)
}
TcpClient client(client_socket);
server->AddToClientsList(client);
server->add_to_clients_list(client);
std::async(std::launch::async, &process_client_messages, server, client);
}

View File

@ -32,7 +32,7 @@ std::vector<std::string> Utility::StringConverter::Split(const std::string & str
return splited;
}
const std::vector<byte> &Utility::BitConverter::FromUint8(uint8 number)
const std::vector<byte> &Utility::BitConverter::ToBytes(uint8 number)
{
return std::vector<byte>();
}
@ -42,7 +42,7 @@ uint8 Utility::BitConverter::ToUint8(const std::vector<byte> &bytes, uint16 star
return uint8();
}
const std::vector<byte> &Utility::BitConverter::FromUint16(uint16 number)
const std::vector<byte> &Utility::BitConverter::ToBytes(uint16 number)
{
return std::vector<byte>();
}
@ -52,7 +52,7 @@ uint16 Utility::BitConverter::ToUint16(const std::vector<byte> &bytes, uint16 st
return uint16();
}
const std::vector<byte> & Utility::BitConverter::FromUint32(uint32 number)
const std::vector<byte> & Utility::BitConverter::ToBytes(uint32 number)
{
return std::vector<byte>();
}
@ -62,7 +62,7 @@ uint32 Utility::BitConverter::ToUint32(const std::vector<byte> & bytes, uint16 s
return uint32();
}
const std::vector<byte> & Utility::BitConverter::FromUint64(uint64 number)
const std::vector<byte> & Utility::BitConverter::ToBytes(uint64 number)
{
return std::vector<byte>();
}
@ -72,7 +72,7 @@ uint64 Utility::BitConverter::ToUint64(const std::vector<byte> & bytes, uint16 s
return uint64();
}
const std::vector<byte> & Utility::BitConverter::FromInt8(int8 number)
const std::vector<byte> & Utility::BitConverter::ToBytes(int8 number)
{
return std::vector<byte>();
}
@ -82,7 +82,7 @@ int8 Utility::BitConverter::ToInt8(const std::vector<byte> & bytes, uint16 start
return int8();
}
const std::vector<byte> & Utility::BitConverter::FromInt16(int16 number)
const std::vector<byte> & Utility::BitConverter::ToBytes(int16 number)
{
return std::vector<byte>();
}
@ -92,7 +92,7 @@ int16 Utility::BitConverter::ToInt16(const std::vector<byte> & bytes, uint16 sta
return int16();
}
const std::vector<byte> & Utility::BitConverter::FromInt32(int32 number)
const std::vector<byte> & Utility::BitConverter::ToBytes(int32 number)
{
return std::vector<byte>();
}
@ -102,7 +102,7 @@ int32 Utility::BitConverter::ToInt32(const std::vector<byte> & bytes, uint16 sta
return int32();
}
const std::vector<byte> & Utility::BitConverter::FromInt64(int64 number)
const std::vector<byte> & Utility::BitConverter::ToBytes(int64 number)
{
return std::vector<byte>();
}
@ -162,6 +162,11 @@ const std::string & Utility::StringConverter::ToString(const std::vector<byte>&
return std::string();
}
const std::vector<byte>& Utility::StringConverter::ToBytes(const std::string & str)
{
return std::vector<byte>();
}
const std::string & Utility::StringConverter::ToString(const std::vector<byte> & bytes, uint16 start_index, uint16 lenght)
{
return std::string();