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
63 lines
1.2 KiB
C++
63 lines
1.2 KiB
C++
#ifndef TCP_SERVER_HPP
|
|
#define TCP_SERVER_HPP
|
|
|
|
#ifdef _MSC_VER
|
|
#pragma once
|
|
#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
|