~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/network/network_data.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: network_data.cpp 12367 2008-03-15 00:31:10Z smatz $ */
2
 
 
3
 
#ifdef ENABLE_NETWORK
4
 
 
5
 
#include "../stdafx.h"
6
 
#include "../debug.h"
7
 
#include "network_data.h"
8
 
#include "network_client.h"
9
 
#include "../command_func.h"
10
 
#include "../callback_table.h"
11
 
#include "../core/alloc_func.hpp"
12
 
#include "../string_func.h"
13
 
#include "../date_func.h"
14
 
#include "../player_func.h"
15
 
 
16
 
// Add a command to the local command queue
17
 
void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp)
18
 
{
19
 
        CommandPacket* new_cp = MallocT<CommandPacket>(1);
20
 
 
21
 
        *new_cp = *cp;
22
 
 
23
 
        if (cs->command_queue == NULL) {
24
 
                cs->command_queue = new_cp;
25
 
        } else {
26
 
                CommandPacket *c = cs->command_queue;
27
 
                while (c->next != NULL) c = c->next;
28
 
                c->next = new_cp;
29
 
        }
30
 
}
31
 
 
32
 
// Prepare a DoCommand to be send over the network
33
 
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
34
 
{
35
 
        CommandPacket c;
36
 
 
37
 
        c.player = _local_player;
38
 
        c.next   = NULL;
39
 
        c.tile   = tile;
40
 
        c.p1     = p1;
41
 
        c.p2     = p2;
42
 
        c.cmd    = cmd;
43
 
 
44
 
        c.callback = 0;
45
 
        while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) {
46
 
                c.callback++;
47
 
        }
48
 
 
49
 
        if (c.callback == _callback_table_count) {
50
 
                DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
51
 
                c.callback = 0; // _callback_table[0] == NULL
52
 
        }
53
 
 
54
 
        ttd_strlcpy(c.text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c.text));
55
 
 
56
 
        if (_network_server) {
57
 
                /* If we are the server, we queue the command in our 'special' queue.
58
 
                 *   In theory, we could execute the command right away, but then the
59
 
                 *   client on the server can do everything 1 tick faster than others.
60
 
                 *   So to keep the game fair, we delay the command with 1 tick
61
 
                 *   which gives about the same speed as most clients.
62
 
                 */
63
 
                c.frame = _frame_counter_max + 1;
64
 
 
65
 
                CommandPacket *new_cp = MallocT<CommandPacket>(1);
66
 
                *new_cp = c;
67
 
                new_cp->my_cmd = true;
68
 
                if (_local_command_queue == NULL) {
69
 
                        _local_command_queue = new_cp;
70
 
                } else {
71
 
                        /* Find last packet */
72
 
                        CommandPacket *cp = _local_command_queue;
73
 
                        while (cp->next != NULL) cp = cp->next;
74
 
                        cp->next = new_cp;
75
 
                }
76
 
 
77
 
                /* Only the local client (in this case, the server) gets the callback */
78
 
                c.callback = 0;
79
 
                /* And we queue it for delivery to the clients */
80
 
                NetworkTCPSocketHandler *cs;
81
 
                FOR_ALL_CLIENTS(cs) {
82
 
                        if (cs->status > STATUS_MAP_WAIT) NetworkAddCommandQueue(cs, &c);
83
 
                }
84
 
                return;
85
 
        }
86
 
 
87
 
        c.frame = 0; // The client can't tell which frame, so just make it 0
88
 
 
89
 
        /* Clients send their command to the server and forget all about the packet */
90
 
        SEND_COMMAND(PACKET_CLIENT_COMMAND)(&c);
91
 
}
92
 
 
93
 
// Execute a DoCommand we received from the network
94
 
void NetworkExecuteCommand(CommandPacket *cp)
95
 
{
96
 
        _current_player = cp->player;
97
 
        _cmd_text = cp->text;
98
 
        /* cp->callback is unsigned. so we don't need to do lower bounds checking. */
99
 
        if (cp->callback > _callback_table_count) {
100
 
                DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback);
101
 
                cp->callback = 0;
102
 
        }
103
 
 
104
 
        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);
105
 
 
106
 
        DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND, cp->my_cmd);
107
 
}
108
 
 
109
 
#endif /* ENABLE_NETWORK */