Added Development information for me to dont forget things xD

Added AllAndMe DistributionType
Added a ThreadPool class
Added VoidNetClientAPI
This commit is contained in:
xX-TheDoctor-Xx
2016-07-18 01:13:20 +01:00
parent 72aa8ee0c4
commit a211400964
23 changed files with 323 additions and 32 deletions

5
DEV_INFO Normal file
View 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

View File

@ -27,6 +27,7 @@
<ClInclude Include="..\..\include\NetworkMessage.hpp" />
<ClInclude Include="..\..\include\Serializer.hpp" />
<ClInclude Include="..\..\include\TcpClient.hpp" />
<ClInclude Include="..\..\include\ThreadPool.hpp" />
<ClInclude Include="..\..\include\Utility.hpp" />
<ClInclude Include="..\..\include\VoidNetClient.hpp" />
<ClInclude Include="..\..\include\VoidNetServer.hpp" />
@ -41,9 +42,12 @@
<ClCompile Include="..\..\src\Serializer.cpp" />
<ClCompile Include="..\..\src\TcpClient.cpp" />
<ClCompile Include="..\..\src\TcpClientWindows.cpp" />
<ClCompile Include="..\..\src\ThreadPool.cpp" />
<ClCompile Include="..\..\src\Utility.cpp" />
<ClCompile Include="..\..\src\VoidNetClient.cpp" />
</ItemGroup>
<ItemGroup>
<Text Include="..\..\DEV_INFO" />
<Text Include="..\..\TODO" />
</ItemGroup>
<PropertyGroup Label="Globals">

View File

@ -45,6 +45,9 @@
<ClInclude Include="..\..\src\Tags.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ThreadPool.hpp">
<Filter>include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Init.cpp">
@ -74,8 +77,15 @@
<ClCompile Include="..\..\src\NetworkMessage.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\VoidNetClient.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ThreadPool.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO" />
<Text Include="..\..\DEV_INFO" />
</ItemGroup>
</Project>

View File

@ -8,10 +8,14 @@
#include "Defs.hpp"
#include "Serializer.hpp"
/*Client Callbacks begin*/
void OnMessage(uint16 sender, uint16 tag, uint16 subject, void *data);
void OnDisconnect(uint16 id);
void OnConnect(uint16 id);
/*Client Callbacks end*/
#endif

View File

@ -185,11 +185,12 @@ enum VoidCode
enum DistributionType
{
All,
AllAndMe,
Server,
Others,
ID,
Reply,
Custom,
}
};
#endif // DEFS_HPP

View File

@ -15,20 +15,22 @@ struct NetworkMessage
NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer);
~NetworkMessage();
const NetworkMessage &EncodeMessage();
static const NetworkBuffer &EncodeMessage(const NetworkMessage &message);
const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer);
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer);
void *DecodeMessageData(const NetworkBuffer &buffer);
uint16 sender;
uint16 destination_id;
uint16 sender = -2;
byte distribution_mode;
uint16 tag;
uint16 destination_id;
byte tag;
uint16 subject;
void *data;
bool valid = false;
private:
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer);
void *DecodeMessageData(const NetworkBuffer &buffer);
NetworkBuffer buffer;
};

View File

@ -6,6 +6,7 @@
#endif
#include "Defs.hpp"
#include <vector>
struct Serializer
{

View File

@ -5,7 +5,7 @@
#pragma once
#endif
enum InternalTags : uint16
enum InternalTags
{
CONNECT = 65534,
DISCONNECT = 65535,

View File

@ -3,6 +3,7 @@
#include "Defs.hpp"
#include "NetworkMessage.hpp"
#include "ThreadPool.hpp"
#include <string>
@ -10,6 +11,8 @@
#pragma once
#endif
#undef SendMessage
class TcpClient
{
public:
@ -27,21 +30,30 @@ public:
NetworkBuffer ReceiveDataArray();
const NetworkMessage &ReceiveData();
bool SendData(const NetworkMessage &message);
VoidCode SendMessage(const NetworkMessage &message);
private:
static VoidCode SendNetworkMessage(const NetworkMessage &message, TcpClient *client);
VoidCode Initialize(const std::string &ip, uint16 port = default_port);
std::string ip;
uint16 port = 0;
bool initialized;
ThreadPool thread_pool;
#ifdef _MSC_VER
SOCKET socket = INVALID_SOCKET;
SOCKET tcp_socket = INVALID_SOCKET;
struct addrinfo *result = nullptr;
struct addrinfo *ptr = nullptr;
struct addrinfo hints;
#endif
};
#ifdef UNICODE
#define SendMessage SendMessageW
#else
#define SendMessage SendMessageA
#endif // !UNICODE
#endif

33
include/ThreadPool.hpp Normal file
View 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

View File

@ -11,5 +11,41 @@
#include "Callbacks.hpp"
#include "NetworkBuffer.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

View File

@ -1,6 +1,8 @@
#include "NetworkMessage.hpp"
#include "Utility.hpp"
#include "Serializer.hpp"
#include "Tags.hpp"
#include <vector>
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()
{
}
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)
@ -38,7 +68,7 @@ void *NetworkMessage::DecodeMessageData(const NetworkBuffer &buffer)
case 0:
{
void *object;
return Serializer::from_bytes(buffer.body, object);
return Serializer::from_bytes(buffer.body + 9, object);
}
default:
{
@ -50,6 +80,12 @@ void *NetworkMessage::DecodeMessageData(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),
buffer.body[6], Utility::BitConverter::ToUint16(buffer.body, 7), buffer);
sender = Utility::BitConverter::ToUint16(buffer.body, 1);
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;
}

View File

@ -1,7 +1,5 @@
#include "Serializer.hpp"
#include <vector>
template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& object)
{
std::vector<byte> bytes;
@ -13,7 +11,7 @@ template<typename T> const std::vector<byte> &Serializer::to_bytes(const T& obje
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");

View File

@ -7,6 +7,8 @@
#include <iostream>
#undef SendMessage
VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
{
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;
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())
std::cerr << WSAGetLastError() << std::endl; // display more info
@ -43,12 +45,12 @@ VoidCode TcpClient::Initialize(const std::string &ip, uint16 port)
return VOID_SUCCESS;
}
TcpClient::TcpClient()
TcpClient::TcpClient() : port(default_port), thread_pool(50)
{
}
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)
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)
return VOID_COULDNT_CONNECT;
}
@ -101,16 +103,19 @@ NetworkBuffer TcpClient::ReceiveDataArray()
{
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();
}
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();
}
@ -119,8 +124,7 @@ NetworkBuffer TcpClient::ReceiveDataArray()
const NetworkMessage &TcpClient::ReceiveData()
{
NetworkBuffer received_data = ReceiveDataArray();
NetworkMessage message = NetworkMessage(received_data);
NetworkMessage message(ReceiveDataArray());
if (message.tag == CONNECT)
OnConnect(message.sender);
else if (message.tag == DISCONNECT)
@ -130,7 +134,16 @@ const NetworkMessage &TcpClient::ReceiveData()
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
View 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
View 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();
}