From 8ceefbd9fe674d6644d52d0373d511ba24b4b5a7 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Mon, 15 Aug 2016 17:26:50 +0100 Subject: [PATCH] Added a bunch of undefs to Defs.hpp Hanshake now has distribution_mode Removed all the undef SendMessage and redefenitions, we are undefing it in Defs.hpp and we are now defing it back Added SendHanshake GetMaxConnections SetMaxConnections and shutdown_internal methods in TcpServer.hpp and changed AcceptConnection parameter to uint16 Removed process_all_data method in VoidNetClient.hpp Placed some functions that are OS independent in main cpp files to prevent the DRY concept Fixed receive_data_array method in TcpClientWindows.cpp VoidNetClientAPI::SendMessage now checks if tags dont have the CONNECT or DISCONNECT value because thats reserved to the server --- TODO | 6 +- .../Release/VoidNetVS.tlog/CL.command.1.tlog | Bin 5898 -> 5898 bytes .../Release/VoidNetVS.tlog/CL.read.1.tlog | Bin 253202 -> 251934 bytes .../Release/VoidNetVS.tlog/CL.write.1.tlog | Bin 11464 -> 12886 bytes include/Defs.hpp | 53 +++- include/Handshake.hpp | 3 +- include/NetworkBuffer.hpp | 2 +- include/TcpClient.hpp | 9 - include/TcpServer.hpp | 25 +- include/VoidNetClient.hpp | 10 - src/Handshake.cpp | 3 +- src/TcpClient.cpp | 98 ++++++++ src/TcpClientWindows.cpp | 121 +-------- src/TcpServer.cpp | 229 ++++++++++++++++++ src/TcpServerWindows.cpp | 204 +--------------- src/VoidNetClient.cpp | 29 +-- 16 files changed, 427 insertions(+), 365 deletions(-) diff --git a/TODO b/TODO index ea1d314..60ad650 100644 --- a/TODO +++ b/TODO @@ -6,4 +6,8 @@ handle ReceiveDataArray errors for windows revamped BitConverter class new StringConverter class -implement ConfigReader methods \ No newline at end of file +implement ConfigReader methods + +maybe i should implement error codes + +plugin system - idk how im going to implement it yet \ No newline at end of file diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog index 56f8723d4350ce90a93d28a6b5b03982416a4af1..7f91709e0057b0cde4b1b708fdd90c73666639cc 100644 GIT binary patch delta 42 ycmeCu>(bkh#I$)G%N^dy1uSx#8<_N%CVvo<+9b-wG`WvOVe$eIwapuZ#Fzj_9S$M@ delta 38 ucmeCu>(bkh#575OZL^Dj8`IA7JI1{-&OZXSzuPlf%RWiOF__*QOtv~4n~FTHb0rtDgcG4Mo$0$ delta 178 zcmbQYfq&92{tZIR(=}?DUZk!|w21wGr%@|j9_rgPLXDKrZ)w+k^dP8VWk`ZE1MHB%Hwz039uwM=}*(*rg# z@og8_$mC=J*E+qR5NORc`ao_fWl@!0VBrARk9+R*CQkj|MbBcU)k qKsaXd0TF@8_r$n1A5hI;ogARhHTj>0!sI(LVv|2;7l7G0qLTo?vmyWh delta 101 zcmV-r0Gj{SWXM^tP#}|_4H}bP7rv7)8wQdL7_%-AQUR0DFBFs71|X9jF&4ATAzB2J z+AWZiiXae^j0bR&{w^SsUK|dSt_cp4kOl^mjxdmuUK}K|jxOc_lWY}|lm0NKll~P> HlfWJtnwTdm diff --git a/include/Defs.hpp b/include/Defs.hpp index 24b31c7..8028c2a 100644 --- a/include/Defs.hpp +++ b/include/Defs.hpp @@ -5,11 +5,60 @@ #pragma once #define WIN32_LEAN_AND_MEAN #define _WINSOCKAPI_ +#define _CRT_SECURE_NO_DEPRECATE #pragma comment(lib, "ws2_32.lib") #include #include #include +#undef GetBinaryType +#undef GetShortPathName +#undef GetLongPathName +#undef GetEnvironmentStrings +#undef SetEnvironmentStrings +#undef FreeEnvironmentStrings +#undef FormatMessage +#undef EncryptFile +#undef DecryptFile +#undef CreateMutex +#undef OpenMutex +#undef CreateEvent +#undef OpenEvent +#undef CreateSemaphore +#undef OpenSemaphore +#undef LoadLibrary +#undef GetModuleFileName +#undef CreateProcess +#undef GetCommandLine +#undef GetEnvironmentVariable +#undef SetEnvironmentVariable +#undef ExpandEnvironmentStrings +#undef OutputDebugString +#undef FindResource +#undef UpdateResource +#undef FindAtom +#undef AddAtom +#undef GetSystemDirectory +#undef GetTempPath +#undef GetTempFileName +#undef SetCurrentDirectory +#undef GetCurrentDirectory +#undef CreateDirectory +#undef RemoveDirectory +#undef CreateFile +#undef DeleteFile +#undef SearchPath +#undef CopyFile +#undef MoveFile +#undef ReplaceFile +#undef GetComputerName +#undef SetComputerName +#undef GetUserName +#undef LogonUser +#undef GetVersion +#undef GetObject +#undef SendMessage + typedef signed char int8, sbyte; typedef unsigned char uint8, byte; typedef signed short int16; @@ -173,7 +222,7 @@ const uint16 default_server_port = 61250; enum DistributionType { All = 1, // Others and Server - AllAndMe, // Other, Server and Ourselfs + AllAndMe, // Other, Server and the user sending the message Server, Others, ID, @@ -181,7 +230,7 @@ enum DistributionType enum ConnectionCode { - Accept = 0, + Accept, Reject }; diff --git a/include/Handshake.hpp b/include/Handshake.hpp index c5ce380..3ebe63b 100644 --- a/include/Handshake.hpp +++ b/include/Handshake.hpp @@ -12,7 +12,7 @@ struct Handshake { Handshake(); - Handshake(uint16 id, byte con_code); + Handshake(uint16 id, byte con_code, byte distribution_mode); ~Handshake(); static const std::vector &EncodeHandshake(const Handshake &handshake); @@ -20,6 +20,7 @@ struct Handshake uint16 id; byte con_code; + byte distribution_mode; }; #endif diff --git a/include/NetworkBuffer.hpp b/include/NetworkBuffer.hpp index 045ba0f..1df0170 100644 --- a/include/NetworkBuffer.hpp +++ b/include/NetworkBuffer.hpp @@ -15,7 +15,7 @@ struct NetworkBuffer NetworkBuffer(); ~NetworkBuffer(); - std::vector header; // size must always be 8 + std::vector header; // size must always be sizeof(int32) std::vector body; bool valid = false; diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index de8f0c6..ad3af38 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -10,7 +10,6 @@ #ifdef _MSC_VER #pragma once -#undef SendMessage #endif class TcpClient @@ -73,12 +72,4 @@ private: #endif }; -#ifdef _MSC_VER -#ifdef UNICODE -#define SendMessage SendMessageW -#else -#define SendMessage SendMessageA -#endif // !UNICODE -#endif - #endif \ No newline at end of file diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp index b5263b1..22e6b47 100644 --- a/include/TcpServer.hpp +++ b/include/TcpServer.hpp @@ -10,10 +10,6 @@ #include -#ifdef _MSC_VER -#undef SendMessage -#endif - class TcpServer { public: @@ -27,22 +23,29 @@ public: void AddToClientsList(TcpClient &client); bool StartServer(bool accept_connections); // if accept_connections is false the user must call the funcion AcceptConnections() void AcceptConnections(); + void SendMessage(const NetworkMessage &message); + void SendHandshake(const Handshake &handshake); void RejectConnection(TcpClient &client); - void AcceptConnection(TcpClient &client); + void AcceptConnection(uint16 client); void CloseSocket(TcpClient &client); void CloseSocket(uint16 id); + uint16 GetMaxConnections(); + void SetMaxConnections(uint16 value); + const TcpClient &GetClientByID(uint16 id); - std::function OnMessage; + std::function OnMessage; // this is going to be used for plugins private: static void process_client_messages(TcpServer *server, TcpClient &client); - static void process_message(TcpServer *server, const NetworkMessage &message); static void accept_connections(TcpServer *server); + + void shutdown_internal(); + bool initialize(uint16 port = default_server_port); bool initialized = false; @@ -59,12 +62,4 @@ private: #endif }; -#ifdef _MSC_VER -#ifdef UNICODE -#define SendMessage SendMessageW -#else -#define SendMessage SendMessageA -#endif // !UNICODE -#endif - #endif diff --git a/include/VoidNetClient.hpp b/include/VoidNetClient.hpp index 3013615..9a564d8 100644 --- a/include/VoidNetClient.hpp +++ b/include/VoidNetClient.hpp @@ -14,8 +14,6 @@ #include #include -#undef SendMessage - struct VoidNetClientAPI { static bool Connect(const std::string &ip, uint16 port = default_client_port); @@ -32,17 +30,9 @@ struct VoidNetClientAPI static void Receive(); private: - static void process_all_data(); - static TcpClient client; static uint16 id; static bool receive; }; -#ifdef UNICODE -#define SendMessage SendMessageW -#else -#define SendMessage SendMessageA -#endif // !UNICODE - #endif diff --git a/src/Handshake.cpp b/src/Handshake.cpp index b579bfc..c28ddff 100644 --- a/src/Handshake.cpp +++ b/src/Handshake.cpp @@ -6,10 +6,11 @@ Handshake::Handshake() id = -2; } -Handshake::Handshake(uint16 id, byte con_code) +Handshake::Handshake(uint16 id, byte con_code, byte distribution_mode) { this->id = id; this->con_code = con_code; + this->distribution_mode = distribution_mode; } Handshake::~Handshake() diff --git a/src/TcpClient.cpp b/src/TcpClient.cpp index 2b86fe3..bd664b1 100644 --- a/src/TcpClient.cpp +++ b/src/TcpClient.cpp @@ -1,3 +1,101 @@ +#include "TcpClient.hpp" +#include "NetworkBuffer.hpp" + +#include +#include + +TcpClient::TcpClient() +{ +} + +TcpClient::TcpClient(const std::string &ip) : port(default_client_port) +{ + initialize(ip); +} + +TcpClient::TcpClient(const std::string &ip, uint16 port) : + ip(ip), port(port) +{ + initialize(ip, port); +} + +const std::string &TcpClient::GetIP() +{ + return ip; +} + +uint16 TcpClient::GetPort() +{ + return port; +} + +void TcpClient::SetIP(const std::string & ip) +{ + this->ip = ip; +} + +void TcpClient::SetPort(uint16 port) +{ + this->port = port; +} + +uint16 TcpClient::GetID() +{ + return id; +} + +void TcpClient::SetID(uint16 id) +{ + this->id = id; +} + +void TcpClient::receive_data(TcpClient *client) +{ + while (client->receive) + { + NetworkMessage message(client->ReceiveMessage()); + if (message.valid) + { + if (message.tag == CONNECT) // some user has connected + std::async(std::launch::async, client->OnConnect, message.sender); + else if (message.tag == DISCONNECT) // some user has disconnected + std::async(std::launch::async, client->OnDisconnect, message.sender); + else + std::async(std::launch::async, client->OnMessage, message.sender, message.tag, message.subject, message.data); // we received data + } + } +} + +void TcpClient::ReceiveMessages() +{ + std::async(std::launch::async, &receive_data, this); +} + +const NetworkMessage & TcpClient::ReceiveMessage() +{ + return receive_data_array(); +} + +void TcpClient::SendMessage(const NetworkMessage &message) +{ + std::async(std::launch::async, &send_network_message, message, this); +} + +void TcpClient::SetOnDisconnectCallback(void(*func)(uint16)) +{ + OnDisconnect = func; +} + +void TcpClient::SetOnConnectCallback(void(*func)(uint16)) +{ + OnConnect = func; +} + +void TcpClient::SetOnMessageCallback(void(*func)(uint16, byte, byte, void*)) +{ + OnMessage = func; +} + #ifdef _MSC_VER #include "TcpClientWindows.cpp" #endif \ No newline at end of file diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index 36567e8..3b6a49d 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -1,14 +1,14 @@ #include "TcpClient.hpp" #include "Utility.hpp" #include "Config.hpp" -#include "NetworkBuffer.hpp" #include "Handshake.hpp" #include -#include -#include -#undef SendMessage +TcpClient::TcpClient(const SOCKET & socket) +{ + tcp_socket = socket; +} bool TcpClient::initialize(const std::string &ip, uint16 port) { @@ -44,26 +44,6 @@ bool TcpClient::initialize(const std::string &ip, uint16 port) return initialized = true; } -TcpClient::TcpClient() -{ -} - -TcpClient::TcpClient(const SOCKET & socket) -{ - tcp_socket = socket; -} - -TcpClient::TcpClient(const std::string &ip) : port(default_client_port) -{ - initialize(ip); -} - -TcpClient::TcpClient(const std::string &ip, uint16 port) : - ip(ip), port(port) -{ - initialize(ip, port); -} - TcpClient::~TcpClient() { freeaddrinfo(result); @@ -89,48 +69,16 @@ void TcpClient::Shutdown() WSACleanup(); } -const std::string &TcpClient::GetIP() -{ - return ip; -} - -uint16 TcpClient::GetPort() -{ - return port; -} - -void TcpClient::SetIP(const std::string & ip) -{ - this->ip = ip; -} - -void TcpClient::SetPort(uint16 port) -{ - this->port = port; -} - -uint16 TcpClient::GetID() -{ - return id; -} - -void TcpClient::SetID(uint16 id) -{ - this->id = id; -} - bool TcpClient::Connect() { if (!initialized) { - if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4) - return false; - if (port == 0) - return false; - if (initialize(ip, port) != true) + if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4 && port == 0 && !initialize(ip, port)) return false; } - uint16 connect_code = ::connect(tcp_socket, result->ai_addr, result->ai_addrlen); + else return false; + + uint16 connect_code = connect(tcp_socket, result->ai_addr, result->ai_addrlen); if (connect_code == SOCKET_ERROR) return false; @@ -161,10 +109,10 @@ const NetworkBuffer &TcpClient::receive_data_array() if (DataAvailable(temp) && temp > sizeof(int32)) { byte *header = new byte[sizeof(int32)](); - if (recv(tcp_socket, reinterpret_cast(header), sizeof(int32), 0) != 4) + if (recv(tcp_socket, reinterpret_cast(header), sizeof(int32), 0) != sizeof(int32)) //invalid header return NetworkBuffer(); - buffer.header = std::vector(header, header + 4); + buffer.header = std::vector(header, header + sizeof(int32)); } else return NetworkBuffer(); @@ -184,33 +132,6 @@ const NetworkBuffer &TcpClient::receive_data_array() return buffer; } -void TcpClient::receive_data(TcpClient *client) -{ - while (client->receive) - { - NetworkMessage message(client->receive_data_array()); - if (message.valid) - { - if (message.tag == CONNECT) // some user has connected - std::async(std::launch::async, client->OnConnect, message.sender); - else if (message.tag == DISCONNECT) // some user has disconnected - std::async(std::launch::async, client->OnDisconnect, message.sender); - else - std::async(std::launch::async, client->OnMessage, message.sender, message.tag, message.subject, message.data); // we received data - } - } -} - -void TcpClient::ReceiveMessages() -{ - std::async(std::launch::async, &receive_data, this); -} - -const NetworkMessage & TcpClient::ReceiveMessage() -{ - return receive_data_array(); -} - void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *client) { NetworkBuffer buffer = NetworkMessage::EncodeMessage(message); @@ -222,11 +143,6 @@ void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *c } } -void TcpClient::SendMessage(const NetworkMessage &message) -{ - std::async(std::launch::async, &send_network_message, message, this); -} - void TcpClient::SendBytes(const std::vector& bytes) { int32 bytes_sent = send(tcp_socket, reinterpret_cast(bytes.data()), bytes.size(), 0); @@ -243,19 +159,4 @@ void TcpClient::SendBytes(byte * bytes, uint32 size) { //something went wrong couldnt send anything/some data } -} - -void TcpClient::SetOnDisconnectCallback(void(*func)(uint16)) -{ - OnDisconnect = func; -} - -void TcpClient::SetOnConnectCallback(void(*func)(uint16)) -{ - OnConnect = func; -} - -void TcpClient::SetOnMessageCallback(void(*func)(uint16, byte, byte, void*)) -{ - OnMessage = func; -} +} \ No newline at end of file diff --git a/src/TcpServer.cpp b/src/TcpServer.cpp index a74d7bf..471b445 100644 --- a/src/TcpServer.cpp +++ b/src/TcpServer.cpp @@ -1,3 +1,232 @@ +#include "TcpServer.hpp" +#include "Config.hpp" +#include "Handshake.hpp" + +#include +#include + +TcpServer::TcpServer() +{ + initialize(); // initialize with the default port +} + +TcpServer::TcpServer(uint16 port) +{ + initialize(port); +} + +TcpServer::~TcpServer() +{ +} + +void TcpServer::Shutdown() +{ + running = false; + + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + (*it).Shutdown(); + clients.erase(it); + } + + shutdown_internal(); +} + +void TcpServer::AcceptConnections() +{ + running = true; + std::async(std::launch::async, &accept_connections, this); +} + +void TcpServer::process_client_messages(TcpServer *server, TcpClient & client) +{ + while (server->running) + { + NetworkMessage message(client.ReceiveMessage()); + if (message.valid) + server->SendMessage(message); + } +} + +void TcpServer::SendMessage(const NetworkMessage & message) +{ + switch (message.distribution_mode) + { + case All: // this will send the message to all except the user that sent it + { + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + if (message.sender != client.GetID()) + client.SendMessage(message); + } + OnMessage(message); + break; + } + case AllAndMe: // this will send the message to EVERYONE including the user that sent it + { + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + client.SendMessage(message); + } + OnMessage(message); + break; + } + case Server: // this will only send the message to the server + { + if (message.tag == DISCONNECT) + CloseSocket(message.sender); + OnMessage(message); + break; + } + case Others: // this will send the message to others, excluding server and the user that sent it + { + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + if (message.sender != client.GetID()) + client.SendMessage(message); + } + break; + } + case ID: // this will send the message to a specific id + { + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + if (message.sender == client.GetID()) + { + client.SendMessage(message); + break; + } + } + break; + } + } +} + +void TcpServer::SendHandshake(const Handshake &handshake) +{ + switch (handshake.distribution_mode) + { + case AllAndMe: // this will send the message to EVERYONE including the user that sent it + { + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + client.SendBytes(Handshake::EncodeHandshake(handshake)); + } + break; + } + case ID: // this will send the message to a specific id + { + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + if (handshake.id == client.GetID()) + { + client.SendBytes(Handshake::EncodeHandshake(handshake)); + break; + } + } + break; + } + } +} + +uint16 TcpServer::AllocateID() // this function is only used in the AddToClientsList function +{ + for (uint16 i = 1; i < max_connections; ++i) + { + bool flag = true; + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + if (client.GetID() == i) + { + flag = false; + break; + } + } + + if (flag) + return i; + } + return 0; +} + +void TcpServer::AddToClientsList(TcpClient & client_socket) +{ + uint16 id = AllocateID(); + if (id > 0) + { + client_socket.SetID(id); + clients.emplace_back(client_socket); + AcceptConnection(client_socket.GetID()); + } + else + { + if (Config::GetUsingConsole()) + std::cout << "No available ID's" << std::endl; + RejectConnection(client_socket); + } +} + +void TcpServer::RejectConnection(TcpClient &client) +{ + Handshake handshake(client.GetID(), ConnectionCode::Reject, ID); + client.SendBytes(Handshake::EncodeHandshake(handshake)); + CloseSocket(client); +} + +void TcpServer::AcceptConnection(uint16 id) +{ + Handshake handshake(id, ConnectionCode::Accept, AllAndMe); + SendHandshake(handshake); +} + +void TcpServer::CloseSocket(TcpClient & client) +{ + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient it_client = *it; + if (client.GetID() == it_client.GetID()) + { + it_client.Shutdown(); + clients.erase(it); + } + } +} + +void TcpServer::CloseSocket(uint16 id) +{ + TcpClient client = GetClientByID(id); + if (client.GetID() != -2) + CloseSocket(client); +} + +const TcpClient & TcpServer::GetClientByID(uint16 id) +{ + for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) + { + TcpClient client = *it; + if (client.GetID() == id) + return client; + } + return TcpClient(); +} + +void TcpServer::SetMaxConnections(uint16 value) +{ + max_connections = value; +} + +uint16 TcpServer::GetMaxConnections() +{ + return max_connections; +} + #ifdef _MSC_VER #include "TcpServerWindows.cpp" #endif \ No newline at end of file diff --git a/src/TcpServerWindows.cpp b/src/TcpServerWindows.cpp index 8b182ae..26d87f4 100644 --- a/src/TcpServerWindows.cpp +++ b/src/TcpServerWindows.cpp @@ -1,14 +1,11 @@ #include "TcpServer.hpp" #include "Config.hpp" #include "Utility.hpp" -#include "Handshake.hpp" #include #include #include -#undef SendMessage - bool TcpServer::initialize(uint16 port) { ZeroMemory(&hints, sizeof(hints)); @@ -52,32 +49,6 @@ bool TcpServer::initialize(uint16 port) return initialized = true; } -TcpServer::TcpServer() -{ - initialize(); // initialize with the default port -} - -TcpServer::TcpServer(uint16 port) -{ - initialize(port); -} - -TcpServer::~TcpServer() -{ - freeaddrinfo(result); - WSACleanup(); - Utility::Delete(result); -} - -void TcpServer::Shutdown() -{ - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - (*it).Shutdown(); - clients.erase(it); - } -} - bool TcpServer::StartServer(bool accept_connections) { if (listen(server_tcp_socket, SOMAXCONN) == SOCKET_ERROR) @@ -95,12 +66,6 @@ bool TcpServer::StartServer(bool accept_connections) return true; } -void TcpServer::AcceptConnections() -{ - running = true; - std::async(std::launch::async, &accept_connections, this); -} - void TcpServer::accept_connections(TcpServer *server) { while (server->running) @@ -123,168 +88,9 @@ void TcpServer::accept_connections(TcpServer *server) } } -void TcpServer::process_client_messages(TcpServer *server, TcpClient & client) +void TcpServer::shutdown_internal() { - while (server->running) - { - NetworkMessage message(client.ReceiveMessage()); - if (message.valid) - server->SendMessage(message); - } -} - -void TcpServer::process_message(TcpServer * server, const NetworkMessage & message) -{ - if (message.valid) - server->SendMessage(message); -} - -void TcpServer::SendMessage(const NetworkMessage & message) -{ - switch (message.distribution_mode) - { - case All: // this will send the message to all except the user that sent it - { - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - if (message.sender != client.GetID()) - client.SendMessage(message); - } - OnMessage(message); - break; - } - case AllAndMe: // this will send the message to EVERYONE including the user that sent it - { - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - client.SendMessage(message); - } - OnMessage(message); - break; - } - case Server: // this will only send the message to the server - { - if (message.tag == DISCONNECT) - CloseSocket(message.sender); - OnMessage(message); - break; - } - case Others: // this will send the message to others, excluding server and the user that sent it - { - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - client.SendMessage(message); - } - break; - } - case ID: // this will send the message to a specific id - { - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - if (message.sender == client.GetID()) - { - client.SendMessage(message); - break; - } - } - break; - } - } -} - -uint16 TcpServer::AllocateID() // this function is only used in the AddToClientsList function -{ - for (uint16 i = 1; i < max_connections; ++i) - { - bool flag = true; - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - if (client.GetID() == i) - { - flag = false; - break; - } - } - - if (flag) - return i; - } - return 0; -} - -void TcpServer::AddToClientsList(TcpClient & client_socket) -{ - uint16 id = AllocateID(); - if (id > 0) - { - client_socket.SetID(id); - clients.emplace_back(client_socket); - AcceptConnection(client_socket); - } - else - { - if (Config::GetUsingConsole()) - std::cout << "No available ID's" << std::endl; - RejectConnection(client_socket); - } -} - -void TcpServer::RejectConnection(TcpClient &client) -{ - Handshake handshake(client.GetID(), ConnectionCode::Reject); - client.SendBytes(Handshake::EncodeHandshake(handshake)); - CloseSocket(client); -} - -void TcpServer::AcceptConnection(TcpClient & client) -{ - Handshake handshake(client.GetID(), ConnectionCode::Accept); - client.SendBytes(Handshake::EncodeHandshake(handshake)); -} - -void TcpServer::CloseSocket(TcpClient & client) -{ - bool removed; - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient it_client = *it; - if (client.GetID() == it_client.GetID()) - { - clients.erase(it); - removed = true; - } - } - - if (removed) - { - NetworkMessage message; - message.sender = -1; - message.distribution_mode = ID; - message.destination_id = client.GetID(); - message.tag = DISCONNECT; - SendMessage(message); - } -} - -void TcpServer::CloseSocket(uint16 id) -{ - TcpClient client = GetClientByID(id); - if (client.GetID() != -2) - CloseSocket(client); -} - -const TcpClient & TcpServer::GetClientByID(uint16 id) -{ - for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) - { - TcpClient client = *it; - if (client.GetID() == id) - return client; - } - return TcpClient(); -} + freeaddrinfo(result); + WSACleanup(); + Utility::Delete(result); +} \ No newline at end of file diff --git a/src/VoidNetClient.cpp b/src/VoidNetClient.cpp index de6eeab..408df29 100644 --- a/src/VoidNetClient.cpp +++ b/src/VoidNetClient.cpp @@ -3,8 +3,6 @@ #include -#undef SendMessage - bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port) { client.SetIP(ip); @@ -39,27 +37,26 @@ void VoidNetClientAPI::SendMessageToAllAndMe(byte tag, byte subject, void *data) void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data) { - NetworkMessage message; - message.tag = tag; - message.subject = subject; - message.data = data; - message.distribution_mode = distribution_mode; - message.sender = id; - message.destination_id = destination_id; - client.SendMessage(message); + if (tag != CONNECT && tag != DISCONNECT) + { + NetworkMessage message; + message.tag = tag; + message.subject = subject; + message.data = data; + message.distribution_mode = distribution_mode; + message.sender = id; + message.destination_id = destination_id; + client.SendMessage(message); + } } void VoidNetClientAPI::Receive() { - std::async(std::launch::async, &process_all_data); -} - -void VoidNetClientAPI::process_all_data() -{ - client.ReceiveMessages(); + std::async(std::launch::async, &client.ReceiveMessages); } void VoidNetClientAPI::Disconnect() { receive = false; + SendMessageToServer }