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