Added Handshake class

Fixed NetworkBuffer include
Fixed some data types
TcpClient: Added SendBytes, SetOnDisconnectCallback, SetOnConnectCallback, SetOnMessageCallback methods and made callback std::function's private and fixed some method implementations
Added AcceptConnection on TcpServer
Fixed TcpServer AddToClientsList
This commit is contained in:
xX-TheDoctor-Xx
2016-08-01 00:28:22 +01:00
parent e677fdeb59
commit 0ac8b4f6e8
18 changed files with 143 additions and 25 deletions

25
include/Handshake.hpp Normal file
View File

@ -0,0 +1,25 @@
#ifndef HANDSHAKE_HPP
#define HANDSHAKE_HPP
#ifdef _MSC_VER
#pragma once
#endif
#include "Defs.hpp"
#include <vector>
struct Handshake
{
Handshake();
Handshake(uint16 id, byte con_code);
~Handshake();
static const std::vector<byte> &EncodeHandshake(const Handshake &handshake);
static Handshake &DecodeHandshake(const std::vector<byte> &bytes);
uint16 id;
byte con_code;
};
#endif

View File

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

View File

@ -12,7 +12,7 @@ struct NetworkMessage
{
NetworkMessage();
NetworkMessage(const NetworkBuffer &buffer);
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, byte subject, NetworkBuffer buffer);
~NetworkMessage();
static const NetworkBuffer &EncodeMessage(const NetworkMessage &message);
@ -23,7 +23,7 @@ struct NetworkMessage
byte distribution_mode;
uint16 destination_id;
byte tag;
uint16 subject;
byte subject;
void *data;
bool valid = false;

View File

@ -42,10 +42,11 @@ public:
//this is a more manual method with no callbacks
const NetworkMessage &ReceiveMessage();
void SendMessage(const NetworkMessage &message);
void SendBytes(const std::vector<byte> &bytes);
std::function<void(uint16)> OnDisconnect;
std::function<void(uint16)> OnConnect;
std::function<void(uint16, byte, uint16, void*)> OnMessage;
void SetOnDisconnectCallback(void (*func)(uint16));
void SetOnConnectCallback(void (*func)(uint16));
void SetOnMessageCallback(void (*func)(uint16, byte, byte, void*));
private:
const NetworkBuffer &receive_data_array();
@ -59,6 +60,10 @@ private:
bool initialized = false;
bool receive = false;
std::function<void(uint16)> OnDisconnect;
std::function<void(uint16)> OnConnect;
std::function<void(uint16, byte, byte, void*)> OnMessage;
#ifdef _MSC_VER
SOCKET tcp_socket = INVALID_SOCKET;
struct addrinfo *result = nullptr;

View File

@ -24,12 +24,13 @@ public:
void Shutdown();
uint16 AllocateID();
void AddToClientsList(const TcpClient &client);
void AddToClientsList(TcpClient &client);
bool StartServer(bool accept_connections);
void AcceptConnections();
void SendMessage(const NetworkMessage &message);
void RejectConnection(TcpClient &client);
void AcceptConnection(TcpClient &client);
std::function<void(const NetworkMessage &message)> OnMessage;