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
This commit is contained in:
xX-TheDoctor-Xx
2016-07-23 12:35:20 +01:00
parent 6fa7872a6d
commit c83b4101d0
11 changed files with 66 additions and 12 deletions

4
TODO
View File

@ -5,3 +5,7 @@ handle ReceiveDataArray errors for windows
revamped BitConverter class
new StringConverter class
implement close() in TcpClientWindows
implement ConfigReader methods

View File

@ -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

View File

@ -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);

View File

@ -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<TcpClient> clients;
#ifdef _MSC_VER
SOCKET socket = INVALID_SOCKET;
SOCKET server_tcp_socket = INVALID_SOCKET;
struct addrinfo *result = nullptr;
struct addrinfo hints;
#endif

View File

@ -8,8 +8,7 @@
#include "Defs.hpp"
#include <string>
#include <streambuf>
#include <istream>
#include <map>
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<std::string, std::string> &ReadNodes();
const std::string &operator[](uint16 index);
private:
std::map<std::string, std::string> nodes;
std::string file_content;
};
};
#endif

View File

@ -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;
}

View File

@ -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;

View File

@ -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<TcpClient>::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;