working net sequencing

This commit is contained in:
jacob 2026-01-16 15:21:39 -06:00
parent 673c865d21
commit e6f1bb6661
2 changed files with 85 additions and 98 deletions

View File

@ -103,13 +103,6 @@ void NET_W32_SignalWorker(void)
(struct sockaddr *)&NET_W32.wake_recv_sock.addr, (struct sockaddr *)&NET_W32.wake_recv_sock.addr,
NET_W32.wake_send_sock.addr_size NET_W32.wake_send_sock.addr_size
); );
i32 wsa_err = WSAGetLastError();
DEBUGBREAKABLE;
} }
NET_W32_Peer *NET_W32_TouchPeerFromKey(NET_W32_Pipe *pipe, NET_Key key) NET_W32_Peer *NET_W32_TouchPeerFromKey(NET_W32_Pipe *pipe, NET_Key key)
@ -377,7 +370,6 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
for (NET_W32_Peer *peer = pipe->first_peer; peer; peer = peer->next) for (NET_W32_Peer *peer = pipe->first_peer; peer; peer = peer->next)
{ {
peer->num_msg_packets_received_this_frame = 0; peer->num_msg_packets_received_this_frame = 0;
peer->num_msg_packets_sent_this_frame = 0;
} }
////////////////////////////// //////////////////////////////
@ -559,23 +551,10 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
src_data.text = buff + sizeof(header); src_data.text = buff + sizeof(header);
src_data.len = len - sizeof(header); src_data.len = len - sizeof(header);
if (!(header.flags & NET_W32_PacketFlag_Heartbeat))
{
LogDebugF(
"Received msg packet. seq: %F, msg seq: %F, data: \"%F\"",
FmtSint(header.seq),
FmtSint(header.msg_seq),
FmtString(src_data)
);
}
//- Fetch peer //- Fetch peer
NET_W32_Peer *peer = NET_W32_TouchPeerFromKey(pipe, key); NET_W32_Peer *peer = NET_W32_TouchPeerFromKey(pipe, key);
peer->key = key; peer->key = key;
peer->last_packet_received_ns = TimeNs();
//- Read packet
{
// Update remote stats
if (header.bottom_ack == peer->remote_bottom_ack) if (header.bottom_ack == peer->remote_bottom_ack)
{ {
peer->remote_ack_bits |= header.ack_bits; peer->remote_ack_bits |= header.ack_bits;
@ -586,6 +565,9 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
peer->remote_ack_bits = header.ack_bits; peer->remote_ack_bits = header.ack_bits;
} }
//- Read msg packet
if (!(header.flags & NET_W32_PacketFlag_Heartbeat))
{
// Update our stats // Update our stats
b32 is_sequential = 0; b32 is_sequential = 0;
b32 should_process = 0; b32 should_process = 0;
@ -594,17 +576,13 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
is_sequential = 1; is_sequential = 1;
should_process = 1; should_process = 1;
} }
if (header.seq > peer->bottom_ack + 1 && header.seq < peer->bottom_ack + 65) else if (header.seq > peer->bottom_ack + 1 && header.seq <= peer->bottom_ack + 65)
{ {
u64 ack_bit = (u64)1 << (header.seq - 2 - peer->bottom_ack); u64 ack_bit = (u64)1 << (header.seq - peer->bottom_ack - 2);
should_process = !!(peer->ack_bits & ack_bit); should_process = !(peer->ack_bits & ack_bit);
peer->ack_bits |= ack_bit; peer->ack_bits |= ack_bit;
} }
if (!(header.flags & NET_W32_PacketFlag_Heartbeat))
{
peer->num_msg_packets_received_this_frame += 1; peer->num_msg_packets_received_this_frame += 1;
}
peer->last_packet_received_ns = TimeNs();
// Process packet // Process packet
if (should_process) if (should_process)
@ -681,10 +659,28 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
peer->first_fragmented_packet = contig_end->next; peer->first_fragmented_packet = contig_end->next;
contig_end->next = 0; contig_end->next = 0;
peer->ack_bits >>= (contig_end->seq - peer->bottom_ack); i64 diff = contig_end->seq - peer->bottom_ack;
if (diff <= 63)
{
peer->ack_bits >>= diff;
}
else
{
peer->ack_bits = 0;
}
peer->bottom_ack = contig_end->seq; peer->bottom_ack = contig_end->seq;
} }
} }
LogDebugF(
"Received msg packet. seq: %F, msg seq: %F, new bottom ack: %F, new ack bits: %F, should_process: %F, data: \"%F\"",
FmtSint(header.seq),
FmtSint(header.msg_seq),
FmtSint(peer->bottom_ack),
FmtUint(peer->ack_bits),
FmtUint(should_process),
FmtString(src_data)
);
} }
} }
} }
@ -751,11 +747,6 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
FmtUint(msg->data.len), FmtUint(msg->data.len),
FmtString(msg->data) FmtString(msg->data)
); );
if (msg->data.len != 1620)
{
DEBUGBREAKABLE;
}
} }
} }
else else
@ -841,6 +832,52 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
} }
} }
//////////////////////////////
//- Send heartbeat packets
for (NET_W32_Peer *peer = pipe->first_peer; peer; peer = peer->next)
{
b32 should_send_heartbeat = 0;
// Send heartbeat packet if we received any packets this frame
if (peer->num_msg_packets_received_this_frame > 0)
{
should_send_heartbeat = 1;
}
// Send heartbeat if we haven't sent any packets in a while
i64 now_ns = TimeNs();
if (now_ns - peer->last_packet_sent_ns > heartbeat_threshold_ns)
{
should_send_heartbeat = 1;
}
if (should_send_heartbeat)
{
struct sockaddr_in6 addr = NET_W32_AddressFromKey(peer->key);
i64 buff_len = 0;
u8 buff[Kibi(2)];
NET_W32_PacketHeader header = Zi;
{
header.magic = magic;
header.flags = NET_W32_PacketFlag_Heartbeat;
header.bottom_ack = peer->bottom_ack;
header.ack_bits = peer->ack_bits;
}
CopyBytes(buff, &header, sizeof(header));
buff_len += sizeof(header);
sendto(
pipe->udp,
(char *)buff,
buff_len,
0,
(struct sockaddr *)&addr,
sizeof(addr)
);
peer->last_packet_sent_ns = now_ns;
}
}
////////////////////////////// //////////////////////////////
//- Send message packets //- Send message packets
@ -858,7 +895,7 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
struct sockaddr_in6 addr = NET_W32_AddressFromKey(key); struct sockaddr_in6 addr = NET_W32_AddressFromKey(key);
// TODO: Maybe send more than just bottom_ack + 65 packets at a time. // TODO: Maybe send more than just bottom_ack + 65 packets at a time.
for (NET_W32_Packet *packet = peer->first_remote_packet; packet && packet->seq < bottom_ack + 65;) for (NET_W32_Packet *packet = peer->first_remote_packet; packet && packet->seq <= bottom_ack + 65;)
{ {
NET_W32_Packet *next = packet->next; NET_W32_Packet *next = packet->next;
{ {
@ -869,7 +906,7 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
{ {
is_acked = 1; is_acked = 1;
} }
else if (seq > bottom_ack + 1 && packet->seq < bottom_ack + 65) else if (seq > bottom_ack + 1 && packet->seq <= bottom_ack + 65)
{ {
u64 ack_bit = (u64)1 << (seq - 2 - bottom_ack); u64 ack_bit = (u64)1 << (seq - 2 - bottom_ack);
is_acked = !!(ack_bits & ack_bit); is_acked = !!(ack_bits & ack_bit);
@ -886,7 +923,6 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
i64 now_ns = TimeNs(); i64 now_ns = TimeNs();
if (packet->last_sent_ns == 0 || (now_ns - packet->last_sent_ns > msg_resend_threshold_ns)) if (packet->last_sent_ns == 0 || (now_ns - packet->last_sent_ns > msg_resend_threshold_ns))
{ {
peer->num_msg_packets_sent_this_frame += 1;
peer->last_packet_sent_ns = now_ns; peer->last_packet_sent_ns = now_ns;
packet->last_sent_ns = now_ns; packet->last_sent_ns = now_ns;
@ -933,54 +969,6 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
packet = next; packet = next;
} }
} }
//////////////////////////////
//- Send heartbeats
for (NET_W32_Peer *peer = pipe->first_peer; peer; peer = peer->next)
{
b32 should_send_heartbeat = 0;
// If we received a message from a peer this frame but did not send any
// back, we should send a heartbeat packet so that the peer can still
// receive up to date ack info
if (peer->num_msg_packets_received_this_frame > 0 && peer->num_msg_packets_sent_this_frame == 0)
{
should_send_heartbeat = 1;
}
// Send heartbeat if we haven't sent any packets in a while
i64 now_ns = TimeNs();
if (now_ns - peer->last_packet_sent_ns > heartbeat_threshold_ns)
{
should_send_heartbeat = 1;
}
if (should_send_heartbeat)
{
struct sockaddr_in6 addr = NET_W32_AddressFromKey(peer->key);
i64 buff_len = 0;
u8 buff[Kibi(2)];
NET_W32_PacketHeader header = Zi;
{
header.magic = magic;
header.flags = NET_W32_PacketFlag_Heartbeat;
header.bottom_ack = peer->bottom_ack;
header.ack_bits = peer->ack_bits;
}
CopyBytes(buff, &header, sizeof(header));
buff_len += sizeof(header);
sendto(
pipe->udp,
(char *)buff,
buff_len,
0,
(struct sockaddr *)&addr,
sizeof(addr)
);
peer->last_packet_sent_ns = now_ns;
}
}
} }
EndScratch(scratch); EndScratch(scratch);

View File

@ -81,9 +81,9 @@ Struct(NET_W32_Peer)
NET_Key key; NET_Key key;
i64 remote_bottom_ack; i64 remote_bottom_ack;
i64 remote_ack_bits; u64 remote_ack_bits;
i64 bottom_ack; i64 bottom_ack;
i64 ack_bits; u64 ack_bits;
NET_W32_Packet *first_remote_packet; NET_W32_Packet *first_remote_packet;
NET_W32_Packet *last_remote_packet; NET_W32_Packet *last_remote_packet;
@ -101,7 +101,6 @@ Struct(NET_W32_Peer)
i64 last_packet_received_ns; i64 last_packet_received_ns;
i64 last_packet_sent_ns; i64 last_packet_sent_ns;
i64 num_msg_packets_received_this_frame; i64 num_msg_packets_received_this_frame;
i64 num_msg_packets_sent_this_frame;
}; };
Struct(NET_W32_PeerBin) Struct(NET_W32_PeerBin)