mirror of https://github.com/OpenTTD/OpenTTD
(svn r10082) -Fix [FS#846]: another memory leak in the networking code (benc).
parent
7767e466e5
commit
d0ad020dc6
|
@ -30,65 +30,61 @@ void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp)
|
||||||
// Prepare a DoCommand to be send over the network
|
// Prepare a DoCommand to be send over the network
|
||||||
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
|
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
|
||||||
{
|
{
|
||||||
CommandPacket *c = MallocT<CommandPacket>(1);
|
CommandPacket c;
|
||||||
byte temp_callback;
|
|
||||||
|
|
||||||
c->player = _local_player;
|
c.player = _local_player;
|
||||||
c->next = NULL;
|
c.next = NULL;
|
||||||
c->tile = tile;
|
c.tile = tile;
|
||||||
c->p1 = p1;
|
c.p1 = p1;
|
||||||
c->p2 = p2;
|
c.p2 = p2;
|
||||||
c->cmd = cmd;
|
c.cmd = cmd;
|
||||||
c->callback = 0;
|
|
||||||
|
|
||||||
temp_callback = 0;
|
c.callback = 0;
|
||||||
|
while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) {
|
||||||
|
c.callback++;
|
||||||
|
}
|
||||||
|
|
||||||
while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback)
|
if (c.callback == _callback_table_count) {
|
||||||
temp_callback++;
|
|
||||||
if (temp_callback == _callback_table_count) {
|
|
||||||
DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
|
DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
|
||||||
temp_callback = 0; /* _callback_table[0] == NULL */
|
c.callback = 0; // _callback_table[0] == NULL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ttd_strlcpy(c.text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c.text));
|
||||||
|
|
||||||
if (_network_server) {
|
if (_network_server) {
|
||||||
// We are the server, so set the command to be executed next possible frame
|
/* If we are the server, we queue the command in our 'special' queue.
|
||||||
c->frame = _frame_counter_max + 1;
|
* In theory, we could execute the command right away, but then the
|
||||||
} else {
|
* client on the server can do everything 1 tick faster than others.
|
||||||
c->frame = 0; // The client can't tell which frame, so just make it 0
|
* So to keep the game fair, we delay the command with 1 tick
|
||||||
}
|
* which gives about the same speed as most clients.
|
||||||
|
*/
|
||||||
|
c.frame = _frame_counter_max + 1;
|
||||||
|
|
||||||
ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text));
|
CommandPacket *new_cp = MallocT<CommandPacket>(1);
|
||||||
|
*new_cp = c;
|
||||||
if (_network_server) {
|
|
||||||
// If we are the server, we queue the command in our 'special' queue.
|
|
||||||
// In theory, we could execute the command right away, but then the
|
|
||||||
// client on the server can do everything 1 tick faster than others.
|
|
||||||
// So to keep the game fair, we delay the command with 1 tick
|
|
||||||
// which gives about the same speed as most clients.
|
|
||||||
NetworkTCPSocketHandler *cs;
|
|
||||||
|
|
||||||
// And we queue it for delivery to the clients
|
|
||||||
FOR_ALL_CLIENTS(cs) {
|
|
||||||
if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, c);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only the server gets the callback, because clients should not get them
|
|
||||||
c->callback = temp_callback;
|
|
||||||
if (_local_command_queue == NULL) {
|
if (_local_command_queue == NULL) {
|
||||||
_local_command_queue = c;
|
_local_command_queue = new_cp;
|
||||||
} else {
|
} else {
|
||||||
// Find last packet
|
/* Find last packet */
|
||||||
CommandPacket *cp = _local_command_queue;
|
CommandPacket *cp = _local_command_queue;
|
||||||
while (cp->next != NULL) cp = cp->next;
|
while (cp->next != NULL) cp = cp->next;
|
||||||
cp->next = c;
|
cp->next = new_cp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Only the local client (in this case, the server) gets the callback */
|
||||||
|
c.callback = 0;
|
||||||
|
/* And we queue it for delivery to the clients */
|
||||||
|
NetworkTCPSocketHandler *cs;
|
||||||
|
FOR_ALL_CLIENTS(cs) {
|
||||||
|
if (cs->status > STATUS_AUTH) NetworkAddCommandQueue(cs, &c);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clients send their command to the server and forget all about the packet
|
c.frame = 0; // The client can't tell which frame, so just make it 0
|
||||||
c->callback = temp_callback;
|
|
||||||
SEND_COMMAND(PACKET_CLIENT_COMMAND)(c);
|
/* Clients send their command to the server and forget all about the packet */
|
||||||
|
SEND_COMMAND(PACKET_CLIENT_COMMAND)(&c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute a DoCommand we received from the network
|
// Execute a DoCommand we received from the network
|
||||||
|
|
Loading…
Reference in New Issue