Files @ r26005:fa9fad1bd9d6
Branch filter:

Location: cpp/openttd-patchpack/source/src/network/network_command.cpp - annotation

Patric Stout
Add: [Network] Keep the refresh button in lowered state while refreshing (#9600)

This gives user visual feedback that the refresh is still pending, and
prevents people from clicking again and again thinking nothing is
happening. This is especially true for connections that fall back to
TURN, as that takes a few seconds to kick in.

Additionally, prevent clicking on the button again while a refresh
is pending. This is only delaying a successful result.
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r10622:217e6d23c755
r10618:994cb635f2d9
r10618:994cb635f2d9
r16923:67617ddad8d5
r10618:994cb635f2d9
r16224:976f6278a22b
r10618:994cb635f2d9
r10618:994cb635f2d9
r15861:5e89486368d2
r10618:994cb635f2d9
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r14221:ad11f2ede08b
r14248:a9050881acd7
r23607:36c15679007d
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r24858:bfbc5d118b1d
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r22376:332c148bca52
r24858:bfbc5d118b1d
r24858:bfbc5d118b1d
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r24582:1447201ea3e3
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r14638:c17bbb7e1736
r14822:bdb7fc47ed89
r15078:76a51dd49982
r18746:ffe36c655ceb
r19236:ce39f6a41576
r14221:ad11f2ede08b
r14221:ad11f2ede08b
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15857:85cb6548cb26
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r25563:ffc7b5a68ed9
r15857:85cb6548cb26
r23607:36c15679007d
r23607:36c15679007d
r15857:85cb6548cb26
r15821:61bc8d5f967c
r15857:85cb6548cb26
r15821:61bc8d5f967c
r15857:85cb6548cb26
r15861:5e89486368d2
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r16687:e1dc9ca6065f
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r16687:e1dc9ca6065f
r15821:61bc8d5f967c
r16687:e1dc9ca6065f
r15821:61bc8d5f967c
r23607:36c15679007d
r16687:e1dc9ca6065f
r23607:36c15679007d
r17367:79d87495caea
r16687:e1dc9ca6065f
r16687:e1dc9ca6065f
r16687:e1dc9ca6065f
r16687:e1dc9ca6065f
r23607:36c15679007d
r17367:79d87495caea
r16687:e1dc9ca6065f
r15861:5e89486368d2
r15861:5e89486368d2
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r16687:e1dc9ca6065f
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r16687:e1dc9ca6065f
r15821:61bc8d5f967c
r16687:e1dc9ca6065f
r16687:e1dc9ca6065f
r23607:36c15679007d
r16687:e1dc9ca6065f
r16687:e1dc9ca6065f
r23607:36c15679007d
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r23607:36c15679007d
r25563:ffc7b5a68ed9
r15821:61bc8d5f967c
r15861:5e89486368d2
r15821:61bc8d5f967c
r15821:61bc8d5f967c
r15857:85cb6548cb26
r15857:85cb6548cb26
r15856:b6687b358c92
r15856:b6687b358c92
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r14223:fd89464d3da0
r10618:994cb635f2d9
r25562:30716ba6a396
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r14223:fd89464d3da0
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r25563:ffc7b5a68ed9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10623:f0fe12cafe3a
r10618:994cb635f2d9
r15857:85cb6548cb26
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r16214:7fd822f2f61a
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r23607:36c15679007d
r14998:f9ef525ede5f
r25707:226494aad20a
r15857:85cb6548cb26
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r14998:f9ef525ede5f
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r15596:0df53125af8a
r15284:a975e22a01ee
r16224:976f6278a22b
r15857:85cb6548cb26
r15821:61bc8d5f967c
r23607:36c15679007d
r10618:994cb635f2d9
r10618:994cb635f2d9
r15821:61bc8d5f967c
r10618:994cb635f2d9
r15821:61bc8d5f967c
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r10624:44138dbb3250
r10624:44138dbb3250
r10624:44138dbb3250
r10618:994cb635f2d9
r15857:85cb6548cb26
r25563:ffc7b5a68ed9
r10618:994cb635f2d9
r15284:a975e22a01ee
r15284:a975e22a01ee
r15284:a975e22a01ee
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r17365:565ae23af76e
r10618:994cb635f2d9
r10618:994cb635f2d9
r10618:994cb635f2d9
r17365:565ae23af76e
r15856:b6687b358c92
r10618:994cb635f2d9
r10618:994cb635f2d9
r10623:f0fe12cafe3a
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r20727:8b7b2fb2c00e
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r23964:f1693194d4bf
r16312:cb5c5460a42f
r15857:85cb6548cb26
r15857:85cb6548cb26
r23607:36c15679007d
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r23964:f1693194d4bf
r23964:f1693194d4bf
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r18956:9f158be2c017
r18956:9f158be2c017
r18977:1c3e0e63f950
r18956:9f158be2c017
r15861:5e89486368d2
r18956:9f158be2c017
r15861:5e89486368d2
r15857:85cb6548cb26
r23607:36c15679007d
r15857:85cb6548cb26
r16923:67617ddad8d5
r25563:ffc7b5a68ed9
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r17629:21e9dfd343cd
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r23607:36c15679007d
r15857:85cb6548cb26
r15857:85cb6548cb26
r23964:f1693194d4bf
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r15857:85cb6548cb26
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r23607:36c15679007d
r10623:f0fe12cafe3a
r16618:d89f0c3f42e7
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r18745:f30e2b3b8a58
r24695:05e093ea8aec
r18745:f30e2b3b8a58
r18745:f30e2b3b8a58
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r25563:ffc7b5a68ed9
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r18020:2cc4cd085970
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r23607:36c15679007d
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r16618:d89f0c3f42e7
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r14221:ad11f2ede08b
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r14221:ad11f2ede08b
r25655:1030dcb7eb52
r23607:36c15679007d
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
r10623:f0fe12cafe3a
/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file network_command.cpp Command handling over network connections. */

#include "../stdafx.h"
#include "network_admin.h"
#include "network_client.h"
#include "network_server.h"
#include "../command_func.h"
#include "../company_func.h"
#include "../settings_type.h"

#include "../safeguards.h"

/** Table with all the callbacks we'll use for conversion*/
static CommandCallback * const _callback_table[] = {
	/* 0x00 */ nullptr,
	/* 0x01 */ CcBuildPrimaryVehicle,
	/* 0x02 */ CcBuildAirport,
	/* 0x03 */ CcBuildBridge,
	/* 0x04 */ CcPlaySound_CONSTRUCTION_WATER,
	/* 0x05 */ CcBuildDocks,
	/* 0x06 */ CcFoundTown,
	/* 0x07 */ CcBuildRoadTunnel,
	/* 0x08 */ CcBuildRailTunnel,
	/* 0x09 */ CcBuildWagon,
	/* 0x0A */ CcRoadDepot,
	/* 0x0B */ CcRailDepot,
	/* 0x0C */ CcPlaceSign,
	/* 0x0D */ CcPlaySound_EXPLOSION,
	/* 0x0E */ CcPlaySound_CONSTRUCTION_OTHER,
	/* 0x0F */ CcPlaySound_CONSTRUCTION_RAIL,
	/* 0x10 */ CcStation,
	/* 0x11 */ CcTerraform,
	/* 0x12 */ CcAI,
	/* 0x13 */ CcCloneVehicle,
	/* 0x14 */ nullptr,
	/* 0x15 */ CcCreateGroup,
	/* 0x16 */ CcFoundRandomTown,
	/* 0x17 */ CcRoadStop,
	/* 0x18 */ CcBuildIndustry,
	/* 0x19 */ CcStartStopVehicle,
	/* 0x1A */ CcGame,
	/* 0x1B */ CcAddVehicleNewGroup,
};

/**
 * Append a CommandPacket at the end of the queue.
 * @param p The packet to append to the queue.
 * @note A new instance of the CommandPacket will be made.
 */
void CommandQueue::Append(CommandPacket *p)
{
	CommandPacket *add = new CommandPacket();
	*add = *p;
	add->next = nullptr;
	if (this->first == nullptr) {
		this->first = add;
	} else {
		this->last->next = add;
	}
	this->last = add;
	this->count++;
}

/**
 * Return the first item in the queue and remove it from the queue.
 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
 * @return the first item in the queue.
 */
CommandPacket *CommandQueue::Pop(bool ignore_paused)
{
	CommandPacket **prev = &this->first;
	CommandPacket *ret = this->first;
	CommandPacket *prev_item = nullptr;
	if (ignore_paused && _pause_mode != PM_UNPAUSED) {
		while (ret != nullptr && !IsCommandAllowedWhilePaused(ret->cmd)) {
			prev_item = ret;
			prev = &ret->next;
			ret = ret->next;
		}
	}
	if (ret != nullptr) {
		if (ret == this->last) this->last = prev_item;
		*prev = ret->next;
		this->count--;
	}
	return ret;
}

/**
 * Return the first item in the queue, but don't remove it.
 * @param ignore_paused Whether to ignore commands that may not be executed while paused.
 * @return the first item in the queue.
 */
CommandPacket *CommandQueue::Peek(bool ignore_paused)
{
	if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;

	for (CommandPacket *p = this->first; p != nullptr; p = p->next) {
		if (IsCommandAllowedWhilePaused(p->cmd)) return p;
	}
	return nullptr;
}

/** Free everything that is in the queue. */
void CommandQueue::Free()
{
	CommandPacket *cp;
	while ((cp = this->Pop()) != nullptr) {
		delete cp;
	}
	assert(this->count == 0);
}

/** Local queue of packets waiting for handling. */
static CommandQueue _local_wait_queue;
/** Local queue of packets waiting for execution. */
static CommandQueue _local_execution_queue;

/**
 * Prepare a DoCommand to be send over the network
 * @param tile The tile to perform a command on (see #CommandProc)
 * @param p1 Additional data for the command (see #CommandProc)
 * @param p2 Additional data for the command (see #CommandProc)
 * @param cmd The command to execute (a CMD_* value)
 * @param callback A callback function to call after the command is finished
 * @param text The text to pass
 * @param company The company that wants to send the command
 */
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company)
{
	assert((cmd & CMD_FLAGS_MASK) == 0);

	CommandPacket c;
	c.company  = company;
	c.tile     = tile;
	c.p1       = p1;
	c.p2       = p2;
	c.cmd      = cmd;
	c.callback = callback;
	c.text     = text;

	if (_network_server) {
		/* If we are the server, we queue the command in our 'special' queue.
		 *   In theory, we could execute the command right away, but then the
		 *   client on the server can do everything 1 tick faster than others.
		 *   So to keep the game fair, we delay the command with 1 tick
		 *   which gives about the same speed as most clients.
		 */
		c.frame = _frame_counter_max + 1;
		c.my_cmd = true;

		_local_wait_queue.Append(&c);
		return;
	}

	c.frame = 0; // The client can't tell which frame, so just make it 0

	/* Clients send their command to the server and forget all about the packet */
	MyClient::SendCommand(&c);
}

/**
 * Sync our local command queue to the command queue of the given
 * socket. This is needed for the case where we receive a command
 * before saving the game for a joining client, but without the
 * execution of those commands. Not syncing those commands means
 * that the client will never get them and as such will be in a
 * desynced state from the time it started with joining.
 * @param cs The client to sync the queue to.
 */
void NetworkSyncCommandQueue(NetworkClientSocket *cs)
{
	for (CommandPacket *p = _local_execution_queue.Peek(); p != nullptr; p = p->next) {
		CommandPacket c = *p;
		c.callback = nullptr;
		cs->outgoing_queue.Append(&c);
	}
}

/**
 * Execute all commands on the local command queue that ought to be executed this frame.
 */
void NetworkExecuteLocalCommandQueue()
{
	assert(IsLocalCompany());

	CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);

	CommandPacket *cp;
	while ((cp = queue.Peek()) != nullptr) {
		/* The queue is always in order, which means
		 * that the first element will be executed first. */
		if (_frame_counter < cp->frame) break;

		if (_frame_counter > cp->frame) {
			/* If we reach here, it means for whatever reason, we've already executed
			 * past the command we need to execute. */
			error("[net] Trying to execute a packet in the past!");
		}

		/* We can execute this command */
		_current_company = cp->company;
		cp->cmd |= CMD_NETWORK_COMMAND;
		DoCommandP(cp, cp->my_cmd);

		queue.Pop();
		delete cp;
	}

	/* Local company may have changed, so we should not restore the old value */
	_current_company = _local_company;
}

/**
 * Free the local command queues.
 */
void NetworkFreeLocalCommandQueue()
{
	_local_wait_queue.Free();
	_local_execution_queue.Free();
}

/**
 * "Send" a particular CommandPacket to all clients.
 * @param cp    The command that has to be distributed.
 * @param owner The client that owns the command,
 */
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
{
	CommandCallback *callback = cp.callback;
	cp.frame = _frame_counter_max + 1;

	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
		if (cs->status >= NetworkClientSocket::STATUS_MAP) {
			/* Callbacks are only send back to the client who sent them in the
			 *  first place. This filters that out. */
			cp.callback = (cs != owner) ? nullptr : callback;
			cp.my_cmd = (cs == owner);
			cs->outgoing_queue.Append(&cp);
		}
	}

	cp.callback = (nullptr != owner) ? nullptr : callback;
	cp.my_cmd = (nullptr == owner);
	_local_execution_queue.Append(&cp);
}

/**
 * "Send" a particular CommandQueue to all clients.
 * @param queue The queue of commands that has to be distributed.
 * @param owner The client that owns the commands,
 */
static void DistributeQueue(CommandQueue *queue, const NetworkClientSocket *owner)
{
#ifdef DEBUG_DUMP_COMMANDS
	/* When replaying we do not want this limitation. */
	int to_go = UINT16_MAX;
#else
	int to_go = _settings_client.network.commands_per_frame;
#endif

	CommandPacket *cp;
	while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) {
		DistributeCommandPacket(*cp, owner);
		NetworkAdminCmdLogging(owner, cp);
		delete cp;
	}
}

/** Distribute the commands of ourself and the clients. */
void NetworkDistributeCommands()
{
	/* First send the server's commands. */
	DistributeQueue(&_local_wait_queue, nullptr);

	/* Then send the queues of the others. */
	for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
		DistributeQueue(&cs->incoming_queue, cs);
	}
}

/**
 * Receives a command from the network.
 * @param p the packet to read from.
 * @param cp the struct to write the data to.
 * @return an error message. When nullptr there has been no error.
 */
const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
{
	cp->company = (CompanyID)p->Recv_uint8();
	cp->cmd     = p->Recv_uint32();
	if (!IsValidCommand(cp->cmd))               return "invalid command";
	if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
	if ((cp->cmd & CMD_FLAGS_MASK) != 0)        return "invalid command flag";

	cp->p1      = p->Recv_uint32();
	cp->p2      = p->Recv_uint32();
	cp->tile    = p->Recv_uint32();
	cp->text    = p->Recv_string(NETWORK_COMPANY_NAME_LENGTH, (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);

	byte callback = p->Recv_uint8();
	if (callback >= lengthof(_callback_table))  return "invalid callback";

	cp->callback = _callback_table[callback];
	return nullptr;
}

/**
 * Sends a command over the network.
 * @param p the packet to send it in.
 * @param cp the packet to actually send.
 */
void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
{
	p->Send_uint8 (cp->company);
	p->Send_uint32(cp->cmd);
	p->Send_uint32(cp->p1);
	p->Send_uint32(cp->p2);
	p->Send_uint32(cp->tile);
	p->Send_string(cp->text);

	byte callback = 0;
	while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
		callback++;
	}

	if (callback == lengthof(_callback_table)) {
		Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp->cmd);
		callback = 0; // _callback_table[0] == nullptr
	}
	p->Send_uint8 (callback);
}