From c83b4101d0ea4d39772e60b73c1a161252726ec5 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Sat, 23 Jul 2016 12:35:20 +0100 Subject: [PATCH] New configuration variable, log_to_file Shutdown function added to TcpClient Shutdown function added to TcpServer Renamed socket to server_tcp_socket in TcpServer Implemented ConfigReader class --- TODO | 6 ++++- .../Release/VoidNetVS.tlog/CL.command.1.tlog | Bin 5934 -> 5934 bytes .../Release/VoidNetVS.tlog/CL.read.1.tlog | Bin 219862 -> 224412 bytes .../Release/VoidNetVS.tlog/CL.write.1.tlog | Bin 6394 -> 7550 bytes include/Config.hpp | 6 +++++ include/TcpClient.hpp | 2 ++ include/TcpServer.hpp | 4 +++- include/Utility.hpp | 15 ++++++++++-- src/Config.cpp | 10 ++++++++ src/TcpClientWindows.cpp | 13 +++++++++++ src/TcpServerWindows.cpp | 22 +++++++++++------- 11 files changed, 66 insertions(+), 12 deletions(-) diff --git a/TODO b/TODO index 376cf97..d943f55 100644 --- a/TODO +++ b/TODO @@ -4,4 +4,8 @@ handle SendNetworkMessage errors for windows handle ReceiveDataArray errors for windows revamped BitConverter class -new StringConverter class \ No newline at end of file +new StringConverter class + +implement close() in TcpClientWindows + +implement ConfigReader methods \ 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 81ec6bd45c7f66d31db0b810c5e01995084e15e4..dbe76bcfe20a00c99d7a767869d6efe746fd60dc 100644 GIT binary patch delta 70 zcmZ3dw@z<@(&RK2kYl0F=++UdFU} UolpbI6+N8E@)v{%0tdwX%WEk$f^UgW>=ijd}RYu4?g7j5jlRzP9NQr<&zhJcgLnrc!_cVkgOpqVXzO#c+$T5^lKl2qombvGZ_8Y&*T^r;*h=~=1eVf&4FGJNHzZC*4(?8E}Myiy;B{u!6k z)~bE?!7gM68_arWj$(M6)Q@bMQj{cxBIKtivN-Axg^|ya4^g2dL=7jsXrSbL4c&IZf#_fo;wBmL@tWgMR? CuE0qE delta 152 zcmV;J0B8T4*$vjU4Ui(2a6AEau_CPAx8^(naAuRP-XOQ8r2)WZmnzu-CYQVp0 zmk{9s1e1_)inB~?>H@c*V*~sMm(1`2AeRIX0uZ;<4+02gldj$%w=8%9E^w2scqW&S G4+0cRxI`oX diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog index 83f3455b21caaf5506920b56d4894c0512ee03cb..e22ff995f3c1ee7689ecd4276f237ae001c529a5 100644 GIT binary patch delta 156 zcmexm_|Iy?9Jb94?8kT~=ZLMDJcmtWvKFJl9Ja}Pq9T)T@fA#dC!(-<4%;!_$qE8|lLLT)bs`ghY!#T~Bn}y{&;kJ| PFnx|u3??;MKyVTO)OsE~ diff --git a/include/Config.hpp b/include/Config.hpp index 5e6c3a7..5a6516d 100644 --- a/include/Config.hpp +++ b/include/Config.hpp @@ -10,8 +10,14 @@ struct Config static void SetUsingConsole(bool value); static bool GetUsingConsole(); + static void SetLogToFile(bool value); + static bool GetLogToFile(); + + + private: static bool using_console; + static bool log_to_file; }; #endif \ No newline at end of file diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index cdd6948..53af62d 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -21,6 +21,8 @@ public: TcpClient(const std::string &ip, uint16 port = default_client_port); ~TcpClient(); + void Shutdown(); + const std::string &GetIP(); void SetIP(const std::string &ip); diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp index caf6eb1..1a63565 100644 --- a/include/TcpServer.hpp +++ b/include/TcpServer.hpp @@ -21,6 +21,8 @@ public: TcpServer(uint16 port = default_server_port); ~TcpServer(); + void Shutdown(); + uint16 AllocateID(); void AddToClientsList(const TcpClient &client); bool StartServer(bool accept_connections); @@ -45,7 +47,7 @@ private: std::vector clients; #ifdef _MSC_VER - SOCKET socket = INVALID_SOCKET; + SOCKET server_tcp_socket = INVALID_SOCKET; struct addrinfo *result = nullptr; struct addrinfo hints; #endif diff --git a/include/Utility.hpp b/include/Utility.hpp index 2b33540..2f15b80 100644 --- a/include/Utility.hpp +++ b/include/Utility.hpp @@ -8,8 +8,7 @@ #include "Defs.hpp" #include -#include -#include +#include struct Utility { @@ -57,6 +56,18 @@ struct Utility static const std::string &ToString(byte *bytes, uint16 start_index = 0, uint16 lenght = 0); }; + + struct ConfigReader + { + const void ReadConfig(const std::string &file_name); + const std::map &ReadNodes(); + + const std::string &operator[](uint16 index); + + private: + std::map nodes; + std::string file_content; + }; }; #endif \ No newline at end of file diff --git a/src/Config.cpp b/src/Config.cpp index 2d4dd36..d502120 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -9,3 +9,13 @@ bool Config::GetUsingConsole() { return using_console; } + +void Config::SetLogToFile(bool value) +{ + log_to_file = value; +} + +bool Config::GetLogToFile() +{ + return log_to_file; +} diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index f4e84bc..3c9d7bd 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -67,6 +67,19 @@ TcpClient::~TcpClient() Utility::Delete(result); } +void TcpClient::Shutdown() +{ + uint16 code = shutdown(tcp_socket, SD_SEND); + if (code == SOCKET_ERROR) + { + if (Config::GetUsingConsole()) + std::cerr << WSAGetLastError() << std::endl; // display more info + } + + closesocket(tcp_socket); + WSACleanup(); +} + const std::string &TcpClient::GetIP() { return ip; diff --git a/src/TcpServerWindows.cpp b/src/TcpServerWindows.cpp index 8aea990..6fcaee6 100644 --- a/src/TcpServerWindows.cpp +++ b/src/TcpServerWindows.cpp @@ -25,9 +25,9 @@ bool TcpServer::initialize(uint16 port) return false; } - socket = ::socket(result->ai_family, result->ai_socktype, result->ai_protocol); + server_tcp_socket = ::socket(result->ai_family, result->ai_socktype, result->ai_protocol); - if (socket == INVALID_SOCKET) + if (server_tcp_socket == INVALID_SOCKET) { if (Config::GetUsingConsole()) std::cerr << WSAGetLastError() << std::endl; // display more info @@ -36,13 +36,13 @@ bool TcpServer::initialize(uint16 port) return false; } - code = bind(socket, result->ai_addr, result->ai_addrlen); + code = bind(server_tcp_socket, result->ai_addr, result->ai_addrlen); if (code == SOCKET_ERROR) { if (Config::GetUsingConsole()) std::cerr << WSAGetLastError() << std::endl; // display more info freeaddrinfo(result); - closesocket(socket); + closesocket(server_tcp_socket); WSACleanup(); return false; } @@ -68,13 +68,19 @@ TcpServer::~TcpServer() Utility::Delete(result); } +void TcpServer::Shutdown() +{ + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + (*it).Shutdown(); +} + bool TcpServer::StartServer(bool accept_connections) { - if (listen(socket, SOMAXCONN) == SOCKET_ERROR) + if (listen(server_tcp_socket, SOMAXCONN) == SOCKET_ERROR) { if (Config::GetUsingConsole()) std::cerr << WSAGetLastError() << std::endl; - closesocket(socket); + closesocket(server_tcp_socket); WSACleanup(); return false; } @@ -96,12 +102,12 @@ void TcpServer::accept_connections(TcpServer *server) std::async(std::launch::async, &process_client_received_data, server); while (server->running) { - SOCKET client_socket = accept(server->socket, 0, 0); + SOCKET client_socket = accept(server->server_tcp_socket, 0, 0); if (client_socket == INVALID_SOCKET) { if (Config::GetUsingConsole()) std::cerr << WSAGetLastError() << std::endl; - closesocket(server->socket); + closesocket(server->server_tcp_socket); WSACleanup(); server->running = false; break;