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
 
@@ -116,385 +116,385 @@ struct PacketWriter : SaveFilter {
 
	 * Transfer all packets from here to the network's queue while holding
 
	 * the lock on our mutex.
 
	 * @param socket The network socket to write to.
 
	 * @return True iff the last packet of the map has been sent.
 
	 */
 
	bool TransferToNetworkQueue(ServerNetworkGameSocketHandler *socket)
 
	{
 
		/* Unsafe check for the queue being empty or not. */
 
		if (this->packets == nullptr) return false;
 

	
 
		std::lock_guard<std::mutex> lock(this->mutex);
 

	
 
		while (this->packets != nullptr) {
 
			Packet *p = Packet::PopFromQueue(&this->packets);
 
			bool last_packet = p->GetPacketType() == PACKET_SERVER_MAP_DONE;
 
			socket->SendPacket(p);
 

	
 
			if (last_packet) return true;
 
		}
 

	
 
		return false;
 
	}
 

	
 
	/** Append the current packet to the queue. */
 
	void AppendQueue()
 
	{
 
		if (this->current == nullptr) return;
 

	
 
		Packet::AddToQueue(&this->packets, this->current);
 
		this->current = nullptr;
 
	}
 

	
 
	/** Prepend the current packet to the queue. */
 
	void PrependQueue()
 
	{
 
		if (this->current == nullptr) return;
 

	
 
		/* Reversed from AppendQueue so the queue gets added to the current one. */
 
		Packet::AddToQueue(&this->current, this->packets);
 
		this->packets = this->current;
 
		this->current = nullptr;
 
	}
 

	
 
	void Write(byte *buf, size_t size) override
 
	{
 
		/* We want to abort the saving when the socket is closed. */
 
		if (this->cs == nullptr) SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
 

	
 
		if (this->current == nullptr) this->current = new Packet(PACKET_SERVER_MAP_DATA, TCP_MTU);
 

	
 
		std::lock_guard<std::mutex> lock(this->mutex);
 

	
 
		byte *bufe = buf + size;
 
		while (buf != bufe) {
 
			size_t written = this->current->Send_bytes(buf, bufe);
 
			buf += written;
 

	
 
			if (!this->current->CanWriteToPacket(1)) {
 
				this->AppendQueue();
 
				if (buf != bufe) this->current = new Packet(PACKET_SERVER_MAP_DATA, TCP_MTU);
 
			}
 
		}
 

	
 
		this->total_size += size;
 
	}
 

	
 
	void Finish() override
 
	{
 
		/* We want to abort the saving when the socket is closed. */
 
		if (this->cs == nullptr) SlError(STR_NETWORK_ERROR_LOSTCONNECTION);
 

	
 
		std::lock_guard<std::mutex> lock(this->mutex);
 

	
 
		/* Make sure the last packet is flushed. */
 
		this->AppendQueue();
 

	
 
		/* Add a packet stating that this is the end to the queue. */
 
		this->current = new Packet(PACKET_SERVER_MAP_DONE);
 
		this->AppendQueue();
 

	
 
		/* Fast-track the size to the client. */
 
		this->current = new Packet(PACKET_SERVER_MAP_SIZE);
 
		this->current->Send_uint32((uint32)this->total_size);
 
		this->PrependQueue();
 
	}
 
};
 

	
 

	
 
/**
 
 * Create a new socket for the server side of the game connection.
 
 * @param s The socket to connect with.
 
 */
 
ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : NetworkGameSocketHandler(s)
 
{
 
	this->status = STATUS_INACTIVE;
 
	this->client_id = _network_client_id++;
 
	this->receive_limit = _settings_client.network.bytes_per_frame_burst;
 

	
 
	/* The Socket and Info pools need to be the same in size. After all,
 
	 * each Socket will be associated with at most one Info object. As
 
	 * such if the Socket was allocated the Info object can as well. */
 
	static_assert(NetworkClientSocketPool::MAX_SIZE == NetworkClientInfoPool::MAX_SIZE);
 
}
 

	
 
/**
 
 * Clear everything related to this client.
 
 */
 
ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler()
 
{
 
	if (_redirect_console_to_client == this->client_id) _redirect_console_to_client = INVALID_CLIENT_ID;
 
	OrderBackup::ResetUser(this->client_id);
 

	
 
	if (this->savegame != nullptr) {
 
		this->savegame->Destroy();
 
		this->savegame = nullptr;
 
	}
 
}
 

	
 
Packet *ServerNetworkGameSocketHandler::ReceivePacket()
 
{
 
	/* Only allow receiving when we have some buffer free; this value
 
	 * can go negative, but eventually it will become positive again. */
 
	if (this->receive_limit <= 0) return nullptr;
 

	
 
	/* We can receive a packet, so try that and if needed account for
 
	 * the amount of received data. */
 
	Packet *p = this->NetworkTCPSocketHandler::ReceivePacket();
 
	if (p != nullptr) this->receive_limit -= p->Size();
 
	return p;
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
 
{
 
	assert(status != NETWORK_RECV_STATUS_OKAY);
 
	/*
 
	 * Sending a message just before leaving the game calls cs->SendPackets.
 
	 * This might invoke this function, which means that when we close the
 
	 * connection after cs->SendPackets we will close an already closed
 
	 * connection. This handles that case gracefully without having to make
 
	 * that code any more complex or more aware of the validity of the socket.
 
	 */
 
	if (this->sock == INVALID_SOCKET) return status;
 

	
 
	if (status != NETWORK_RECV_STATUS_CLIENT_QUIT && status != NETWORK_RECV_STATUS_SERVER_ERROR && !this->HasClientQuit() && this->status >= STATUS_AUTHORIZED) {
 
		/* 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()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_GAME_INFO, TCP_MTU);
 
	SerializeNetworkGameInfo(p, GetCurrentNetworkServerGameInfo());
 

	
 
	this->SendPacket(p);
 

	
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Send an error to the client, and close its connection.
 
 * @param error The error to disconnect for.
 
 * @param reason In case of kicking a client, specifies the reason for kicking the client.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendError(NetworkErrorCode error, const std::string &reason)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_ERROR);
 

	
 
	p->Send_uint8(error);
 
	if (!reason.empty()) p->Send_string(reason);
 
	this->SendPacket(p);
 

	
 
	StringID strid = GetNetworkErrorMsg(error);
 

	
 
	/* Only send when the current client was in game */
 
	if (this->status > STATUS_AUTHORIZED) {
 
		std::string client_name = this->GetClientName();
 

	
 
		Debug(net, 1, "'{}' made an error and has been disconnected: {}", client_name, GetString(strid));
 

	
 
		if (error == NETWORK_ERROR_KICKED && !reason.empty()) {
 
			NetworkTextMessage(NETWORK_ACTION_KICKED, CC_DEFAULT, false, client_name, reason, strid);
 
		} else {
 
			NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "", strid);
 
		}
 

	
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status >= STATUS_AUTHORIZED && new_cs != this) {
 
				/* Some errors we filter to a more general error. Clients don't have to know the real
 
				 *  reason a joining failed. */
 
				if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION) {
 
					error = NETWORK_ERROR_ILLEGAL_PACKET;
 
				}
 
				new_cs->SendErrorQuit(this->client_id, error);
 
			}
 
		}
 

	
 
		NetworkAdminClientError(this->client_id, error);
 
	} else {
 
		Debug(net, 1, "Client {} made an error and has been disconnected: {}", this->client_id, GetString(strid));
 
	}
 

	
 
	/* The client made a mistake, so drop the connection now! */
 
	return this->CloseConnection(NETWORK_RECV_STATUS_SERVER_ERROR);
 
}
 

	
 
/** Send the check for the NewGRFs. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_CHECK_NEWGRFS, TCP_MTU);
 
	const GRFConfig *c;
 
	uint grf_count = 0;
 

	
 
	for (c = _grfconfig; c != nullptr; c = c->next) {
 
		if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
 
	}
 

	
 
	p->Send_uint8 (grf_count);
 
	for (c = _grfconfig; c != nullptr; c = c->next) {
 
		if (!HasBit(c->flags, GCF_STATIC)) SerializeGRFIdentifier(p, &c->ident);
 
	}
 

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

	
 
/** Request the game password. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedGamePassword()
 
{
 
	/* Invalid packet when status is STATUS_AUTH_GAME or higher */
 
	if (this->status >= STATUS_AUTH_GAME) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
 

	
 
	this->status = STATUS_AUTH_GAME;
 
	/* Reset 'lag' counters */
 
	this->last_frame = this->last_frame_server = _frame_counter;
 

	
 
	Packet *p = new Packet(PACKET_SERVER_NEED_GAME_PASSWORD);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Request the company password. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNeedCompanyPassword()
 
{
 
	/* Invalid packet when status is STATUS_AUTH_COMPANY or higher */
 
	if (this->status >= STATUS_AUTH_COMPANY) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
 

	
 
	this->status = STATUS_AUTH_COMPANY;
 
	/* Reset 'lag' counters */
 
	this->last_frame = this->last_frame_server = _frame_counter;
 

	
 
	Packet *p = new Packet(PACKET_SERVER_NEED_COMPANY_PASSWORD);
 
	p->Send_uint32(_settings_game.game_creation.generation_seed);
 
	p->Send_string(_settings_client.network.network_id);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Send the client a welcome message with some basic information. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendWelcome()
 
{
 
	Packet *p;
 

	
 
	/* Invalid packet when status is AUTH or higher */
 
	if (this->status >= STATUS_AUTHORIZED) return this->CloseConnection(NETWORK_RECV_STATUS_MALFORMED_PACKET);
 

	
 
	this->status = STATUS_AUTHORIZED;
 
	/* Reset 'lag' counters */
 
	this->last_frame = this->last_frame_server = _frame_counter;
 

	
 
	_network_game_info.clients_on++;
 

	
 
	p = new Packet(PACKET_SERVER_WELCOME);
 
	p->Send_uint32(this->client_id);
 
	p->Send_uint32(_settings_game.game_creation.generation_seed);
 
	p->Send_string(_settings_client.network.network_id);
 
	this->SendPacket(p);
 

	
 
	/* Transmit info about all the active clients */
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs != this && new_cs->status >= STATUS_AUTHORIZED) {
 
			this->SendClientInfo(new_cs->GetInfo());
 
		}
 
	}
 
	/* Also send the info of the server */
 
	return this->SendClientInfo(NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER));
 
}
 

	
 
/** Tell the client that its put in a waiting queue. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendWait()
 
{
 
	int waiting = 1; // current player getting the map counts as 1
 
	Packet *p;
 

	
 
	/* Count how many clients are waiting in the queue, in front of you! */
 
@@ -616,384 +616,389 @@ NetworkRecvStatus ServerNetworkGameSocke
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Request the client to sync. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendSync()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_SYNC);
 
	p->Send_uint32(_frame_counter);
 
	p->Send_uint32(_sync_seed_1);
 

	
 
#ifdef NETWORK_SEND_DOUBLE_SEED
 
	p->Send_uint32(_sync_seed_2);
 
#endif
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Send a command to the client to execute.
 
 * @param cp The command to send.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCommand(const CommandPacket *cp)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_COMMAND);
 

	
 
	this->NetworkGameSocketHandler::SendCommand(p, cp);
 
	p->Send_uint32(cp->frame);
 
	p->Send_bool  (cp->my_cmd);
 

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

	
 
/**
 
 * Send a chat message.
 
 * @param action The action associated with the message.
 
 * @param client_id The origin of the chat message.
 
 * @param self_send Whether we did send the message.
 
 * @param msg The actual message.
 
 * @param data Arbitrary extra data.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendChat(NetworkAction action, ClientID client_id, bool self_send, const std::string &msg, int64 data)
 
{
 
	if (this->status < STATUS_PRE_ACTIVE) return NETWORK_RECV_STATUS_OKAY;
 

	
 
	Packet *p = new Packet(PACKET_SERVER_CHAT);
 

	
 
	p->Send_uint8 (action);
 
	p->Send_uint32(client_id);
 
	p->Send_bool  (self_send);
 
	p->Send_string(msg);
 
	p->Send_uint64(data);
 

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

	
 
/**
 
 * Tell the client another client quit with an error.
 
 * @param client_id The client that quit.
 
 * @param errorno The reason the client quit.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendErrorQuit(ClientID client_id, NetworkErrorCode errorno)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_ERROR_QUIT);
 

	
 
	p->Send_uint32(client_id);
 
	p->Send_uint8 (errorno);
 

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

	
 
/**
 
 * Tell the client another client quit.
 
 * @param client_id The client that quit.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendQuit(ClientID client_id)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_QUIT);
 

	
 
	p->Send_uint32(client_id);
 

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

	
 
/** Tell the client we're shutting down. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendShutdown()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_SHUTDOWN);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Tell the client we're starting a new game. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGame()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_NEWGAME);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Send the result of a console action.
 
 * @param colour The colour of the result.
 
 * @param command The command that was executed.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendRConResult(uint16 colour, const std::string &command)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_RCON);
 

	
 
	p->Send_uint16(colour);
 
	p->Send_string(command);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/**
 
 * Tell that a client moved to another company.
 
 * @param client_id The client that moved.
 
 * @param company_id The company the client moved to.
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendMove(ClientID client_id, CompanyID company_id)
 
{
 
	Packet *p = new Packet(PACKET_SERVER_MOVE);
 

	
 
	p->Send_uint32(client_id);
 
	p->Send_uint8(company_id);
 
	this->SendPacket(p);
 
	return NETWORK_RECV_STATUS_OKAY;
 
}
 

	
 
/** Send an update about the company password states. */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::SendCompanyUpdate()
 
{
 
	Packet *p = new Packet(PACKET_SERVER_COMPANY_UPDATE);
 

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

	
 
/** Send an update about the max company/spectator counts. */
 
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;
 
	Debug(desync, 1, "client: {:08x}; {:02x}; {:02x}; {:02x}", _date, _date_fract, (int)ci->client_playas, (int)ci->index);
 

	
 
	/* Make sure companies to which people try to join are not autocleaned */
 
	if (Company::IsValidID(playas)) _network_company_states[playas].months_empty = 0;
 

	
 
	this->status = STATUS_NEWGRFS_CHECK;
 

	
 
	if (_grfconfig == nullptr) {
 
		/* Behave as if we received PACKET_CLIENT_NEWGRFS_CHECKED */
 
		return this->Receive_CLIENT_NEWGRFS_CHECKED(nullptr);
 
	}
 

	
 
	return this->SendNewGRFCheck();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GAME_PASSWORD(Packet *p)
 
{
 
	if (this->status != STATUS_AUTH_GAME) {
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
 

	
 
	/* Check game password. Allow joining if we cleared the password meanwhile */
 
	if (!_settings_client.network.server_password.empty() &&
 
			_settings_client.network.server_password.compare(password) != 0) {
 
		/* Password is invalid */
 
		return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
 
	}
 

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

	
 
	/* Valid password, allow user */
 
	return this->SendWelcome();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMPANY_PASSWORD(Packet *p)
 
{
 
	if (this->status != STATUS_AUTH_COMPANY) {
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	std::string password = p->Recv_string(NETWORK_PASSWORD_LENGTH);
 

	
 
	/* Check company password. Allow joining if we cleared the password meanwhile.
 
	 * Also, check the company is still valid - client could be moved to spectators
 
	 * in the middle of the authorization process */
 
	CompanyID playas = this->GetInfo()->client_playas;
 
	if (Company::IsValidID(playas) && !_network_company_states[playas].password.empty() &&
 
			_network_company_states[playas].password.compare(password) != 0) {
 
		/* Password is invalid */
 
		return this->SendError(NETWORK_ERROR_WRONG_PASSWORD);
 
	}
 

	
 
	return this->SendWelcome();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_GETMAP(Packet *p)
 
{
 
	/* The client was never joined.. so this is impossible, right?
 
	 *  Ignore the packet, give the client a warning, and close the connection */
 
	if (this->status < STATUS_AUTHORIZED || this->HasClientQuit()) {
 
		return this->SendError(NETWORK_ERROR_NOT_AUTHORIZED);
 
	}
 

	
 
	/* Check if someone else is receiving the map */
 
	for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
		if (new_cs->status == STATUS_MAP) {
 
			/* Tell the new client to wait */
 
			this->status = STATUS_MAP_WAIT;
 
			return this->SendWait();
 
		}
 
	}
 

	
 
	/* We receive a request to upload the map.. give it to the client! */
 
	return this->SendMap();
 
}
 

	
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet *p)
 
{
 
	/* Client has the map, now start syncing */
 
	if (this->status == STATUS_DONE_MAP && !this->HasClientQuit()) {
 
		std::string client_name = this->GetClientName();
 

	
 
		NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, "", this->client_id);
 
		InvalidateWindowData(WC_CLIENT_LIST, 0);
 

	
 
		/* Mark the client as pre-active, and wait for an ACK
 
		 *  so we know it is done loading and in sync with us */
 
		this->status = STATUS_PRE_ACTIVE;
 
		NetworkHandleCommandQueue(this);
 
		this->SendFrame();
 
		this->SendSync();
 

	
 
		/* This is the frame the client receives
 
		 *  we need it later on to make sure the client is not too slow */
 
		this->last_frame = _frame_counter;
 
		this->last_frame_server = _frame_counter;
 

	
 
		for (NetworkClientSocket *new_cs : NetworkClientSocket::Iterate()) {
 
			if (new_cs->status >= STATUS_AUTHORIZED) {
 
				new_cs->SendClientInfo(this->GetInfo());
 
				new_cs->SendJoin(this->client_id);
 
			}
 
		}
 

	
 
		NetworkAdminClientInfo(this, true);
 

	
 
		/* also update the new client with our max values */
 
		this->SendConfigUpdate();
 

	
 
		/* quickly update the syncing client with company details */
 
		return this->SendCompanyUpdate();
 
	}
 

	
 
	/* Wrong status for this packet, give a warning to client, and close connection */
 
	return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
}
 

	
 
/**
 
 * The client has done a command and wants us to handle it
 
 * @param p the packet in which the command was sent
 
 */
 
NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet *p)
 
{
 
	/* The client was never joined.. so this is impossible, right?
 
	 *  Ignore the packet, give the client a warning, and close the connection */
 
	if (this->status < STATUS_DONE_MAP || this->HasClientQuit()) {
 
		return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
 
	}
 

	
 
	if (this->incoming_queue.Count() >= _settings_client.network.max_commands_in_queue) {
 
		return this->SendError(NETWORK_ERROR_TOO_MANY_COMMANDS);
 
	}
 

	
 
	CommandPacket cp;
 
	const char *err = this->ReceiveCommand(p, &cp);
 

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

	
 
	NetworkClientInfo *ci = this->GetInfo();
0 comments (0 inline, 0 general)