Updated TODO list
Removed windows specific files since the code is the same for linux UdpClient: added basic methods to interact with the variables of that class, added initialization and started with sending and receiving messages UdpServer: added initialization and basic variables Utility: added a bunch of methods to the StringConverter utility class VoidNetClientAPI: SendMessage now checks if message is not an handshake
This commit is contained in:
@ -243,6 +243,6 @@ enum InternalTags
|
||||
DisconnectTag = 255,
|
||||
};
|
||||
|
||||
#define IS_HANDSHAKE(name) name.subject == 1 && (name.tag == DisconnectTag || name.tag == ConnectTag || name.tag == Accept || name.tag == Close || name.tag == Reject)
|
||||
#define IS_HANDSHAKE(name) name.subject == 1 || (name.tag == DisconnectTag || name.tag == ConnectTag || name.tag == Accept || name.tag == Close || name.tag == Reject)
|
||||
|
||||
#endif // DEFS_HPP
|
||||
@ -10,8 +10,16 @@
|
||||
|
||||
struct Initialization
|
||||
{
|
||||
static bool initialize();
|
||||
static bool Initialize();
|
||||
|
||||
#ifdef _MSC_VER
|
||||
const WSADATA &GetData();
|
||||
#endif
|
||||
|
||||
private:
|
||||
#ifdef _MSC_VER
|
||||
static WSADATA wsa_data;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -7,6 +7,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
@ -39,18 +40,24 @@ struct TcpClient
|
||||
|
||||
//this is a more manual method with no callbacks
|
||||
const NetworkMessage &ReceiveMessage();
|
||||
void SendMessage(const NetworkMessage &message);
|
||||
void SendBytes(const std::vector<byte> &bytes);
|
||||
void SendBytes(byte *bytes, uint32 lenght);
|
||||
|
||||
std::future<bool> SendMessage(const NetworkMessage &message);
|
||||
bool SendBytes(const std::vector<byte> &bytes);
|
||||
bool SendBytes(byte *bytes, uint32 lenght);
|
||||
|
||||
void SetOnDisconnectCallback(void (*func)(uint16));
|
||||
void SetOnConnectCallback(void (*func)(uint16));
|
||||
void SetOnMessageCallback(void (*func)(uint16, byte, byte, void*));
|
||||
|
||||
static const TcpClient &DefaultTcpClient();
|
||||
|
||||
private:
|
||||
TcpClient();
|
||||
|
||||
const NetworkBuffer &receive_data_array();
|
||||
static void receive_data(TcpClient *client);
|
||||
static void send_network_message(const NetworkMessage &message, TcpClient *client);
|
||||
static bool send_network_message(const NetworkMessage &message, TcpClient *client);
|
||||
|
||||
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
||||
|
||||
uint16 id = -2;
|
||||
@ -63,11 +70,9 @@ private:
|
||||
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;
|
||||
struct addrinfo hints;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -22,7 +22,7 @@ struct TcpServer
|
||||
bool StartServer(bool accept_connections); // if accept_connections is false the user must call the funcion AcceptConnections()
|
||||
void AcceptConnections();
|
||||
|
||||
void SendMessage(const NetworkMessage &message);
|
||||
bool SendMessage(const NetworkMessage &message);
|
||||
|
||||
void RejectConnection(TcpClient &client);
|
||||
void AcceptConnection(uint16 client);
|
||||
@ -56,11 +56,9 @@ private:
|
||||
|
||||
std::vector<TcpClient> clients;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
SOCKET server_tcp_socket = INVALID_SOCKET;
|
||||
struct addrinfo *result = nullptr;
|
||||
struct addrinfo hints;
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -9,11 +9,49 @@
|
||||
#include "NetworkMessage.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
|
||||
struct UdpClient
|
||||
{
|
||||
|
||||
UdpClient(const SOCKET &socket);
|
||||
UdpClient(const std::string &ip);
|
||||
UdpClient(const std::string &ip, uint16 = default_client_port);
|
||||
~UdpClient();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
const std::string &GetIP();
|
||||
void SetIP(const std::string &ip);
|
||||
|
||||
uint16 GetPort();
|
||||
void SetPort(uint16 port);
|
||||
|
||||
uint16 GetID();
|
||||
void SetID(uint16 id);
|
||||
|
||||
std::future<bool> SendMessage(const NetworkMessage &message);
|
||||
bool SendBytes(const std::vector<byte> &bytes);
|
||||
bool SendBytes(byte *bytes, uint32 lenght);
|
||||
|
||||
private:
|
||||
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
||||
|
||||
static bool send_network_message(const NetworkMessage &message, UdpClient *client);
|
||||
|
||||
uint16 id = -2;
|
||||
|
||||
std::string ip;
|
||||
uint16 port = 0;
|
||||
bool initialized = false;
|
||||
bool receive = false;
|
||||
|
||||
std::function<void(uint16)> OnDisconnect;
|
||||
std::function<void(uint16)> OnConnect;
|
||||
std::function<void(uint16, byte, byte, void*)> OnMessage;
|
||||
|
||||
SOCKET udp_socket = INVALID_SOCKET;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
34
include/UdpServer.hpp
Normal file
34
include/UdpServer.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef UDP_SERVER_HPP
|
||||
#define UDP_SERVER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "UdpClient.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct UdpServer
|
||||
{
|
||||
private:
|
||||
bool initialize(uint16 port = default_server_port);
|
||||
|
||||
uint16 allocate_id();
|
||||
|
||||
void shutdown_internal();
|
||||
|
||||
bool initialized = false;
|
||||
bool running = false;
|
||||
|
||||
uint16 max_connections = 0;
|
||||
|
||||
std::vector<UdpClient> clients;
|
||||
|
||||
SOCKET server_udp_socket = INVALID_SOCKET;
|
||||
struct sockaddr_in server;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -56,6 +56,15 @@ struct Utility
|
||||
static const std::string &ToString(int64 value);
|
||||
static const std::string &ToString(const std::vector<byte> &bytes);
|
||||
|
||||
static uint8 ToUint8(const std::string &str);
|
||||
static uint16 ToUint16(const std::string &str);
|
||||
static uint32 ToUint32(const std::string &str);
|
||||
static uint64 ToUint64(const std::string &str);
|
||||
static int8 ToInt8(const std::string &str);
|
||||
static int16 ToInt16(const std::string &str);
|
||||
static int32 ToInt32(const std::string &str);
|
||||
static int64 ToInt64(const std::string &str);
|
||||
|
||||
static const std::vector<byte> &ToBytes(const std::string &str);
|
||||
|
||||
static const std::string &ToString(const std::vector<byte> &bytes, uint16 start_index = 0, uint16 lenght = 0);
|
||||
@ -64,6 +73,11 @@ struct Utility
|
||||
static std::vector<std::string> Split(const std::string &str, const std::string &delimiter);
|
||||
};
|
||||
|
||||
struct IPUtil
|
||||
{
|
||||
static bool ValidIPV4(const std::string &ip);
|
||||
};
|
||||
|
||||
struct ConfigReader
|
||||
{
|
||||
void ReadConfig(const std::string &file_name);
|
||||
|
||||
Reference in New Issue
Block a user