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:
4
TODO
4
TODO
@ -5,3 +5,7 @@ handle ReceiveDataArray errors for windows
|
|||||||
|
|
||||||
revamped BitConverter class
|
revamped BitConverter class
|
||||||
new StringConverter class
|
new StringConverter class
|
||||||
|
|
||||||
|
implement close() in TcpClientWindows
|
||||||
|
|
||||||
|
implement ConfigReader methods
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -10,8 +10,14 @@ struct Config
|
|||||||
static void SetUsingConsole(bool value);
|
static void SetUsingConsole(bool value);
|
||||||
static bool GetUsingConsole();
|
static bool GetUsingConsole();
|
||||||
|
|
||||||
|
static void SetLogToFile(bool value);
|
||||||
|
static bool GetLogToFile();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool using_console;
|
static bool using_console;
|
||||||
|
static bool log_to_file;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -21,6 +21,8 @@ public:
|
|||||||
TcpClient(const std::string &ip, uint16 port = default_client_port);
|
TcpClient(const std::string &ip, uint16 port = default_client_port);
|
||||||
~TcpClient();
|
~TcpClient();
|
||||||
|
|
||||||
|
void Shutdown();
|
||||||
|
|
||||||
const std::string &GetIP();
|
const std::string &GetIP();
|
||||||
void SetIP(const std::string &ip);
|
void SetIP(const std::string &ip);
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,8 @@ public:
|
|||||||
TcpServer(uint16 port = default_server_port);
|
TcpServer(uint16 port = default_server_port);
|
||||||
~TcpServer();
|
~TcpServer();
|
||||||
|
|
||||||
|
void Shutdown();
|
||||||
|
|
||||||
uint16 AllocateID();
|
uint16 AllocateID();
|
||||||
void AddToClientsList(const TcpClient &client);
|
void AddToClientsList(const TcpClient &client);
|
||||||
bool StartServer(bool accept_connections);
|
bool StartServer(bool accept_connections);
|
||||||
@ -45,7 +47,7 @@ private:
|
|||||||
std::vector<TcpClient> clients;
|
std::vector<TcpClient> clients;
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
SOCKET socket = INVALID_SOCKET;
|
SOCKET server_tcp_socket = INVALID_SOCKET;
|
||||||
struct addrinfo *result = nullptr;
|
struct addrinfo *result = nullptr;
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -8,8 +8,7 @@
|
|||||||
#include "Defs.hpp"
|
#include "Defs.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <streambuf>
|
#include <map>
|
||||||
#include <istream>
|
|
||||||
|
|
||||||
struct Utility
|
struct Utility
|
||||||
{
|
{
|
||||||
@ -57,6 +56,18 @@ struct Utility
|
|||||||
|
|
||||||
static const std::string &ToString(byte *bytes, uint16 start_index = 0, uint16 lenght = 0);
|
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
|
#endif
|
||||||
@ -9,3 +9,13 @@ bool Config::GetUsingConsole()
|
|||||||
{
|
{
|
||||||
return using_console;
|
return using_console;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Config::SetLogToFile(bool value)
|
||||||
|
{
|
||||||
|
log_to_file = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Config::GetLogToFile()
|
||||||
|
{
|
||||||
|
return log_to_file;
|
||||||
|
}
|
||||||
|
|||||||
@ -67,6 +67,19 @@ TcpClient::~TcpClient()
|
|||||||
Utility::Delete(result);
|
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()
|
const std::string &TcpClient::GetIP()
|
||||||
{
|
{
|
||||||
return ip;
|
return ip;
|
||||||
|
|||||||
@ -25,9 +25,9 @@ bool TcpServer::initialize(uint16 port)
|
|||||||
return false;
|
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())
|
if (Config::GetUsingConsole())
|
||||||
std::cerr << WSAGetLastError() << std::endl; // display more info
|
std::cerr << WSAGetLastError() << std::endl; // display more info
|
||||||
@ -36,13 +36,13 @@ bool TcpServer::initialize(uint16 port)
|
|||||||
return false;
|
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 (code == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
if (Config::GetUsingConsole())
|
if (Config::GetUsingConsole())
|
||||||
std::cerr << WSAGetLastError() << std::endl; // display more info
|
std::cerr << WSAGetLastError() << std::endl; // display more info
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
closesocket(socket);
|
closesocket(server_tcp_socket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -68,13 +68,19 @@ TcpServer::~TcpServer()
|
|||||||
Utility::Delete(result);
|
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)
|
bool TcpServer::StartServer(bool accept_connections)
|
||||||
{
|
{
|
||||||
if (listen(socket, SOMAXCONN) == SOCKET_ERROR)
|
if (listen(server_tcp_socket, SOMAXCONN) == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
if (Config::GetUsingConsole())
|
if (Config::GetUsingConsole())
|
||||||
std::cerr << WSAGetLastError() << std::endl;
|
std::cerr << WSAGetLastError() << std::endl;
|
||||||
closesocket(socket);
|
closesocket(server_tcp_socket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -96,12 +102,12 @@ void TcpServer::accept_connections(TcpServer *server)
|
|||||||
std::async(std::launch::async, &process_client_received_data, server);
|
std::async(std::launch::async, &process_client_received_data, server);
|
||||||
while (server->running)
|
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 (client_socket == INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
if (Config::GetUsingConsole())
|
if (Config::GetUsingConsole())
|
||||||
std::cerr << WSAGetLastError() << std::endl;
|
std::cerr << WSAGetLastError() << std::endl;
|
||||||
closesocket(server->socket);
|
closesocket(server->server_tcp_socket);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
server->running = false;
|
server->running = false;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user