freeing packets and commands
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Apr 2022 18:40:09 +0000 (13:40 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Apr 2022 18:40:09 +0000 (13:40 -0500)
example/udp_client.onyx
example/udp_server.onyx
src/packet.onyx
src/peer.onyx

index 507545a49ee87f48ce906e623ac4cd48e3469f89..cd943c01b2975b047df27b80b0d9c46c3d789a4a 100644 (file)
@@ -1,5 +1,5 @@
 #load "core/std"
-#load "src/module"
+#load "./../src/module"
 
 use package core
 onyx_net :: package onyx_net
index 184127a126f3139c4ed34ce536b695ed758ada71..184b9f8610b5c2a331708656676df21de95c38e0 100644 (file)
@@ -1,5 +1,5 @@
 #load "core/std"
-#load "src/module"
+#load "./../src/module"
 
 use package core
 onyx_net :: package onyx_net
@@ -36,7 +36,7 @@ main :: (args) => {
                 if it.data == "What's Up?" {
                     packet := new(onyx_net.Packet);
                     packet.flags |= .Reliable;
-                    packet.data = #file_contents "src/host.onyx";
+                    packet.data = #file_contents "./../src/host.onyx";
 
                     onyx_net.peer_send(it.peer, 0, packet);
                 }
index ace41865aeb2f3fdfbbe933937bb58cc1c658da9..a2a7e58d5d575dfb37ed2a010c063ccee9fd5a60 100644 (file)
@@ -26,6 +26,14 @@ Packet :: struct {
 
     flags: Flags;
     data: [] u8;
+
+    // Set this to true to have the internal code
+    // free the data when it is done with the packet.
+    free_data := false;
+
+    // The allocator used for the data. Only used if
+    // free_data is set to true.
+    data_allocator := context.allocator;
 }
 
 Acknowledgement :: struct {
index e40d929c49721e9cadbea14fb241e4fde3c788f9..6a2b1c49202fba5e9bd2c21f2524bf16ae42482c 100644 (file)
@@ -216,6 +216,8 @@ peer_flush_outgoing_commands :: (peer: ^Peer) -> i32 {
         
         if (it.command.command & .Flag_Acknowledge) != 0 {
             peer.sent_reliable_commands << it;
+        } else {
+            peer_free_outgoing_command(peer, it);
         }
     }
 
@@ -284,13 +286,20 @@ peer_remove_sent_reliable_command :: (peer: ^Peer, seq_num: u16, channel: Channe
 
     if command == null do return .None;
 
-    defer {
-        cfree(command.command);
-        cfree(command);
-    }
+    defer peer_free_outgoing_command(peer, command);
     return command_get_effective(command.command.command);
 }
 
+peer_free_outgoing_command :: (peer: ^Peer, command: ^Outgoing_Command) {
+    if command.packet.free_data {
+        raw_free(command.packet.data_allocator, command.packet.data.data);
+    }
+
+    if command.packet != null do cfree(command.packet);
+    cfree(command.command);
+    cfree(command);
+}
+
 
 Channel_ID :: u8;