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

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,11 +6,12 @@
#endif
#include "Defs.hpp"
#include <vector>
struct Serializer
{
template<typename T> static const std::vector<byte> &to_bytes(const T &object);
template<typename T> static const T &from_bytes(byte *bytes, T &object);
template<typename T> static const T& from_bytes(byte *bytes, T& object);
};
#endif

14
include/Tags.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef TAGS_HPP
#define TAGS_HPP
#ifdef _MSC_VER
#pragma once
#endif
enum InternalTags
{
CONNECT = 65534,
DISCONNECT = 65535,
};
#endif

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