networking progress

This commit is contained in:
jacob 2026-01-15 19:00:45 -06:00
parent 9f0ea6e45b
commit 418465408f
2 changed files with 100 additions and 8 deletions

View File

@ -1,3 +1,5 @@
#define NET_PacketSize 1024
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Opaque types //~ Opaque types

View File

@ -237,6 +237,7 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
u32 magic; u32 magic;
PacketFlag flags; PacketFlag flags;
i64 seq; i64 seq;
i64 msg_seq;
i64 bottom_ack; i64 bottom_ack;
u64 ack_bits; u64 ack_bits;
}; };
@ -280,6 +281,8 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
////////////////////////////// //////////////////////////////
//- Read socket //- Read socket
// TODO: Rate limit
{ {
i32 len = 0; i32 len = 0;
while (len >= 0) while (len >= 0)
@ -288,7 +291,11 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
struct sockaddr_in6 addr = peer->addr; struct sockaddr_in6 addr = peer->addr;
i32 addr_sz = sizeof(addr); i32 addr_sz = sizeof(addr);
len = recvfrom(pipe->udp, buff, countof(buff), 0, (struct sockaddr *)&sockaddr_in6, &addr_sz); len = recvfrom(pipe->udp, buff, countof(buff), 0, (struct sockaddr *)&sockaddr_in6, &addr_sz);
if (len >= sizeof(PacketHeader) && MatchBytes(buff, &magic, sizeof(magic))) if (
len >= sizeof(PacketHeader) &&
len <= sizeof(PacketHeader) + NET_PacketSize &&
MatchBytes(buff, &magic, sizeof(magic))
)
{ {
NET_Key key = NET_W32_KeyFromAddress(addr); NET_Key key = NET_W32_KeyFromAddress(addr);
PacketHeader header = Zi; PacketHeader header = Zi;
@ -321,8 +328,8 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
peer = PushStruct(perm, NET_W32_Peer); peer = PushStruct(perm, NET_W32_Peer);
} }
peer->key = key; peer->key = key;
DllQueueInsert(pipe->first_peer, pipe->last_peer, pipe); DllQueuePush(pipe->first_peer, pipe->last_peer, pipe);
DllQueueInsertNP(bin->first, bin->last, pipe, next_in_bin, prev_in_bin); DllQueuePushNP(bin->first, bin->last, pipe, next_in_bin, prev_in_bin);
} }
} }
@ -342,14 +349,16 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
} }
// Update our acks // Update our acks
b32 is_sequential = 0;
b32 should_process = 0; b32 should_process = 0;
if (header.seq == peer->bottom_ack + 1) if (header.seq == peer->bottom_ack + 1)
{ {
is_sequential = 1;
should_process = 1; should_process = 1;
peer->bottom_ack = header.seq; peer->bottom_ack = header.seq;
peer->ack_bits >>= 1; peer->ack_bits >>= 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 = 1 << (header.seq - 2 - peer->bottom_ack); u64 ack_bit = 1 << (header.seq - 2 - peer->bottom_ack);
should_process = !!(peer->ack_bits & ack_bit); should_process = !!(peer->ack_bits & ack_bit);
@ -359,16 +368,87 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
// Process packet // Process packet
if (should_process) if (should_process)
{ {
// NET_W32_Packet *packet = NET_W32_Packet *packet = first_free_packet;
if (packet)
{
String old_packet_data = packet->data;
ZeroStruct(packet);
packet->data = old_packet_data;
packet->data.len = 0;
}
else
{
packet = PushStruct(perm, NET_W32_Packet);
packet->data.text = PushStructsNoZero(perm, u8, NET_PacketSize);
}
packet->flags = header.flags;
packet->seq = header.seq;
packet->msg_seq = header.msg_seq;
// Insert packet
// TODO: Ring buffer
// if (is_sequential)
// {
// DllQueuePush(peer->first_msg_packet, peer->last_msg_packet, packet);
// for (NET_W32_Packet *tmp = peer->first_packet; tmp;)
// {
// NET_W32_Packet *next = tmp->next;
// if (tmp->seq == peer->last_msg_packet->seq + 1)
// {
// DllQueueRemove(peer->first_packet,
// }
// else
// {
// break;
// }
// tmp = next;
// }
// }
// else
// {
{
NET_W32_Packet *left = peer->last_packet;
for (; left; left = left->prev)
{
if (left < packet->seq)
{
break;
}
}
DllQueueInsert(peer->first_unsequential_packet, peer->last_unsequential_packet, left, packet);
}
if (is_sequential)
{
}
} }
} }
} }
} }
} }
//////////////////////////////
//- Assemble messages
{
for (NET_W32_Peer *peer = pipe->first_peer; peer; peer = peer->next)
{
for (NET_W32_Packet *packet = pipe->first_packet; packet;)
{
NET_W32_Packet *next = packet->next;
{
if (packet->
}
packet = next;
}
}
}
////////////////////////////// //////////////////////////////
//- Write peers //- Write peers
// TODO: Rate limit
for (NET_W32_Peer *peer = pipe->first_peer; peer; peer = peer->next) for (NET_W32_Peer *peer = pipe->first_peer; peer; peer = peer->next)
{ {
// bottom_ack represents the highest continuous sequence acknowledgement, meaning sequences in range [0, bottom_ack] are always acked. // bottom_ack represents the highest continuous sequence acknowledgement, meaning sequences in range [0, bottom_ack] are always acked.
@ -379,7 +459,8 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
struct sockaddr_in6 addr = peer->addr; struct sockaddr_in6 addr = peer->addr;
for (NET_W32_Packet *packet = peer->first_packet; packet && packet->seq < bottom_ack + 65;) // 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;)
{ {
NET_W32_Packet *next = packet->next; NET_W32_Packet *next = packet->next;
{ {
@ -390,7 +471,7 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
{ {
is_acked = 1; is_acked = 1;
} }
else if (seq > bottom_ack + 1) else if (seq > bottom_ack + 1 && packet->seq < bottom_ack + 65)
{ {
u64 ack_bit = 1 << (seq - 2 - bottom_ack); u64 ack_bit = 1 << (seq - 2 - bottom_ack);
is_acked = !!(ack_bits & ack_bit); is_acked = !!(ack_bits & ack_bit);
@ -399,7 +480,7 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
if (is_acked) if (is_acked)
{ {
// Prune acked packet // Prune acked packet
DllQueueRemove(peer->first_packet, peer->last_packet, packet); DllQueueRemove(peer->first_remote_packet, peer->last_packet, packet);
SllStackPush(first_free_packet, packet); SllStackPush(first_free_packet, packet);
} }
else else
@ -514,6 +595,15 @@ void NET_W32_TickForever(WaveLaneCtx *lane)
// } // }
// } // }
EndScratch(scratch); EndScratch(scratch);
} }
} }