2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-08 14:38:21 +00:00
|
|
|
/** @file network_command.cpp Command handling over network connections. */
|
2008-05-06 15:11:33 +00:00
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "../stdafx.h"
|
2010-12-30 18:14:37 +00:00
|
|
|
#include "network_admin.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network_client.h"
|
2010-10-15 19:58:56 +00:00
|
|
|
#include "network_server.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "../command_func.h"
|
2022-12-07 20:37:23 +00:00
|
|
|
#include "../command_aux.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "../company_func.h"
|
2010-08-19 08:59:36 +00:00
|
|
|
#include "../settings_type.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2010-01-11 20:00:14 +00:00
|
|
|
/** Table with all the callbacks we'll use for conversion*/
|
2010-01-15 16:41:15 +00:00
|
|
|
static CommandCallback * const _callback_table[] = {
|
2019-04-10 21:07:06 +00:00
|
|
|
/* 0x00 */ nullptr,
|
2010-01-11 20:00:14 +00:00
|
|
|
/* 0x01 */ CcBuildPrimaryVehicle,
|
|
|
|
/* 0x02 */ CcBuildAirport,
|
|
|
|
/* 0x03 */ CcBuildBridge,
|
2021-02-20 18:01:04 +00:00
|
|
|
/* 0x04 */ CcPlaySound_CONSTRUCTION_WATER,
|
2010-01-11 20:00:14 +00:00
|
|
|
/* 0x05 */ CcBuildDocks,
|
|
|
|
/* 0x06 */ CcFoundTown,
|
|
|
|
/* 0x07 */ CcBuildRoadTunnel,
|
|
|
|
/* 0x08 */ CcBuildRailTunnel,
|
|
|
|
/* 0x09 */ CcBuildWagon,
|
|
|
|
/* 0x0A */ CcRoadDepot,
|
|
|
|
/* 0x0B */ CcRailDepot,
|
|
|
|
/* 0x0C */ CcPlaceSign,
|
2016-05-22 10:07:48 +00:00
|
|
|
/* 0x0D */ CcPlaySound_EXPLOSION,
|
2021-02-20 18:01:04 +00:00
|
|
|
/* 0x0E */ CcPlaySound_CONSTRUCTION_OTHER,
|
|
|
|
/* 0x0F */ CcPlaySound_CONSTRUCTION_RAIL,
|
2010-01-11 20:00:14 +00:00
|
|
|
/* 0x10 */ CcStation,
|
|
|
|
/* 0x11 */ CcTerraform,
|
|
|
|
/* 0x12 */ CcAI,
|
|
|
|
/* 0x13 */ CcCloneVehicle,
|
|
|
|
/* 0x14 */ CcGiveMoney,
|
|
|
|
/* 0x15 */ CcCreateGroup,
|
|
|
|
/* 0x16 */ CcFoundRandomTown,
|
2010-02-24 21:45:23 +00:00
|
|
|
/* 0x17 */ CcRoadStop,
|
2010-03-14 19:59:45 +00:00
|
|
|
/* 0x18 */ CcBuildIndustry,
|
2010-04-24 20:55:51 +00:00
|
|
|
/* 0x19 */ CcStartStopVehicle,
|
2011-12-19 20:50:54 +00:00
|
|
|
/* 0x1A */ CcGame,
|
2012-04-17 19:44:16 +00:00
|
|
|
/* 0x1B */ CcAddVehicleNewGroup,
|
2015-08-02 18:37:42 +00:00
|
|
|
/* 0x1C */ CcAddPlan,
|
2016-02-14 17:55:51 +00:00
|
|
|
/* 0x1D */ CcSetVirtualTrain,
|
2017-01-21 01:44:05 +00:00
|
|
|
/* 0x1E */ CcVirtualTrainWagonsMoved,
|
2016-02-14 17:55:51 +00:00
|
|
|
/* 0x1F */ CcDeleteVirtualTrain,
|
|
|
|
/* 0x20 */ CcAddVirtualEngine,
|
2020-09-08 22:56:38 +00:00
|
|
|
/* 0x21 */ CcMoveNewVirtualEngine,
|
2022-04-18 16:56:12 +00:00
|
|
|
/* 0x22 */ CcAddNewSchDispatchSchedule,
|
2010-01-11 20:00:14 +00:00
|
|
|
};
|
|
|
|
|
2010-08-15 23:44:45 +00:00
|
|
|
/**
|
|
|
|
* Append a CommandPacket at the end of the queue.
|
|
|
|
* @param p The packet to append to the queue.
|
2010-08-18 22:40:17 +00:00
|
|
|
* @note A new instance of the CommandPacket will be made.
|
2010-08-15 23:44:45 +00:00
|
|
|
*/
|
2018-08-13 11:16:41 +00:00
|
|
|
void CommandQueue::Append(CommandPacket *p, bool move)
|
2010-08-15 23:44:45 +00:00
|
|
|
{
|
2018-08-13 11:16:41 +00:00
|
|
|
CommandPacket *add = new CommandPacket();
|
|
|
|
if (move) {
|
|
|
|
*add = std::move(*p);
|
|
|
|
} else {
|
|
|
|
*add = *p;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
add->next = nullptr;
|
|
|
|
if (this->first == nullptr) {
|
2010-08-18 22:40:17 +00:00
|
|
|
this->first = add;
|
2010-08-15 23:44:45 +00:00
|
|
|
} else {
|
2010-08-18 22:40:17 +00:00
|
|
|
this->last->next = add;
|
2010-08-15 23:44:45 +00:00
|
|
|
}
|
2010-08-18 22:40:17 +00:00
|
|
|
this->last = add;
|
2010-08-19 08:59:36 +00:00
|
|
|
this->count++;
|
2010-08-15 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the first item in the queue and remove it from the queue.
|
2010-12-07 21:12:23 +00:00
|
|
|
* @param ignore_paused Whether to ignore commands that may not be executed while paused.
|
2010-08-15 23:44:45 +00:00
|
|
|
* @return the first item in the queue.
|
|
|
|
*/
|
2018-08-13 11:16:41 +00:00
|
|
|
std::unique_ptr<CommandPacket> CommandQueue::Pop(bool ignore_paused)
|
2010-08-15 23:44:45 +00:00
|
|
|
{
|
2018-08-13 11:16:41 +00:00
|
|
|
|
2010-12-07 21:12:23 +00:00
|
|
|
CommandPacket **prev = &this->first;
|
2010-08-15 23:44:45 +00:00
|
|
|
CommandPacket *ret = this->first;
|
2019-04-10 21:07:06 +00:00
|
|
|
CommandPacket *prev_item = nullptr;
|
2010-12-07 21:12:23 +00:00
|
|
|
if (ignore_paused && _pause_mode != PM_UNPAUSED) {
|
2019-04-10 21:07:06 +00:00
|
|
|
while (ret != nullptr && !IsCommandAllowedWhilePaused(ret->cmd)) {
|
2011-02-20 21:00:24 +00:00
|
|
|
prev_item = ret;
|
2010-12-07 21:12:23 +00:00
|
|
|
prev = &ret->next;
|
|
|
|
ret = ret->next;
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
if (ret != nullptr) {
|
2011-02-20 21:00:24 +00:00
|
|
|
if (ret == this->last) this->last = prev_item;
|
2010-12-07 21:12:23 +00:00
|
|
|
*prev = ret->next;
|
2010-08-19 08:59:36 +00:00
|
|
|
this->count--;
|
|
|
|
}
|
2018-08-13 11:16:41 +00:00
|
|
|
return std::unique_ptr<CommandPacket>(ret);
|
2010-08-15 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the first item in the queue, but don't remove it.
|
2010-12-07 21:12:23 +00:00
|
|
|
* @param ignore_paused Whether to ignore commands that may not be executed while paused.
|
2010-08-15 23:44:45 +00:00
|
|
|
* @return the first item in the queue.
|
|
|
|
*/
|
2010-12-07 21:12:23 +00:00
|
|
|
CommandPacket *CommandQueue::Peek(bool ignore_paused)
|
2010-08-15 23:44:45 +00:00
|
|
|
{
|
2010-12-07 21:12:23 +00:00
|
|
|
if (!ignore_paused || _pause_mode == PM_UNPAUSED) return this->first;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
for (CommandPacket *p = this->first; p != nullptr; p = p->next) {
|
2010-12-07 21:12:23 +00:00
|
|
|
if (IsCommandAllowedWhilePaused(p->cmd)) return p;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2010-08-15 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Free everything that is in the queue. */
|
|
|
|
void CommandQueue::Free()
|
|
|
|
{
|
2018-08-13 11:16:41 +00:00
|
|
|
std::unique_ptr<CommandPacket> cp;
|
2019-04-11 17:14:13 +00:00
|
|
|
while ((cp = this->Pop()) != nullptr) {}
|
2010-08-19 08:59:36 +00:00
|
|
|
assert(this->count == 0);
|
2010-08-15 23:44:45 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 22:40:17 +00:00
|
|
|
/** Local queue of packets waiting for handling. */
|
|
|
|
static CommandQueue _local_wait_queue;
|
2010-08-18 21:30:38 +00:00
|
|
|
/** Local queue of packets waiting for execution. */
|
|
|
|
static CommandQueue _local_execution_queue;
|
2009-01-08 13:57:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
2020-10-17 13:42:46 +00:00
|
|
|
* @param p3 Additional data for the command (see #CommandProc)
|
2009-01-08 13:57:50 +00:00
|
|
|
* @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
|
2010-01-11 20:32:32 +00:00
|
|
|
* @param company The company that wants to send the command
|
2022-12-07 20:37:23 +00:00
|
|
|
* @param aux_data Auxiliary command data
|
2009-01-08 13:57:50 +00:00
|
|
|
*/
|
2022-12-07 20:37:23 +00:00
|
|
|
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint64 p3, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company, const CommandAuxiliaryBase *aux_data)
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
2009-01-07 15:36:37 +00:00
|
|
|
assert((cmd & CMD_FLAGS_MASK) == 0);
|
2009-01-07 15:27:19 +00:00
|
|
|
|
2007-06-10 19:59:08 +00:00
|
|
|
CommandPacket c;
|
2010-01-11 20:32:32 +00:00
|
|
|
c.company = company;
|
2009-01-08 14:40:18 +00:00
|
|
|
c.tile = tile;
|
|
|
|
c.p1 = p1;
|
|
|
|
c.p2 = p2;
|
2020-10-17 13:42:46 +00:00
|
|
|
c.p3 = p3;
|
2009-01-08 14:40:18 +00:00
|
|
|
c.cmd = cmd;
|
|
|
|
c.callback = callback;
|
2022-12-07 20:37:23 +00:00
|
|
|
if (aux_data != nullptr) c.aux_data.reset(aux_data->Clone());
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2022-12-07 20:37:23 +00:00
|
|
|
if (text != nullptr) {
|
|
|
|
c.text.assign(text);
|
2015-08-02 18:37:42 +00:00
|
|
|
} else {
|
2022-12-07 20:37:23 +00:00
|
|
|
c.text.clear();
|
2015-08-02 18:37:42 +00:00
|
|
|
}
|
2007-01-02 17:34:03 +00:00
|
|
|
|
|
|
|
if (_network_server) {
|
2007-06-10 19:59:08 +00:00
|
|
|
/* 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;
|
2009-01-08 14:40:18 +00:00
|
|
|
c.my_cmd = true;
|
2007-06-10 19:59:08 +00:00
|
|
|
|
2018-08-13 11:16:41 +00:00
|
|
|
_local_wait_queue.Append(std::move(c));
|
2007-01-02 17:34:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-10 19:59:08 +00:00
|
|
|
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 */
|
2010-10-15 14:29:13 +00:00
|
|
|
MyClient::SendCommand(&c);
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
2010-04-13 18:55:31 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
for (CommandPacket *p = _local_execution_queue.Peek(); p != nullptr; p = p->next) {
|
2010-04-13 18:55:31 +00:00
|
|
|
CommandPacket c = *p;
|
2021-06-16 19:12:08 +00:00
|
|
|
c.callback = nullptr;
|
2018-08-13 11:16:41 +00:00
|
|
|
cs->outgoing_queue.Append(std::move(c));
|
2010-04-13 18:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-08 13:57:50 +00:00
|
|
|
/**
|
|
|
|
* Execute all commands on the local command queue that ought to be executed this frame.
|
|
|
|
*/
|
|
|
|
void NetworkExecuteLocalCommandQueue()
|
|
|
|
{
|
2022-10-15 20:11:36 +00:00
|
|
|
extern ClientID _cmd_client_id;
|
2010-07-31 21:43:07 +00:00
|
|
|
assert(IsLocalCompany());
|
2010-06-05 13:32:42 +00:00
|
|
|
|
2010-10-15 19:58:56 +00:00
|
|
|
CommandQueue &queue = (_network_server ? _local_execution_queue : ClientNetworkGameSocketHandler::my_client->incoming_queue);
|
2010-08-18 22:40:17 +00:00
|
|
|
|
2010-08-15 23:44:45 +00:00
|
|
|
CommandPacket *cp;
|
2019-04-10 21:07:06 +00:00
|
|
|
while ((cp = queue.Peek()) != nullptr) {
|
2009-01-08 13:57:50 +00:00
|
|
|
/* The queue is always in order, which means
|
|
|
|
* that the first element will be executed first. */
|
2010-08-15 23:44:45 +00:00
|
|
|
if (_frame_counter < cp->frame) break;
|
2009-01-08 13:57:50 +00:00
|
|
|
|
2010-08-15 23:44:45 +00:00
|
|
|
if (_frame_counter > cp->frame) {
|
2009-01-08 13:57:50 +00:00
|
|
|
/* 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 */
|
2009-01-08 14:55:28 +00:00
|
|
|
_current_company = cp->company;
|
2022-10-15 20:11:36 +00:00
|
|
|
_cmd_client_id = cp->client_id;
|
2009-01-08 14:55:28 +00:00
|
|
|
cp->cmd |= CMD_NETWORK_COMMAND;
|
|
|
|
DoCommandP(cp, cp->my_cmd);
|
2009-01-08 13:57:50 +00:00
|
|
|
|
2010-08-18 22:40:17 +00:00
|
|
|
queue.Pop();
|
2009-01-08 13:57:50 +00:00
|
|
|
}
|
2010-06-05 13:32:42 +00:00
|
|
|
|
|
|
|
/* Local company may have changed, so we should not restore the old value */
|
|
|
|
_current_company = _local_company;
|
2022-10-15 20:11:36 +00:00
|
|
|
_cmd_client_id = INVALID_CLIENT_ID;
|
2009-01-08 13:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-20 20:55:21 +00:00
|
|
|
* Free the local command queues.
|
2009-01-08 13:57:50 +00:00
|
|
|
*/
|
|
|
|
void NetworkFreeLocalCommandQueue()
|
|
|
|
{
|
2011-02-20 20:55:21 +00:00
|
|
|
_local_wait_queue.Free();
|
2010-08-18 21:30:38 +00:00
|
|
|
_local_execution_queue.Free();
|
2009-01-08 13:57:50 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 22:40:17 +00:00
|
|
|
/**
|
|
|
|
* "Send" a particular CommandPacket to all clients.
|
|
|
|
* @param cp The command that has to be distributed.
|
|
|
|
* @param owner The client that owns the command,
|
|
|
|
*/
|
2013-09-14 11:34:09 +00:00
|
|
|
static void DistributeCommandPacket(CommandPacket &cp, const NetworkClientSocket *owner)
|
2010-08-18 22:40:17 +00:00
|
|
|
{
|
|
|
|
CommandCallback *callback = cp.callback;
|
|
|
|
cp.frame = _frame_counter_max + 1;
|
|
|
|
|
2019-12-16 19:56:10 +00:00
|
|
|
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
|
2010-10-24 20:07:32 +00:00
|
|
|
if (cs->status >= NetworkClientSocket::STATUS_MAP) {
|
2010-08-18 22:40:17 +00:00
|
|
|
/* Callbacks are only send back to the client who sent them in the
|
|
|
|
* first place. This filters that out. */
|
2019-04-10 21:07:06 +00:00
|
|
|
cp.callback = (cs != owner) ? nullptr : callback;
|
2010-08-18 22:40:17 +00:00
|
|
|
cp.my_cmd = (cs == owner);
|
2018-08-13 11:16:41 +00:00
|
|
|
cs->outgoing_queue.Append(cp);
|
2010-08-18 22:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 19:56:10 +00:00
|
|
|
cp.callback = (nullptr != owner) ? nullptr : callback;
|
|
|
|
cp.my_cmd = (nullptr == owner);
|
2018-08-13 11:16:41 +00:00
|
|
|
_local_execution_queue.Append(cp);
|
2010-08-18 22:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "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)
|
|
|
|
{
|
2012-01-17 21:12:19 +00:00
|
|
|
#ifdef DEBUG_DUMP_COMMANDS
|
|
|
|
/* When replaying we do not want this limitation. */
|
2012-01-22 21:27:15 +00:00
|
|
|
int to_go = UINT16_MAX;
|
2012-01-17 21:12:19 +00:00
|
|
|
#else
|
2010-08-19 08:59:36 +00:00
|
|
|
int to_go = _settings_client.network.commands_per_frame;
|
2012-01-17 21:12:19 +00:00
|
|
|
#endif
|
2010-08-19 08:59:36 +00:00
|
|
|
|
2018-08-13 11:16:41 +00:00
|
|
|
std::unique_ptr<CommandPacket> cp;
|
2019-04-10 21:07:06 +00:00
|
|
|
while (--to_go >= 0 && (cp = queue->Pop(true)) != nullptr) {
|
2010-08-18 22:40:17 +00:00
|
|
|
DistributeCommandPacket(*cp, owner);
|
2018-08-13 11:16:41 +00:00
|
|
|
NetworkAdminCmdLogging(owner, cp.get());
|
2010-08-18 22:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/** Distribute the commands of ourself and the clients. */
|
2010-08-18 22:40:17 +00:00
|
|
|
void NetworkDistributeCommands()
|
|
|
|
{
|
|
|
|
/* First send the server's commands. */
|
2019-04-10 21:07:06 +00:00
|
|
|
DistributeQueue(&_local_wait_queue, nullptr);
|
2010-08-18 22:40:17 +00:00
|
|
|
|
|
|
|
/* Then send the queues of the others. */
|
2019-12-16 19:56:10 +00:00
|
|
|
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
|
2010-08-18 22:40:17 +00:00
|
|
|
DistributeQueue(&cs->incoming_queue, cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-08 14:40:18 +00:00
|
|
|
/**
|
|
|
|
* Receives a command from the network.
|
|
|
|
* @param p the packet to read from.
|
|
|
|
* @param cp the struct to write the data to.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return an error message. When nullptr there has been no error.
|
2009-01-08 14:40:18 +00:00
|
|
|
*/
|
2010-11-30 13:38:46 +00:00
|
|
|
const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
|
2009-01-08 14:40:18 +00:00
|
|
|
{
|
|
|
|
cp->company = (CompanyID)p->Recv_uint8();
|
|
|
|
cp->cmd = p->Recv_uint32();
|
2011-12-19 20:50:44 +00:00
|
|
|
if (!IsValidCommand(cp->cmd)) return "invalid command";
|
2021-01-22 15:24:29 +00:00
|
|
|
if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
|
2011-12-19 20:50:44 +00:00
|
|
|
if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
|
|
|
|
|
2009-01-08 14:40:18 +00:00
|
|
|
cp->p1 = p->Recv_uint32();
|
|
|
|
cp->p2 = p->Recv_uint32();
|
2020-10-17 13:42:46 +00:00
|
|
|
cp->p3 = p->Recv_uint64();
|
2009-01-08 14:40:18 +00:00
|
|
|
cp->tile = p->Recv_uint32();
|
2022-12-07 20:37:23 +00:00
|
|
|
|
|
|
|
StringValidationSettings settings = (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK;
|
|
|
|
p->Recv_string(cp->text, settings);
|
2009-01-08 14:40:18 +00:00
|
|
|
|
|
|
|
byte callback = p->Recv_uint8();
|
2011-08-26 16:39:02 +00:00
|
|
|
if (callback >= lengthof(_callback_table)) return "invalid callback";
|
2009-01-08 14:40:18 +00:00
|
|
|
|
|
|
|
cp->callback = _callback_table[callback];
|
2022-12-07 20:37:23 +00:00
|
|
|
|
|
|
|
uint16 aux_data_size = p->Recv_uint16();
|
|
|
|
if (aux_data_size > 0 && p->CanReadFromPacket(aux_data_size, true)) {
|
|
|
|
CommandAuxiliarySerialised *aux_data = new CommandAuxiliarySerialised();
|
|
|
|
cp->aux_data.reset(aux_data);
|
|
|
|
aux_data->serialised_data.resize(aux_data_size);
|
|
|
|
p->Recv_binary((char *)(aux_data->serialised_data.data()), aux_data_size);
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2009-01-08 14:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a command over the network.
|
|
|
|
* @param p the packet to send it in.
|
|
|
|
* @param cp the packet to actually send.
|
|
|
|
*/
|
2010-11-30 13:38:46 +00:00
|
|
|
void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
|
2009-01-08 14:40:18 +00:00
|
|
|
{
|
|
|
|
p->Send_uint8 (cp->company);
|
|
|
|
p->Send_uint32(cp->cmd);
|
|
|
|
p->Send_uint32(cp->p1);
|
|
|
|
p->Send_uint32(cp->p2);
|
2020-10-17 13:42:46 +00:00
|
|
|
p->Send_uint64(cp->p3);
|
2009-01-08 14:40:18 +00:00
|
|
|
p->Send_uint32(cp->tile);
|
2022-12-07 20:37:23 +00:00
|
|
|
p->Send_string(cp->text.c_str());
|
2009-01-08 14:40:18 +00:00
|
|
|
|
|
|
|
byte callback = 0;
|
2010-01-11 20:00:14 +00:00
|
|
|
while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
|
2009-01-08 14:40:18 +00:00
|
|
|
callback++;
|
|
|
|
}
|
|
|
|
|
2010-01-11 20:00:14 +00:00
|
|
|
if (callback == lengthof(_callback_table)) {
|
2021-05-12 14:34:02 +00:00
|
|
|
DEBUG(net, 0, "Unknown callback for command; no callback sent (command: %d)", cp->cmd);
|
2019-04-10 21:07:06 +00:00
|
|
|
callback = 0; // _callback_table[0] == nullptr
|
2009-01-08 14:40:18 +00:00
|
|
|
}
|
|
|
|
p->Send_uint8 (callback);
|
2022-12-07 20:37:23 +00:00
|
|
|
|
|
|
|
size_t aux_data_size_pos = p->Size();
|
|
|
|
p->Send_uint16(0);
|
|
|
|
if (cp->aux_data != nullptr) {
|
|
|
|
CommandSerialisationBuffer serialiser(p->GetSerialisationBuffer(), p->GetSerialisationLimit());
|
|
|
|
cp->aux_data->Serialise(serialiser);
|
|
|
|
p->WriteAtOffset_uint16(aux_data_size_pos, (uint16)(p->Size() - aux_data_size_pos - 2));
|
|
|
|
}
|
2009-01-08 14:40:18 +00:00
|
|
|
}
|