Added Development information for me to dont forget things xD
Added AllAndMe DistributionType Added a ThreadPool class Added VoidNetClientAPI
This commit is contained in:
5
DEV_INFO
Normal file
5
DEV_INFO
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
id -1 is server / valid message if other parameters are valid as well
|
||||||
|
id -2 is invalid network message
|
||||||
|
|
||||||
|
|
||||||
|
currently working on: TcpClient::SendMessage
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -27,6 +27,7 @@
|
|||||||
<ClInclude Include="..\..\include\NetworkMessage.hpp" />
|
<ClInclude Include="..\..\include\NetworkMessage.hpp" />
|
||||||
<ClInclude Include="..\..\include\Serializer.hpp" />
|
<ClInclude Include="..\..\include\Serializer.hpp" />
|
||||||
<ClInclude Include="..\..\include\TcpClient.hpp" />
|
<ClInclude Include="..\..\include\TcpClient.hpp" />
|
||||||
|
<ClInclude Include="..\..\include\ThreadPool.hpp" />
|
||||||
<ClInclude Include="..\..\include\Utility.hpp" />
|
<ClInclude Include="..\..\include\Utility.hpp" />
|
||||||
<ClInclude Include="..\..\include\VoidNetClient.hpp" />
|
<ClInclude Include="..\..\include\VoidNetClient.hpp" />
|
||||||
<ClInclude Include="..\..\include\VoidNetServer.hpp" />
|
<ClInclude Include="..\..\include\VoidNetServer.hpp" />
|
||||||
@ -41,9 +42,12 @@
|
|||||||
<ClCompile Include="..\..\src\Serializer.cpp" />
|
<ClCompile Include="..\..\src\Serializer.cpp" />
|
||||||
<ClCompile Include="..\..\src\TcpClient.cpp" />
|
<ClCompile Include="..\..\src\TcpClient.cpp" />
|
||||||
<ClCompile Include="..\..\src\TcpClientWindows.cpp" />
|
<ClCompile Include="..\..\src\TcpClientWindows.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\ThreadPool.cpp" />
|
||||||
<ClCompile Include="..\..\src\Utility.cpp" />
|
<ClCompile Include="..\..\src\Utility.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\VoidNetClient.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Text Include="..\..\DEV_INFO" />
|
||||||
<Text Include="..\..\TODO" />
|
<Text Include="..\..\TODO" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<PropertyGroup Label="Globals">
|
<PropertyGroup Label="Globals">
|
||||||
|
|||||||
@ -45,6 +45,9 @@
|
|||||||
<ClInclude Include="..\..\src\Tags.hpp">
|
<ClInclude Include="..\..\src\Tags.hpp">
|
||||||
<Filter>include</Filter>
|
<Filter>include</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\include\ThreadPool.hpp">
|
||||||
|
<Filter>include</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="..\..\src\Init.cpp">
|
<ClCompile Include="..\..\src\Init.cpp">
|
||||||
@ -74,8 +77,15 @@
|
|||||||
<ClCompile Include="..\..\src\NetworkMessage.cpp">
|
<ClCompile Include="..\..\src\NetworkMessage.cpp">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\VoidNetClient.cpp">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\ThreadPool.cpp">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Text Include="..\..\TODO" />
|
<Text Include="..\..\TODO" />
|
||||||
|
<Text Include="..\..\DEV_INFO" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@ -8,10 +8,14 @@
|
|||||||
#include "Defs.hpp"
|
#include "Defs.hpp"
|
||||||
#include "Serializer.hpp"
|
#include "Serializer.hpp"
|
||||||
|
|
||||||
|
/*Client Callbacks begin*/
|
||||||
|
|
||||||
void OnMessage(uint16 sender, uint16 tag, uint16 subject, void *data);
|
void OnMessage(uint16 sender, uint16 tag, uint16 subject, void *data);
|
||||||
|
|
||||||
void OnDisconnect(uint16 id);
|
void OnDisconnect(uint16 id);
|
||||||
|
|
||||||
void OnConnect(uint16 id);
|
void OnConnect(uint16 id);
|
||||||
|
|
||||||
|
/*Client Callbacks end*/
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -185,11 +185,12 @@ enum VoidCode
|
|||||||
enum DistributionType
|
enum DistributionType
|
||||||
{
|
{
|
||||||
All,
|
All,
|
||||||
|
AllAndMe,
|
||||||
Server,
|
Server,
|
||||||
Others,
|
Others,
|
||||||
ID,
|
ID,
|
||||||
Reply,
|
Reply,
|
||||||
Custom,
|
Custom,
|
||||||
}
|
};
|
||||||
|
|
||||||
#endif // DEFS_HPP
|
#endif // DEFS_HPP
|
||||||
@ -15,20 +15,22 @@ struct NetworkMessage
|
|||||||
NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer);
|
NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer);
|
||||||
~NetworkMessage();
|
~NetworkMessage();
|
||||||
|
|
||||||
const NetworkMessage &EncodeMessage();
|
static const NetworkBuffer &EncodeMessage(const NetworkMessage &message);
|
||||||
|
|
||||||
const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer);
|
const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer);
|
||||||
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer);
|
|
||||||
void *DecodeMessageData(const NetworkBuffer &buffer);
|
|
||||||
|
|
||||||
uint16 sender;
|
uint16 sender = -2;
|
||||||
uint16 destination_id;
|
|
||||||
byte distribution_mode;
|
byte distribution_mode;
|
||||||
uint16 tag;
|
uint16 destination_id;
|
||||||
|
byte tag;
|
||||||
uint16 subject;
|
uint16 subject;
|
||||||
void *data;
|
void *data;
|
||||||
|
|
||||||
|
bool valid = false;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer);
|
||||||
|
void *DecodeMessageData(const NetworkBuffer &buffer);
|
||||||
NetworkBuffer buffer;
|
NetworkBuffer buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "Defs.hpp"
|
#include "Defs.hpp"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
struct Serializer
|
struct Serializer
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum InternalTags : uint16
|
enum InternalTags
|
||||||
{
|
{
|
||||||
CONNECT = 65534,
|
CONNECT = 65534,
|
||||||
DISCONNECT = 65535,
|
DISCONNECT = 65535,
|
||||||
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "Defs.hpp"
|
#include "Defs.hpp"
|
||||||
#include "NetworkMessage.hpp"
|
#include "NetworkMessage.hpp"
|
||||||
|
#include "ThreadPool.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -10,6 +11,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#undef SendMessage
|
||||||
|
|
||||||
class TcpClient
|
class TcpClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -27,21 +30,30 @@ public:
|
|||||||
|
|
||||||
NetworkBuffer ReceiveDataArray();
|
NetworkBuffer ReceiveDataArray();
|
||||||
const NetworkMessage &ReceiveData();
|
const NetworkMessage &ReceiveData();
|
||||||
bool SendData(const NetworkMessage &message);
|
VoidCode SendMessage(const NetworkMessage &message);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static VoidCode SendNetworkMessage(const NetworkMessage &message, TcpClient *client);
|
||||||
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
|
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
|
||||||
|
|
||||||
std::string ip;
|
std::string ip;
|
||||||
uint16 port = 0;
|
uint16 port = 0;
|
||||||
bool initialized;
|
bool initialized;
|
||||||
|
|
||||||
|
ThreadPool thread_pool;
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
SOCKET socket = INVALID_SOCKET;
|
SOCKET tcp_socket = INVALID_SOCKET;
|
||||||
struct addrinfo *result = nullptr;
|
struct addrinfo *result = nullptr;
|
||||||
struct addrinfo *ptr = nullptr;
|
struct addrinfo *ptr = nullptr;
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef UNICODE
|
||||||
|
#define SendMessage SendMessageW
|
||||||
|
#else
|
||||||
|
#define SendMessage SendMessageA
|
||||||
|
#endif // !UNICODE
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
33
include/ThreadPool.hpp
Normal file
33
include/ThreadPool.hpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef THREAD_POOL_HPP
|
||||||
|
#define THREAD_POOL_HPP
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma once
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <queue>
|
||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
|
class ThreadPool
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ThreadPool(int threads);
|
||||||
|
~ThreadPool();
|
||||||
|
void Enqueue(std::function<void()> f);
|
||||||
|
void Shutdown();
|
||||||
|
const ThreadPool &operator=(ThreadPool &pool);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::thread> threadPool;
|
||||||
|
std::queue<std::function<void()>> tasks;
|
||||||
|
std::mutex tasksMutex;
|
||||||
|
std::condition_variable condition;
|
||||||
|
bool terminate;
|
||||||
|
bool stopped;
|
||||||
|
void Invoke();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -11,5 +11,41 @@
|
|||||||
#include "Callbacks.hpp"
|
#include "Callbacks.hpp"
|
||||||
#include "NetworkBuffer.hpp"
|
#include "NetworkBuffer.hpp"
|
||||||
#include "NetworkMessage.hpp"
|
#include "NetworkMessage.hpp"
|
||||||
|
#include "ThreadPool.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#undef SendMessage
|
||||||
|
|
||||||
|
struct VoidNetClientAPI
|
||||||
|
{
|
||||||
|
static bool Connect(const std::string &ip, uint16 port = default_port);
|
||||||
|
static void Disconnect();
|
||||||
|
|
||||||
|
static bool SendMessageToServer(byte tag, byte subject, void *data);
|
||||||
|
static bool SendMessageToID(uint16 id, byte tag, byte subject, void *data);
|
||||||
|
static bool SendMessageToOthers(byte tag, byte subject, void *data);
|
||||||
|
static bool SendMessageToAll(byte tag, byte subject, void *data);
|
||||||
|
static bool SendMessageToAllAndMe(byte tag, byte subject, void *data);
|
||||||
|
|
||||||
|
static bool SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data);
|
||||||
|
|
||||||
|
static void Receive();
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void ProcessAllData();
|
||||||
|
|
||||||
|
static TcpClient *client;
|
||||||
|
static uint16 id;
|
||||||
|
static std::thread receive_thread;
|
||||||
|
static bool receive;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef UNICODE
|
||||||
|
#define SendMessage SendMessageW
|
||||||
|
#else
|
||||||
|
#define SendMessage SendMessageA
|
||||||
|
#endif // !UNICODE
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
#include "NetworkMessage.hpp"
|
#include "NetworkMessage.hpp"
|
||||||
#include "Utility.hpp"
|
#include "Utility.hpp"
|
||||||
#include "Serializer.hpp"
|
#include "Serializer.hpp"
|
||||||
|
#include "Tags.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
NetworkMessage::NetworkMessage()
|
NetworkMessage::NetworkMessage()
|
||||||
@ -12,13 +14,41 @@ NetworkMessage::NetworkMessage(uint16 sender, byte distribution_mode, uint16 des
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NetworkMessage::NetworkMessage(const NetworkBuffer &buffer)
|
||||||
|
{
|
||||||
|
*this = DecodeMessage(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
NetworkMessage::~NetworkMessage()
|
NetworkMessage::~NetworkMessage()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
const NetworkMessage &NetworkMessage::EncodeMessage()
|
const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message)
|
||||||
{
|
{
|
||||||
return NetworkMessage();
|
NetworkBuffer buffer;
|
||||||
|
uint16 size = buffer.body_size + 1;
|
||||||
|
byte *encoded_message = new byte[size]();
|
||||||
|
buffer.body_size = size;
|
||||||
|
|
||||||
|
byte *encoded_sender = &encoded_message[1];
|
||||||
|
encoded_sender = Utility::BitConverter::FromUint16(message.sender);
|
||||||
|
|
||||||
|
encoded_message[3] = message.distribution_mode;
|
||||||
|
|
||||||
|
byte *encoded_destination_id = &encoded_message[4];
|
||||||
|
encoded_destination_id = Utility::BitConverter::FromUint16(message.destination_id);
|
||||||
|
|
||||||
|
encoded_message[6] = message.tag;
|
||||||
|
|
||||||
|
byte *encoded_subject = &encoded_message[7];
|
||||||
|
encoded_subject = Utility::BitConverter::FromUint16(message.subject);
|
||||||
|
|
||||||
|
byte *encoded_data = &encoded_message[9];
|
||||||
|
std::vector<byte> serialized_data = Serializer::to_bytes(message.data);
|
||||||
|
encoded_data = &serialized_data[0];
|
||||||
|
|
||||||
|
buffer.body = encoded_message;
|
||||||
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
|
const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
|
||||||
@ -38,7 +68,7 @@ void *NetworkMessage::DecodeMessageData(const NetworkBuffer &buffer)
|
|||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
void *object;
|
void *object;
|
||||||
return Serializer::from_bytes(buffer.body, object);
|
return Serializer::from_bytes(buffer.body + 9, object);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
@ -50,6 +80,12 @@ void *NetworkMessage::DecodeMessageData(const NetworkBuffer &buffer)
|
|||||||
|
|
||||||
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer)
|
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer)
|
||||||
{
|
{
|
||||||
return NetworkMessage(Utility::BitConverter::ToUint16(buffer.body, 1), buffer.body[3], Utility::BitConverter::ToUint16(buffer.body, 4),
|
sender = Utility::BitConverter::ToUint16(buffer.body, 1);
|
||||||
buffer.body[6], Utility::BitConverter::ToUint16(buffer.body, 7), buffer);
|
distribution_mode = buffer.body[3];
|
||||||
|
destination_id = Utility::BitConverter::ToUint16(buffer.body, 4);
|
||||||
|
tag = buffer.body[6];
|
||||||
|
subject = Utility::BitConverter::ToUint16(buffer.body, 7);
|
||||||
|
this->buffer = buffer;
|
||||||
|
valid = sender != -2 && tag != CONNECT && tag != DISCONNECT;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
#include "Serializer.hpp"
|
#include "Serializer.hpp"
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& object)
|
template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& object)
|
||||||
{
|
{
|
||||||
std::vector<byte> bytes;
|
std::vector<byte> bytes;
|
||||||
@ -13,7 +11,7 @@ template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& obje
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> T& Serializer::from_bytes(byte *bytes, T& object)
|
template<typename T> const T& Serializer::from_bytes(byte *bytes, T& object)
|
||||||
{
|
{
|
||||||
static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type");
|
static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type");
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#undef SendMessage
|
||||||
|
|
||||||
VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
||||||
{
|
{
|
||||||
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
|
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
|
||||||
@ -28,9 +30,9 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ptr = result;
|
ptr = result;
|
||||||
socket = ::socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
tcp_socket = ::socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
||||||
|
|
||||||
if (socket == INVALID_SOCKET)
|
if (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
|
||||||
@ -43,12 +45,12 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
|
|||||||
return VOID_SUCCESS;
|
return VOID_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpClient::TcpClient()
|
TcpClient::TcpClient() : port(default_port), thread_pool(50)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpClient::TcpClient(const std::string &ip, uint16 port) :
|
TcpClient::TcpClient(const std::string &ip, uint16 port) :
|
||||||
ip(ip), port(port)
|
ip(ip), port(port), thread_pool(50)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,7 +94,7 @@ VoidCode TcpClient::Connect()
|
|||||||
if (code != VOID_SUCCESS)
|
if (code != VOID_SUCCESS)
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
uint16 connect_code = ::connect(socket, ptr->ai_addr, ptr->ai_addrlen);
|
uint16 connect_code = ::connect(tcp_socket, ptr->ai_addr, ptr->ai_addrlen);
|
||||||
if (connect_code == SOCKET_ERROR)
|
if (connect_code == SOCKET_ERROR)
|
||||||
return VOID_COULDNT_CONNECT;
|
return VOID_COULDNT_CONNECT;
|
||||||
}
|
}
|
||||||
@ -101,16 +103,19 @@ NetworkBuffer TcpClient::ReceiveDataArray()
|
|||||||
{
|
{
|
||||||
NetworkBuffer buffer;
|
NetworkBuffer buffer;
|
||||||
|
|
||||||
if (recv(socket, reinterpret_cast<char*>(buffer.body_size), 4, 0) != 4 || WSAGetLastError() != 0)
|
int32 header_received = recv(tcp_socket, reinterpret_cast<char*>(buffer.body_size), 4, 0);
|
||||||
|
|
||||||
|
if (header_received != 4 || WSAGetLastError() != 0) // this header is completely unrelated to the network message header - this header is the body size of the network message
|
||||||
{
|
{
|
||||||
// there was a problem receiving the body size of the message
|
// there was a problem receiving the body size of the message or theres no header to receive
|
||||||
return NetworkBuffer();
|
return NetworkBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.body = new byte[buffer.body_size]();
|
buffer.body = new byte[buffer.body_size]();
|
||||||
if (recv(socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0) != buffer.body_size || WSAGetLastError() != 0)
|
int32 body_received = recv(tcp_socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0);
|
||||||
|
if (body_received != buffer.body_size || WSAGetLastError() != 0)
|
||||||
{
|
{
|
||||||
//there was a problem receiving the body of the message
|
//there was a problem receiving the body of the message or theres no body to receive
|
||||||
return NetworkBuffer();
|
return NetworkBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,8 +124,7 @@ NetworkBuffer TcpClient::ReceiveDataArray()
|
|||||||
|
|
||||||
const NetworkMessage &TcpClient::ReceiveData()
|
const NetworkMessage &TcpClient::ReceiveData()
|
||||||
{
|
{
|
||||||
NetworkBuffer received_data = ReceiveDataArray();
|
NetworkMessage message(ReceiveDataArray());
|
||||||
NetworkMessage message = NetworkMessage(received_data);
|
|
||||||
if (message.tag == CONNECT)
|
if (message.tag == CONNECT)
|
||||||
OnConnect(message.sender);
|
OnConnect(message.sender);
|
||||||
else if (message.tag == DISCONNECT)
|
else if (message.tag == DISCONNECT)
|
||||||
@ -130,7 +134,16 @@ const NetworkMessage &TcpClient::ReceiveData()
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TcpClient::SendData(const NetworkMessage &message)
|
VoidCode TcpClient::SendNetworkMessage(const NetworkMessage &message, TcpClient *client)
|
||||||
{
|
{
|
||||||
return false;
|
NetworkBuffer buffer = message.EncodeMessage(message);
|
||||||
|
int32 sent_bytes = send(client->tcp_socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
VoidCode TcpClient::SendMessage(const NetworkMessage &message)
|
||||||
|
{
|
||||||
|
thread_pool.Enqueue([]()
|
||||||
|
{
|
||||||
|
//SendNetworkMessage(message, this);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
57
src/ThreadPool.cpp
Normal file
57
src/ThreadPool.cpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include "ThreadPool.hpp"
|
||||||
|
|
||||||
|
ThreadPool::ThreadPool(int threads) :
|
||||||
|
terminate(false),
|
||||||
|
stopped(false)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < threads; i++)
|
||||||
|
threadPool.emplace_back(std::thread(&ThreadPool::Invoke, this));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThreadPool::Enqueue(std::function<void()> f)
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(tasksMutex);
|
||||||
|
tasks.push(f);
|
||||||
|
condition.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThreadPool::Invoke() {
|
||||||
|
|
||||||
|
std::function<void()> task;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(tasksMutex);
|
||||||
|
condition.wait(lock, [this] { return !tasks.empty() || terminate; });
|
||||||
|
if (terminate && tasks.empty())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
task = tasks.front();
|
||||||
|
tasks.pop();
|
||||||
|
task();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThreadPool::Shutdown()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(tasksMutex);
|
||||||
|
terminate = true;
|
||||||
|
condition.notify_all();
|
||||||
|
|
||||||
|
for (std::thread &thread : threadPool)
|
||||||
|
thread.join();
|
||||||
|
|
||||||
|
threadPool.empty();
|
||||||
|
stopped = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ThreadPool &ThreadPool::operator=(ThreadPool &pool)
|
||||||
|
{
|
||||||
|
return pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadPool::~ThreadPool()
|
||||||
|
{
|
||||||
|
if (!stopped)
|
||||||
|
Shutdown();
|
||||||
|
}
|
||||||
79
src/VoidNetClient.cpp
Normal file
79
src/VoidNetClient.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "VoidNetClient.hpp"
|
||||||
|
#include "Utility.hpp"
|
||||||
|
#include "Tags.hpp"
|
||||||
|
|
||||||
|
#undef SendMessage
|
||||||
|
|
||||||
|
bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port)
|
||||||
|
{
|
||||||
|
client->SetIP(ip);
|
||||||
|
client->SetPort(port);
|
||||||
|
return client->Connect() == VOID_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VoidNetClientAPI::SendMessageToServer(byte tag, byte subject, void *data)
|
||||||
|
{
|
||||||
|
return SendMessage(Server, 0, tag, subject, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VoidNetClientAPI::SendMessageToID(uint16 destination_id, byte tag, byte subject, void *data)
|
||||||
|
{
|
||||||
|
return SendMessage(ID, destination_id, tag, subject, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VoidNetClientAPI::SendMessageToOthers(byte tag, byte subject, void *data)
|
||||||
|
{
|
||||||
|
return SendMessage(Others, 0, tag, subject, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VoidNetClientAPI::SendMessageToAll(byte tag, byte subject, void *data)
|
||||||
|
{
|
||||||
|
return SendMessage(All, 0, tag, subject, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VoidNetClientAPI::SendMessageToAllAndMe(byte tag, byte subject, void *data)
|
||||||
|
{
|
||||||
|
return SendMessage(AllAndMe, 0, tag, subject, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data)
|
||||||
|
{
|
||||||
|
NetworkMessage message;
|
||||||
|
message.tag = tag;
|
||||||
|
message.subject = subject;
|
||||||
|
message.data = data;
|
||||||
|
message.distribution_mode = distribution_mode;
|
||||||
|
message.sender = id;
|
||||||
|
message.destination_id = destination_id;
|
||||||
|
return client->SendMessage(message) == VOID_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VoidNetClientAPI::Receive()
|
||||||
|
{
|
||||||
|
receive_thread = std::thread(ProcessAllData);
|
||||||
|
receive_thread.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VoidNetClientAPI::ProcessAllData()
|
||||||
|
{
|
||||||
|
while (receive)
|
||||||
|
{
|
||||||
|
NetworkMessage message = client->ReceiveDataArray();
|
||||||
|
if (message.valid)
|
||||||
|
{
|
||||||
|
if (message.tag == CONNECT)
|
||||||
|
OnConnect(message.sender);
|
||||||
|
else if (message.tag == DISCONNECT)
|
||||||
|
OnDisconnect(message.sender);
|
||||||
|
else
|
||||||
|
OnMessage(message.sender, message.tag, message.subject, message.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VoidNetClientAPI::Disconnect()
|
||||||
|
{
|
||||||
|
Utility::Delete(client);
|
||||||
|
receive = false;
|
||||||
|
receive_thread.join();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user