mirror of
https://github.com/Ed94/raddebugger.git
synced 2026-11-21 13:22:23 -08:00
checkpoint on sockets layer
This commit is contained in:
1 parent
3e704eabb0
commit
3bab86e6af
14 files changed
+486
-38
No files matched your search
+1
-1
@@ -7,6 +7,7 @@ target:
|
||||
working_directory: build
|
||||
arguments: "--user:raddbg_test.user"
|
||||
debug_subprocesses: 0
|
||||
enabled: 1
|
||||
}
|
||||
target:
|
||||
{
|
||||
@@ -17,7 +18,6 @@ target:
|
||||
{
|
||||
executable: "build/mule_main.exe"
|
||||
working_directory: build
|
||||
enabled: 1
|
||||
}
|
||||
target:
|
||||
{
|
||||
|
||||
@@ -58,6 +58,9 @@ main_thread_base_entry_point(int arguments_count, char **arguments)
|
||||
}
|
||||
|
||||
//- rjf: initialize all included layers
|
||||
#if defined(SOCKET_H) && !defined(SOCKET_INIT_MANUAL)
|
||||
sock_init();
|
||||
#endif
|
||||
#if defined(HTTP_H) && !defined(HTTP_INIT_MANUAL)
|
||||
http_init();
|
||||
#endif
|
||||
@@ -222,6 +225,9 @@ async_thread_entry_point(void *params)
|
||||
#if defined(FILE_STREAM_H)
|
||||
fs_async_tick();
|
||||
#endif
|
||||
#if defined(SOCKET_H)
|
||||
sock_async_tick();
|
||||
#endif
|
||||
#if defined(HTTP_H)
|
||||
http_async_tick();
|
||||
#endif
|
||||
|
||||
+94
-37
@@ -13,16 +13,42 @@ make_ring(Arena *arena, U64 size)
|
||||
return ring;
|
||||
}
|
||||
|
||||
internal B32
|
||||
ring_try_write(Ring *ring, U64 size, void *ptr)
|
||||
internal void *
|
||||
ring_try_push(Ring *ring, U64 size)
|
||||
{
|
||||
U64 bytes_unconsumed = (ring->write_pos - ring->read_pos);
|
||||
U64 bytes_available = ring->size - bytes_unconsumed;
|
||||
B32 result = 0;
|
||||
void *base = 0;
|
||||
if(bytes_available >= size)
|
||||
{
|
||||
base = ring->base + ring->write_pos%ring->size;
|
||||
ring->write_pos += size;
|
||||
}
|
||||
return base;
|
||||
}
|
||||
|
||||
internal void *
|
||||
ring_try_pop(Ring *ring, U64 size)
|
||||
{
|
||||
U64 bytes_unconsumed = (ring->write_pos - ring->read_pos);
|
||||
void *base = 0;
|
||||
if(bytes_unconsumed >= size)
|
||||
{
|
||||
base = ring->base + ring->read_pos%ring->size;
|
||||
ring->read_pos += size;
|
||||
}
|
||||
return base;
|
||||
}
|
||||
|
||||
internal B32
|
||||
ring_try_write(Ring *ring, U64 size, void *ptr)
|
||||
{
|
||||
B32 result = 0;
|
||||
void *dst = ring_try_push(ring, size);
|
||||
if(dst)
|
||||
{
|
||||
result = 1;
|
||||
ring->write_pos += wrapped_write(ring->base, ring->size, ring->write_pos, ptr, size);
|
||||
MemoryCopy(dst, ptr, size);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -30,12 +56,12 @@ ring_try_write(Ring *ring, U64 size, void *ptr)
|
||||
internal B32
|
||||
ring_try_read(Ring *ring, U64 size, void *ptr)
|
||||
{
|
||||
U64 bytes_unconsumed = (ring->write_pos - ring->read_pos);
|
||||
B32 result = 0;
|
||||
if(bytes_unconsumed >= size)
|
||||
void *src = ring_try_pop(ring, size);
|
||||
if(src)
|
||||
{
|
||||
result = 1;
|
||||
ring->read_pos += wrapped_read(ring->base, ring->size, ring->read_pos, ptr, size);
|
||||
MemoryCopy(ptr, src, size);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -76,16 +102,27 @@ internal void
|
||||
guarded_ring_close(RingGuard *guard)
|
||||
{
|
||||
mutex_drop(guard->r->mutex);
|
||||
cond_var_broadcast(guard->r->cv);
|
||||
}
|
||||
|
||||
internal void *
|
||||
guarded_ring_try_push(RingGuard *guard, U64 size)
|
||||
{
|
||||
void *result = ring_try_push(guard->r->ring, size);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void *
|
||||
guarded_ring_try_pop(RingGuard *guard, U64 size)
|
||||
{
|
||||
void *result = ring_try_pop(guard->r->ring, size);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
guarded_ring_try_write(RingGuard *guard, U64 size, void *ptr)
|
||||
{
|
||||
B32 result = ring_try_write(guard->r->ring, size, ptr);
|
||||
if(result)
|
||||
{
|
||||
cond_var_broadcast(guard->r->cv);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -93,9 +130,43 @@ internal B32
|
||||
guarded_ring_try_read(RingGuard *guard, U64 size, void *ptr)
|
||||
{
|
||||
B32 result = ring_try_read(guard->r->ring, size, ptr);
|
||||
if(result)
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void *
|
||||
guarded_ring_push_or_wait(RingGuard *guard, U64 size, U64 endt_us)
|
||||
{
|
||||
void *result = 0;
|
||||
for(;!result;)
|
||||
{
|
||||
cond_var_broadcast(guard->r->cv);
|
||||
result = guarded_ring_try_push(guard, size);
|
||||
if(now_time_us() >= endt_us)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(!result)
|
||||
{
|
||||
cond_var_wait(guard->r->cv, guard->r->mutex, endt_us);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
internal void *
|
||||
guarded_ring_pop_or_wait(RingGuard *guard, U64 size, U64 endt_us)
|
||||
{
|
||||
void *result = 0;
|
||||
for(;!result;)
|
||||
{
|
||||
result = guarded_ring_try_pop(guard, size);
|
||||
if(now_time_us() >= endt_us)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(!result)
|
||||
{
|
||||
cond_var_wait(guard->r->cv, guard->r->mutex, endt_us);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -103,37 +174,23 @@ guarded_ring_try_read(RingGuard *guard, U64 size, void *ptr)
|
||||
internal B32
|
||||
guarded_ring_write_or_wait(RingGuard *guard, U64 size, void *ptr, U64 endt_us)
|
||||
{
|
||||
B32 write_good = 0;
|
||||
for(;!write_good;)
|
||||
void *dst = guarded_ring_push_or_wait(guard, size, endt_us);
|
||||
B32 result = !!dst;
|
||||
if(dst)
|
||||
{
|
||||
write_good = guarded_ring_try_write(guard, size, ptr);
|
||||
if(now_time_us() >= endt_us)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(!write_good)
|
||||
{
|
||||
cond_var_wait(guard->r->cv, guard->r->mutex, endt_us);
|
||||
}
|
||||
MemoryCopy(dst, ptr, size);
|
||||
}
|
||||
return write_good;
|
||||
return result;
|
||||
}
|
||||
|
||||
internal B32
|
||||
guarded_ring_read_or_wait(RingGuard *guard, U64 size, void *ptr, U64 endt_us)
|
||||
{
|
||||
B32 read_good = 0;
|
||||
for(;!read_good;)
|
||||
void *src = guarded_ring_pop_or_wait(guard, size, endt_us);
|
||||
B32 result = !!src;
|
||||
if(src)
|
||||
{
|
||||
read_good = guarded_ring_try_read(guard, size, ptr);
|
||||
if(now_time_us() >= endt_us)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(!read_good)
|
||||
{
|
||||
cond_var_wait(guard->r->cv, guard->r->mutex, endt_us);
|
||||
}
|
||||
MemoryCopy(ptr, src, size);
|
||||
}
|
||||
return read_good;
|
||||
return result;
|
||||
}
|
||||
@@ -31,6 +31,8 @@ struct RingGuard
|
||||
//~ rjf: Ring Functions
|
||||
|
||||
internal Ring *make_ring(Arena *arena, U64 size);
|
||||
internal void *ring_try_push(Ring *ring, U64 size);
|
||||
internal void *ring_try_pop(Ring *ring, U64 size);
|
||||
internal B32 ring_try_write(Ring *ring, U64 size, void *ptr);
|
||||
internal B32 ring_try_read(Ring *ring, U64 size, void *ptr);
|
||||
#define ring_try_write_struct(ring, ptr) ring_try_write((ring), sizeof(*(ptr)), (ptr))
|
||||
@@ -43,10 +45,14 @@ internal GuardedRing *guarded_ring_alloc(Arena *arena, U64 size);
|
||||
internal void guarded_ring_release(GuardedRing *ring);
|
||||
internal RingGuard guarded_ring_open(GuardedRing *ring);
|
||||
internal void guarded_ring_close(RingGuard *guard);
|
||||
internal void *guarded_ring_try_push(RingGuard *guard, U64 size);
|
||||
internal void *guarded_ring_try_pop(RingGuard *guard, U64 size);
|
||||
internal B32 guarded_ring_try_write(RingGuard *guard, U64 size, void *ptr);
|
||||
internal B32 guarded_ring_try_read(RingGuard *guard, U64 size, void *ptr);
|
||||
#define guarded_ring_try_write_struct(ring, ptr) guarded_ring_try_write((ring), sizeof(*(ptr)), (ptr))
|
||||
#define guarded_ring_try_read_struct(ring, ptr) guarded_ring_try_read((ring), sizeof(*(ptr)), (ptr))
|
||||
internal void *guarded_ring_push_or_wait(RingGuard *guard, U64 size, U64 endt_us);
|
||||
internal void *guarded_ring_pop_or_wait(RingGuard *guard, U64 size, U64 endt_us);
|
||||
internal B32 guarded_ring_write_or_wait(RingGuard *guard, U64 size, void *ptr, U64 endt_us);
|
||||
internal B32 guarded_ring_read_or_wait(RingGuard *guard, U64 size, void *ptr, U64 endt_us);
|
||||
#define guarded_ring_write_struct_or_wait(ring, ptr, endt_us) guarded_ring_write_or_wait((ring), sizeof(*(ptr)), (ptr), (endt_us))
|
||||
|
||||
@@ -7442,6 +7442,7 @@ rd_window_frame(void)
|
||||
{
|
||||
rd_cmd_kind_info_table[RD_CmdKind_Open].string,
|
||||
rd_cmd_kind_info_table[RD_CmdKind_OpenSourceFileFromDebugInfo].string,
|
||||
rd_cmd_kind_info_table[RD_CmdKind_OpenCrashDump].string,
|
||||
{0},//-
|
||||
rd_cmd_kind_info_table[RD_CmdKind_NewProject].string,
|
||||
rd_cmd_kind_info_table[RD_CmdKind_OpenProject].string,
|
||||
@@ -7460,6 +7461,7 @@ rd_window_frame(void)
|
||||
{
|
||||
'o',
|
||||
'i',
|
||||
'd',
|
||||
0,//-
|
||||
'j',
|
||||
'p',
|
||||
|
||||
@@ -388,6 +388,7 @@
|
||||
#include "mdesk/mdesk.h"
|
||||
#include "window_manager/window_manager_inc.h"
|
||||
#include "shell/shell_inc.h"
|
||||
// #include "socket/socket_inc.h"
|
||||
#include "http/http_inc.h"
|
||||
#include "symbol_server/symbol_server_inc.h"
|
||||
#include "config/config_inc.h"
|
||||
@@ -450,6 +451,7 @@
|
||||
#include "mdesk/mdesk.c"
|
||||
#include "window_manager/window_manager_inc.c"
|
||||
#include "shell/shell_inc.c"
|
||||
// #include "socket/socket_inc.c"
|
||||
#include "http/http_inc.c"
|
||||
#include "symbol_server/symbol_server_inc.c"
|
||||
#include "config/config_inc.c"
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: Helpers
|
||||
|
||||
internal SOCK_Endpoint
|
||||
sock_endpoint_from_string_port(String8 address, U16 port)
|
||||
{
|
||||
//- rjf: detect kind
|
||||
SOCK_EndpointKind kind = SOCK_EndpointKind_IPv4;
|
||||
{
|
||||
if(str8_find_needle(address, 0, s(":"), 0) < address.size)
|
||||
{
|
||||
kind = SOCK_EndpointKind_IPv6;
|
||||
}
|
||||
}
|
||||
|
||||
//- rjf: fill based on format
|
||||
SOCK_Endpoint ep = {0};
|
||||
ep.port = port;
|
||||
ep.kind = kind;
|
||||
switch(kind)
|
||||
{
|
||||
default:
|
||||
case SOCK_EndpointKind_IPv4:
|
||||
{
|
||||
U64 off = 0;
|
||||
U64 part_start_off = 0;
|
||||
for(;off < 4;)
|
||||
{
|
||||
U64 part_end_off = str8_find_needle(address, part_start_off+1, s("."), 0);
|
||||
String8 part = str8_substr(address, r1u64(part_start_off, part_end_off));
|
||||
U64 part_val = u64_from_str8(part, 10);
|
||||
ep.address_u8[off] = (U8)part_val;
|
||||
part_start_off = part_end_off+1;
|
||||
off += 1;
|
||||
if(part_end_off >= address.size)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}break;
|
||||
case SOCK_EndpointKind_IPv6:
|
||||
{
|
||||
U64 off = 0;
|
||||
U64 part_start_off = 0;
|
||||
U64 double_colon_off = 0;
|
||||
for(;off < 8;)
|
||||
{
|
||||
U64 part_end_off = str8_find_needle(address, part_start_off+1, s(":"), 0);
|
||||
String8 part = str8_substr(address, r1u64(part_start_off, part_end_off));
|
||||
U64 part_val = u64_from_str8(part, 16);
|
||||
ep.address_u16[off] = (U16)part_val;
|
||||
part_start_off = part_end_off+1;
|
||||
if(part_end_off+1 < address.size && address.str[part_end_off+1] == ':')
|
||||
{
|
||||
double_colon_off = off;
|
||||
}
|
||||
off += 1;
|
||||
if(part_end_off >= address.size)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(off < 8 && double_colon_off < 8)
|
||||
{
|
||||
U64 shift_amt = 8 - off;
|
||||
MemoryCopy(&ep.address_u16[0] + double_colon_off + shift_amt, &ep.address_u16[0] + double_colon_off, sizeof(U16) * (off - double_colon_off));
|
||||
}
|
||||
}break;
|
||||
}
|
||||
}
|
||||
|
||||
internal SOCK_Endpoint
|
||||
sock_endpoint_from_string(String8 address_and_port)
|
||||
{
|
||||
String8 addr = {0};
|
||||
U64 port_off = 0;
|
||||
if(address_and_port.size > 0 && address_and_port.str[0] == '[')
|
||||
{
|
||||
U64 addr_end_off = str8_find_needle(address_and_port, 1, s("]"), 0);
|
||||
addr = str8_substr(address_and_port, r1u64(1, addr_end_off));
|
||||
port_off = str8_find_needle(address_and_port, addr_end_off+1, s(":"), 0)+1;
|
||||
}
|
||||
else
|
||||
{
|
||||
U64 colon_off = str8_find_needle(address_and_port, 0, s(":"), 0);
|
||||
port_off = colon_off + 1;
|
||||
addr = str8_prefix(address_and_port, colon_off);
|
||||
}
|
||||
String8 port_string = str8_skip(address_and_port, port_off);
|
||||
U16 port = u64_from_str8(port_string, 10);
|
||||
SOCK_Endpoint ep = sock_endpoint_from_string_port(addr, port);
|
||||
return ep;
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
#ifndef SOCKET_H
|
||||
#define SOCKET_H
|
||||
|
||||
#if !defined(SOCKET_PORT)
|
||||
# define SOCKET_PORT 7423
|
||||
#endif
|
||||
|
||||
typedef enum SOCK_Protocol
|
||||
{
|
||||
SOCK_Protocol_TCP,
|
||||
SOCK_Protocol_UDP,
|
||||
}
|
||||
SOCK_Protocol;
|
||||
|
||||
typedef U8 SOCK_EndpointKind;
|
||||
typedef enum SOCK_EndpointKindEnum
|
||||
{
|
||||
SOCK_EndpointKind_IPv4,
|
||||
SOCK_EndpointKind_IPv6,
|
||||
}
|
||||
SOCK_EndpointKindEnum;
|
||||
|
||||
typedef struct SOCK_Endpoint SOCK_Endpoint;
|
||||
struct SOCK_Endpoint
|
||||
{
|
||||
U16 port;
|
||||
SOCK_EndpointKind kind;
|
||||
U8 _pad_0;
|
||||
U32 _pad_1;
|
||||
union
|
||||
{
|
||||
U8 address_u8[16];
|
||||
U16 address_u16[8];
|
||||
};
|
||||
};
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: Helpers
|
||||
|
||||
internal SOCK_Endpoint sock_endpoint_from_string_port(String8 address, U16 port);
|
||||
internal SOCK_Endpoint sock_endpoint_from_string(String8 address_and_port);
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Top-Level Layer Calls
|
||||
|
||||
#if !defined(NEED_ASYNC)
|
||||
# define NEED_ASYNC 1
|
||||
#endif
|
||||
internal void sock_init(void);
|
||||
internal void sock_async_tick(void);
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Sends
|
||||
|
||||
internal U64 sock_send(U8 *ptr, U64 size, SOCK_Protocol *protocol_in, SOCK_Endpoint *endpoint_in, U64 endt_us);
|
||||
#define sock_send_struct(ptr, protocol_in, endpoint_in, endt_us) sock_send((ptr), sizeof(*(ptr)), (endpoint_in), (endt_us))
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Receives
|
||||
|
||||
internal U64 sock_recv(U8 *ptr, U64 size, SOCK_Protocol *protocol_out, SOCK_Endpoint *endpoint_out, U64 endt_us);
|
||||
#define sock_recv_struct(ptr, protocol_out, endpoint_out, endt_us) sock_recv((ptr), sizeof(*(ptr)), (endpoint_out), (endt_us))
|
||||
|
||||
#endif // SOCKET_H
|
||||
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
#include "socket.c"
|
||||
#if OS_WINDOWS
|
||||
# include "win32/socket/win32_socket.c"
|
||||
#else
|
||||
# include "socket_stub.c"
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
#ifndef SOCKET_INC_H
|
||||
#define SOCKET_INC_H
|
||||
|
||||
#include "socket.h"
|
||||
#if OS_WINDOWS
|
||||
# include "win32/socket/win32_socket.h"
|
||||
#else
|
||||
# include "socket_stub.h"
|
||||
#endif
|
||||
|
||||
#endif // SOCKET_INC_H
|
||||
@@ -0,0 +1,7 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
internal void sock_init(void){}
|
||||
internal void sock_async_tick(void){}
|
||||
internal U64 sock_send(U8 *ptr, U64 size, SOCK_Protocol *protocol_in, SOCK_Endpoint *endpoint_in, U64 endt_us){return 0;}
|
||||
internal U64 sock_recv(U8 *ptr, U64 size, SOCK_Protocol *protocol_in, SOCK_Endpoint *endpoint_in, U64 endt_us){return 0;}
|
||||
@@ -0,0 +1,7 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
#ifndef SOCKET_STUB_H
|
||||
#define SOCKET_STUB_H
|
||||
|
||||
#endif // SOCKET_STUB_H
|
||||
@@ -0,0 +1,136 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: Listener Threads
|
||||
|
||||
internal void
|
||||
w32_sock_tcp_listener_thread_entry_point(void *p)
|
||||
{
|
||||
ThreadNameF("w32_sock_tcp_listener_thread");
|
||||
for(;;)
|
||||
{
|
||||
SOCKET new_socket = accept(w32_sock_state->tcp_listen_socket, 0, 0);
|
||||
if(new_socket != INVALID_SOCKET)
|
||||
{
|
||||
// TODO(rjf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void
|
||||
w32_sock_udp_listener_thread_entry_point(void *p)
|
||||
{
|
||||
ThreadNameF("w32_sock_udp_listener_thread");
|
||||
for(;;)
|
||||
{
|
||||
SOCKET new_socket = accept(w32_sock_state->udp_listen_socket, 0, 0);
|
||||
if(new_socket != INVALID_SOCKET)
|
||||
{
|
||||
// TODO(rjf)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Top-Level Layer Calls
|
||||
|
||||
internal void
|
||||
sock_init(void)
|
||||
{
|
||||
// NOTE(rjf): winsock2 is already initialized by the base layer for RIO function grabbing.
|
||||
|
||||
//- rjf: set up state
|
||||
Arena *arena = arena_alloc();
|
||||
w32_sock_state = push_array(arena, W32_SOCK_State, 1);
|
||||
w32_sock_state->arena = arena;
|
||||
w32_sock_state->u2s_ring = guarded_ring_alloc(arena, KB(256));
|
||||
w32_sock_state->s2u_ring = guarded_ring_alloc(arena, KB(256));
|
||||
|
||||
//- rjf: create listener sockets
|
||||
w32_sock_state->tcp_listen_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
||||
w32_sock_state->udp_listen_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
||||
{
|
||||
DWORD ipv6only = 0;
|
||||
setsockopt(w32_sock_state->tcp_listen_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
|
||||
setsockopt(w32_sock_state->udp_listen_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
|
||||
}
|
||||
|
||||
//- rjf: bind listener sockets
|
||||
{
|
||||
struct sockaddr_in server_addr;
|
||||
server_addr.sin_family = AF_INET6;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(SOCKET_PORT);
|
||||
bind(w32_sock_state->tcp_listen_socket, (SOCKADDR *)&server_addr, sizeof(server_addr));
|
||||
bind(w32_sock_state->udp_listen_socket, (SOCKADDR *)&server_addr, sizeof(server_addr));
|
||||
}
|
||||
|
||||
//- rjf: start listening
|
||||
{
|
||||
listen(w32_sock_state->tcp_listen_socket, SOMAXCONN);
|
||||
listen(w32_sock_state->udp_listen_socket, SOMAXCONN);
|
||||
}
|
||||
|
||||
//- rjf: launch one-off listener threads to block & accept connections
|
||||
w32_sock_state->tcp_listener_thread = thread_launch(w32_sock_tcp_listener_thread_entry_point, 0);
|
||||
w32_sock_state->udp_listener_thread = thread_launch(w32_sock_udp_listener_thread_entry_point, 0);
|
||||
}
|
||||
|
||||
internal void
|
||||
sock_async_tick(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Sends
|
||||
|
||||
internal U64
|
||||
sock_send(U8 *ptr, U64 size, SOCK_Protocol *protocol_in, SOCK_Endpoint *endpoint_in, U64 endt_us)
|
||||
{
|
||||
U64 result = 0;
|
||||
U64 needed_size = sizeof(*protocol_in) + sizeof(*endpoint_in) + sizeof(U64) + size;
|
||||
RingGuard guard = guarded_ring_open(w32_sock_state->u2s_ring);
|
||||
{
|
||||
void *dst = guarded_ring_push_or_wait(&guard, needed_size, endt_us);
|
||||
if(dst != 0)
|
||||
{
|
||||
MemoryCopy((U8 *)dst + 0, protocol_in, sizeof(*protocol_in));
|
||||
MemoryCopy((U8 *)dst + sizeof(*protocol_in), endpoint_in, sizeof(*endpoint_in));
|
||||
MemoryCopy((U8 *)dst + sizeof(*protocol_in) + sizeof(endpoint_in), &size, sizeof(size));
|
||||
MemoryCopy((U8 *)dst + sizeof(*protocol_in) + sizeof(endpoint_in) + sizeof(U64), ptr, size);
|
||||
result = needed_size;
|
||||
}
|
||||
}
|
||||
guarded_ring_close(&guard);
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: @per_os_impl Receives
|
||||
|
||||
internal U64
|
||||
sock_recv(U8 *ptr, U64 size, SOCK_Protocol *protocol_out, SOCK_Endpoint *endpoint_out, U64 endt_us)
|
||||
{
|
||||
U64 received_size = 0;
|
||||
U64 header_size = sizeof(*protocol_out) + sizeof(*endpoint_out) + sizeof(U64);
|
||||
{
|
||||
RingGuard guard = guarded_ring_open(w32_sock_state->u2s_ring);
|
||||
{
|
||||
void *src = guarded_ring_pop_or_wait(&guard, header_size, endt_us);
|
||||
if(src != 0)
|
||||
{
|
||||
U64 payload_size = 0;
|
||||
MemoryCopy(protocol_out, (U8 *)src + 0, sizeof(*protocol_out));
|
||||
MemoryCopy(endpoint_out, (U8 *)src + sizeof(*protocol_out), sizeof(*endpoint_out));
|
||||
MemoryCopy(&payload_size, (U8 *)src + sizeof(*protocol_out) + sizeof(*endpoint_out), sizeof(U64));
|
||||
void *payload = guarded_ring_pop_or_wait(&guard, payload_size, max_U64);
|
||||
MemoryCopy(ptr, payload, payload_size);
|
||||
received_size = payload_size;
|
||||
}
|
||||
}
|
||||
guarded_ring_close(&guard);
|
||||
}
|
||||
return received_size;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) Epic Games Tools
|
||||
// Licensed under the MIT license (https://opensource.org/license/mit/)
|
||||
|
||||
#ifndef WIN32_SOCKET_H
|
||||
#define WIN32_SOCKET_H
|
||||
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "ws2_32")
|
||||
|
||||
typedef struct W32_SOCK_Connection W32_SOCK_Connection;
|
||||
struct W32_SOCK_Connection
|
||||
{
|
||||
W32_SOCK_Connection *next;
|
||||
SOCK_Endpoint endpoint;
|
||||
SOCK_Protocol protocol;
|
||||
SOCKET socket;
|
||||
};
|
||||
|
||||
typedef struct W32_SOCK_State W32_SOCK_State;
|
||||
struct W32_SOCK_State
|
||||
{
|
||||
Arena *arena;
|
||||
GuardedRing *u2s_ring;
|
||||
GuardedRing *s2u_ring;
|
||||
SOCKET tcp_listen_socket;
|
||||
SOCKET udp_listen_socket;
|
||||
Thread tcp_listener_thread;
|
||||
Thread udp_listener_thread;
|
||||
};
|
||||
|
||||
global W32_SOCK_State *w32_sock_state = 0;
|
||||
|
||||
////////////////////////////////
|
||||
//~ rjf: Listener Threads
|
||||
|
||||
internal void w32_sock_tcp_listener_thread_entry_point(void *p);
|
||||
internal void w32_sock_udp_listener_thread_entry_point(void *p);
|
||||
|
||||
#endif // WIN32_SOCKET_H
|
||||
Reference in new issue
Block a user