Added Connection Codes
Private methods dont have capital letters and are words are separated by underscores Added id's to TcpClient's class Removed ptr from TcpClient and Server Worked on TcpServer Theres now a default server port Added ReceiveMessage function to TcpClient to receive only 1 message Fixed VoidNetClientAPI Receive function
This commit is contained in:
@ -167,7 +167,8 @@ typedef unsigned long long uint64;
|
||||
|
||||
#endif // compiler data type defenitions
|
||||
|
||||
const uint16 default_port = 60250;
|
||||
const uint16 default_client_port = 60250;
|
||||
const uint16 default_server_port = 61250;
|
||||
|
||||
enum DistributionType
|
||||
{
|
||||
@ -178,4 +179,10 @@ enum DistributionType
|
||||
ID,
|
||||
};
|
||||
|
||||
enum ConnectionCode
|
||||
{
|
||||
Accept = 0,
|
||||
Reject
|
||||
};
|
||||
|
||||
#endif // DEFS_HPP
|
||||
@ -29,8 +29,8 @@ struct NetworkMessage
|
||||
bool valid = false;
|
||||
|
||||
private:
|
||||
const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer);
|
||||
void *DecodeMessageData(const NetworkBuffer &buffer);
|
||||
const NetworkMessage &decode_message_header(const NetworkBuffer &buffer);
|
||||
void *decode_message_data(const NetworkBuffer &buffer);
|
||||
NetworkBuffer buffer;
|
||||
};
|
||||
|
||||
|
||||
@ -16,8 +16,9 @@
|
||||
class TcpClient
|
||||
{
|
||||
public:
|
||||
TcpClient();
|
||||
TcpClient(const std::string &ip, uint16 port = default_port);
|
||||
TcpClient(const SOCKET &socket);
|
||||
TcpClient(const std::string &ip);
|
||||
TcpClient(const std::string &ip, uint16 port = default_client_port);
|
||||
~TcpClient();
|
||||
|
||||
const std::string &GetIP();
|
||||
@ -26,9 +27,13 @@ public:
|
||||
uint16 GetPort();
|
||||
void SetPort(uint16 port);
|
||||
|
||||
uint16 GetID();
|
||||
void SetID(uint16 id);
|
||||
|
||||
bool Connect();
|
||||
|
||||
void ReceiveMessages();
|
||||
const NetworkMessage &ReceiveMessage();
|
||||
void SendMessage(const NetworkMessage &message);
|
||||
|
||||
std::function<void(uint16)> OnDisconnect;
|
||||
@ -36,11 +41,12 @@ public:
|
||||
std::function<void(uint16, uint16, uint16, void*)> OnMessage;
|
||||
|
||||
private:
|
||||
const NetworkBuffer &ReceiveDataArray();
|
||||
static void ReceiveData(TcpClient *client);
|
||||
static void SendNetworkMessage(const NetworkMessage &message, TcpClient *client);
|
||||
bool Initialize(const std::string &ip, uint16 port = default_port);
|
||||
const NetworkBuffer &receive_data_array();
|
||||
static void receive_data(TcpClient *client);
|
||||
static void send_network_message(const NetworkMessage &message, TcpClient *client);
|
||||
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
||||
|
||||
uint16 id = 0;
|
||||
std::string ip;
|
||||
uint16 port = 0;
|
||||
bool initialized = false;
|
||||
@ -49,7 +55,6 @@ private:
|
||||
#ifdef _MSC_VER
|
||||
SOCKET tcp_socket = INVALID_SOCKET;
|
||||
struct addrinfo *result = nullptr;
|
||||
struct addrinfo *ptr = nullptr;
|
||||
struct addrinfo hints;
|
||||
#endif
|
||||
};
|
||||
|
||||
@ -3,6 +3,60 @@
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "TcpClient.hpp"
|
||||
|
||||
#include <functional>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#undef SendMessage
|
||||
#endif
|
||||
|
||||
class TcpServer
|
||||
{
|
||||
public:
|
||||
TcpServer();
|
||||
TcpServer(uint16 port = default_server_port);
|
||||
~TcpServer();
|
||||
|
||||
uint16 AllocateID();
|
||||
void AddToClientsList(const TcpClient &client);
|
||||
bool StartServer(bool accept_connections);
|
||||
void AcceptConnections();
|
||||
void SendMessage(const NetworkMessage &message);
|
||||
|
||||
void RejectConnection(TcpClient &client);
|
||||
|
||||
std::function<void(const NetworkMessage &message)> OnMessage;
|
||||
|
||||
private:
|
||||
static void process_message(TcpServer *server, const NetworkMessage &message);
|
||||
static void process_client_received_data(TcpServer *server);
|
||||
static void accept_connections(TcpServer *server);
|
||||
bool initialize(uint16 port = default_server_port);
|
||||
|
||||
bool initialized = false;
|
||||
bool running = false;
|
||||
|
||||
uint16 max_connections = 0;
|
||||
|
||||
std::vector<TcpClient> clients;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
SOCKET socket = INVALID_SOCKET;
|
||||
struct addrinfo *result = nullptr;
|
||||
struct addrinfo hints;
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifdef UNICODE
|
||||
#define SendMessage SendMessageW
|
||||
#else
|
||||
#define SendMessage SendMessageA
|
||||
#endif // !UNICODE
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
|
||||
struct VoidNetClientAPI
|
||||
{
|
||||
static bool Connect(const std::string &ip, uint16 port = default_port);
|
||||
static bool Connect(const std::string &ip, uint16 port = default_client_port);
|
||||
static void Disconnect();
|
||||
|
||||
static void SendMessageToServer(byte tag, byte subject, void *data);
|
||||
@ -32,7 +32,7 @@ struct VoidNetClientAPI
|
||||
static void Receive();
|
||||
|
||||
private:
|
||||
static void ProcessAllData();
|
||||
static void process_all_data();
|
||||
|
||||
static TcpClient *client;
|
||||
static uint16 id;
|
||||
|
||||
Reference in New Issue
Block a user