# network_data.cpp

    00001 /* $Id: network_data.cpp 12367 2008-03-15 00:31:10Z smatz $ */
    00002
    00003 #ifdef ENABLE_NETWORK
    00004
    00005 #include "../stdafx.h"
    00006 #include "../debug.h"
    00007 #include "network_data.h"
    00008 #include "network_client.h"
    00009 #include "../command_func.h"
    00010 #include "../callback_table.h"
    00011 #include "../core/alloc_func.hpp"
    00012 #include "../string_func.h"
    00013 #include "../date_func.h"
    00014 #include "../player_func.h"
    00015
    00016 // Add a command to the local command queue
    00017 void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp)
    00018 {
    00019   CommandPacket* new_cp = MallocT<CommandPacket>(1);
    00020
    00021   *new_cp = *cp;
    00022
    00023   if (cs->command_queue == NULL) {
    00024     cs->command_queue = new_cp;
    00025   } else {
    00026     CommandPacket *c = cs->command_queue;
    00027     while (c->next != NULL) c = c->next;
    00028     c->next = new_cp;
    00029   }
    00030 }
    00031
    00032 // Prepare a DoCommand to be send over the network
    00033 void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
    00034 {
    00035   CommandPacket c;
    00036
    00037   c.player = _local_player;
    00038   c.next   = NULL;
    00039   c.tile   = tile;
    00040   c.p1     = p1;
    00041   c.p2     = p2;
    00042   c.cmd    = cmd;
    00043
    00044   c.callback = 0;
    00045   while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) {
    00046     c.callback++;
    00047   }
    00048
    00049   if (c.callback == _callback_table_count) {
    00050     DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
    00051     c.callback = 0; // _callback_table[0] == NULL
    00052   }
    00053
    00054   ttd_strlcpy(c.text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c.text));
    00055
    00056   if (_network_server) {
    00057     /* If we are the server, we queue the command in our 'special' queue.
    00058      *   In theory, we could execute the command right away, but then the
    00059      *   client on the server can do everything 1 tick faster than others.
    00060      *   So to keep the game fair, we delay the command with 1 tick
    00061      *   which gives about the same speed as most clients.
    00062      */
    00063     c.frame = _frame_counter_max + 1;
    00064
    00065     CommandPacket *new_cp = MallocT<CommandPacket>(1);
    00066     *new_cp = c;
    00067     new_cp->my_cmd = true;
    00068     if (_local_command_queue == NULL) {
    00069       _local_command_queue = new_cp;
    00070     } else {
    00071       /* Find last packet */
    00072       CommandPacket *cp = _local_command_queue;
    00073       while (cp->next != NULL) cp = cp->next;
    00074       cp->next = new_cp;
    00075     }
    00076
    00077     /* Only the local client (in this case, the server) gets the callback */
    00078     c.callback = 0;
    00079     /* And we queue it for delivery to the clients */
    00080     NetworkTCPSocketHandler *cs;
    00081     FOR_ALL_CLIENTS(cs) {
    00082       if (cs->status > STATUS_MAP_WAIT) NetworkAddCommandQueue(cs, &c);
    00083     }
    00084     return;
    00085   }
    00086
    00087   c.frame = 0; // The client can't tell which frame, so just make it 0
    00088
    00089   /* Clients send their command to the server and forget all about the packet */
    00090   SEND_COMMAND(PACKET_CLIENT_COMMAND)(&c);
    00091 }
    00092
    00093 // Execute a DoCommand we received from the network
    00094 void NetworkExecuteCommand(CommandPacket *cp)
    00095 {
    00096   _current_player = cp->player;
    00097   _cmd_text = cp->text;
    00098   /* cp->callback is unsigned. so we don't need to do lower bounds checking. */
    00099   if (cp->callback > _callback_table_count) {
    00100     DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback);
    00101     cp->callback = 0;
    00102   }
    00103
    00104   DebugDumpCommands("ddc:cmd:%d;%d;%d;%d;%d;%d;%d;%s\n", _date, _date_fract, (int)cp->player, cp->tile, cp->p1, cp->p2, cp->cmd, cp->text);
    00105
    00106   DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND, cp->my_cmd);
    00107 }
    00108
    00109 #endif /* ENABLE_NETWORK */