author | rubidium |
Tue, 02 Jan 2007 19:19:48 +0000 | |
changeset 5475 | 2e6990a8c7c4 |
parent 5469 | network/network_data.c@7edfc643abbc |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
4826
6a545d194528
(svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents:
4077
diff
changeset
|
3 |
#ifdef ENABLE_NETWORK |
6a545d194528
(svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents:
4077
diff
changeset
|
4 |
|
5469
7edfc643abbc
(svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
5380
diff
changeset
|
5 |
#include "../stdafx.h" |
7edfc643abbc
(svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
5380
diff
changeset
|
6 |
#include "../debug.h" |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
7 |
#include "network_data.h" |
5469
7edfc643abbc
(svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
5380
diff
changeset
|
8 |
#include "../string.h" |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
9 |
#include "network_client.h" |
5469
7edfc643abbc
(svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
5380
diff
changeset
|
10 |
#include "../command.h" |
7edfc643abbc
(svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents:
5380
diff
changeset
|
11 |
#include "../callback_table.h" |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
12 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
13 |
// Add a command to the local command queue |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
543
diff
changeset
|
14 |
void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
15 |
{ |
4077
d4d440dd8925
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
3547
diff
changeset
|
16 |
CommandPacket* new_cp = malloc(sizeof(*new_cp)); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
17 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
18 |
*new_cp = *cp; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
19 |
|
4077
d4d440dd8925
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
3547
diff
changeset
|
20 |
if (cs->command_queue == NULL) { |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
21 |
cs->command_queue = new_cp; |
4077
d4d440dd8925
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
3547
diff
changeset
|
22 |
} else { |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
23 |
CommandPacket *c = cs->command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
24 |
while (c->next != NULL) c = c->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
25 |
c->next = new_cp; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
26 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
27 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
28 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
29 |
// Prepare a DoCommand to be send over the network |
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1820
diff
changeset
|
30 |
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
31 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
32 |
CommandPacket *c = malloc(sizeof(CommandPacket)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
33 |
byte temp_callback; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
34 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
35 |
c->player = _local_player; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
36 |
c->next = NULL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
37 |
c->tile = tile; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
38 |
c->p1 = p1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
39 |
c->p2 = p2; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
40 |
c->cmd = cmd; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
41 |
c->callback = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
42 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
43 |
temp_callback = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
44 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
45 |
while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
46 |
temp_callback++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
47 |
if (temp_callback == _callback_table_count) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4880
diff
changeset
|
48 |
DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
49 |
temp_callback = 0; /* _callback_table[0] == NULL */ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
50 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
51 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
52 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
53 |
// We are the server, so set the command to be executed next possible frame |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
54 |
c->frame = _frame_counter_max + 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
55 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
56 |
c->frame = 0; // The client can't tell which frame, so just make it 0 |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
57 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
58 |
|
1820
d03c56850dc2
(svn r2324) Introduce _cmd_text for passing strings with a command instead of abusing _decode_parameters as text buffer. This should prevent several possible buffer overruns and is a bit cleaner to use. As bonus it reduces the size of most command packets by 79 bytes.
tron
parents:
1299
diff
changeset
|
59 |
ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text)); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
60 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
61 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
62 |
// If we are the server, we queue the command in our 'special' queue. |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
63 |
// In theory, we could execute the command right away, but then the |
826 | 64 |
// client on the server can do everything 1 tick faster than others. |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
65 |
// So to keep the game fair, we delay the command with 1 tick |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
66 |
// which gives about the same speed as most clients. |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
543
diff
changeset
|
67 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
68 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
69 |
// And we queue it for delivery to the clients |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
70 |
FOR_ALL_CLIENTS(cs) { |
4077
d4d440dd8925
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
3547
diff
changeset
|
71 |
if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
72 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
73 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
74 |
// Only the server gets the callback, because clients should not get them |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
75 |
c->callback = temp_callback; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
76 |
if (_local_command_queue == NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
77 |
_local_command_queue = c; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
78 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
79 |
// Find last packet |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
80 |
CommandPacket *cp = _local_command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
81 |
while (cp->next != NULL) cp = cp->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
82 |
cp->next = c; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
83 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
84 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
85 |
return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
86 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
87 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
88 |
// Clients send their command to the server and forget all about the packet |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
89 |
c->callback = temp_callback; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
90 |
SEND_COMMAND(PACKET_CLIENT_COMMAND)(c); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
91 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
92 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
93 |
// Execute a DoCommand we received from the network |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
94 |
void NetworkExecuteCommand(CommandPacket *cp) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
95 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
96 |
_current_player = cp->player; |
1820
d03c56850dc2
(svn r2324) Introduce _cmd_text for passing strings with a command instead of abusing _decode_parameters as text buffer. This should prevent several possible buffer overruns and is a bit cleaner to use. As bonus it reduces the size of most command packets by 79 bytes.
tron
parents:
1299
diff
changeset
|
97 |
_cmd_text = cp->text; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
98 |
/* cp->callback is unsigned. so we don't need to do lower bounds checking. */ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
99 |
if (cp->callback > _callback_table_count) { |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
4880
diff
changeset
|
100 |
DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
101 |
cp->callback = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
102 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
103 |
DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
104 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
105 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
106 |
#endif /* ENABLE_NETWORK */ |