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 81ec6bd..dbe76bc 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 9611acb..1cd5e00 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 83f3455..e22ff99 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/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;