From 996b88c0f64b9a108fe0b9a29518a9fb1b855a06 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Thu, 18 Aug 2016 22:23:43 +0100 Subject: [PATCH] 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 --- TODO | 8 ++--- .../Release/VoidNetVS.tlog/CL.command.1.tlog | Bin 5698 -> 7466 bytes .../Release/VoidNetVS.tlog/CL.read.1.tlog | Bin 252370 -> 337586 bytes .../Release/VoidNetVS.tlog/CL.write.1.tlog | Bin 14862 -> 16432 bytes .../VoidNetVS.tlog/Lib-link.write.1.tlog | Bin 1162 -> 1512 bytes .../Release/VoidNetVS.tlog/Lib.read.1.tlog | Bin 2648 -> 3506 bytes .../Release/VoidNetVS.tlog/lib.command.1.tlog | Bin 1634 -> 2142 bytes VoidNetVS/VoidNetVS/VoidNetVS.vcxproj | 6 ++-- include/Defs.hpp | 2 ++ include/Utility.hpp | 18 +++++----- src/Handshake.cpp | 13 +++---- src/NetworkMessage.cpp | 33 ++++++++++-------- src/TcpClient.cpp | 2 +- src/TcpClientWindows.cpp | 2 +- src/TcpServerWindows.cpp | 2 +- src/Utility.cpp | 21 ++++++----- 16 files changed, 59 insertions(+), 48 deletions(-) diff --git a/TODO b/TODO index 60ad650..d2032d8 100644 --- a/TODO +++ b/TODO @@ -1,13 +1,13 @@ initialization code for other operating systems - currently VoidNet only supports windows -initialization code for tcp client and tcp server for other operating systems +initialization code for tcp client and tcp server for other operating systems - currently windows only handle SendNetworkMessage errors for windows handle ReceiveDataArray errors for windows revamped BitConverter class new StringConverter class -implement ConfigReader methods - maybe i should implement error codes -plugin system - idk how im going to implement it yet \ No newline at end of file +plugin system - idk how im going to implement it yet + +maybe i should use virtual methods for the server and client, maybe... \ 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 5e21b6177274b4d19d0c23ea62cac10a601d046e..ae829d4564ae654ac96988c90de5b57002c0ed95 100644 GIT binary patch delta 200 zcmX@4v&w3NAJgVGmK>JNcHC>&CToaEZI0uuH}HOA F1OUYA5;p(< diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog index 0111a24e984282363f2240ce47470ec2641df146..d97acd36500f67f9b6212e105524fb6f96ae66a3 100644 GIT binary patch delta 203 zcmcb#nSaw+kqI+4U(5V+zJ1$b#_ih{GffJZelCYuYI@!mCbj7X6B+rY8?0yI*)Fq$ zvB!9OLLTD>lj#E4Oby%T-C#ChoW3rHSz)`!7pAs~?UyVWk7Q04s0M1gSIHzdvBGZq ziyYl+Xc^P`A*F0)}{(CLsbEx14SR VpaPeW$O8_S4%`6@w}2r6Flu0AFpK~I diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog index bf30e6d91fddbb414caec0f20d6a2be942bd08e0..c48c6aba3d86b419ae645d2323412697f01677bd 100644 GIT binary patch delta 351 zcmeAx+0ekaVUhCW53E*`KWP1%d{0yX$Yz`TKsRsm2d!%yo8){LH-BI~!Z`VW;FZY- z)Lu+}z%4QPf`Q-U3oIg&EEOgz$O%l=F%N=q1LTw@^O??={7+P7bA#D4#>onn89=R4 zV1pCbnkG9)HcU?75u1FAao=PG4h@Kf{3?@Ana-NLPDctXo+WGtG?#-noWYa9kHLk( zpCOzfc=ASFYcvr-F?5lOjK?Q`;1WVp_fR_*O=KfeG|&aZWMRR= NgJu#!7Ts2$3;=1bZ?gaZ delta 158 zcmdncz}Q!^VUhA?9rbNYlMe{yO+KI|vH5|P8q?$gYlX=lSgj@}usxXkflF@kJyC_p z5A1{{NpMUq(2s%&=K!VHHXl%HVV``9F=%p@u-#+@If2c)j2##!f8bY{{J?V7 - true + false - true + false - true + false diff --git a/include/Defs.hpp b/include/Defs.hpp index 8cdec9c..2d401a6 100644 --- a/include/Defs.hpp +++ b/include/Defs.hpp @@ -243,4 +243,6 @@ enum InternalTags DISCONNECT = 255, }; +#define IS_HANDSHAKE(name) name.subject == 1 && (name.tag == DISCONNECT || name.tag == CONNECT || name.tag == ConnectionCode::Accept || name.tag == ConnectionCode::Close || name.tag == ConnectionCode::Reject) + #endif // DEFS_HPP \ No newline at end of file diff --git a/include/Utility.hpp b/include/Utility.hpp index 76730f9..b4978a3 100644 --- a/include/Utility.hpp +++ b/include/Utility.hpp @@ -18,28 +18,28 @@ struct Utility struct BitConverter { - static const std::vector &FromUint8(uint8 number); + static const std::vector &ToBytes(uint8 number); static uint8 ToUint8(const std::vector &bytes, uint16 start_index = 0); - static const std::vector &FromUint16(uint16 number); + static const std::vector &ToBytes(uint16 number); static uint16 ToUint16(const std::vector &bytes, uint16 start_index = 0); - static const std::vector &FromUint32(uint32 number); + static const std::vector &ToBytes(uint32 number); static uint32 ToUint32(const std::vector &bytes, uint16 start_index = 0); - static const std::vector &FromUint64(uint64 number); + static const std::vector &ToBytes(uint64 number); static uint64 ToUint64(const std::vector &bytes, uint16 start_index = 0); - static const std::vector &FromInt8(int8 number); + static const std::vector &ToBytes(int8 number); static int8 ToInt8(const std::vector &bytes, uint16 start_index = 0); - static const std::vector &FromInt16(int16 number); + static const std::vector &ToBytes(int16 number); static int16 ToInt16(const std::vector &bytes, uint16 start_index = 0); - static const std::vector &FromInt32(int32 number); + static const std::vector &ToBytes(int32 number); static int32 ToInt32(const std::vector &bytes, uint16 start_index = 0); - static const std::vector &FromInt64(int64 number); + static const std::vector &ToBytes(int64 number); static int64 ToInt64(const std::vector &bytes, uint16 start_index = 0); }; @@ -56,7 +56,7 @@ 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::vector &ToBytes(const std::string &str); static const std::string &ToString(const std::vector &bytes, uint16 start_index = 0, uint16 lenght = 0); diff --git a/src/Handshake.cpp b/src/Handshake.cpp index 1453c4c..9c3698d 100644 --- a/src/Handshake.cpp +++ b/src/Handshake.cpp @@ -21,12 +21,13 @@ const std::vector& Handshake::EncodeHandshake(const Handshake & handshake) { std::vector handshake_bytes; - std::vector id = Utility::BitConverter::FromUint16(handshake.id); - std::vector 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 id = Utility::BitConverter::ToBytes(handshake.id); + std::vector con_mode = Utility::BitConverter::ToBytes(handshake.con_code); + std::vector type = Utility::BitConverter::ToBytes(static_cast(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; } diff --git a/src/NetworkMessage.cpp b/src/NetworkMessage.cpp index 82dde05..123d3b7 100644 --- a/src/NetworkMessage.cpp +++ b/src/NetworkMessage.cpp @@ -29,33 +29,36 @@ const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message { NetworkBuffer net_buffer; - if (message.subject != 1) + if (!IS_HANDSHAKE(message)) { - 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 sender = Utility::BitConverter::ToBytes(message.sender); + std::vector distribution_mode = Utility::BitConverter::ToBytes(message.distribution_mode); + std::vector destination_id = Utility::BitConverter::ToBytes(message.destination_id); + std::vector tag = Utility::BitConverter::ToBytes(message.tag); + std::vector subject = Utility::BitConverter::ToBytes(message.subject); std::vector data; if (message.data != nullptr) data = Serializer::to_bytes(message.data); + std::vector 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 handshake_bytes = Handshake::EncodeHandshake(Handshake::NetworkMessageToHandshake(message)); - net_buffer.header = Utility::BitConverter::FromInt32(handshake_bytes.size()); + std::vector type = Utility::BitConverter::ToBytes(static_cast(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; diff --git a/src/TcpClient.cpp b/src/TcpClient.cpp index c4aa5bc..0abd2d2 100644 --- a/src/TcpClient.cpp +++ b/src/TcpClient.cpp @@ -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); diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index 57e35e5..95a28ac 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -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) { diff --git a/src/TcpServerWindows.cpp b/src/TcpServerWindows.cpp index 26d87f4..24fbd70 100644 --- a/src/TcpServerWindows.cpp +++ b/src/TcpServerWindows.cpp @@ -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); } diff --git a/src/Utility.cpp b/src/Utility.cpp index dad36a4..dd67a24 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -32,7 +32,7 @@ std::vector Utility::StringConverter::Split(const std::string & str return splited; } -const std::vector &Utility::BitConverter::FromUint8(uint8 number) +const std::vector &Utility::BitConverter::ToBytes(uint8 number) { return std::vector(); } @@ -42,7 +42,7 @@ uint8 Utility::BitConverter::ToUint8(const std::vector &bytes, uint16 star return uint8(); } -const std::vector &Utility::BitConverter::FromUint16(uint16 number) +const std::vector &Utility::BitConverter::ToBytes(uint16 number) { return std::vector(); } @@ -52,7 +52,7 @@ uint16 Utility::BitConverter::ToUint16(const std::vector &bytes, uint16 st return uint16(); } -const std::vector & Utility::BitConverter::FromUint32(uint32 number) +const std::vector & Utility::BitConverter::ToBytes(uint32 number) { return std::vector(); } @@ -62,7 +62,7 @@ uint32 Utility::BitConverter::ToUint32(const std::vector & bytes, uint16 s return uint32(); } -const std::vector & Utility::BitConverter::FromUint64(uint64 number) +const std::vector & Utility::BitConverter::ToBytes(uint64 number) { return std::vector(); } @@ -72,7 +72,7 @@ uint64 Utility::BitConverter::ToUint64(const std::vector & bytes, uint16 s return uint64(); } -const std::vector & Utility::BitConverter::FromInt8(int8 number) +const std::vector & Utility::BitConverter::ToBytes(int8 number) { return std::vector(); } @@ -82,7 +82,7 @@ int8 Utility::BitConverter::ToInt8(const std::vector & bytes, uint16 start return int8(); } -const std::vector & Utility::BitConverter::FromInt16(int16 number) +const std::vector & Utility::BitConverter::ToBytes(int16 number) { return std::vector(); } @@ -92,7 +92,7 @@ int16 Utility::BitConverter::ToInt16(const std::vector & bytes, uint16 sta return int16(); } -const std::vector & Utility::BitConverter::FromInt32(int32 number) +const std::vector & Utility::BitConverter::ToBytes(int32 number) { return std::vector(); } @@ -102,7 +102,7 @@ int32 Utility::BitConverter::ToInt32(const std::vector & bytes, uint16 sta return int32(); } -const std::vector & Utility::BitConverter::FromInt64(int64 number) +const std::vector & Utility::BitConverter::ToBytes(int64 number) { return std::vector(); } @@ -162,6 +162,11 @@ const std::string & Utility::StringConverter::ToString(const std::vector& return std::string(); } +const std::vector& Utility::StringConverter::ToBytes(const std::string & str) +{ + return std::vector(); +} + const std::string & Utility::StringConverter::ToString(const std::vector & bytes, uint16 start_index, uint16 lenght) { return std::string();