Skip to content

Ch 10. Incorrect PlayerRealtimeChange and PlayerConnect: Excluding sender, Redundant mSocket #23

@kyuhyunp

Description

@kyuhyunp

There are two problems with handling the PlayerRealtimeChange packet.

  1. The server sends the packet to all clients without excluding the receivingPeer who already knows about the change.

After changing void notifyPlayerSpawn(int32_t aircraftIdentifier), void notifyPlayerRealtimeChange(int32_t aircraftIdentifier, int32_t action, bool actionEnabled), and void nofifyPlayerEvent(int32_t aircraftIdentifier, int32_t action) to private, add RemotePeer& changer to notifyPlayerRealtimeChange.

Send the packet to every client except the changer.

sf::Packet packet;
packet << static_cast<int32_t> (Server::PlayerRealtimeChange)
	<< aircraftIdentifier << action << actionEnabled;

for (auto& peer : mPeers)
{
	if (peer->ready && peer.get() != &changer)
	{
		peer->socket.send(packet);
	}
}

Without this,

Player::handleNetworkRealtimeChange(Action action, bool actionEnabled)
{
	mActionProxies[action] = actionEnabled;
}

would record the network changes to mActionProxies for the sender.

  1. mSocket which is redundant for remote multiplayers is passed to Player in PlayerConnect in MultiplayerGameState

We pass the address of mSocket to remote players. However, this is redundant as the remote player cannot send the packet using mSocket as it belongs to a local aircraft. We should remove this to reduce unnecessary complexity in the code.

We don't even need mSocket for remote players.

For the three cases,

Case 1: Single-Player
Case 2: Multiplayer Local
Case 3: Multiplayer Remote

Case 3 is the only remote case, but !isLocal() already differentiates Case 3 from Cases 1 and 2. Cases 1 and 2 simply adds the command to the CommandQueue.

Therefore, handleRealtimeInput can use if (isLocal()) and handleRealtimeNetworkInput can use if (!isLocal()) instead of the complex expression with mSocket.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions