void NetworkClient::attemptConnectionToServer(sf::IpAddress serverIpAddress, unsigned short serverPort) {
	ipAddressOfServer = serverIpAddress;
	// connect to remote server
	if(serverIpAddress.toString() == sf::IpAddress::getLocalAddress().toString()) {
		connectionState = 3;
		return;
	}
	
	if(connectionState != 0) {
		connectionState = 0;
	}
	else
		connectionState = 0;
	tcpSocket.setBlocking(false);
	tcpSocket.connect(serverIpAddress, serverPort);
	connectionState = 1;
}
				inline bool connect(sf::IpAddress mIp, unsigned short mPort)
				{
					if(busy) { ssvu::lo("ManagedSocket") << "Error: already connected" << std::endl; return false; }

					for(int i{0}; i < 5; ++i)
					{
						if(!busy && socket.connect(mIp, mPort) == sf::Socket::Done) goto succeed;
						std::this_thread::sleep_for(std::chrono::milliseconds(50));
					}

					return false;

					succeed:
					if(!busy)
					{
						ssvu::lo("ManagedSocket") << "Connecting..." << std::endl;
						busy = true;
						handlerFuture = std::async(std::launch::async, [this]{ while(busy) update(); });
					}
					ssvu::lo("ManagedSocket") << "Connected to " << mIp.toString() << ":" << mPort << std::endl;
					return true;
				}
Beispiel #3
0
Client::Client(SceneGame *scene, sf::IpAddress server)
{
	alive = true;
	_scene = scene;
	int command;
	int args;
	//creation de la lisaison durable sur le serveur
	_socket = new sf::UdpSocket();
	if (_socket->bind(NETWORK_PORT+1) != sf::Socket::Done)
	{
		assert("Impossible d'ouvrir le port d'ecoute pour le serveur !");
	}
	std::cout << "Tentative de connexion vers " << server.toString() << std::endl;
	char* data = (char *)"ready to rock !";
	_socket->send(data, 32, server, NETWORK_PORT);
	//attente de la r�c�ption du port
	char buffer[1024];
	size_t received;
	_socket->receive(buffer, sizeof(buffer), received, server, _serverPort);
	//traitement des donn�es
	sscanf(buffer, "%d:%d;", &command, &args);
	if(command == NETWORK_SENDPORT)
	{
		_server = server;
		_serverPort = args;
		std::cout << "Connexion établie au serveur a l'adresse " << _server << " sur le port " <<  _serverPort << std::endl;
	}
	else
	{
		assert("Message inattendu.");
		//TODO: faire un truc moins brutus
		exit(1);
	}
	_server = server;
	prepare();
}
Beispiel #4
0
void Server::manageClient(sf::IpAddress client)
{
	srand(time(0x00));
	bool done = false;
	//sélection d'un port aléatoire au dessus du port 1024
	int port = rand() % (65536 - 2048) + 2048;
	//création du pprt d'écoute
	sf::UdpSocket socket;
	while (socket.bind(port) != sf::Socket::Done)
	{
		port = rand() % (65536 - 2048) + 2048;
	}
	_clientPorts.push_back(port);
	std::cout << "Ouverture d'un port d'ecoute pour " << client.toString() << " sur le port " << port << std::endl;
	//détermination du message
	std::stringstream buffer; buffer << NETWORK_SENDPORT << ":" << port;
	//envoi du message
	socket.send(buffer.str().c_str(), sizeof(buffer), client, NETWORK_PORT + 1);
	char data[1024];
	sf::IpAddress distant;
	unsigned short distant_port;
	size_t received;
	int command, args;
	Packet* current = 0x00;
	while(!done)
	{
		//reception des données brutes
		socket.receive(data, sizeof(data), received, distant, distant_port);
		if(distant != client && distant != sf::IpAddress("127.0.0.1"))
		{
			assert("Paquet recu d'une source illegale.");
		}
		else
		{
			//formatage des données
			sscanf(data, "%d:%d", &command, &args);
			switch (command) {
				case NETWORK_KILL:
					if(distant == sf::IpAddress("127.0.0.1"))
						done = true;
				break;
				case NETWORK_STOP:
					if(current != 0x00)
					{
						current->process(_scene, socket, client, NETWORK_PORT + 1);
						delete current;
					}
					break;
				case NETWORK_NEXT:
					if(current != 0x00)
						current->addData(args);
				break;
				default:
					assert("Paquet incomprehensible recu.");
					break;

			}
		}
	}
	//On indique au client que la connexion est terminée
	buffer.str("");buffer << NETWORK_KILL  << ":0";
	socket.send(buffer.str().c_str(), sizeof(buffer), client, NETWORK_PORT +1);
}