working networking

This commit is contained in:
jacob 2026-01-15 23:22:11 -06:00
parent 418465408f
commit 9912e0bfdd
7 changed files with 772 additions and 387 deletions

View File

@ -16,7 +16,20 @@ void SetMemoryReadWrite(void *address, u64 size);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Memory ops //~ Memory ops
Inline b32 MatchBytesZero(u8 *v, u64 size)
{
for (u64 idx = 0; idx < size; ++idx)
{
if (v[idx] != 0)
{
return 0;
}
}
return 1;
}
//- Wrappers //- Wrappers
#define MatchStructZero(ptr) MatchBytesZero((u8 *)(ptr), sizeof(*ptr))
#define MatchBytes(p1, p2, n) (CmpBytes((p1), (p2), (n)) == 0) #define MatchBytes(p1, p2, n) (CmpBytes((p1), (p2), (n)) == 0)
#define MatchStruct(p1, p2) MatchBytes((p1), (p2), sizeof(*p1)) #define MatchStruct(p1, p2) MatchBytes((p1), (p2), sizeof(*p1))
#define ZeroBytes(ptr, count) SetBytes((ptr), 0, (count)) #define ZeroBytes(ptr, count) SetBytes((ptr), 0, (count))
@ -26,6 +39,7 @@ void SetMemoryReadWrite(void *address, u64 size);
#define CopyStructs(ptr_dst, ptr_src, n) CopyBytes((ptr_dst), (ptr_src), sizeof(*(ptr_dst)) * (n)) #define CopyStructs(ptr_dst, ptr_src, n) CopyBytes((ptr_dst), (ptr_src), sizeof(*(ptr_dst)) * (n))
#define CopyStruct(ptr_dst, ptr_src) CopyStructs((ptr_dst), (ptr_src), 1) #define CopyStruct(ptr_dst, ptr_src) CopyStructs((ptr_dst), (ptr_src), 1)
//- Stubs
#define CopyBytes(dst, src, count) memcpy(dst, src, count) #define CopyBytes(dst, src, count) memcpy(dst, src, count)
#define SetBytes(dst, c, count) memset(dst, c, count) #define SetBytes(dst, c, count) memset(dst, c, count)
#define CmpBytes(p1, p2, count) memcmp(p1, p2, count) #define CmpBytes(p1, p2, count) memcmp(p1, p2, count)

View File

@ -1,3 +1,6 @@
// NOTE: Burst messages with lengths exceeding packet size will degrade
// into sequenced messages
#define NET_PacketSize 1024 #define NET_PacketSize 1024
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -12,11 +15,11 @@ Struct(NET_PipeHandle) { u64 v; };
Struct(NET_Key) Struct(NET_Key)
{ {
u64 v; u8 addr[32];
}; };
#define NET_NilKey ((NET_Key) { 0 }) #define NET_NilKey ((NET_Key) { 0 })
#define NET_IsKeyNil(k) ((k).v == 0) #define NET_IsKeyNil(k) (MatchStructZero(&k))
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Message types //~ Message types
@ -26,7 +29,7 @@ Struct(NET_Msg)
NET_Msg *next; NET_Msg *next;
NET_Msg *prev; NET_Msg *prev;
NET_Key src; NET_Key sender;
String data; String data;
}; };
@ -48,7 +51,7 @@ void NET_Bootstrap(void);
NET_PipeHandle NET_AcquirePipe(void); NET_PipeHandle NET_AcquirePipe(void);
void NET_Bind(NET_PipeHandle pipe, u64 port); void NET_Bind(NET_PipeHandle pipe, u64 port);
u64 NET_BoundPortFromPipe(NET_PipeHandle pipe_handle); NET_Key NET_KeyFromString(String host, String port);
void NET_Push(NET_PipeHandle pipe_handle, NET_Key dst, String data, b32 unreliable); NET_MsgList NET_Swap(Arena *arena, NET_PipeHandle pipe);
NET_MsgList NET_Pop(Arena *arena, NET_PipeHandle pipe); void NET_Push(NET_PipeHandle pipe_handle, NET_Key dst, String data, b32 burst);

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,141 @@
////////////////////////////////////////////////////////////
//~ Command buff types
Struct(NET_W32_Cmd)
{
NET_W32_Cmd *next;
NET_Key key;
String data;
b32 burst;
};
Struct(NET_W32_CmdList)
{
i64 count;
NET_W32_Cmd *first;
NET_W32_Cmd *last;
};
Struct(NET_W32_CmdBuff)
{
Arena *arena;
NET_W32_CmdList cmds;
};
////////////////////////////////////////////////////////////
//~ Message buff types
Struct(NET_W32_MsgBuff)
{
Arena *arena;
NET_MsgList msgs;
};
////////////////////////////////////////////////////////////
//~ Packet types
Enum(NET_W32_PacketFlag)
{
NET_W32_PacketFlag_None = 0,
NET_W32_PacketFlag_EndMsg = (1 << 0),
NET_W32_PacketFlag_Heartbeat = (1 << 2),
// NET_W32_PacketFlag_Burst = (1 << 3),
};
Struct(NET_W32_PacketHeader)
{
u32 magic;
NET_W32_PacketFlag flags;
i64 seq;
i64 msg_seq;
i64 bottom_ack;
u64 ack_bits;
};
Struct(NET_W32_Packet)
{
NET_W32_Packet *next;
NET_W32_Packet *prev;
NET_W32_PacketFlag flags;
i64 seq;
i64 msg_seq;
String data;
};
////////////////////////////////////////////////////////////
//~ Peer types
Struct(NET_W32_Peer)
{
NET_W32_Peer *next;
NET_W32_Peer *prev;
NET_W32_Peer *next_in_bin;
NET_W32_Peer *prev_in_bin;
u64 hash;
NET_Key key;
i64 remote_bottom_ack;
i64 remote_ack_bits;
i64 bottom_ack;
i64 ack_bits;
NET_W32_Packet *first_remote_packet;
NET_W32_Packet *last_remote_packet;
NET_W32_Packet *first_fragmented_packet;
NET_W32_Packet *last_fragmented_packet;
NET_W32_Packet *first_contiguous_packet;
NET_W32_Packet *last_contiguous_packet;
i64 seq;
i64 msg_seq;
String fragment;
i64 last_packet_received_ns;
i64 last_packet_sent_ns;
i64 num_msg_packets_received_this_frame;
i64 num_msg_packets_sent_this_frame;
};
Struct(NET_W32_PeerBin)
{
NET_W32_Peer *first;
NET_W32_Peer *last;
};
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Pipe types //~ Pipe types
Struct(NET_W32_Pipe) Struct(NET_W32_Pipe)
{ {
//- Shared data
NET_W32_Pipe *next;
NET_W32_Pipe *prev;
Atomic64 desired_port; // >64k means ephemeral
TicketMutex back_cmd_buff_seq_tm;
i64 back_cmd_buff_seq;
NET_W32_CmdBuff cmd_buffs[2];
TicketMutex back_msg_buff_seq_tm;
i64 back_msg_buff_seq;
NET_W32_MsgBuff msg_buffs[2];
//- Worker data
NET_W32_Peer *first_peer;
NET_W32_Peer *last_peer;
i64 peer_bins_count;
NET_W32_PeerBin *peer_bins;
u64 bound_port; u64 bound_port;
SOCKET udp; SOCKET udp;
}; };
@ -12,7 +145,13 @@ Struct(NET_W32_Pipe)
Struct(NET_W32_Ctx) Struct(NET_W32_Ctx)
{ {
i32 _; TicketMutex pipes_tm;
i64 pipes_count;
NET_W32_Pipe *first_pipe;
NET_W32_Pipe *last_pipe;
NET_W32_Peer *first_free_peer;
NET_W32_Packet *first_free_packet;
}; };
extern NET_W32_Ctx NET_W32; extern NET_W32_Ctx NET_W32;
@ -21,6 +160,11 @@ extern NET_W32_Ctx NET_W32;
//~ Helpers //~ Helpers
NET_W32_Pipe *NET_W32_PipeFromHandle(NET_PipeHandle pipe_handle); NET_W32_Pipe *NET_W32_PipeFromHandle(NET_PipeHandle pipe_handle);
NET_Key NET_W32_KeyFromAddress(struct sockaddr_in6 addr);
struct sockaddr_in6 NET_W32_AddressFromKey(NET_Key key);
u64 NET_W32_HashFromKey(NET_Key key);
void NET_W32_SignalWorker(void);
NET_W32_Peer *NET_W32_TouchPeerFromKey(NET_W32_Pipe *pipe, NET_Key key);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Worker //~ Worker

View File

@ -327,7 +327,7 @@ Struct(P_Msg)
//- In //- In
P_Key dst_user; // If dst is 0, then cmd is meant to be broadcast P_Key dst_user; // If dst is 0, then cmd is meant to be broadcast
b32 unreliable; b32 burst;
//- Out //- Out

View File

@ -130,7 +130,7 @@ void S_TickForever(WaveLaneCtx *lane)
P_MsgList in_msgs = Zi; P_MsgList in_msgs = Zi;
{ {
NET_MsgList net_msgs = NET_Pop(frame_arena, net_pipe); NET_MsgList net_msgs = NET_Swap(frame_arena, net_pipe);
for (NET_Msg *net_msg = net_msgs.first; net_msg; net_msg = net_msg->next) for (NET_Msg *net_msg = net_msgs.first; net_msg; net_msg = net_msg->next)
{ {
String packed = net_msg->data; String packed = net_msg->data;
@ -430,7 +430,7 @@ void S_TickForever(WaveLaneCtx *lane)
// Arena *msgs_arena = P_tl.out_msgs_arena; // Arena *msgs_arena = P_tl.out_msgs_arena;
// P_Msg *msg = P_PushMsg(P_MsgKind_SimSnapshot, Zstr); // P_Msg *msg = P_PushMsg(P_MsgKind_SimSnapshot, Zstr);
// msg->dst_user = user->key; // msg->dst_user = user->key;
// msg->unreliable = 1; // msg->burst = 1;
// P_SimSnapshot *snapshot = &msg->sim_snapshot; // P_SimSnapshot *snapshot = &msg->sim_snapshot;
// { // {
// snapshot->world_seed = world->seed; // snapshot->world_seed = world->seed;
@ -486,7 +486,7 @@ void S_TickForever(WaveLaneCtx *lane)
{ {
if (user->is_user) if (user->is_user)
{ {
NET_Push(net_pipe, user->net, packed, msg->unreliable); NET_Push(net_pipe, user->net, packed, msg->burst);
} }
} }
} }
@ -496,7 +496,7 @@ void S_TickForever(WaveLaneCtx *lane)
if (!NET_IsKeyNil(user->net)) if (!NET_IsKeyNil(user->net))
{ {
String packed = P_PackMessage(frame_arena, msg); String packed = P_PackMessage(frame_arena, msg);
NET_Push(net_pipe, user->net, packed, msg->unreliable); NET_Push(net_pipe, user->net, packed, msg->burst);
} }
} }
} }

View File

@ -346,7 +346,6 @@ void V_TickForever(WaveLaneCtx *lane)
const f32 meters_per_draw_width = 18; const f32 meters_per_draw_width = 18;
NET_PipeHandle net_pipe = NET_AcquirePipe(); NET_PipeHandle net_pipe = NET_AcquirePipe();
NET_Bind(net_pipe, 0);
////////////////////////////// //////////////////////////////
//- Init vis state //- Init vis state
@ -2630,7 +2629,8 @@ void V_TickForever(WaveLaneCtx *lane)
P_MsgList in_msgs = Zi; P_MsgList in_msgs = Zi;
{ {
NET_MsgList net_msgs = NET_Pop(frame->arena, net_pipe); NET_Bind(net_pipe, 0);
NET_MsgList net_msgs = NET_Swap(frame->arena, net_pipe);
for (NET_Msg *net_msg = net_msgs.first; net_msg; net_msg = net_msg->next) for (NET_Msg *net_msg = net_msgs.first; net_msg; net_msg = net_msg->next)
{ {
String packed = net_msg->data; String packed = net_msg->data;
@ -2644,6 +2644,12 @@ void V_TickForever(WaveLaneCtx *lane)
} }
} }
////////////////////////////// //////////////////////////////
//- Pop snapshot from sim //- Pop snapshot from sim
@ -3747,11 +3753,22 @@ void V_TickForever(WaveLaneCtx *lane)
////////////////////////////// //////////////////////////////
//- Send messages //- Send messages
// FIXME: Remove this (testing)
NET_Key server_key = NET_KeyFromString(Lit("127.0.0.1"), Lit("22121"));
NET_Push(net_pipe, server_key, Lit("HELLO!!!!"), 0);
for (P_MsgNode *msg_node = P_tl.out_msgs.first; msg_node; msg_node = msg_node->next) for (P_MsgNode *msg_node = P_tl.out_msgs.first; msg_node; msg_node = msg_node->next)
{ {
P_Msg *msg = &msg_node->msg; P_Msg *msg = &msg_node->msg;
String packed = P_PackMessage(frame->arena, msg); String packed = P_PackMessage(frame->arena, msg);
NET_Push(net_pipe, user->net, packed, msg->unreliable); NET_Push(net_pipe, server_key, packed, msg->burst);
} }
////////////////////////////// //////////////////////////////