From 9d1c5186100f96f6d7ede4c4be8006abc0708847 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Thu, 18 Aug 2016 15:08:51 +0100 Subject: [PATCH] Added conversion from Handshake to NetworkMessage and vice versa Added a new ConnectionCode - Close TcpServer: AddToClientsList and AllocateID are now called add_to_clients_list and allocate_id respectively and are now private, removed SendHandshake method - we now use SendMessage method and the HandshakeToNetworkMessage method in the Handshake class Added a new method to Utility::StringConverter - FromString Changed NetworkMessage conversions to allow Handshake conversions NetworkMessage: if subject is 1 then the NetworkMessage is a handshake I may be missing something but im going to check it I may be overcomplicating this but i want to make it as simple as possible xD --- DEV_INFO | 4 +- .../Release/VoidNetVS.tlog/CL.command.1.tlog | Bin 5898 -> 5698 bytes .../Release/VoidNetVS.tlog/CL.read.1.tlog | Bin 252568 -> 252370 bytes .../Release/VoidNetVS.tlog/CL.write.1.tlog | Bin 13286 -> 14862 bytes VoidNetVS/VoidNetVS/VoidNetVS.vcxproj | 2 +- include/Defs.hpp | 3 +- include/Handshake.hpp | 3 + include/TcpServer.hpp | 7 +- include/Utility.hpp | 2 + include/VoidNetServer.hpp | 2 + src/Handshake.cpp | 26 +++++- src/NetworkMessage.cpp | 86 +++++++++++------- src/TcpClient.cpp | 11 ++- src/TcpClientWindows.cpp | 16 ++-- src/TcpServer.cpp | 47 ++-------- src/Utility.cpp | 4 +- 16 files changed, 118 insertions(+), 95 deletions(-) diff --git a/DEV_INFO b/DEV_INFO index eb5d275..cc71118 100644 --- a/DEV_INFO +++ b/DEV_INFO @@ -1,2 +1,4 @@ id -1 is server / valid message if other parameters are valid as well -id -2 is invalid network message \ No newline at end of file +id -2 is invalid network message + +NetworkMessage: subject 1 is reserved for handshake validation \ No newline at end of file diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog index 8b65bd7c07d2754a7980d2fcb9ae4fcd6c2eb161..5e21b6177274b4d19d0c23ea62cac10a601d046e 100644 GIT binary patch delta 368 zcmeCuJESwgg;S3~k0Az#^(Gezh$DC#8#$Q}JaoC0jFSskV(`kZV-cC$$D%O#4~N?1 z1wvf7)NbC$xt(z`7iSh81=9FkvWeGW@&pmK$uSv!;aI3V6*=4O923;K2Zz+ delta 255 zcmX@4)1^1TWwHU2*yIv!5g;wIc^3Bq#>rw#37CR@OmdU;m=q?rvB+Ucma(N^3a;U( zz$VByiBoE_9Jd#m+ylnR8e&qDlAY50H45m|p`RgAxOW5(5F35(5Ijm%iiz61UoH0tEAy>;nQOw|-&+ z8hDqo0|Gj?-d_Wt0+;ZX0~)j5Yz{M*OgsX9w~(O%a5a}8q5_TqQm+ml delta 90 zcmV-g0Hy!Zvk#cH50H41u$>0C?rZ~e0+Y6BAc2^Lm;sfT0>GEfYy%pXZu9~smoSk5 w4wo>m0t~ktqydaGw_t7o@HCUQX&{$Qc>)r*Y5@WS^S9`11E2!8l=K1`c;BBT9smFU diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog index 813be0b5bc9b166e69713c0bbf5ce829b6478635..bf30e6d91fddbb414caec0f20d6a2be942bd08e0 100644 GIT binary patch delta 202 zcmaEs-dD0Ai*<6Iw$kK#Qr|ZBi5f9Z4$zmI93UJs*+98(@&O|SsN}rO4~*CtHy3b} zF;4yet&eK<$#4fTqz(@#ak~H7sd+JnYlW#HZo17pk1ptt@N!1ZZ;5AnySuitable Speed MultiThreaded - F:\VoidNet\include;F:\VoidNet\src + ..\..\include;..\..\src true diff --git a/include/Defs.hpp b/include/Defs.hpp index 75d8065..8cdec9c 100644 --- a/include/Defs.hpp +++ b/include/Defs.hpp @@ -233,7 +233,8 @@ enum DistributionType enum ConnectionCode { Accept, - Reject + Reject, + Close }; enum InternalTags diff --git a/include/Handshake.hpp b/include/Handshake.hpp index 3ebe63b..14125e6 100644 --- a/include/Handshake.hpp +++ b/include/Handshake.hpp @@ -6,6 +6,7 @@ #endif #include "Defs.hpp" +#include "NetworkMessage.hpp" #include @@ -17,6 +18,8 @@ struct Handshake static const std::vector &EncodeHandshake(const Handshake &handshake); static Handshake &DecodeHandshake(const std::vector &bytes); + static const NetworkMessage &HandshakeToNetworkMessage(const Handshake &handshake); + static const Handshake &NetworkMessageToHandshake(const NetworkMessage &message); uint16 id; byte con_code; diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp index 4ac6ddf..f4bd958 100644 --- a/include/TcpServer.hpp +++ b/include/TcpServer.hpp @@ -19,13 +19,10 @@ struct TcpServer void Shutdown(); - uint16 AllocateID(); - void AddToClientsList(TcpClient &client); bool StartServer(bool accept_connections); // if accept_connections is false the user must call the funcion AcceptConnections() void AcceptConnections(); void SendMessage(const NetworkMessage &message); - void SendHandshake(const Handshake &handshake); void RejectConnection(TcpClient &client); void AcceptConnection(uint16 client); @@ -44,6 +41,10 @@ private: static void process_client_messages(TcpServer *server, TcpClient &client); static void accept_connections(TcpServer *server); + void add_to_clients_list(TcpClient &client); + + uint16 allocate_id(); + void shutdown_internal(); bool initialize(uint16 port = default_server_port); diff --git a/include/Utility.hpp b/include/Utility.hpp index a257d00..76730f9 100644 --- a/include/Utility.hpp +++ b/include/Utility.hpp @@ -56,6 +56,8 @@ struct Utility static const std::string &ToString(int64 value); static const std::string &ToString(const std::vector &bytes); + static const std::vector &FromString(const std::string &str); + static const std::string &ToString(const std::vector &bytes, uint16 start_index = 0, uint16 lenght = 0); static const std::string &Trim(std::string &str, char ch); diff --git a/include/VoidNetServer.hpp b/include/VoidNetServer.hpp index a2dff8f..8bd5b68 100644 --- a/include/VoidNetServer.hpp +++ b/include/VoidNetServer.hpp @@ -9,6 +9,8 @@ struct VoidNetServer { + void StartServer(uint16 port); + void StopServer(); private: TcpServer server; }; diff --git a/src/Handshake.cpp b/src/Handshake.cpp index c28ddff..1453c4c 100644 --- a/src/Handshake.cpp +++ b/src/Handshake.cpp @@ -24,8 +24,9 @@ const std::vector& Handshake::EncodeHandshake(const Handshake & handshake) std::vector id = Utility::BitConverter::FromUint16(handshake.id); std::vector con_mode = Utility::BitConverter::FromUint8(handshake.con_code); - handshake_bytes.insert(handshake_bytes.end(), id.begin(), id.end()); - handshake_bytes.insert(handshake_bytes.end(), con_mode.begin(), con_mode.end()); + 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()); return handshake_bytes; } @@ -34,8 +35,25 @@ Handshake & Handshake::DecodeHandshake(const std::vector& bytes) { Handshake handshake; - handshake.id = Utility::BitConverter::ToUint16(bytes); - handshake.con_code = Utility::BitConverter::ToUint8(bytes, 2); + handshake.id = Utility::BitConverter::ToUint16(bytes, 1); + handshake.con_code = Utility::BitConverter::ToUint8(bytes, 3); return handshake; } + +const NetworkMessage & Handshake::HandshakeToNetworkMessage(const Handshake & handshake) +{ + NetworkMessage message; + message.sender = handshake.id; + message.tag = handshake.con_code; + message.subject = 1; + return message; +} + +const Handshake & Handshake::NetworkMessageToHandshake(const NetworkMessage & message) +{ + Handshake handshake; + handshake.id = message.sender; + handshake.con_code = message.tag; + return handshake; +} diff --git a/src/NetworkMessage.cpp b/src/NetworkMessage.cpp index f152de4..82dde05 100644 --- a/src/NetworkMessage.cpp +++ b/src/NetworkMessage.cpp @@ -1,6 +1,7 @@ #include "NetworkMessage.hpp" #include "Utility.hpp" #include "Serializer.hpp" +#include "Handshake.hpp" #include @@ -28,25 +29,36 @@ const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message { NetworkBuffer net_buffer; - std::vector sender = Utility::BitConverter::FromUint16(message.sender); - std::vector distribution_mode = Utility::BitConverter::FromUint8(message.distribution_mode); - std::vector destination_id = Utility::BitConverter::FromUint16(message.destination_id); - std::vector tag = Utility::BitConverter::FromUint8(message.tag); - std::vector subject = Utility::BitConverter::FromUint16(message.subject); - std::vector data; - if (message.data != nullptr) - data = Serializer::to_bytes(message.data); + if (message.subject != 1) + { + std::vector sender = Utility::BitConverter::FromUint16(message.sender); + std::vector distribution_mode = Utility::BitConverter::FromUint8(message.distribution_mode); + std::vector destination_id = Utility::BitConverter::FromUint16(message.destination_id); + std::vector tag = Utility::BitConverter::FromUint8(message.tag); + std::vector subject = Utility::BitConverter::FromUint16(message.subject); + std::vector data; + if (message.data != nullptr) + data = Serializer::to_bytes(message.data); - net_buffer.body.insert(net_buffer.body.end(), sender.begin(), sender.end()); - net_buffer.body.insert(net_buffer.body.end(), distribution_mode.begin(), distribution_mode.end()); - net_buffer.body.insert(net_buffer.body.end(), destination_id.begin(), destination_id.end()); - net_buffer.body.insert(net_buffer.body.end(), tag.begin(), tag.end()); - net_buffer.body.insert(net_buffer.body.end(), subject.begin(), subject.end()); - if (message.data != nullptr && data.size() > 0) - net_buffer.body.insert(net_buffer.body.end(), data.begin(), data.end()); - net_buffer.header = Utility::BitConverter::FromInt32(sender.size() + distribution_mode.size() + destination_id.size() + - tag.size() + subject.size() + data.size()); - net_buffer.valid = true; + 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()); + 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() + + tag.size() + subject.size() + data.size()); + net_buffer.valid = true; + } + else + { + std::vector handshake_bytes = Handshake::EncodeHandshake(Handshake::NetworkMessageToHandshake(message)); + net_buffer.header = Utility::BitConverter::FromInt32(handshake_bytes.size()); + net_buffer.body = handshake_bytes; + } + return net_buffer; } return NetworkBuffer(); } @@ -55,32 +67,42 @@ const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer) { if (buffer.valid) { - 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::ToUint8(buffer.body, 7); - message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT; - - if (Utility::BitConverter::ToInt32(buffer.header) < 9) - return message; - byte version = buffer.body[0]; - switch (version) + byte type = buffer.body[0]; + switch (type) { case 0: { + 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::ToUint8(buffer.body, 7); + message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT; + if (Utility::BitConverter::ToInt32(buffer.header) < 9) + return message; void *object; object = Serializer::from_bytes(buffer.body, object); + return message; + } + case 1: + { + NetworkMessage message; + message.sender = Utility::BitConverter::ToUint16(buffer.body, 1); + message.tag = Utility::BitConverter::ToUint8(buffer.body, 3); + message.subject = 1; + message.valid = message.sender != -2 && (message.tag == DISCONNECT || message.tag == CONNECT || message.tag == ConnectionCode::Accept || + message.tag == ConnectionCode::Close || message.tag == ConnectionCode::Reject); + return message; + break; } default: { - //version not supported + //type not supported throw std::runtime_error("NetworkMessage - Decoding version not supported"); } } - return message; } return NetworkMessage(); } \ No newline at end of file diff --git a/src/TcpClient.cpp b/src/TcpClient.cpp index bd664b1..c4aa5bc 100644 --- a/src/TcpClient.cpp +++ b/src/TcpClient.cpp @@ -56,10 +56,13 @@ void TcpClient::receive_data(TcpClient *client) NetworkMessage message(client->ReceiveMessage()); if (message.valid) { - if (message.tag == CONNECT) // some user has connected - std::async(std::launch::async, client->OnConnect, message.sender); - else if (message.tag == DISCONNECT) // some user has disconnected - std::async(std::launch::async, client->OnDisconnect, message.sender); + if (message.subject == 1) // its a handshake + { + if (message.tag == CONNECT) // some user has connected - not us, never + std::async(std::launch::async, client->OnConnect, message.sender); + else if (message.tag == DISCONNECT || message.tag == ConnectionCode::Close) // some user has disconnected + std::async(std::launch::async, client->OnDisconnect, message.sender); + } else std::async(std::launch::async, client->OnMessage, message.sender, message.tag, message.subject, message.data); // we received data } diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index 2e2c4d9..57e35e5 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -53,11 +53,8 @@ TcpClient::~TcpClient() void TcpClient::Shutdown() { - NetworkMessage message; - message.sender = id; - message.distribution_mode = Server; - message.tag = DISCONNECT; - SendMessage(message); + Handshake handshake(id, Close, Server); + SendMessage(Handshake::HandshakeToNetworkMessage(handshake)); uint16 code = closesocket(tcp_socket); if (code == SOCKET_ERROR) { @@ -82,14 +79,13 @@ bool TcpClient::Connect() if (connect_code == SOCKET_ERROR) return false; - NetworkBuffer buffer(receive_data_array()); - if (buffer.valid) + NetworkMessage message(receive_data_array()); + if (message.valid && message.subject == 1) { - Handshake handshake = Handshake::DecodeHandshake(buffer.body); - if (handshake.con_code == ConnectionCode::Accept) + if (message.tag == ConnectionCode::Accept) { receive = true; - OnConnect(handshake.id); + OnConnect(message.sender); return true; } } diff --git a/src/TcpServer.cpp b/src/TcpServer.cpp index d153329..5963aa3 100644 --- a/src/TcpServer.cpp +++ b/src/TcpServer.cpp @@ -34,8 +34,11 @@ void TcpServer::Shutdown() void TcpServer::AcceptConnections() { - running = true; - std::async(std::launch::async, &accept_connections, this); + if (!running) + { + running = true; + std::async(std::launch::async, &accept_connections, this); + } } void TcpServer::process_client_messages(TcpServer *server, TcpClient & client) @@ -109,36 +112,7 @@ void TcpServer::SendMessage(const NetworkMessage & message) } } -void TcpServer::SendHandshake(const Handshake & handshake) -{ - switch (handshake.distribution_mode) - { - case AllAndMe: // this will send the message to EVERYONE including the user that sent it - { - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - client.SendBytes(Handshake::EncodeHandshake(handshake)); - } - break; - } - case ID: // this will send the message to a specific id - { - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - if (handshake.id == client.GetID()) - { - client.SendBytes(Handshake::EncodeHandshake(handshake)); - break; - } - } - break; - } - } -} - -uint16 TcpServer::AllocateID() // this function is only used in the AddToClientsList function +uint16 TcpServer::allocate_id() // this function is only used in the AddToClientsList function { for (uint16 i = 1; i < max_connections; ++i) { @@ -159,9 +133,9 @@ uint16 TcpServer::AllocateID() // this function is only used in the AddToClients return 0; } -void TcpServer::AddToClientsList(TcpClient & client_socket) +void TcpServer::add_to_clients_list(TcpClient & client_socket) { - uint16 id = AllocateID(); + uint16 id = allocate_id(); if (id > 0) { client_socket.SetID(id); @@ -179,14 +153,13 @@ void TcpServer::AddToClientsList(TcpClient & client_socket) void TcpServer::RejectConnection(TcpClient &client) { Handshake handshake(client.GetID(), ConnectionCode::Reject, ID); - client.SendBytes(Handshake::EncodeHandshake(handshake)); - CloseSocket(client); + SendMessage(Handshake::HandshakeToNetworkMessage(handshake)); } void TcpServer::AcceptConnection(uint16 id) { Handshake handshake(id, ConnectionCode::Accept, AllAndMe); - SendHandshake(handshake); + SendMessage(Handshake::HandshakeToNetworkMessage(handshake)); } void TcpServer::CloseSocket(TcpClient & client) diff --git a/src/Utility.cpp b/src/Utility.cpp index 7285157..dad36a4 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -185,10 +185,10 @@ void Utility::ConfigReader::ReadConfig(const std::string & file_name) return; std::fstream file; file.open(file_name); - if (file.is_open) + if (file.is_open()) { longlong file_lenght = file.gcount(); - char *content = new char[file_lenght](); + char *content = new char[static_cast(file_lenght)](); file.read(content, file_lenght); file_content = std::string(content); }