TcpClient: replaced C style function parameters with std::function
Added initialization to config Added receiving for UdpClient Removed InitWindows.cpp Laying foundation for PluginManager
This commit is contained in:
@ -25,6 +25,7 @@
|
||||
<ClInclude Include="..\..\include\Init.hpp" />
|
||||
<ClInclude Include="..\..\include\NetworkBuffer.hpp" />
|
||||
<ClInclude Include="..\..\include\NetworkMessage.hpp" />
|
||||
<ClInclude Include="..\..\include\PluginManager.hpp" />
|
||||
<ClInclude Include="..\..\include\Serializer.hpp" />
|
||||
<ClInclude Include="..\..\include\TcpClient.hpp" />
|
||||
<ClInclude Include="..\..\include\TcpServer.hpp" />
|
||||
|
||||
@ -51,6 +51,9 @@
|
||||
<ClInclude Include="..\..\include\UdpServer.hpp">
|
||||
<Filter>include</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\PluginManager.hpp">
|
||||
<Filter>include</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\src\Init.cpp">
|
||||
|
||||
@ -5,14 +5,20 @@
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Utility.hpp"
|
||||
|
||||
struct Config
|
||||
{
|
||||
static void Initialize();
|
||||
|
||||
static void SetUsingConsole(bool value);
|
||||
static bool GetUsingConsole();
|
||||
|
||||
static void SetLogToFile(bool value);
|
||||
static bool GetLogToFile();
|
||||
|
||||
static Utility::ConfigReader Configuration;
|
||||
|
||||
private:
|
||||
static bool using_console;
|
||||
static bool log_to_file;
|
||||
|
||||
13
include/PluginManager.hpp
Normal file
13
include/PluginManager.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
#ifndef PLUGIN_MANAGER_HPP
|
||||
#define PLUGIN_MANAGER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
struct PluginManager
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -45,9 +45,9 @@ struct TcpClient
|
||||
bool SendBytes(const std::vector<byte> &bytes);
|
||||
bool SendBytes(byte *bytes, uint32 lenght);
|
||||
|
||||
void SetOnDisconnectCallback(void (*func)(uint16));
|
||||
void SetOnConnectCallback(void (*func)(uint16));
|
||||
void SetOnMessageCallback(void (*func)(uint16, byte, byte, void*));
|
||||
void SetOnDisconnectCallback(std::function<void(uint16)> func);
|
||||
void SetOnConnectCallback(std::function<void(uint16)> func);
|
||||
void SetOnMessageCallback(std::function<void(uint16, byte, byte, void*)> func);
|
||||
|
||||
static const TcpClient &DefaultTcpClient();
|
||||
|
||||
|
||||
@ -31,11 +31,17 @@ struct UdpClient
|
||||
uint16 GetID();
|
||||
void SetID(uint16 id);
|
||||
|
||||
void ReceiveMessages();
|
||||
|
||||
const NetworkMessage &ReceiveMessage();
|
||||
|
||||
std::future<bool> SendMessage(const NetworkMessage &message);
|
||||
bool SendBytes(const std::vector<byte> &bytes);
|
||||
bool SendBytes(byte *bytes, uint32 lenght);
|
||||
|
||||
private:
|
||||
const NetworkBuffer &receive_data_array();
|
||||
static void receive_data(UdpClient *client);
|
||||
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
||||
|
||||
static bool send_network_message(const NetworkMessage &message, UdpClient *client);
|
||||
|
||||
@ -1,5 +1,13 @@
|
||||
#include "Config.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
void Config::Initialize()
|
||||
{
|
||||
Configuration.ReadConfig("config.dat");
|
||||
Configuration.ReadNodes();
|
||||
}
|
||||
|
||||
inline void Config::SetUsingConsole(bool value)
|
||||
{
|
||||
using_console = value;
|
||||
|
||||
22
src/Init.cpp
22
src/Init.cpp
@ -1,3 +1,23 @@
|
||||
#include "Init.hpp"
|
||||
#include "Config.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include "InitWindows.cpp"
|
||||
bool Initialization::Initialize()
|
||||
{
|
||||
uint16 code = WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
||||
if (code != 0)
|
||||
{
|
||||
if (Config::GetUsingConsole())
|
||||
std::cerr << code << std::endl; // display some more information too
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const WSADATA &Initialization::GetData()
|
||||
{
|
||||
return wsa_data;
|
||||
}
|
||||
#endif
|
||||
@ -1,21 +0,0 @@
|
||||
#include "Init.hpp"
|
||||
#include "Config.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
bool Initialization::Initialize()
|
||||
{
|
||||
uint16 code = WSAStartup(MAKEWORD(2, 2), &wsa_data);
|
||||
if (code != 0)
|
||||
{
|
||||
if (Config::GetUsingConsole())
|
||||
std::cerr << code << std::endl; // display some more information too
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const WSADATA &Initialization::GetData()
|
||||
{
|
||||
return wsa_data;
|
||||
}
|
||||
@ -89,17 +89,17 @@ std::future<bool> TcpClient::SendMessage(const NetworkMessage &message)
|
||||
return std::async(std::launch::async, &send_network_message, message, this);
|
||||
}
|
||||
|
||||
void TcpClient::SetOnDisconnectCallback(void(*func)(uint16))
|
||||
void TcpClient::SetOnDisconnectCallback(std::function<void(uint16)> func)
|
||||
{
|
||||
OnDisconnect = func;
|
||||
}
|
||||
|
||||
void TcpClient::SetOnConnectCallback(void(*func)(uint16))
|
||||
void TcpClient::SetOnConnectCallback(std::function<void(uint16)> func)
|
||||
{
|
||||
OnConnect = func;
|
||||
}
|
||||
|
||||
void TcpClient::SetOnMessageCallback(void(*func)(uint16, byte, byte, void*))
|
||||
void TcpClient::SetOnMessageCallback(std::function<void(uint16, byte, byte, void*)> func)
|
||||
{
|
||||
OnMessage = func;
|
||||
}
|
||||
|
||||
@ -36,6 +36,15 @@ void UdpClient::SetID(uint16 id)
|
||||
this->id = id;
|
||||
}
|
||||
|
||||
void UdpClient::ReceiveMessages()
|
||||
{
|
||||
}
|
||||
|
||||
const NetworkMessage & UdpClient::ReceiveMessage()
|
||||
{
|
||||
// TODO: insert return statement here
|
||||
}
|
||||
|
||||
std::future<bool> UdpClient::SendMessage(const NetworkMessage & message)
|
||||
{
|
||||
return std::async(std::launch::async, &send_network_message, message, this);
|
||||
@ -56,6 +65,15 @@ UdpClient::UdpClient(const SOCKET & socket)
|
||||
udp_socket = socket;
|
||||
}
|
||||
|
||||
const NetworkBuffer & UdpClient::receive_data_array()
|
||||
{
|
||||
// TODO: insert return statement here
|
||||
}
|
||||
|
||||
void UdpClient::receive_data(UdpClient * client)
|
||||
{
|
||||
}
|
||||
|
||||
bool UdpClient::initialize(const std::string &ip, uint16 port)
|
||||
{
|
||||
if (Utility::IPUtil::ValidIPV4(ip) || port == 0)
|
||||
|
||||
Reference in New Issue
Block a user