}
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);
string.advance(^current_data, sizeof typeof *header);
return_block :: macro () {
- printf("DISCARDING: {}\n", peer_id);
return host.event.type != .None;
}
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 {
next :: (use ctx: ^Context) -> (^Event, bool) {
if host_pulse(host, timeout) {
- host.produced_event = false;
return ^host.event, true;
}
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;
}
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 {
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;
}
}
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]);
}
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;
}
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);
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;
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;
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);
+ }
}
}
}
while true {
- println("Getting events...");
for server->get_events(timeout = 2000) {
printf("{*}\n", it);
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);
+ }
+ }
+ }
}
}