2007-01-02 17:34:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "network_client.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "network.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "../command_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "../company_func.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
|
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[] = {
|
2010-01-11 20:00:14 +00:00
|
|
|
/* 0x00 */ NULL,
|
|
|
|
/* 0x01 */ CcBuildPrimaryVehicle,
|
|
|
|
/* 0x02 */ CcBuildAirport,
|
|
|
|
/* 0x03 */ CcBuildBridge,
|
|
|
|
/* 0x04 */ CcBuildCanal,
|
|
|
|
/* 0x05 */ CcBuildDocks,
|
|
|
|
/* 0x06 */ CcFoundTown,
|
|
|
|
/* 0x07 */ CcBuildRoadTunnel,
|
|
|
|
/* 0x08 */ CcBuildRailTunnel,
|
|
|
|
/* 0x09 */ CcBuildWagon,
|
|
|
|
/* 0x0A */ CcRoadDepot,
|
|
|
|
/* 0x0B */ CcRailDepot,
|
|
|
|
/* 0x0C */ CcPlaceSign,
|
|
|
|
/* 0x0D */ CcPlaySound10,
|
|
|
|
/* 0x0E */ CcPlaySound1D,
|
|
|
|
/* 0x0F */ CcPlaySound1E,
|
|
|
|
/* 0x10 */ CcStation,
|
|
|
|
/* 0x11 */ CcTerraform,
|
2010-02-10 16:24:05 +00:00
|
|
|
#ifdef ENABLE_AI
|
2010-01-11 20:00:14 +00:00
|
|
|
/* 0x12 */ CcAI,
|
2010-02-10 16:24:05 +00:00
|
|
|
#else
|
|
|
|
/* 0x12 */ NULL,
|
|
|
|
#endif /* ENABLE_AI */
|
2010-01-11 20:00:14 +00:00
|
|
|
/* 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-01-11 20:00:14 +00:00
|
|
|
};
|
|
|
|
|
2009-01-08 13:57:50 +00:00
|
|
|
/** Local queue of packets */
|
|
|
|
static CommandPacket *_local_command_queue = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a command to the local or client socket command queue,
|
|
|
|
* based on the socket.
|
|
|
|
* @param cp the command packet to add
|
|
|
|
* @param cs the socket to send to (NULL = locally)
|
|
|
|
*/
|
|
|
|
void NetworkAddCommandQueue(CommandPacket cp, NetworkClientSocket *cs)
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
2009-01-08 13:57:50 +00:00
|
|
|
CommandPacket *new_cp = MallocT<CommandPacket>(1);
|
|
|
|
*new_cp = cp;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2009-01-08 13:57:50 +00:00
|
|
|
CommandPacket **begin = (cs == NULL ? &_local_command_queue : &cs->command_queue);
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2009-01-08 13:57:50 +00:00
|
|
|
if (*begin == NULL) {
|
|
|
|
*begin = new_cp;
|
2007-01-02 17:34:03 +00:00
|
|
|
} else {
|
2009-01-08 13:57:50 +00:00
|
|
|
CommandPacket *c = *begin;
|
2007-01-02 17:34:03 +00:00
|
|
|
while (c->next != NULL) c = c->next;
|
|
|
|
c->next = new_cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
* @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
|
2009-01-08 13:57:50 +00:00
|
|
|
*/
|
2010-01-11 20:32:32 +00:00
|
|
|
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text, CompanyID company)
|
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.next = NULL;
|
|
|
|
c.tile = tile;
|
|
|
|
c.p1 = p1;
|
|
|
|
c.p2 = p2;
|
|
|
|
c.cmd = cmd;
|
|
|
|
c.callback = callback;
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2008-12-28 14:37:19 +00:00
|
|
|
strecpy(c.text, (text != NULL) ? text : "", lastof(c.text));
|
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
|
|
|
|
2009-01-08 13:57:50 +00:00
|
|
|
NetworkAddCommandQueue(c);
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-06-10 19:59:08 +00:00
|
|
|
/* Only the local client (in this case, the server) gets the callback */
|
|
|
|
c.callback = 0;
|
|
|
|
/* And we queue it for delivery to the clients */
|
2008-12-23 09:47:42 +00:00
|
|
|
NetworkClientSocket *cs;
|
2008-12-23 09:02:41 +00:00
|
|
|
FOR_ALL_CLIENT_SOCKETS(cs) {
|
2009-01-08 13:57:50 +00:00
|
|
|
if (cs->status > STATUS_MAP_WAIT) NetworkAddCommandQueue(c, cs);
|
2007-06-10 19:59:08 +00:00
|
|
|
}
|
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 */
|
|
|
|
SEND_COMMAND(PACKET_CLIENT_COMMAND)(&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)
|
|
|
|
{
|
|
|
|
for (CommandPacket *p = _local_command_queue; p != NULL; p = p->next) {
|
|
|
|
CommandPacket c = *p;
|
|
|
|
c.callback = 0;
|
|
|
|
NetworkAddCommandQueue(c, cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
while (_local_command_queue != NULL) {
|
|
|
|
|
|
|
|
/* The queue is always in order, which means
|
|
|
|
* that the first element will be executed first. */
|
|
|
|
if (_frame_counter < _local_command_queue->frame) break;
|
|
|
|
|
|
|
|
if (_frame_counter > _local_command_queue->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!");
|
|
|
|
}
|
|
|
|
|
2009-01-08 14:55:28 +00:00
|
|
|
CommandPacket *cp = _local_command_queue;
|
|
|
|
|
2009-01-08 13:57:50 +00:00
|
|
|
/* We can execute this command */
|
2009-01-08 14:55:28 +00:00
|
|
|
_current_company = cp->company;
|
|
|
|
cp->cmd |= CMD_NETWORK_COMMAND;
|
|
|
|
DoCommandP(cp, cp->my_cmd);
|
2009-01-08 13:57:50 +00:00
|
|
|
|
|
|
|
_local_command_queue = _local_command_queue->next;
|
|
|
|
free(cp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free the local command queue.
|
|
|
|
*/
|
|
|
|
void NetworkFreeLocalCommandQueue()
|
|
|
|
{
|
|
|
|
/* Free all queued commands */
|
|
|
|
while (_local_command_queue != NULL) {
|
|
|
|
CommandPacket *p = _local_command_queue;
|
|
|
|
_local_command_queue = _local_command_queue->next;
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @return an error message. When NULL there has been no error.
|
|
|
|
*/
|
|
|
|
const char *NetworkClientSocket::Recv_Command(Packet *p, CommandPacket *cp)
|
|
|
|
{
|
|
|
|
cp->company = (CompanyID)p->Recv_uint8();
|
|
|
|
cp->cmd = p->Recv_uint32();
|
|
|
|
cp->p1 = p->Recv_uint32();
|
|
|
|
cp->p2 = p->Recv_uint32();
|
|
|
|
cp->tile = p->Recv_uint32();
|
|
|
|
p->Recv_string(cp->text, lengthof(cp->text));
|
|
|
|
|
|
|
|
byte callback = p->Recv_uint8();
|
|
|
|
|
|
|
|
if (!IsValidCommand(cp->cmd)) return "invalid command";
|
|
|
|
if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "offline only command";
|
|
|
|
if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
|
2010-01-11 20:00:14 +00:00
|
|
|
if (callback > lengthof(_callback_table)) return "invalid callback";
|
2009-01-08 14:40:18 +00:00
|
|
|
|
|
|
|
cp->callback = _callback_table[callback];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a command over the network.
|
|
|
|
* @param p the packet to send it in.
|
|
|
|
* @param cp the packet to actually send.
|
|
|
|
*/
|
|
|
|
void NetworkClientSocket::Send_Command(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;
|
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)) {
|
2009-05-10 17:27:25 +00:00
|
|
|
DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", cp->callback);
|
2009-01-08 14:40:18 +00:00
|
|
|
callback = 0; // _callback_table[0] == NULL
|
|
|
|
}
|
|
|
|
p->Send_uint8 (callback);
|
|
|
|
}
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|