28 } |
28 } |
29 |
29 |
30 // Prepare a DoCommand to be send over the network |
30 // Prepare a DoCommand to be send over the network |
31 void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
31 void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
32 { |
32 { |
33 CommandPacket *c = MallocT<CommandPacket>(1); |
33 CommandPacket c; |
34 byte temp_callback; |
|
35 |
34 |
36 c->player = _local_player; |
35 c.player = _local_player; |
37 c->next = NULL; |
36 c.next = NULL; |
38 c->tile = tile; |
37 c.tile = tile; |
39 c->p1 = p1; |
38 c.p1 = p1; |
40 c->p2 = p2; |
39 c.p2 = p2; |
41 c->cmd = cmd; |
40 c.cmd = cmd; |
42 c->callback = 0; |
|
43 |
41 |
44 temp_callback = 0; |
42 c.callback = 0; |
45 |
43 while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) { |
46 while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback) |
44 c.callback++; |
47 temp_callback++; |
|
48 if (temp_callback == _callback_table_count) { |
|
49 DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); |
|
50 temp_callback = 0; /* _callback_table[0] == NULL */ |
|
51 } |
45 } |
52 |
46 |
53 if (_network_server) { |
47 if (c.callback == _callback_table_count) { |
54 // We are the server, so set the command to be executed next possible frame |
48 DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); |
55 c->frame = _frame_counter_max + 1; |
49 c.callback = 0; // _callback_table[0] == NULL |
56 } else { |
|
57 c->frame = 0; // The client can't tell which frame, so just make it 0 |
|
58 } |
50 } |
59 |
51 |
60 ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text)); |
52 ttd_strlcpy(c.text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c.text)); |
61 |
53 |
62 if (_network_server) { |
54 if (_network_server) { |
63 // If we are the server, we queue the command in our 'special' queue. |
55 /* If we are the server, we queue the command in our 'special' queue. |
64 // In theory, we could execute the command right away, but then the |
56 * In theory, we could execute the command right away, but then the |
65 // client on the server can do everything 1 tick faster than others. |
57 * client on the server can do everything 1 tick faster than others. |
66 // So to keep the game fair, we delay the command with 1 tick |
58 * So to keep the game fair, we delay the command with 1 tick |
67 // which gives about the same speed as most clients. |
59 * which gives about the same speed as most clients. |
68 NetworkTCPSocketHandler *cs; |
60 */ |
|
61 c.frame = _frame_counter_max + 1; |
69 |
62 |
70 // And we queue it for delivery to the clients |
63 CommandPacket *new_cp = MallocT<CommandPacket>(1); |
71 FOR_ALL_CLIENTS(cs) { |
64 *new_cp = c; |
72 if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c); |
65 new_cp->my_cmd = true; |
|
66 if (_local_command_queue == NULL) { |
|
67 _local_command_queue = new_cp; |
|
68 } else { |
|
69 /* Find last packet */ |
|
70 CommandPacket *cp = _local_command_queue; |
|
71 while (cp->next != NULL) cp = cp->next; |
|
72 cp->next = new_cp; |
73 } |
73 } |
74 |
74 |
75 // Only the server gets the callback, because clients should not get them |
75 /* Only the local client (in this case, the server) gets the callback */ |
76 c->callback = temp_callback; |
76 c.callback = 0; |
77 if (_local_command_queue == NULL) { |
77 /* And we queue it for delivery to the clients */ |
78 _local_command_queue = c; |
78 NetworkTCPSocketHandler *cs; |
79 } else { |
79 FOR_ALL_CLIENTS(cs) { |
80 // Find last packet |
80 if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, &c); |
81 CommandPacket *cp = _local_command_queue; |
|
82 while (cp->next != NULL) cp = cp->next; |
|
83 cp->next = c; |
|
84 } |
81 } |
85 |
|
86 return; |
82 return; |
87 } |
83 } |
88 |
84 |
89 // Clients send their command to the server and forget all about the packet |
85 c.frame = 0; // The client can't tell which frame, so just make it 0 |
90 c->callback = temp_callback; |
86 |
91 SEND_COMMAND(PACKET_CLIENT_COMMAND)(c); |
87 /* Clients send their command to the server and forget all about the packet */ |
|
88 SEND_COMMAND(PACKET_CLIENT_COMMAND)(&c); |
92 } |
89 } |
93 |
90 |
94 // Execute a DoCommand we received from the network |
91 // Execute a DoCommand we received from the network |
95 void NetworkExecuteCommand(CommandPacket *cp) |
92 void NetworkExecuteCommand(CommandPacket *cp) |
96 { |
93 { |
99 /* cp->callback is unsigned. so we don't need to do lower bounds checking. */ |
96 /* cp->callback is unsigned. so we don't need to do lower bounds checking. */ |
100 if (cp->callback > _callback_table_count) { |
97 if (cp->callback > _callback_table_count) { |
101 DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback); |
98 DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback); |
102 cp->callback = 0; |
99 cp->callback = 0; |
103 } |
100 } |
104 DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND); |
101 |
|
102 #ifdef DEBUG_DUMP_COMMANDS |
|
103 debug_dump_commands("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); |
|
104 #endif /* DUMP_COMMANDS */ |
|
105 |
|
106 DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND, cp->my_cmd); |
105 } |
107 } |
106 |
108 |
107 #endif /* ENABLE_NETWORK */ |
109 #endif /* ENABLE_NETWORK */ |