Changeset - r25924:31e3cdb1821b
[Not reviewed]
master
0 1 0
Patric Stout - 3 years ago 2021-08-23 17:38:02
truebrain@openttd.org
Fix #9490: [Network] a full server couldn't be queried either (#9508)

You can now still query a full server, as long as the maximum
amount of allowed connections isn't reached. This means that as
long as there are not 255 clients connected to a server, you can
always connect to query.
1 file changed with 6 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/network/network_server.cpp
Show inline comments
 
@@ -260,97 +260,97 @@ NetworkRecvStatus ServerNetworkGameSocke
 
		/* We did not receive a leave message from this client... */
 
		std::string client_name = this->GetClientName();
 

	
 
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", STR_NETWORK_ERROR_CLIENT_CONNECTION_LOST);
 

	
 
		/* Inform other clients of this... strange leaving ;) */
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status > STATUS_AUTHORIZED && this != new_cs) {
 
				new_cs->SendErrorQuit(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
 
			}
 
		}
 
	}
 

	
 
	/* If we were transfering a map to this client, stop the savegame creation
 
	 * process and queue the next client to receive the map. */
 
	if (this->status == STATUS_MAP) {
 
		/* Ensure the saving of the game is stopped too. */
 
		this->savegame->Destroy();
 
		this->savegame = nullptr;
 

	
 
		this->CheckNextClientToSendMap(this);
 
	}
 

	
 
	NetworkAdminClientError(this->client_id, NETWORK_ERROR_CONNECTION_LOST);
 
	Debug(net, 3, "Closed client connection {}", this->client_id);
 

	
 
	/* We just lost one client :( */
 
	if (this->status >= STATUS_AUTHORIZED) _network_game_info.clients_on--;
 
	extern byte _network_clients_connected;
 
	_network_clients_connected--;
 

	
 
	this->SendPackets(true);
 

	
 
	delete this->GetInfo();
 
	delete this;
 

	
 
	InvalidateWindowData(WC_CLIENT_LIST, 0);
 

	
 
	return status;
 
}
 

	
 
/**
 
 * Whether an connection is allowed or not at this moment.
 
 * @return true if the connection is allowed.
 
 */
 
/* static */ bool ServerNetworkGameSocketHandler::AllowConnection()
 
{
 
	extern byte _network_clients_connected;
 
	bool accept = _network_clients_connected < MAX_CLIENTS && _network_game_info.clients_on < _settings_client.network.max_clients;
 
	bool accept = _network_clients_connected < MAX_CLIENTS;
 

	
 
	/* We can't go over the MAX_CLIENTS limit here. However, the
 
	 * pool must have place for all clients and ourself. */
 
	static_assert(NetworkClientSocketPool::MAX_SIZE == MAX_CLIENTS + 1);
 
	assert(!accept || ServerNetworkGameSocketHandler::CanAllocateItem());
 
	return accept;
 
}
 

	
 
/** Send the packets for the server sockets. */
 
/* static */ void ServerNetworkGameSocketHandler::Send()
 
{
 
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
 
		if (cs->writable) {
 
			if (cs->SendPackets() != SPS_CLOSED && cs->status == STATUS_MAP) {
 
				/* This client is in the middle of a map-send, call the function for that */
 
				cs->SendMap();
 
			}
 
		}
 
	}
 
}
 

	
 
static void NetworkHandleCommandQueue(NetworkClientSocket *cs);
 

	
 
/***********
 
 * Sending functions
 
 *   DEF_SERVER_SEND_COMMAND has parameter: NetworkClientSocket *cs
 
 ************/
 

	
 
/**
 
 * Send the client information about a client.
 
 * @param ci The client to send information about.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendClientInfo(NetworkClientInfo *ci)
 
{
 
	if (ci->client_id != INVALID_CLIENT_ID) {
 
		Packet *p = new Packet(PACKET_SERVER_CLIENT_INFO);
 
		p->Send_uint32(ci->client_id);
 
		p->Send_uint8 (ci->client_playas);
 
		p->Send_string(ci->client_name);
 

	
 
		this->SendPacket(p);
 
	}
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Send the client information about the server. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendGameInfo()
 
{
 
@@ -760,96 +760,101 @@ NetworkRecvStatus ServerNetworkGameSocke
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendConfigUpdate()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_CONFIG_UPDATE);
 

	
 
	p->Send_uint8(_settings_client.network.max_companies);
 
	p->Send_string(_settings_client.network.server_name);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/***********
 
 * Receiving functions
 
 *   DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkClientSocket *cs, Packet *p
 
 ************/
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_INFO(Packet *p)
 
{
 
	return this->SendGameInfo();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED(Packet *p)
 
{
 
	if (this->status != STATUS_NEWGRFS_CHECK) {
 
		/* Illegal call, return error and ignore the packet */
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	NetworkClientInfo *ci = this->GetInfo();
 

	
 
	/* We now want a password from the client else we do not allow them in! */
 
	if (!_settings_client.network.server_password.empty()) {
 
		return this->SendNeedGamePassword();
 
	}
 

	
 
	if (Company::IsValidID(ci->client_playas) && !_network_company_states[ci->client_playas].password.empty()) {
 
		return this->SendNeedCompanyPassword();
 
	}
 

	
 
	return this->SendWelcome();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
 
{
 
	if (this->status != STATUS_INACTIVE) {
 
		/* Illegal call, return error and ignore the packet */
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	if (_network_game_info.clients_on >= _settings_client.network.max_clients) {
 
		/* Turns out we are full. Inform the user about this. */
 
		return this->SendError(NETWORK_ERROR_FULL);
 
	}
 

	
 
	std::string client_revision = p->Recv_string(NETWORK_REVISION_LENGTH);
 
	uint32 newgrf_version = p->Recv_uint32();
 

	
 
	/* Check if the client has revision control enabled */
 
	if (!IsNetworkCompatibleVersion(client_revision) || _openttd_newgrf_version != newgrf_version) {
 
		/* Different revisions!! */
 
		return this->SendError(NETWORK_ERROR_WRONG_REVISION);
 
	}
 

	
 
	std::string client_name = p->Recv_string(NETWORK_CLIENT_NAME_LENGTH);
 
	CompanyID playas = (Owner)p->Recv_uint8();
 

	
 
	if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CLIENT_QUIT;
 

	
 
	/* join another company does not affect these values */
 
	switch (playas) {
 
		case COMPANY_NEW_COMPANY: // New company
 
			if (Company::GetNumItems() >= _settings_client.network.max_companies) {
 
				return this->SendError(NETWORK_ERROR_FULL);
 
			}
 
			break;
 
		case COMPANY_SPECTATOR: // Spectator
 
			break;
 
		default: // Join another company (companies 1-8 (index 0-7))
 
			if (!Company::IsValidHumanID(playas)) {
 
				return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
 
			}
 
			break;
 
	}
 

	
 
	if (!NetworkIsValidClientName(client_name)) {
 
		/* An invalid client name was given. However, the client ensures the name
 
		 * is valid before it is sent over the network, so something went horribly
 
		 * wrong. This is probably someone trying to troll us. */
 
		return this->SendError(NETWORK_ERROR_INVALID_CLIENT_NAME);
 
	}
 

	
 
	if (!NetworkMakeClientNameUnique(client_name)) { // Change name if duplicate
 
		/* We could not create a name for this client */
 
		return this->SendError(NETWORK_ERROR_NAME_IN_USE);
 
	}
 

	
 
	assert(NetworkClientInfo::CanAllocateItem());
 
	NetworkClientInfo *ci = new NetworkClientInfo(this->client_id);
 
	this->SetInfo(ci);
 
	ci->join_date = _date;
 
	ci->client_name = client_name;
 
	ci->client_playas = playas;
0 comments (0 inline, 0 general)