Added Development information for me to dont forget things xD
Added AllAndMe DistributionType Added a ThreadPool class Added VoidNetClientAPI
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
@ -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");
|
||||
|
||||
|
||||
14
src/Tags.hpp
14
src/Tags.hpp
@ -1,14 +0,0 @@
|
||||
#ifndef TAGS_HPP
|
||||
#define TAGS_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
enum InternalTags : uint16
|
||||
{
|
||||
CONNECT = 65534,
|
||||
DISCONNECT = 65535,
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -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
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