summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-06-10 19:59:08 +0000
committerrubidium <rubidium@openttd.org>2007-06-10 19:59:08 +0000
commitd0ad020dc685299fd3efa6aadeaea009ccbaa300 (patch)
treea0123ab67cfe66dd0e151744b08f986889ada287
parent7767e466e58ee157fb40c86d7c13f3f826e31193 (diff)
downloadopenttd-d0ad020dc685299fd3efa6aadeaea009ccbaa300.tar.xz
(svn r10082) -Fix [FS#846]: another memory leak in the networking code (benc).
-rw-r--r--src/network/network_data.cpp84
1 files changed, 40 insertions, 44 deletions
diff --git a/src/network/network_data.cpp b/src/network/network_data.cpp
index 7cb27adf7..627d5bfbd 100644
--- a/src/network/network_data.cpp
+++ b/src/network/network_data.cpp
@@ -30,65 +30,61 @@ void NetworkAddCommandQueue(NetworkTCPSocketHandler *cs, CommandPacket *cp)
// Prepare a DoCommand to be send over the network
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
{
- CommandPacket *c = MallocT<CommandPacket>(1);
- byte temp_callback;
-
- c->player = _local_player;
- c->next = NULL;
- c->tile = tile;
- c->p1 = p1;
- c->p2 = p2;
- c->cmd = cmd;
- c->callback = 0;
-
- temp_callback = 0;
-
- while (temp_callback < _callback_table_count && _callback_table[temp_callback] != callback)
- temp_callback++;
- if (temp_callback == _callback_table_count) {
- DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
- temp_callback = 0; /* _callback_table[0] == NULL */
+ CommandPacket c;
+
+ c.player = _local_player;
+ c.next = NULL;
+ c.tile = tile;
+ c.p1 = p1;
+ c.p2 = p2;
+ c.cmd = cmd;
+
+ c.callback = 0;
+ while (c.callback < _callback_table_count && _callback_table[c.callback] != callback) {
+ c.callback++;
}
- if (_network_server) {
- // We are the server, so set the command to be executed next possible frame
- c->frame = _frame_counter_max + 1;
- } else {
- c->frame = 0; // The client can't tell which frame, so just make it 0
+ if (c.callback == _callback_table_count) {
+ DEBUG(net, 0, "Unknown callback. (Pointer: %p) No callback sent", callback);
+ c.callback = 0; // _callback_table[0] == NULL
}
- ttd_strlcpy(c->text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c->text));
+ ttd_strlcpy(c.text, (_cmd_text != NULL) ? _cmd_text : "", lengthof(c.text));
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 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.
+ */
+ c.frame = _frame_counter_max + 1;
+
+ CommandPacket *new_cp = MallocT<CommandPacket>(1);
+ *new_cp = c;
if (_local_command_queue == NULL) {
- _local_command_queue = c;
+ _local_command_queue = new_cp;
} else {
- // Find last packet
+ /* Find last packet */
CommandPacket *cp = _local_command_queue;
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;
}
- // Clients send their command to the server and forget all about the packet
- c->callback = temp_callback;
- SEND_COMMAND(PACKET_CLIENT_COMMAND)(c);
+ c.frame = 0; // The client can't tell which frame, so just make it 0
+
+ /* 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