示例#1
0
void PhatChat::Server::Client::handlePacket ( sf::Packet packet )
{
	unsigned char operationCodeValue = 0 ;
	packet >> operationCodeValue ;
	PhatChat::OperationCode operationCode = static_cast <PhatChat::OperationCode> ( operationCodeValue ) ;

	if ( operationCode == PhatChat::OperationCode::PING )
	{
		PhatChat::PingPacket pingPacket ( PhatChat::PingPacket::decode ( packet ) ) ;
		std::cout << "Received ping packet with value " << pingPacket.getValue ( ) << "!" << std::endl ;

		sf::Packet newPacket ( PhatChat::PongPacket ( pingPacket.getValue ( ) ).encode ( ) ) ;

        this->socket.send ( newPacket ) ;
	}
	else if ( operationCode == PhatChat::OperationCode::PONG )
	{
		PhatChat::PongPacket pongPacket = PhatChat::PongPacket::decode ( packet ) ;
		std::cout << "Received pong packet with value " << pongPacket.getValue ( ) << "!" << std::endl ;
	}
	else if ( operationCode == PhatChat::OperationCode::REQUEST_USERNAME )
	{
        PhatChat::RequestUsernamePacket requestUsernamePacket = PhatChat::RequestUsernamePacket::decode ( packet ) ;
        this->setUsername ( requestUsernamePacket.getUsername ( ) ) ;
	}
	else if ( operationCode == PhatChat::OperationCode::MESSAGE )
	{
        PhatChat::MessagePacket messagePacket = PhatChat::MessagePacket::decode ( packet ) ;
        
        std::string username = this->username.empty ( ) ? messagePacket.getUsername ( ) : this->username ;
        std::cout << "Received message from " << username << " saying \"" << messagePacket.getMessage ( ) << "\"!" << std::endl ;
        
        if ( * messagePacket.getMessage ( ).begin ( ) == '/' )
        	this->handleCommand ( messagePacket.getMessage ( ) ) ;
        else
		{
        	messagePacket.setUsername ( username ) ;
        	
		    for ( auto client : this->clientManager )
		    {
		        packet = messagePacket.encode ( ) ;
		        client->getSocket ( ).send ( packet ) ;
		    }
		 }
	}
	else
		std::cout << "Operation code is unknown! Skip " << packet.getDataSize ( ) << " bytes." << std::endl ;
}
示例#2
0
bool MasterEntityList::getChangedEntities(sf::Packet& packet)
{
    bool anyChanged = false;
    packet << Packet::EntityUpdate;
    // Get deleted entities
    if (!deletedEnts.empty())
    {
        std::cout << "Deleted entity IDs: ";
        for (auto& entId: deletedEnts)
        {
            std::cout << entId << " ";
            packet << entId << -1;
            anyChanged = true;
        }
        std::cout << std::endl;
        deletedEnts.clear();
    }
    // Get changed entities
    if (entCount > 0)
    {
        for (auto& ent: ents)
        {
            if (ent != nullptr && ent->hasChanged())
            {
                ent->getData(packet);
                ent->setChanged(false);
                anyChanged = true;
            }
            if (packet.getDataSize() >= sf::UdpSocket::MaxDatagramSize - 256)
                break;
        }
        /*if (anyChanged)
            cout << "getChangedEntities()\n";*/
        return anyChanged;
    }
    else
        return false;
}
示例#3
0
void Connection::prepSend(Protocol proto, const sf::Packet& rData, sf::Packet& data)
{
    data << static_cast<int32_t>(proto);
    data << ++m_lastSendRecieve[proto].first;
    data.append(rData.getData(), rData.getDataSize());
}
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;
			}
		}
	}
}
示例#5
0
void NetPlayClient::Send(sf::Packet& packet)
{
	ENetPacket* epac = enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE);
	enet_peer_send(m_server, 0, epac);
}
示例#6
0
		void Manager::send(const Client &client, const sf::Packet &packet)
		{
			assert(client.getPeer());
			ENetPacket *netPacket = enet_packet_create(packet.getData(), packet.getDataSize(), 0);
			enet_peer_send(client.getPeer(), 0, netPacket);
		}
示例#7
0
		void Manager::broadcast(const sf::Packet &packet)
		{
			assert(_host);
			ENetPacket *netPacket = enet_packet_create(packet.getData(), packet.getDataSize(), 0);
			enet_host_broadcast(_host, 0, netPacket);
		}
示例#8
0
void NetPlayServer::Send(ENetPeer* socket, const sf::Packet& packet)
{
  ENetPacket* epac =
      enet_packet_create(packet.getData(), packet.getDataSize(), ENET_PACKET_FLAG_RELIABLE);
  enet_peer_send(socket, 0, epac);
}