author | KUDr |
Fri, 12 Jan 2007 15:43:00 +0000 | |
changeset 5620 | eab6f02899a0 |
parent 5609 | dc6a58930ba4 |
child 5624 | 6afe9d27430a |
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" |
5587
167d9a91ef02
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5584
diff
changeset
|
12 |
#include "../helpers.hpp" |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
13 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
14 |
// 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
|
15 |
void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
16 |
{ |
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
17 |
CommandPacket* new_cp = MallocT<CommandPacket>(1); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
18 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
19 |
*new_cp = *cp; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
20 |
|
4077
d4d440dd8925
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
3547
diff
changeset
|
21 |
if (cs->command_queue == NULL) { |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
22 |
cs->command_queue = new_cp; |
4077
d4d440dd8925
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
3547
diff
changeset
|
23 |
} else { |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
24 |
CommandPacket *c = cs->command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
25 |
while (c->next != NULL) c = c->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
26 |
c->next = new_cp; |
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
30 |
// 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
|
31 |
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
|
32 |
{ |
5609
dc6a58930ba4
(svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents:
5587
diff
changeset
|
33 |
CommandPacket *c = MallocT<CommandPacket>(1); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
34 |
byte temp_callback; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
35 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
36 |
c->player = _local_player; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
37 |
c->next = NULL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
38 |
c->tile = tile; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
39 |
c->p1 = p1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
40 |
c->p2 = p2; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
41 |
c->cmd = cmd; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
42 |
c->callback = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
43 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
44 |
temp_callback = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
45 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
46 |
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
|
47 |
temp_callback++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
temp_callback = 0; /* _callback_table[0] == NULL */ |
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
53 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
54 |
// 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
|
55 |
c->frame = _frame_counter_max + 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
56 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
57 |
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
|
58 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
59 |
|
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
|
60 |
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
|
61 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
62 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
63 |
// 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
|
64 |
// In theory, we could execute the command right away, but then the |
826 | 65 |
// 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
|
66 |
// 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
|
67 |
// 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
|
68 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
69 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
70 |
// And we queue it for delivery to the clients |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
71 |
FOR_ALL_CLIENTS(cs) { |
4077
d4d440dd8925
(svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents:
3547
diff
changeset
|
72 |
if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c); |
543
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
75 |
// 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
|
76 |
c->callback = temp_callback; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
77 |
if (_local_command_queue == NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
78 |
_local_command_queue = c; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
79 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
80 |
// Find last packet |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
81 |
CommandPacket *cp = _local_command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
82 |
while (cp->next != NULL) cp = cp->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
83 |
cp->next = c; |
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
86 |
return; |
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
89 |
// 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
|
90 |
c->callback = temp_callback; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
91 |
SEND_COMMAND(PACKET_CLIENT_COMMAND)(c); |
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
94 |
// Execute a DoCommand we received from the network |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
95 |
void NetworkExecuteCommand(CommandPacket *cp) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
96 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
97 |
_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
|
98 |
_cmd_text = cp->text; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
99 |
/* 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
|
100 |
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
|
101 |
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
|
102 |
cp->callback = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
103 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
104 |
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
|
105 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
106 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff
changeset
|
107 |
#endif /* ENABLE_NETWORK */ |