Example #1
0
void CommunicationManager::readOrder(Client* client, sf::Packet packet)
{
	//On récupère la première partie du packet
		std::string firstPart;
		if(packet >> firstPart)
		{
			if(strcmp(firstPart.data(), "ping") == 0)
			{
				//Si c'est un ping, on renvoie pong en réponse
				std::cout << "ping" << std::endl;
				packet.clear();
				packet << "pong";
				client->socket().send(packet);
			}
			if(strcmp(firstPart.data(), "chat") == 0)
			{
				//Si c'est un chat, on récupère la deuxieme partie
				//et on la relaie aux autres joueurs
				std::string secondPart;
				packet >> secondPart;
				for (std::list<Client*>::iterator it = m_clients.begin(); it != m_clients.end(); ++it)
					sendChatMessage((*it), secondPart);
			}
			if(firstPart.compare("myNameIs") == 0)
				packet >> client->m_name;
		}
void PacketBroker::receive(sf::Packet& packet, Protocol protocol)
{
	using namespace sf;

	if (protocol == Protocol::TCP)
	{
		if (Globals::Networking->ReceiveSafe(packet) != Socket::Status::Done)
			return;
	}
	else
	{
		PacketHandler::RemoteAddress remoteAddress;
		if (Globals::Networking->ReceiveFast(packet, remoteAddress) != Socket::Status::Done)
			return;

		if (!Globals::Networking->isConnectedAddress(remoteAddress))
			return;

		ushort sequence = 0;
		packet >> sequence;

		// TODO: Rejection threshold
		if (sequence == 0 || sequence <= lastSequence)
		{
			PrintDebug(">> Received out of order packet. Rejecting.");
			lastSequence = sequence % USHRT_MAX;
			return;
		}

		lastSequence = sequence % USHRT_MAX;
	}

	PlayerNumber pnum = -1;

	if (netStat)
		addBytesReceived(packet.getDataSize());

	while (!packet.endOfPacket())
	{
		MessageID newType;
		packet >> newType;

		// TODO: Re-implement packet loop failsafe using read offset.

		switch (newType)
		{
			case MessageID::None:
				PrintDebug("\a>> Reached end of packet.");
				break;

			case MessageID::Count:
				PrintDebug(">> Received message count?! Malformed packet warning!");
				break;

			case MessageID::N_Disconnect:
				PrintDebug(">> Received disconnect request from client.");
				Globals::Networking->Disconnect();
				break;

			case MessageID::N_Ready:
			{
				MessageID waitID; packet >> waitID;
				auto it = waitRequests.find(waitID);

				if (it != waitRequests.end())
				{
					it->second = true;
				}
				else
				{
					waitRequests[waitID] = true;
				}

				break;
			}

			case MessageID::N_PlayerNumber:
				packet >> pnum;
				break;

				// TODO: verification that this is from the host
			case MessageID::N_SetPlayer:
			{
				PlayerNumber changed;
				packet >> changed;
				SetPlayerNumber(changed);
				break;
			}

			case MessageID::S_KeepAlive:
				receivedKeepalive = Millisecs();
				packet.seekRead(sizeof(ushort), SEEK_CUR);
				break;

			default:
			{
				if (newType < MessageID::N_END)
				{
					packet.clear();
					break;
				}

				ushort length;
				packet >> length;

				AddTypeReceived(newType, length, protocol == Protocol::TCP);

				if (pnum >= 0)
				{
					if (receiveSystem(newType, pnum, packet))
						break;

					if (pnum != playerNum)
					{
						if (!writePlayer)
							inPlayer.Copy(Player[pnum]);

						if (receivePlayer(newType, pnum, packet))
						{
							if (GameState >= GameState::Ingame)
							{
								writePlayer = false;
								PlayerObject::WritePlayer(Player[pnum], &inPlayer);
							}

							break;
						}
					}

					if (receiveMenu(newType, pnum, packet))
						break;
				}

				if (runMessageHandler(newType, pnum, packet))
					break;

				PrintDebug("\t\t[P%d] Skipping %d bytes for id %02d", pnum, length, newType);
				packet.seekRead(length, SEEK_CUR);

				break;
			}
		}
	}
}