Skip to content

Ch 10. GameServer's incorrect handling of incoming connection and disconnection #21

Description

@kyuhyunp

The current code unnecessarily added the following lines to listen for the new connections during handleDisconnections.

if (mConnectedPlayers < mMaxConnectedPlayers)
{
	mPeers.push_back(PeerPtr(new RemotePeer()));
	setListening(true);
}

I think the reason why this was done was to do setListening(true); after handleIncomingConnections'

if (mConnectedPlayers >= mMaxConnectedPlayers)
	setListening(false);

However, it is incorrect to add a new spot in mPeers during handleDisconnections.

Instead, handleDisconnections should only setListening(true) when the mPeers was full like below.

void GameServer::handleDisconnections()
{ 
	for (auto itr = mPeers.begin(); itr != mPeers.end();)
	{
		PeerPtr& peer = *itr;
		if (peer->timedOut)
		{
			for (int32_t aircraftIdentifier : peer->aircraftIdentifiers)
			{
				sf::Packet packet;
				packet << static_cast<int32_t> (Server::PlayerDisconnect);
				sendToAll(packet);

				mAircraftInfo.erase(aircraftIdentifier);
			}

			if (mConnectedPlayers == mMaxConnectedPlayers)
			{
				setListening(true);
			}

			--mConnectedPlayers;
			mAircraftCount -= peer->aircraftIdentifiers.size();

			itr = mPeers.erase(itr);

			broadcastMessage("An ally has disconnected.");
		}
		else
		{
			++itr;
		}
	}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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