From: Brendan Hansen Date: Tue, 29 Mar 2022 04:06:20 +0000 (-0500) Subject: bugfixes and cleanup X-Git-Tag: v0.0.1~10 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=2e655165e47648923c4166145c05f08d32436e13;p=onyx_net.git bugfixes and cleanup --- diff --git a/src/host.onyx b/src/host.onyx index 85ec307..6cc260b 100644 --- a/src/host.onyx +++ b/src/host.onyx @@ -128,7 +128,8 @@ host_broadcast :: (host: ^Host, channel: Channel_ID, packet: ^Packet) { } host_pulse :: (host: ^Host, timeout: u32 = 0) -> bool { - host.current_time = ~~ os.time(); + host.produced_event = false; + host.current_time = ~~ (os.time() & cast(u64) 0xFFFFFFFF); sent_command := host_send_commands(host); received_command := host_receive_commands(host, timeout); @@ -202,7 +203,6 @@ host_handle_incoming_commands :: (host: ^Host) -> bool { string.advance(^current_data, sizeof typeof *header); return_block :: macro () { - printf("DISCARDING: {}\n", peer_id); return host.event.type != .None; } @@ -211,7 +211,9 @@ host_handle_incoming_commands :: (host: ^Host) -> bool { command := cast(^Protocol_Command_Header) current_data.data; command_id := command_get_effective(command.command); - printf("Received command: {} {*p}\n", command_sizes[command_id], cast(Command) command_id, command); + command.seq_number = net.network_to_host(command.seq_number); + + // printf("Received command: {} {} {*p}\n", command_sizes[command_id], cast(Command) command_id, command); string.advance(^current_data, command_sizes[command_id]); switch command_id { @@ -278,7 +280,6 @@ host_get_events :: (host: ^Host, timeout: u32 = 0) -> Iterator(^Event) { next :: (use ctx: ^Context) -> (^Event, bool) { if host_pulse(host, timeout) { - host.produced_event = false; return ^host.event, true; } @@ -323,7 +324,7 @@ host_get_events :: (host: ^Host, timeout: u32 = 0) -> Iterator(^Event) { recv_sent_time := cast(u32) net.network_to_host(command.recv_sent_time); recv_sent_time |= host.current_time & 0xFFFF0000; - if (recv_sent_time & 0x8000) > (host.current_time & 0x8000) { + if (recv_sent_time & 0x8000) == 0x8000 && (host.current_time & 0x8000) == 0 { recv_sent_time -= 0x10000; } @@ -335,7 +336,7 @@ host_get_events :: (host: ^Host, timeout: u32 = 0) -> Iterator(^Event) { recv_seq_number := net.network_to_host(command.recv_seq_number); removed_command_id := peer_remove_sent_reliable_command(peer, recv_seq_number, command.channel); - printf("Received acknowledgement for {}.\n", removed_command_id); + // printf("Received acknowledgement for {}.\n", removed_command_id); switch peer.state { case .Acknowledging_Connection { @@ -368,7 +369,7 @@ host_get_events :: (host: ^Host, timeout: u32 = 0) -> Iterator(^Event) { break; } elseif it.state == .Connecting && it.addr.addr == host.received_addr.addr { - if it.addr.port == host.received_addr.port && it.connect_id == command.connect_id do return null; + if it.addr.port == host.received_addr.port && it.connect_id == net.network_to_host(command.connect_id) do return null; } } diff --git a/src/packet.onyx b/src/packet.onyx index 77175c7..99eca06 100644 --- a/src/packet.onyx +++ b/src/packet.onyx @@ -42,19 +42,18 @@ outgoing_command_pack_into_buffer :: (out: ^Outgoing_Command, peer: ^Peer, buf: write_offset += data.count; } - peer_id := net.host_to_network(peer.outgoing_id); - sent_time := net.host_to_network(cast(u16)(peer.host.current_time & 0xFFFF)); + header: Protocol_Header; + header.peer_id = net.host_to_network(peer.outgoing_id); + header.sent_time = net.host_to_network(cast(u16)(peer.host.current_time & 0xFFFF)); // // Connect packets expect to have a peer id of FFFF. // However, the outgoing_id needs to remain its original value. if command_get_effective(out.command.command) == .Connect { - peer_id = 65535; + header.peer_id = 65535; } - @HACK @HACK - write((cast(^u8) ^peer_id)[0 .. 2]); - write((cast(^u8) ^sent_time)[0 .. 2]); + write((cast(^u8) ^header)[0 .. sizeof typeof header]); command_size := command_sizes[~~ command_get_effective(out.command.command)]; write((cast(^u8) out.command)[0 .. command_size]); diff --git a/src/peer.onyx b/src/peer.onyx index 1bebba7..926be77 100644 --- a/src/peer.onyx +++ b/src/peer.onyx @@ -98,12 +98,16 @@ peer_setup_outgoing_command :: (peer: ^Peer, command: ^Outgoing_Command) { } peer_send :: (peer: ^Peer, channel_id: Channel_ID, packet: ^Packet) -> bool { - if peer.state != .Connected || ~~channel_id > peer.channels.count do return false; + if peer.state != .Connected || ~~channel_id > peer.channels.count { + return false; + } channel := ^peer.channels[channel_id]; @TODO // Fragmented sending - if packet.data.count > peer.mtu - sizeof Protocol_Header - sizeof Protocol_Send do return false; + if packet.data.count > peer.mtu - sizeof Protocol_Header - sizeof Protocol_Send { + return false; + } command := new(Protocol_Send); command.channel = channel_id; @@ -142,7 +146,10 @@ peer_flush_outgoing_commands :: (peer: ^Peer) -> i32 { } total_sent += sent_bytes; - peer.sent_reliable_commands << it; + + if (it.command.command & .Flag_Acknowledge) != 0 { + peer.sent_reliable_commands << it; + } } array.clear(^peer.outgoing_commands); @@ -157,8 +164,8 @@ peer_send_acknowledgements :: (peer: ^Peer) { command.command = .Acknowledge; command.channel = ack.command.channel; command.seq_number = 0; - command.recv_seq_number = ack.command.seq_number; - command.recv_sent_time = ack.sent_time; + command.recv_seq_number = net.host_to_network(ack.command.seq_number); + command.recv_sent_time = net.host_to_network(ack.sent_time); out: Outgoing_Command; out.command = ^command; diff --git a/udp_client.onyx b/udp_client.onyx index b7b244b..d721fb9 100644 --- a/udp_client.onyx +++ b/udp_client.onyx @@ -27,10 +27,13 @@ main :: (args) => { peer := onyx_net.host_connect(host, ^addr, 2); while true { - println("Waiting for events...."); - for host->get_events(timeout=1000) { + for host->get_events(timeout=500) { printf("{*}\n", it); + if it.type == .Connection { + println("Successfully connected to server!"); + } + if it.type == .Message { packet := new(onyx_net.Packet); packet.flags |= .Reliable; @@ -39,38 +42,14 @@ main :: (args) => { onyx_net.peer_send(it.peer, 0, packet); } } - } -} - -old_main :: (args) => { - udp_socket, err := net.socket_create(.Inet, .Dgram); - assert(err == .None, "Failed to create socket"); - defer udp_socket->close(); - - dest_addr: net.Socket_Address; - dest_addr.port = 8080; - // dest_addr.addr = octets_to_addr(45, 76, 30, 19); - dest_addr.addr = octets_to_addr(127, 0, 0, 1); - - random.set_seed(12341); - for 10 { - to_send := random.string(50, alpha_numeric=true); + if random.between(0, 3) == 0 { + packet := new(onyx_net.Packet); + packet.flags |= .Reliable; + packet.data = "What's Up?"; + println("Sending what's up..."); - sent_bytes := udp_socket->sendto(to_send, ^dest_addr); - printf("Sent {} bytes!\n", sent_bytes); - - string.free(to_send); - } - - for 1000 { - recv_buffer: [1024] u8; - recv_addr, recv_bytes := udp_socket->recvfrom(recv_buffer); - - tmp := recv_addr->addr_as_str(); - printf("{} sent {} bytes\n", tmp, recv_bytes); - string.free(tmp); - - printf("{}\n", recv_buffer[0 .. recv_bytes]); + onyx_net.peer_send(peer, 0, packet); + } } } diff --git a/udp_server.onyx b/udp_server.onyx index 304839e..90e924e 100644 --- a/udp_server.onyx +++ b/udp_server.onyx @@ -16,7 +16,6 @@ main :: (args) => { } while true { - println("Getting events..."); for server->get_events(timeout = 2000) { printf("{*}\n", it); @@ -25,33 +24,18 @@ main :: (args) => { packet.flags |= .Reliable; packet.data = "Welcome!!!"; - onyx_net.peer_send(it.peer, 0, packet); + onyx_net.host_broadcast(server, 0, packet); } - } - } -} - -old_main :: (args) => { - - udp_socket, err := net.socket_create(.Inet, .Dgram); - assert(err == .None, "Failed to create socket"); - - assert(udp_socket->bind(8080), "Failed to bind socket"); - udp_socket->setting(.NonBlocking, 1); + if it.type == .Message { + if it.data == "What's Up?" { + packet := new(onyx_net.Packet); + packet.flags |= .Reliable; + packet.data = "Not much"; - while true { - recv_buffer: [1024] u8; - recv_addr, recv_bytes := udp_socket->recvfrom(recv_buffer); - - tmp := recv_addr->addr_as_str(); - printf("{} sent {} bytes\n", tmp, recv_bytes); - string.free(tmp); - - printf("{}\n", recv_buffer[0 .. recv_bytes]); - - send_buffer: [1024] u8; - to_send := conv.format(send_buffer, "Recieved: '{}'", recv_buffer[0 .. recv_bytes]); - udp_socket->sendto(to_send, ^recv_addr); + onyx_net.peer_send(it.peer, 0, packet); + } + } + } } }