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
This commit is contained in:
xX-TheDoctor-Xx
2016-08-16 00:22:10 +01:00
parent e5ca02be0d
commit 2314e862b1
16 changed files with 100 additions and 20 deletions

View File

@ -28,6 +28,7 @@
<ClInclude Include="..\..\include\Serializer.hpp" />
<ClInclude Include="..\..\include\TcpClient.hpp" />
<ClInclude Include="..\..\include\TcpServer.hpp" />
<ClInclude Include="..\..\include\UdpClient.hpp" />
<ClInclude Include="..\..\include\Utility.hpp" />
<ClInclude Include="..\..\include\VoidNetClient.hpp" />
<ClInclude Include="..\..\include\VoidNetServer.hpp" />

View File

@ -45,6 +45,9 @@
<ClInclude Include="..\..\include\Handshake.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\UdpClient.hpp">
<Filter>include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Init.cpp">

View File

@ -13,8 +13,6 @@ struct Config
static void SetLogToFile(bool value);
static bool GetLogToFile();
private:
static bool using_console;
static bool log_to_file;

View File

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

View File

@ -12,9 +12,8 @@
#pragma once
#endif
class TcpClient
struct TcpClient
{
public:
TcpClient();
TcpClient(const SOCKET &socket);
TcpClient(const std::string &ip);

View File

@ -11,9 +11,8 @@
#include <functional>
class TcpServer
struct TcpServer
{
public:
TcpServer();
TcpServer(uint16 port = default_server_port);
~TcpServer();

13
include/UdpClient.hpp Normal file
View File

@ -0,0 +1,13 @@
#ifndef UDP_CLIENT_HPP
#define UDP_CLIENT_HPP
#ifdef _MSC_VER
#pragma once
#endif
struct UdpClient
{
};
#endif

View File

@ -57,14 +57,17 @@ struct Utility
static const std::string &ToString(const std::vector<byte> &bytes);
static const std::string &ToString(const std::vector<byte> &bytes, uint16 start_index = 0, uint16 lenght = 0);
static const std::string &Trim(std::string &str, char ch);
static std::vector<std::string> Split(const std::string &str, const std::string &delimiter);
};
struct ConfigReader
{
void ReadConfig(const std::string &file_name);
const std::map<std::string, std::string> &ReadNodes();
void ReadNodes();
const std::string &operator[](uint16 index);
const std::string &operator[](const std::string &key);
private:
std::map<std::string, std::string> nodes;

View File

@ -32,7 +32,7 @@ struct VoidNetClientAPI
private:
static void receive_data();
static TcpClient client;
static TcpClient tcp_client;
static uint16 id;
};

View File

@ -5,4 +5,12 @@
#pragma once
#endif
#include "TcpServer.hpp"
struct VoidNetServer
{
private:
TcpServer server;
};
#endif

View File

@ -1,5 +1,9 @@
#include "Utility.hpp"
#include <fstream>
#include <sstream>
#include <algorithm>
void Utility::Delete(void *pointer)
{
if (pointer == nullptr)
@ -16,6 +20,18 @@ void Utility::DeleteArray(void *pointer)
pointer = nullptr;
}
std::vector<std::string> Utility::StringConverter::Split(const std::string & str, const std::string & delimiter)
{
std::vector<std::string> splited;
if (str.empty() && delimiter.empty())
return std::vector<std::string>();
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter[0]))
splited.push_back(token);
return splited;
}
const std::vector<byte> &Utility::BitConverter::FromUint8(uint8 number)
{
return std::vector<byte>();
@ -151,17 +167,55 @@ const std::string & Utility::StringConverter::ToString(const std::vector<byte> &
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<std::string, std::string>& Utility::ConfigReader::ReadNodes()
void Utility::ConfigReader::ReadNodes()
{
return std::map<std::string, std::string>();
if (file_content.empty())
return;
std::stringstream ss(file_content);
std::string temp;
std::vector<std::string> nodes_lines;
while (std::getline(ss, temp, '\n'))
{
if (temp.substr(0, 2) != "//")
nodes_lines.emplace_back(temp);
}
for (std::vector<std::string>::iterator it = nodes_lines.begin(); it != nodes_lines.end(); ++it)
{
std::string node_str = Utility::StringConverter::Trim(*it, ' ');
std::vector<std::string> node = Utility::StringConverter::Split(node_str, "=");
nodes.insert(std::pair<std::string, std::string>(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);
}

View File

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