From 2314e862b192525f2093d92921f38f3561723103 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Tue, 16 Aug 2016 00:22:10 +0100 Subject: [PATCH] Added a new type: longlong TcpClient and TcpServer are now structs intead of classes Starting with UdpClient Added 2 new methods to StringConverter: Split and Trim ConfigReader: Changed ReadNodes return type to void, parameter of operator [] is now a std::string Starting with the VoidNetServer class which is an all in one for Tcp and Udp Finally implemented the methods for the ConfigReader --- .../Release/VoidNetVS.tlog/CL.command.1.tlog | Bin 5294 -> 5898 bytes .../Release/VoidNetVS.tlog/CL.read.1.tlog | Bin 221080 -> 252568 bytes .../Release/VoidNetVS.tlog/CL.write.1.tlog | Bin 12482 -> 13286 bytes .../Release/VoidNetVS.tlog/unsuccessfulbuild | 0 VoidNetVS/VoidNetVS/VoidNetVS.vcxproj | 1 + VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters | 3 + include/Config.hpp | 2 - include/Defs.hpp | 2 + include/TcpClient.hpp | 3 +- include/TcpServer.hpp | 3 +- include/UdpClient.hpp | 13 ++++ include/Utility.hpp | 7 +- include/VoidNetClient.hpp | 2 +- include/VoidNetServer.hpp | 8 +++ src/Utility.cpp | 64 ++++++++++++++++-- src/VoidNetClient.cpp | 12 ++-- 16 files changed, 100 insertions(+), 20 deletions(-) delete mode 100644 VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/unsuccessfulbuild create mode 100644 include/UdpClient.hpp diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog index 937ff5b46a4c3545cf904a4e52f289a697857783..8b65bd7c07d2754a7980d2fcb9ae4fcd6c2eb161 100644 GIT binary patch delta 88 zcmZ3d*`>GP6Ypj_0WYS_KX~1kCQlHN+q^)Ck7;rrivn*LgFk~OgA0QngDXSG=I^|m dOll}X49*NbKn1Qq1tAQ2Kwbb4@-lES002w>6R7|I delta 30 mcmeCuTc^3<6Yt~+B5a#&1l*V=|KXR~%)@8Ew7Ek_jR^p-RtfI_ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog index fef2bccf3945825d37df8d917d59d6fc496feedf..56ae1e27a3a84e101101638c6f49c953108f9d37 100644 GIT binary patch delta 195 zcmbQSpLfPq{)QIDElih~w`T!Fk020aGnjrq+?(>(&2)p${qZI@YOK6%O2DBePiU?9#FtoWja~Fyly*39+Mv9^nyYrw&?+3 i%pKdW1u?&3+I}mHISgn~5Od%3eWA=f+YKU_dsqRjHa|B2 delta 46 zcmV+}0MY-LwGWuT4S<9Jv;xutm%iiz61UoH0z`SY(gOngXqRZ_0uGl(rU5XwV&(!i EdD{gOr2qf` diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog index 48ac30ba8d91b7f2a9d06c1d1e719db6b16dd754..813be0b5bc9b166e69713c0bbf5ce829b6478635 100644 GIT binary patch delta 61 zcmX?<_$+4F;3mB@~m66;hMX86J}^5FnF$8y2&wE$RW12NaV~GH{dk B5X=Ao diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/unsuccessfulbuild b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/unsuccessfulbuild deleted file mode 100644 index e69de29..0000000 diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj index 1eef0c8..a7e809d 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj @@ -28,6 +28,7 @@ + diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters index c6ec425..527b7fc 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters @@ -45,6 +45,9 @@ include + + include + diff --git a/include/Config.hpp b/include/Config.hpp index 5a6516d..2dcb94a 100644 --- a/include/Config.hpp +++ b/include/Config.hpp @@ -13,8 +13,6 @@ struct Config static void SetLogToFile(bool value); static bool GetLogToFile(); - - private: static bool using_console; static bool log_to_file; diff --git a/include/Defs.hpp b/include/Defs.hpp index 8028c2a..75d8065 100644 --- a/include/Defs.hpp +++ b/include/Defs.hpp @@ -216,6 +216,8 @@ typedef unsigned long long uint64; #endif // compiler data type defenitions +typedef long long longlong; + const uint16 default_client_port = 60250; const uint16 default_server_port = 61250; diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index ad3af38..382dcff 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -12,9 +12,8 @@ #pragma once #endif -class TcpClient +struct TcpClient { -public: TcpClient(); TcpClient(const SOCKET &socket); TcpClient(const std::string &ip); diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp index bea2620..4ac6ddf 100644 --- a/include/TcpServer.hpp +++ b/include/TcpServer.hpp @@ -11,9 +11,8 @@ #include -class TcpServer +struct TcpServer { -public: TcpServer(); TcpServer(uint16 port = default_server_port); ~TcpServer(); diff --git a/include/UdpClient.hpp b/include/UdpClient.hpp new file mode 100644 index 0000000..e95709b --- /dev/null +++ b/include/UdpClient.hpp @@ -0,0 +1,13 @@ +#ifndef UDP_CLIENT_HPP +#define UDP_CLIENT_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +struct UdpClient +{ + +}; + +#endif diff --git a/include/Utility.hpp b/include/Utility.hpp index 2d56b4a..a257d00 100644 --- a/include/Utility.hpp +++ b/include/Utility.hpp @@ -57,14 +57,17 @@ struct Utility static const std::string &ToString(const std::vector &bytes); 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); + static std::vector Split(const std::string &str, const std::string &delimiter); }; struct ConfigReader { void ReadConfig(const std::string &file_name); - const std::map &ReadNodes(); + void ReadNodes(); - const std::string &operator[](uint16 index); + const std::string &operator[](const std::string &key); private: std::map nodes; diff --git a/include/VoidNetClient.hpp b/include/VoidNetClient.hpp index 5bf0c51..712fd8b 100644 --- a/include/VoidNetClient.hpp +++ b/include/VoidNetClient.hpp @@ -32,7 +32,7 @@ struct VoidNetClientAPI private: static void receive_data(); - static TcpClient client; + static TcpClient tcp_client; static uint16 id; }; diff --git a/include/VoidNetServer.hpp b/include/VoidNetServer.hpp index 870304b..a2dff8f 100644 --- a/include/VoidNetServer.hpp +++ b/include/VoidNetServer.hpp @@ -5,4 +5,12 @@ #pragma once #endif +#include "TcpServer.hpp" + +struct VoidNetServer +{ +private: + TcpServer server; +}; + #endif \ No newline at end of file diff --git a/src/Utility.cpp b/src/Utility.cpp index 3f3b7cb..7285157 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -1,5 +1,9 @@ #include "Utility.hpp" +#include +#include +#include + void Utility::Delete(void *pointer) { if (pointer == nullptr) @@ -16,6 +20,18 @@ void Utility::DeleteArray(void *pointer) pointer = nullptr; } +std::vector Utility::StringConverter::Split(const std::string & str, const std::string & delimiter) +{ + std::vector splited; + if (str.empty() && delimiter.empty()) + return std::vector(); + std::stringstream ss(str); + std::string token; + while (std::getline(ss, token, delimiter[0])) + splited.push_back(token); + return splited; +} + const std::vector &Utility::BitConverter::FromUint8(uint8 number) { return std::vector(); @@ -151,17 +167,55 @@ const std::string & Utility::StringConverter::ToString(const std::vector & return std::string(); } +const std::string & Utility::StringConverter::Trim(std::string & str, char ch) +{ + if (str.empty() && ch == 0) + return std::string(); + for (std::string::iterator it = str.begin(); it != str.end(); ++it) + { + if (*it == ch) + str.erase(it); + } + return str; +} + void Utility::ConfigReader::ReadConfig(const std::string & file_name) { - + if (file_name.empty()) + return; + std::fstream file; + file.open(file_name); + if (file.is_open) + { + longlong file_lenght = file.gcount(); + char *content = new char[file_lenght](); + file.read(content, file_lenght); + file_content = std::string(content); + } } -const std::map& Utility::ConfigReader::ReadNodes() +void Utility::ConfigReader::ReadNodes() { - return std::map(); + if (file_content.empty()) + return; + std::stringstream ss(file_content); + std::string temp; + std::vector nodes_lines; + while (std::getline(ss, temp, '\n')) + { + if (temp.substr(0, 2) != "//") + nodes_lines.emplace_back(temp); + } + + for (std::vector::iterator it = nodes_lines.begin(); it != nodes_lines.end(); ++it) + { + std::string node_str = Utility::StringConverter::Trim(*it, ' '); + std::vector node = Utility::StringConverter::Split(node_str, "="); + nodes.insert(std::pair(node.at(0), node.at(1))); + } } -const std::string & Utility::ConfigReader::operator[](uint16 index) +const std::string & Utility::ConfigReader::operator[](const std::string &key) { - return std::string(); + return nodes.at(key); } diff --git a/src/VoidNetClient.cpp b/src/VoidNetClient.cpp index 776074c..dde69e3 100644 --- a/src/VoidNetClient.cpp +++ b/src/VoidNetClient.cpp @@ -5,9 +5,9 @@ bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port) { - client.SetIP(ip); - client.SetPort(port); - return client.Connect(); + tcp_client.SetIP(ip); + tcp_client.SetPort(port); + return tcp_client.Connect(); } void VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data) @@ -46,7 +46,7 @@ void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id message.distribution_mode = distribution_mode; message.sender = id; message.destination_id = destination_id; - client.SendMessage(message); + tcp_client.SendMessage(message); } } @@ -57,10 +57,10 @@ void VoidNetClientAPI::Receive() void VoidNetClientAPI::receive_data() { - client.ReceiveMessages(); + tcp_client.ReceiveMessages(); } void VoidNetClientAPI::Disconnect() { - client.Shutdown(); + tcp_client.Shutdown(); }