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 5e21b61..ae829d4 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog index 0111a24..d97acd3 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog index bf30e6d..c48c6ab 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog index b1b8729..2d168be 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog index 7743a89..37a6441 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog index f50ac56..8151521 100644 Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj index 3e9b936..7cacbc9 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj @@ -38,18 +38,18 @@ - 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();