tron@2186: /* $Id$ */ tron@2186: Darkvater@4826: #ifdef ENABLE_NETWORK Darkvater@4826: rubidium@5469: #include "../stdafx.h" rubidium@5469: #include "../debug.h" truelight@543: #include "network_data.h" rubidium@5469: #include "../string.h" truelight@543: #include "network_client.h" rubidium@5469: #include "../command.h" rubidium@5469: #include "../callback_table.h" rubidium@5587: #include "../helpers.hpp" truelight@543: truelight@543: // Add a command to the local command queue truelight@716: void NetworkAddCommandQueue(NetworkClientState *cs, CommandPacket *cp) truelight@543: { KUDr@5609: CommandPacket* new_cp = MallocT(1); truelight@543: truelight@543: *new_cp = *cp; truelight@543: tron@4077: if (cs->command_queue == NULL) { truelight@543: cs->command_queue = new_cp; tron@4077: } else { truelight@543: CommandPacket *c = cs->command_queue; truelight@543: while (c->next != NULL) c = c->next; truelight@543: c->next = new_cp; truelight@543: } truelight@543: } truelight@543: truelight@543: // Prepare a DoCommand to be send over the network tron@1977: void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) truelight@543: { KUDr@5609: CommandPacket *c = MallocT(1); truelight@543: byte temp_callback; truelight@543: truelight@543: c->player = _local_player; truelight@543: c->next = NULL; truelight@543: c->tile = tile; truelight@543: c->p1 = p1; truelight@543: c->p2 = p2; truelight@543: c->cmd = cmd; truelight@543: c->callback = 0; truelight@543: truelight@543: temp_callback = 0; truelight@543: truelight@543: while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback) truelight@543: temp_callback++; truelight@543: if (temp_callback == _callback_table_count) { Darkvater@5380: DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback); truelight@543: temp_callback = 0; /* _callback_table[0] == NULL */ truelight@543: } truelight@543: truelight@543: if (_network_server) { truelight@543: // We are the server, so set the command to be executed next possible frame truelight@543: c->frame = _frame_counter_max + 1; truelight@543: } else { truelight@543: c->frame = 0; // The client can't tell which frame, so just make it 0 truelight@543: } truelight@543: tron@1820: ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text)); truelight@543: truelight@543: if (_network_server) { truelight@543: // If we are the server, we queue the command in our 'special' queue. truelight@543: // In theory, we could execute the command right away, but then the miham@826: // client on the server can do everything 1 tick faster than others. truelight@543: // So to keep the game fair, we delay the command with 1 tick truelight@543: // which gives about the same speed as most clients. truelight@716: NetworkClientState *cs; truelight@543: truelight@543: // And we queue it for delivery to the clients truelight@543: FOR_ALL_CLIENTS(cs) { tron@4077: if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c); truelight@543: } truelight@543: truelight@543: // Only the server gets the callback, because clients should not get them truelight@543: c->callback = temp_callback; truelight@543: if (_local_command_queue == NULL) { truelight@543: _local_command_queue = c; truelight@543: } else { truelight@543: // Find last packet truelight@543: CommandPacket *cp = _local_command_queue; truelight@543: while (cp->next != NULL) cp = cp->next; truelight@543: cp->next = c; truelight@543: } truelight@543: truelight@543: return; truelight@543: } truelight@543: truelight@543: // Clients send their command to the server and forget all about the packet truelight@543: c->callback = temp_callback; truelight@543: SEND_COMMAND(PACKET_CLIENT_COMMAND)(c); truelight@543: } truelight@543: truelight@543: // Execute a DoCommand we received from the network truelight@543: void NetworkExecuteCommand(CommandPacket *cp) truelight@543: { truelight@543: _current_player = cp->player; tron@1820: _cmd_text = cp->text; truelight@543: /* cp->callback is unsigned. so we don't need to do lower bounds checking. */ truelight@543: if (cp->callback > _callback_table_count) { Darkvater@5380: DEBUG(net, 0, "Received out-of-bounds callback (%d)", cp->callback); truelight@543: cp->callback = 0; truelight@543: } truelight@543: DoCommandP(cp->tile, cp->p1, cp->p2, _callback_table[cp->callback], cp->cmd | CMD_NETWORK_COMMAND); truelight@543: } truelight@543: truelight@543: #endif /* ENABLE_NETWORK */