code cleanup; removing uses of 'memory' package
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Apr 2022 20:53:27 +0000 (15:53 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Apr 2022 20:53:27 +0000 (15:53 -0500)
example/udp_client.onyx
src/host.onyx
src/peer.onyx

index cd943c01b2975b047df27b80b0d9c46c3d789a4a..71b5313d320fd76d0a80f8ce3109de8ca3b51fd7 100644 (file)
@@ -27,11 +27,7 @@ main :: (args) => {
     peer := onyx_net.host_connect(host, ^addr, 2);
 
     input_thread: thread.Thread;
-    td_type :: struct { peer: ^onyx_net.Peer; } 
-    td := td_type.{ peer };
-
-    thread.spawn(^input_thread, ^td, (data) => {
-        peer := data.peer;
+    thread.spawn(^input_thread, peer, (peer) => {
         input_reader := io.reader_make(^stdin);
 
         while true {
index 978a0b83aff9d274497c7e0d28a39af7ab2276b8..ab7f5e45f71dbae421af30ef126fc835f7add752 100644 (file)
@@ -38,8 +38,8 @@ host_create :: (addr: ^net.Socket_Address, peer_count: u32) -> (^Host, Host_Crea
     host := new(Host);
     defer if errored do cfree(host);
 
-    memory.alloc_slice(^host.peers, peer_count);
-    defer if errored do memory.free_slice(^host.peers);
+    host.peers = make([] Peer, peer_count);
+    defer if errored do delete(^host.peers);
     host.connected_peers = 0;
 
     if socket, err := net.socket_create(.Inet, .Dgram); err != .None {
@@ -92,7 +92,7 @@ host_connect :: (host: ^Host, addr: ^net.Socket_Address, channel_count: u32) ->
 
     if peer == null do return null;
 
-    memory.alloc_slice(^peer.channels, channel_count);
+    peer.channels = make([] Channel, channel_count);
     peer.host = host;
     peer.state = .Connecting;
     peer.addr = *addr;
@@ -118,8 +118,8 @@ host_connect :: (host: ^Host, addr: ^net.Socket_Address, channel_count: u32) ->
 
 host_free :: (host: ^Host) {
     net.socket_close(^host.socket);
-    memory.free_slice(^host.peers);
-    cfree(host);
+    delete(^host.peers);
+    delete(host);
 }
 
 host_broadcast :: (host: ^Host, channel: Channel_ID, packet: ^Packet) {
@@ -421,7 +421,7 @@ host_get_events :: (host: ^Host, timeout: u32 = 0) -> Iterator(^Event) {
     if peer == null do return null;
 
     channel_count := net.network_to_host(command.channel_count);
-    memory.alloc_slice(^peer.channels, channel_count);
+    peer.channels = make([] Channel, channel_count);
     for ^peer.channels do peer_setup_channel(peer, it);
 
     peer.state = .Acknowledging_Connection;
@@ -533,7 +533,7 @@ host_get_events :: (host: ^Host, timeout: u32 = 0) -> Iterator(^Event) {
         fragment = array.alloc_one(^channel.pending_fragments);
         fragment.fragment_id = fragment_id;
         fragment.fragments_remaining = net.network_to_host(command.fragment_count);
-        memory.alloc_slice(^fragment.data, net.network_to_host(command.total_length));
+        fragment.data = make([] u8, net.network_to_host(command.total_length));
     }
 
     memory.copy(fragment.data.data + net.network_to_host(command.fragment_offset), data.data, data_length);
index c6e5128a533c35c4ef91ba16de83e16596c2a4cb..e8c003422b01ef4b1453b92425f009fb56a2cbd2 100644 (file)
@@ -56,9 +56,9 @@ Peer :: struct {
 }
 
 peer_destroy :: (peer: ^Peer) {
-    if peer.channels.data != null          do memory.free_slice(^peer.channels);
-    if peer.outgoing_commands.data != null do array.free(^peer.outgoing_commands);
-    if peer.incoming_commands.data != null do array.free(^peer.incoming_commands);
+    if peer.channels.data != null          do delete(^peer.channels);
+    if peer.outgoing_commands.data != null do delete(^peer.outgoing_commands);
+    if peer.incoming_commands.data != null do delete(^peer.incoming_commands);
 }
 
 peer_disconnect :: (peer: ^Peer) {
@@ -75,8 +75,8 @@ peer_setup_channel :: (peer: ^Peer, channel: ^Channel) {
     channel.reliable_windows_cursor = 0;
     channel.next_fragment_id = 0;
 
-    memory.alloc_slice(^channel.reliable_windows, 32);
-    for ^channel.reliable_windows do *it = 65535;
+    channel.reliable_windows = make([] u16, 32);
+    array.fill(channel.reliable_windows, 65535);
 }
 
 peer_queue_outgoing_command :: #match {
@@ -229,6 +229,8 @@ peer_flush_outgoing_commands :: (peer: ^Peer) -> i32 {
 peer_send_acknowledgements :: (peer: ^Peer) {
     send_buffer: [65535] u8;
 
+    bytes_to_be_sent := 0;
+
     for ^ack: peer.acknowledgements {
         seq_num := net.host_to_network(ack.command.seq_number);
 
@@ -242,10 +244,21 @@ peer_send_acknowledgements :: (peer: ^Peer) {
         out: Outgoing_Command;
         out.command = ^command;
         
-        to_send, success := out->pack_into_buffer(peer, send_buffer);
+        to_send, success := out->pack_into_buffer(peer, send_buffer[bytes_to_be_sent..65535]);
         if !success do continue;
 
-        sent_bytes := peer.host.socket->sendto(to_send, ^peer.addr);
+        bytes_to_be_sent += to_send.count;
+        
+        if bytes_to_be_sent >= peer.mtu - sizeof Protocol_Acknowledge {
+            sent_bytes := peer.host.socket->sendto(send_buffer[0 .. bytes_to_be_sent], ^peer.addr);
+            if sent_bytes < 0 do return;
+
+            bytes_to_be_sent = 0;
+        }
+    }
+
+    if bytes_to_be_sent > 0 {
+        sent_bytes := peer.host.socket->sendto(send_buffer[0 .. bytes_to_be_sent], ^peer.addr);
         if sent_bytes < 0 do return;
     }