2007-01-02 17:34:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-01-08 14:38:21 +00:00
|
|
|
/** @file network_command.cpp Command handling over network connections. */
|
2008-05-06 16:11:33 +01:00
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
2008-05-30 19:20:26 +01:00
|
|
|
#include "network_internal.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "network_client.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "../command_func.h"
|
2007-01-02 17:34:03 +00:00
|
|
|
#include "../callback_table.h"
|
2007-12-25 09:48:53 +00:00
|
|
|
#include "../core/alloc_func.hpp"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "../string_func.h"
|
2008-01-11 00:30:32 +00:00
|
|
|
#include "../date_func.h"
|
2008-09-30 21:51:04 +01:00
|
|
|
#include "../company_func.h"
|
2007-01-02 17:34:03 +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
|
|
|
|
*/
|
2008-12-28 14:37:19 +00:00
|
|
|
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const char *text)
|
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 20:59:08 +01:00
|
|
|
CommandPacket c;
|
2008-09-30 21:39:50 +01:00
|
|
|
c.company = _local_company;
|
|
|
|
c.next = NULL;
|
|
|
|
c.tile = tile;
|
|
|
|
c.p1 = p1;
|
|
|
|
c.p2 = p2;
|
|
|
|
c.cmd = cmd;
|
2007-06-10 20:59:08 +01:00
|
|
|
|
|
|
|
c.callback = 0;
|
|
|
|
while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) {
|
|
|
|
c.callback++;
|
2007-01-02 17:34:03 +00:00
|
|
|
}
|
|
|
|
|
2007-06-10 20:59:08 +01:00
|
|
|
if (c.callback == _callback_table_count) {
|
|
|
|
DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
|
|
|
|
c.callback = 0; // _callback_table[0] == NULL
|
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 20:59:08 +01: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 13:57:50 +00:00
|
|
|
NetworkAddCommandQueue(c);
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-06-10 20:59:08 +01: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 20:59:08 +01:00
|
|
|
}
|
2007-01-02 17:34:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-10 20:59:08 +01: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
|
|
|
}
|
|
|
|
|
2009-01-08 13:57:50 +00:00
|
|
|
/**
|
|
|
|
* Execute a DoCommand we received from the network
|
|
|
|
* @param cp the command to execute
|
|
|
|
*/
|
|
|
|
static void NetworkExecuteCommand(CommandPacket *cp)
|
2007-01-02 17:34:03 +00:00
|
|
|
{
|
2008-09-30 21:39:50 +01:00
|
|
|
_current_company = cp->company;
|
2007-01-02 17:34:03 +00:00
|
|
|
/* cp->callback is unsigned. so we don't need to do lower bounds checking. */
|
|
|
|
if (cp->callback > _callback_table_count) {
|
|
|
|
DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback);
|
|
|
|
cp->callback = 0;
|
|
|
|
}
|
2007-07-07 11:06:10 +01:00
|
|
|
|
2008-12-28 14:37:19 +00:00
|
|
|
DoCommandP(cp->tile, cp->p1, cp->p2, cp->cmd | CMD_NETWORK_COMMAND, _callback_table[cp->callback], cp->text, cp->my_cmd);
|
2007-01-02 17:34:03 +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()
|
|
|
|
{
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can execute this command */
|
|
|
|
NetworkExecuteCommand(_local_command_queue);
|
|
|
|
|
|
|
|
CommandPacket *cp = _local_command_queue;
|
|
|
|
_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|