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

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