From d851e9c7762bba2c40e9ea0e4fdeb4e935009406 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 7 Apr 2022 15:53:27 -0500 Subject: [PATCH] code cleanup; removing uses of 'memory' package --- example/udp_client.onyx | 6 +----- src/host.onyx | 14 +++++++------- src/peer.onyx | 27 ++++++++++++++++++++------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/example/udp_client.onyx b/example/udp_client.onyx index cd943c0..71b5313 100644 --- a/example/udp_client.onyx +++ b/example/udp_client.onyx @@ -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 { diff --git a/src/host.onyx b/src/host.onyx index 978a0b8..ab7f5e4 100644 --- a/src/host.onyx +++ b/src/host.onyx @@ -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); diff --git a/src/peer.onyx b/src/peer.onyx index c6e5128..e8c0034 100644 --- a/src/peer.onyx +++ b/src/peer.onyx @@ -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; } -- 2.25.1