<commit_before>#include "stdafx.h"
#include "NetworkServer.hpp"

NetworkServer* NetworkServer::self = nullptr;

NetworkServer::NetworkServer() :
	port(ANVPORT)
{
	//Singleton check
	if(self) exit(-1);
	self = this;


}

NetworkServer* NetworkServer::getSingleton()
{
	return self;
}


int NetworkServer::initialize(int port)
{
	std::stringstream ss;
	if(port < netNS::MIN_PORT)
	{
		AnnDebug() << "Invalid port number";
		return netNS::NET_ERROR;
	}
	// ------ init network stuff -------
	error = net.createServer(port, netNS::UDP);
	if(error != netNS::NET_OK)
	{
		AnnDebug() << net.getError(error);
		return netNS::NET_ERROR;
	}

	for (int i=0; i<MAX_CLIENT; i++)
	{
		player[i].setConnected(false);
		player[i].setActive(false);
	}

	AnnDebug() << "---- Server ----";
	net.getLocalIP(localIP);
	ss << "Server IP: " << localIP;
	AnnDebug() << ss.str();
	ss.str("");
	ss << "Port: " << port;
	AnnDebug() << ss.str();
	return netNS::NET_OK;
}

// --- Do network communications ---
void NetworkServer::communicate(float frameTime)
{
	// communicate with client 
	doClientCommunication(); 
	// Calculate elapsed time for network communications
	netTime += frameTime;
	if(netTime < netNS::NET_TIMER)
		return;

	netTime -= netNS::NET_TIMER;
	// check for inactive clients, called every NET_TIMER seconds
	checkNetworkTimeout(); 
}

// --- Check for network timeout ---

void NetworkServer::checkNetworkTimeout()
{
	std::stringstream ss;
	for (int i=0; i<MAX_CLIENT; i++)
	{
		if (player[i].getConnected())
		{
			player[i].incTimeout();
			// if communication timout 
			if (player[i].getTimeout() > netNS::MAX_ERRORS)
			{
				player[i].setActive(false);
				ss << "***** Player " << i << " disconnected. *****";
				AnnDebug() << ss.str();
			}
		}
	}
}

void NetworkServer::doClientCommunication() 
{
	int playN;
	int size;
	prepareDataForClient(); 
	for (int i=0; i<MAX_CLIENT; i++)
	{
		size = sizeof(toServerData);
		if(net.readData((char*) &toServerData, size, remoteIP, port) == netNS::NET_OK)
		{ 
			if(size > 0)
			{
				/*AnnDebug() << "there are things here...";
				//AnnDebug() << (char*) &toServerData;
				AnnDebug() << "player n : " << toServerData.playerN;
				AnnDebug() << "x " << toServerData.x;
				AnnDebug() << "y " << toServerData.y;
				AnnDebug() << "z " << toServerData.z;*/
				playN = toServerData.playerN;
				if (playN == 255)
				{
					clientWantToJoin(); 
				}
				else
				{
					if (player[playN].getConnected())
					{
						if (player[playN].getActive());
							//player[playN].setButtons(toServerData.buttons);
						size = sizeof(toClientData);
						// send player the latest game data
						net.sendData((char*) &toClientData, size, player[i].getNetIP(), port);
						player[playN].setTimeout(0);
						player[playN].setCommWarnings(0);
					}
				}
			}
		}
		else    // No more incomming data ? break !!
		{
			break;
		}
	}
}

void NetworkServer::prepareDataForClient() 
{
	/*for (int i=0; i<MAX_CLIENT; i++)
	{
		toClientData.player[i].playerData = player[i].getNetData();
	}*/

	//toClientData.puckPosition = AnnVect3(5,5,5);
	//toClientData.serverPaddlePos = AnnVect3(10, 10, 10);
	toClientData.postition = AnnVect3(3,2,1);<commit_msg>display some of the data retrived by the network<commit_after>#include "stdafx.h"
#include "NetworkServer.hpp"

NetworkServer* NetworkServer::self = nullptr;

NetworkServer::NetworkServer() :
	port(ANVPORT)
{
	//Singleton check
	if(self) exit(-1);
	self = this;


}

NetworkServer* NetworkServer::getSingleton()
{
	return self;
}


int NetworkServer::initialize(int port)
{
	std::stringstream ss;
	if(port < netNS::MIN_PORT)
	{
		AnnDebug() << "Invalid port number";
		return netNS::NET_ERROR;
	}
	// ------ init network stuff -------
	error = net.createServer(port, netNS::UDP);
	if(error != netNS::NET_OK)
	{
		AnnDebug() << net.getError(error);
		return netNS::NET_ERROR;
	}

	for (int i=0; i<MAX_CLIENT; i++)
	{
		player[i].setConnected(false);
		player[i].setActive(false);
	}

	AnnDebug() << "---- Server ----";
	net.getLocalIP(localIP);
	ss << "Server IP: " << localIP;
	AnnDebug() << ss.str();
	ss.str("");
	ss << "Port: " << port;
	AnnDebug() << ss.str();
	return netNS::NET_OK;
}

// --- Do network communications ---
void NetworkServer::communicate(float frameTime)
{
	// communicate with client 
	doClientCommunication(); 
	// Calculate elapsed time for network communications
	netTime += frameTime;
	if(netTime < netNS::NET_TIMER)
		return;

	netTime -= netNS::NET_TIMER;
	// check for inactive clients, called every NET_TIMER seconds
	checkNetworkTimeout(); 
}

// --- Check for network timeout ---

void NetworkServer::checkNetworkTimeout()
{
	std::stringstream ss;
	for (int i=0; i<MAX_CLIENT; i++)
	{
		if (player[i].getConnected())
		{
			player[i].incTimeout();
			// if communication timout 
			if (player[i].getTimeout() > netNS::MAX_ERRORS)
			{
				player[i].setActive(false);
				ss << "***** Player " << i << " disconnected. *****";
				AnnDebug() << ss.str();
			}
		}
	}
}

void NetworkServer::doClientCommunication() 
{
	int playN;
	int size;
	prepareDataForClient(); 
	for (int i=0; i<MAX_CLIENT; i++)
	{
		size = sizeof(toServerData);
		if(net.readData((char*) &toServerData, size, remoteIP, port) == netNS::NET_OK)
		{ 
					AnnDebug() << "Got " << size << " bytes from the network";
			if(size > 0)
			{
				/*AnnDebug() << "there are things here...";
				//AnnDebug() << (char*) &toServerData;
				AnnDebug() << "player n : " << toServerData.playerN;
				AnnDebug() << "x " << toServerData.x;
				AnnDebug() << "y " << toServerData.y;
				AnnDebug() << "z " << toServerData.z;*/
				playN = toServerData.playerN;
				if (playN == 255)
				{
					clientWantToJoin(); 
				}
				else
				{
					AnnDebug() << "got this : " << toServerData.ClientPaddlePos;
					if (player[playN].getConnected())
					{
						if (player[playN].getActive());
							//player[playN].setButtons(toServerData.buttons);
						size = sizeof(toClientData);
						// send player the latest game data
						net.sendData((char*) &toClientData, size, player[i].getNetIP(), port);
						player[playN].setTimeout(0);
						player[playN].setCommWarnings(0);
					}
				}
			}
		}
		else    // No more incomming data ? break !!
		{
			break;
		}
	}
}

void NetworkServer::prepareDataForClient() 
{
	/*for (int i=0; i<MAX_CLIENT; i++)
	{
		toClientData.player[i].playerData = player[i].getNetData();
	}*/

	//toClientData.puckPosition = AnnVect3(5,5,5);
	//toClientData.serverPaddlePos = AnnVect3(10, 10, 10);
	toClientData.postition = AnnVect3(3,2,1);
{'fraction_non_alphanumeric': 0.18031532221031685, 'fraction_numerical': 0.007959589774988519, 'mean_word_length': 3.4814814814814814, 'pattern_counts': {'":': 0, '<': 117, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4377142', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2360, 'n_words': 464}