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:
4
TODO
4
TODO
@ -7,3 +7,7 @@ revamped BitConverter class
|
|||||||
new StringConverter 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
|
#pragma once
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#define _WINSOCKAPI_
|
#define _WINSOCKAPI_
|
||||||
|
#define _CRT_SECURE_NO_DEPRECATE
|
||||||
#pragma comment(lib, "ws2_32.lib")
|
#pragma comment(lib, "ws2_32.lib")
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <WS2tcpip.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 signed char int8, sbyte;
|
||||||
typedef unsigned char uint8, byte;
|
typedef unsigned char uint8, byte;
|
||||||
typedef signed short int16;
|
typedef signed short int16;
|
||||||
@ -173,7 +222,7 @@ const uint16 default_server_port = 61250;
|
|||||||
enum DistributionType
|
enum DistributionType
|
||||||
{
|
{
|
||||||
All = 1, // Others and Server
|
All = 1, // Others and Server
|
||||||
AllAndMe, // Other, Server and Ourselfs
|
AllAndMe, // Other, Server and the user sending the message
|
||||||
Server,
|
Server,
|
||||||
Others,
|
Others,
|
||||||
ID,
|
ID,
|
||||||
@ -181,7 +230,7 @@ enum DistributionType
|
|||||||
|
|
||||||
enum ConnectionCode
|
enum ConnectionCode
|
||||||
{
|
{
|
||||||
Accept = 0,
|
Accept,
|
||||||
Reject
|
Reject
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
struct Handshake
|
struct Handshake
|
||||||
{
|
{
|
||||||
Handshake();
|
Handshake();
|
||||||
Handshake(uint16 id, byte con_code);
|
Handshake(uint16 id, byte con_code, byte distribution_mode);
|
||||||
~Handshake();
|
~Handshake();
|
||||||
|
|
||||||
static const std::vector<byte> &EncodeHandshake(const Handshake &handshake);
|
static const std::vector<byte> &EncodeHandshake(const Handshake &handshake);
|
||||||
@ -20,6 +20,7 @@ struct Handshake
|
|||||||
|
|
||||||
uint16 id;
|
uint16 id;
|
||||||
byte con_code;
|
byte con_code;
|
||||||
|
byte distribution_mode;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -15,7 +15,7 @@ struct NetworkBuffer
|
|||||||
NetworkBuffer();
|
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;
|
std::vector<byte> body;
|
||||||
|
|
||||||
bool valid = false;
|
bool valid = false;
|
||||||
|
|||||||
@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma once
|
#pragma once
|
||||||
#undef SendMessage
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class TcpClient
|
class TcpClient
|
||||||
@ -73,12 +72,4 @@ private:
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#ifdef UNICODE
|
|
||||||
#define SendMessage SendMessageW
|
|
||||||
#else
|
|
||||||
#define SendMessage SendMessageA
|
|
||||||
#endif // !UNICODE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -10,10 +10,6 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#undef SendMessage
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class TcpServer
|
class TcpServer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -27,22 +23,29 @@ public:
|
|||||||
void AddToClientsList(TcpClient &client);
|
void AddToClientsList(TcpClient &client);
|
||||||
bool StartServer(bool accept_connections); // if accept_connections is false the user must call the funcion AcceptConnections()
|
bool StartServer(bool accept_connections); // if accept_connections is false the user must call the funcion AcceptConnections()
|
||||||
void AcceptConnections();
|
void AcceptConnections();
|
||||||
|
|
||||||
void SendMessage(const NetworkMessage &message);
|
void SendMessage(const NetworkMessage &message);
|
||||||
|
void SendHandshake(const Handshake &handshake);
|
||||||
|
|
||||||
void RejectConnection(TcpClient &client);
|
void RejectConnection(TcpClient &client);
|
||||||
void AcceptConnection(TcpClient &client);
|
void AcceptConnection(uint16 client);
|
||||||
|
|
||||||
void CloseSocket(TcpClient &client);
|
void CloseSocket(TcpClient &client);
|
||||||
void CloseSocket(uint16 id);
|
void CloseSocket(uint16 id);
|
||||||
|
|
||||||
|
uint16 GetMaxConnections();
|
||||||
|
void SetMaxConnections(uint16 value);
|
||||||
|
|
||||||
const TcpClient &GetClientByID(uint16 id);
|
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:
|
private:
|
||||||
static void process_client_messages(TcpServer *server, TcpClient &client);
|
static void process_client_messages(TcpServer *server, TcpClient &client);
|
||||||
static void process_message(TcpServer *server, const NetworkMessage &message);
|
|
||||||
static void accept_connections(TcpServer *server);
|
static void accept_connections(TcpServer *server);
|
||||||
|
|
||||||
|
void shutdown_internal();
|
||||||
|
|
||||||
bool initialize(uint16 port = default_server_port);
|
bool initialize(uint16 port = default_server_port);
|
||||||
|
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
@ -59,12 +62,4 @@ private:
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#ifdef UNICODE
|
|
||||||
#define SendMessage SendMessageW
|
|
||||||
#else
|
|
||||||
#define SendMessage SendMessageA
|
|
||||||
#endif // !UNICODE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -14,8 +14,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#undef SendMessage
|
|
||||||
|
|
||||||
struct VoidNetClientAPI
|
struct VoidNetClientAPI
|
||||||
{
|
{
|
||||||
static bool Connect(const std::string &ip, uint16 port = default_client_port);
|
static bool Connect(const std::string &ip, uint16 port = default_client_port);
|
||||||
@ -32,17 +30,9 @@ struct VoidNetClientAPI
|
|||||||
static void Receive();
|
static void Receive();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void process_all_data();
|
|
||||||
|
|
||||||
static TcpClient client;
|
static TcpClient client;
|
||||||
static uint16 id;
|
static uint16 id;
|
||||||
static bool receive;
|
static bool receive;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef UNICODE
|
|
||||||
#define SendMessage SendMessageW
|
|
||||||
#else
|
|
||||||
#define SendMessage SendMessageA
|
|
||||||
#endif // !UNICODE
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,10 +6,11 @@ Handshake::Handshake()
|
|||||||
id = -2;
|
id = -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
Handshake::Handshake(uint16 id, byte con_code)
|
Handshake::Handshake(uint16 id, byte con_code, byte distribution_mode)
|
||||||
{
|
{
|
||||||
this->id = id;
|
this->id = id;
|
||||||
this->con_code = con_code;
|
this->con_code = con_code;
|
||||||
|
this->distribution_mode = distribution_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
Handshake::~Handshake()
|
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
|
#ifdef _MSC_VER
|
||||||
#include "TcpClientWindows.cpp"
|
#include "TcpClientWindows.cpp"
|
||||||
#endif
|
#endif
|
||||||
@ -1,14 +1,14 @@
|
|||||||
#include "TcpClient.hpp"
|
#include "TcpClient.hpp"
|
||||||
#include "Utility.hpp"
|
#include "Utility.hpp"
|
||||||
#include "Config.hpp"
|
#include "Config.hpp"
|
||||||
#include "NetworkBuffer.hpp"
|
|
||||||
#include "Handshake.hpp"
|
#include "Handshake.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#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)
|
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;
|
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()
|
TcpClient::~TcpClient()
|
||||||
{
|
{
|
||||||
freeaddrinfo(result);
|
freeaddrinfo(result);
|
||||||
@ -89,48 +69,16 @@ void TcpClient::Shutdown()
|
|||||||
WSACleanup();
|
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()
|
bool TcpClient::Connect()
|
||||||
{
|
{
|
||||||
if (!initialized)
|
if (!initialized)
|
||||||
{
|
{
|
||||||
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4)
|
if (ip.size() == 0 || std::count(ip.begin(), ip.end(), '.') != 4 && port == 0 && !initialize(ip, port))
|
||||||
return false;
|
|
||||||
if (port == 0)
|
|
||||||
return false;
|
|
||||||
if (initialize(ip, port) != true)
|
|
||||||
return false;
|
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)
|
if (connect_code == SOCKET_ERROR)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -161,10 +109,10 @@ const NetworkBuffer &TcpClient::receive_data_array()
|
|||||||
if (DataAvailable(temp) && temp > sizeof(int32))
|
if (DataAvailable(temp) && temp > sizeof(int32))
|
||||||
{
|
{
|
||||||
byte *header = new byte[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
|
//invalid header
|
||||||
return NetworkBuffer();
|
return NetworkBuffer();
|
||||||
buffer.header = std::vector<byte>(header, header + 4);
|
buffer.header = std::vector<byte>(header, header + sizeof(int32));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return NetworkBuffer();
|
return NetworkBuffer();
|
||||||
@ -184,33 +132,6 @@ const NetworkBuffer &TcpClient::receive_data_array()
|
|||||||
return buffer;
|
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)
|
void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *client)
|
||||||
{
|
{
|
||||||
NetworkBuffer buffer = NetworkMessage::EncodeMessage(message);
|
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)
|
void TcpClient::SendBytes(const std::vector<byte>& bytes)
|
||||||
{
|
{
|
||||||
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(bytes.data()), bytes.size(), 0);
|
int32 bytes_sent = send(tcp_socket, reinterpret_cast<const char*>(bytes.data()), bytes.size(), 0);
|
||||||
@ -244,18 +160,3 @@ void TcpClient::SendBytes(byte * bytes, uint32 size)
|
|||||||
//something went wrong couldnt send anything/some data
|
//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
|
#ifdef _MSC_VER
|
||||||
#include "TcpServerWindows.cpp"
|
#include "TcpServerWindows.cpp"
|
||||||
#endif
|
#endif
|
||||||
@ -1,14 +1,11 @@
|
|||||||
#include "TcpServer.hpp"
|
#include "TcpServer.hpp"
|
||||||
#include "Config.hpp"
|
#include "Config.hpp"
|
||||||
#include "Utility.hpp"
|
#include "Utility.hpp"
|
||||||
#include "Handshake.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <future>
|
#include <future>
|
||||||
|
|
||||||
#undef SendMessage
|
|
||||||
|
|
||||||
bool TcpServer::initialize(uint16 port)
|
bool TcpServer::initialize(uint16 port)
|
||||||
{
|
{
|
||||||
ZeroMemory(&hints, sizeof(hints));
|
ZeroMemory(&hints, sizeof(hints));
|
||||||
@ -52,32 +49,6 @@ bool TcpServer::initialize(uint16 port)
|
|||||||
return initialized = true;
|
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)
|
bool TcpServer::StartServer(bool accept_connections)
|
||||||
{
|
{
|
||||||
if (listen(server_tcp_socket, SOMAXCONN) == SOCKET_ERROR)
|
if (listen(server_tcp_socket, SOMAXCONN) == SOCKET_ERROR)
|
||||||
@ -95,12 +66,6 @@ bool TcpServer::StartServer(bool accept_connections)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpServer::AcceptConnections()
|
|
||||||
{
|
|
||||||
running = true;
|
|
||||||
std::async(std::launch::async, &accept_connections, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TcpServer::accept_connections(TcpServer *server)
|
void TcpServer::accept_connections(TcpServer *server)
|
||||||
{
|
{
|
||||||
while (server->running)
|
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)
|
freeaddrinfo(result);
|
||||||
{
|
WSACleanup();
|
||||||
NetworkMessage message(client.ReceiveMessage());
|
Utility::Delete(result);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
#include <future>
|
#include <future>
|
||||||
|
|
||||||
#undef SendMessage
|
|
||||||
|
|
||||||
bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port)
|
bool VoidNetClientAPI::Connect(const std::string &ip, uint16 port)
|
||||||
{
|
{
|
||||||
client.SetIP(ip);
|
client.SetIP(ip);
|
||||||
@ -38,6 +36,8 @@ void VoidNetClientAPI::SendMessageToAllAndMe(byte tag, byte subject, void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data)
|
void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data)
|
||||||
|
{
|
||||||
|
if (tag != CONNECT && tag != DISCONNECT)
|
||||||
{
|
{
|
||||||
NetworkMessage message;
|
NetworkMessage message;
|
||||||
message.tag = tag;
|
message.tag = tag;
|
||||||
@ -48,18 +48,15 @@ void VoidNetClientAPI::SendMessage(byte distribution_mode, uint16 destination_id
|
|||||||
message.destination_id = destination_id;
|
message.destination_id = destination_id;
|
||||||
client.SendMessage(message);
|
client.SendMessage(message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void VoidNetClientAPI::Receive()
|
void VoidNetClientAPI::Receive()
|
||||||
{
|
{
|
||||||
std::async(std::launch::async, &process_all_data);
|
std::async(std::launch::async, &client.ReceiveMessages);
|
||||||
}
|
|
||||||
|
|
||||||
void VoidNetClientAPI::process_all_data()
|
|
||||||
{
|
|
||||||
client.ReceiveMessages();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VoidNetClientAPI::Disconnect()
|
void VoidNetClientAPI::Disconnect()
|
||||||
{
|
{
|
||||||
receive = false;
|
receive = false;
|
||||||
|
SendMessageToServer
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user