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"
|
|
12 |
|
|
13 |
// Add a command to the local command queue
|
|
14 |
void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp)
|
|
15 |
{
|
|
16 |
CommandPacket* new_cp = malloc(sizeof(*new_cp));
|
|
17 |
|
|
18 |
*new_cp = *cp;
|
|
19 |
|
|
20 |
if (cs->command_queue == NULL) {
|
|
21 |
cs->command_queue = new_cp;
|
|
22 |
} else {
|
|
23 |
CommandPacket *c = cs->command_queue;
|
|
24 |
while (c->next != NULL) c = c->next;
|
|
25 |
c->next = new_cp;
|
|
26 |
}
|
|
27 |
}
|
|
28 |
|
|
29 |
// Prepare a DoCommand to be send over the network
|
|
30 |
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
|
|
31 |
{
|
|
32 |
CommandPacket *c = malloc(sizeof(CommandPacket));
|
|
33 |
byte temp_callback;
|
|
34 |
|
|
35 |
c->player = _local_player;
|
|
36 |
c->next = NULL;
|
|
37 |
c->tile = tile;
|
|
38 |
c->p1 = p1;
|
|
39 |
c->p2 = p2;
|
|
40 |
c->cmd = cmd;
|
|
41 |
c->callback = 0;
|
|
42 |
|
|
43 |
temp_callback = 0;
|
|
44 |
|
|
45 |
while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback)
|
|
46 |
temp_callback++;
|
|
47 |
if (temp_callback == _callback_table_count) {
|
|
48 |
DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
|
|
49 |
temp_callback = 0; /* _callback_table[0] == NULL */
|
|
50 |
}
|
|
51 |
|
|
52 |
if (_network_server) {
|
|
53 |
// We are the server, so set the command to be executed next possible frame
|
|
54 |
c->frame = _frame_counter_max + 1;
|
|
55 |
} else {
|
|
56 |
c->frame = 0; // The client can't tell which frame, so just make it 0
|
|
57 |
}
|
|
58 |
|
|
59 |
ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text));
|
|
60 |
|
|
61 |
if (_network_server) {
|
|
62 |
// If we are the server, we queue the command in our 'special' queue.
|
|
63 |
// In theory, we could execute the command right away, but then the
|
|
64 |
// client on the server can do everything 1 tick faster than others.
|
|
65 |
// So to keep the game fair, we delay the command with 1 tick
|
|
66 |
// which gives about the same speed as most clients.
|
|
67 |
NetworkClientState *cs;
|
|
68 |
|
|
69 |
// And we queue it for delivery to the clients
|
|
70 |
FOR_ALL_CLIENTS(cs) {
|
|
71 |
if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c);
|
|
72 |
}
|
|
73 |
|
|
74 |
// Only the server gets the callback, because clients should not get them
|
|
75 |
c->callback = temp_callback;
|
|
76 |
if (_local_command_queue == NULL) {
|
|
77 |
_local_command_queue = c;
|
|
78 |
} else {
|
|
79 |
// Find last packet
|
|
80 |
CommandPacket *cp = _local_command_queue;
|
|
81 |
while (cp->next != NULL) cp = cp->next;
|
|
82 |
cp->next = c;
|
|
83 |
}
|
|
84 |
|
|
85 |
return;
|
|
86 |
}
|
|
87 |
|
|
88 |
// Clients send their command to the server and forget all about the packet
|
|
89 |
c->callback = temp_callback;
|
|
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 |
DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND);
|
|
104 |
}
|
|
105 |
|
|
106 |
#endif /* ENABLE_NETWORK */
|