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 {
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 {
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;
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) {
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;
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);
}
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) {
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 {
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);
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;
}