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
This commit is contained in:
6
TODO
6
TODO
@ -6,4 +6,8 @@ handle ReceiveDataArray errors for windows
|
||||
revamped BitConverter class
|
||||
new StringConverter class
|
||||
|
||||
implement ConfigReader methods
|
||||
implement ConfigReader methods
|
||||
|
||||
maybe i should implement error codes
|
||||
|
||||
plugin system - idk how im going to implement it yet
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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 <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
|
||||
#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
|
||||
};
|
||||
|
||||
|
||||
@ -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<byte> &EncodeHandshake(const Handshake &handshake);
|
||||
@ -20,6 +20,7 @@ struct Handshake
|
||||
|
||||
uint16 id;
|
||||
byte con_code;
|
||||
byte distribution_mode;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -15,7 +15,7 @@ struct NetworkBuffer
|
||||
NetworkBuffer();
|
||||
~NetworkBuffer();
|
||||
|
||||
std::vector<byte> header; // size must always be 8
|
||||
std::vector<byte> header; // size must always be sizeof(int32)
|
||||
std::vector<byte> body;
|
||||
|
||||
bool valid = false;
|
||||
|
||||
@ -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
|
||||
@ -10,10 +10,6 @@
|
||||
|
||||
#include <functional>
|
||||
|
||||
#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<void(const NetworkMessage &message)> OnMessage;
|
||||
std::function<void(const NetworkMessage &message)> 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
|
||||
|
||||
@ -14,8 +14,6 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#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
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -1,3 +1,101 @@
|
||||
#include "TcpClient.hpp"
|
||||
#include "NetworkBuffer.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <future>
|
||||
|
||||
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
|
||||
@ -1,14 +1,14 @@
|
||||
#include "TcpClient.hpp"
|
||||
#include "Utility.hpp"
|
||||
#include "Config.hpp"
|
||||
#include "NetworkBuffer.hpp"
|
||||
#include "Handshake.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <future>
|
||||
|
||||
#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<char*>(header), sizeof(int32), 0) != 4)
|
||||
if (recv(tcp_socket, reinterpret_cast<char*>(header), sizeof(int32), 0) != sizeof(int32))
|
||||
//invalid header
|
||||
return NetworkBuffer();
|
||||
buffer.header = std::vector<byte>(header, header + 4);
|
||||
buffer.header = std::vector<byte>(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<byte>& bytes)
|
||||
{
|
||||
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(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;
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,232 @@
|
||||
#include "TcpServer.hpp"
|
||||
#include "Config.hpp"
|
||||
#include "Handshake.hpp"
|
||||
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
|
||||
TcpServer::TcpServer()
|
||||
{
|
||||
initialize(); // initialize with the default port
|
||||
}
|
||||
|
||||
TcpServer::TcpServer(uint16 port)
|
||||
{
|
||||
initialize(port);
|
||||
}
|
||||
|
||||
TcpServer::~TcpServer()
|
||||
{
|
||||
}
|
||||
|
||||
void TcpServer::Shutdown()
|
||||
{
|
||||
running = false;
|
||||
|
||||
for (std::vector<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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
|
||||
@ -1,14 +1,11 @@
|
||||
#include "TcpServer.hpp"
|
||||
#include "Config.hpp"
|
||||
#include "Utility.hpp"
|
||||
#include "Handshake.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <future>
|
||||
|
||||
#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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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<TcpClient>::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);
|
||||
}
|
||||
@ -3,8 +3,6 @@
|
||||
|
||||
#include <future>
|
||||
|
||||
#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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user