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:
xX-TheDoctor-Xx
2016-08-15 17:26:50 +01:00
parent fcc5c570e1
commit 8ceefbd9fe
16 changed files with 427 additions and 365 deletions

View File

@ -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()

View File

@ -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

View File

@ -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;
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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
}