Skip to content

Commit 374208b

Browse files
committed
More simplification. Almost able to run a test.
1 parent abdd4f4 commit 374208b

30 files changed

Lines changed: 1639 additions & 2596 deletions

docs/functions_to_implement.txt

Whitespace-only changes.

docs/sockets_threadlets_event_loop.md

Lines changed: 0 additions & 426 deletions
This file was deleted.

src/include/ptk_array.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <stdbool.h>
1414
#include <stdlib.h>
1515
#include <string.h>
16-
#include <ptk_alloc.h>
16+
#include <ptk_mem.h>
1717
#include <ptk_err.h>
1818
#include <ptk_log.h>
1919

@@ -55,21 +55,21 @@
5555
} \
5656
} \
5757
if (arr->elements) { \
58-
ptk_free(&arr->elements); \
58+
ptk_local_free(&arr->elements); \
5959
} \
6060
} \
6161
\
6262
static inline PREFIX##_array_t * PREFIX##_array_create(size_t initial_size, void (*element_destructor)(T *element)) { \
6363
if (initial_size == 0) return NULL; \
6464
\
65-
PREFIX##_array_t *arr = ptk_alloc(sizeof(PREFIX##_array_t), PREFIX##_PRIVATE_array_destructor); \
65+
PREFIX##_array_t *arr = ptk_local_alloc(sizeof(PREFIX##_array_t), PREFIX##_PRIVATE_array_destructor); \
6666
if (!arr) return NULL; \
6767
\
6868
arr->len = initial_size; \
6969
arr->element_destructor = element_destructor; \
70-
arr->elements = ptk_alloc(initial_size * sizeof(T), NULL); \
70+
arr->elements = ptk_local_alloc(initial_size * sizeof(T), NULL); \
7171
if (!arr->elements) { \
72-
ptk_free(&arr); \
72+
ptk_local_free(&arr); \
7373
return NULL; \
7474
} \
7575
\
@@ -83,7 +83,7 @@
8383
if (!arr) return PTK_ERR_NULL_PTR; \
8484
if (new_len == 0) return PTK_ERR_INVALID_PARAM; \
8585
\
86-
T *new_elements = ptk_realloc(arr->elements, new_len * sizeof(T)); \
86+
T *new_elements = ptk_local_realloc(arr->elements, new_len * sizeof(T)); \
8787
if (!new_elements) { \
8888
error("Failed to allocate %zu bytes for " #PREFIX "_array", new_len * sizeof(T)); \
8989
return PTK_ERR_NO_RESOURCES; \

src/include/ptk_buf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include <ptk_err.h>
1717
#include <ptk_array.h>
18-
#include <ptk_alloc.h>
18+
#include <ptk_mem.h>
1919

2020

2121
//=============================================================================

src/include/ptk_sock.h

Lines changed: 72 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
* be interrupted by other threads.
88
*
99
* Under the hood this is implemented by a platform-specific event loop.
10-
* Multiple threads run their own copies of the event look. Each socket is
10+
* Multiple threads run their own copies of the event loop. Each socket is
1111
* monitored by only one thread. Sockets cannot migrate.
12-
*
13-
* Rather than OS threads, this uses
1412
*/
1513

1614
#include <stdint.h>
@@ -20,10 +18,22 @@
2018
#include <ptk_err.h>
2119
#include <ptk_utils.h>
2220
#include <ptk_array.h>
21+
#include <ptk_mem.h>
2322

2423
// Forward declarations
2524
typedef struct ptk_sock ptk_sock;
2625

26+
/**
27+
* Socket thread function signature
28+
*
29+
* This function is called in a dedicated thread for each socket.
30+
* The thread has access to the socket and shared context data.
31+
*
32+
* @param socket The socket being handled in this thread
33+
* @param shared_context Shared memory handle for cross-thread data
34+
*/
35+
typedef void (*ptk_socket_thread_func)(ptk_sock *socket, ptk_shared_handle_t shared_context);
36+
2737
/**
2838
* Socket types.
2939
*/
@@ -39,8 +49,6 @@ typedef enum {
3949
// COMMON DATA TYPES
4050
//=============================================================================
4151

42-
PTK_ARRAY_DECLARE(ptk_buf, ptk_buf);
43-
4452
//=============================================================================
4553
// ADDRESS STRUCTURES AND FUNCTIONS
4654
//=============================================================================
@@ -153,13 +161,6 @@ ptk_network_interface_array_t *ptk_network_discover_interfaces(void);
153161
// GENERIC SOCKET OPERATIONS
154162
//=============================================================================
155163

156-
/**
157-
* Get socket type.
158-
*
159-
* @param sock The socket to query.
160-
* @return Socket type.
161-
*/
162-
ptk_sock_type ptk_socket_type(ptk_sock *sock);
163164

164165

165166
/**
@@ -189,87 +190,103 @@ ptk_err ptk_socket_wait(ptk_sock *sock, ptk_duration_ms timeout_ms);
189190
*/
190191
ptk_err ptk_socket_signal(ptk_sock *sock);
191192

193+
/**
194+
* @brief Close a socket and stop its dedicated thread
195+
*
196+
* Closes the socket and signals its dedicated thread to terminate.
197+
* This function is safe to call from any thread.
198+
*
199+
* @param socket The socket to close
200+
*/
201+
void ptk_socket_close(ptk_sock *socket);
202+
192203
//=============================================================================
193204
// TCP Client Sockets
194205
//=============================================================================
195206

196207
/**
197-
* Connect to a TCP server (blocking).
208+
* Connect to a TCP server and start a dedicated thread.
198209
*
199-
* @param remote_addr The remote address to connect to.
200-
* @param timeout_ms Timeout in milliseconds (0 for infinite).
210+
* Creates a TCP connection to the remote address and spawns a dedicated thread
211+
* that will run the provided thread function with the socket and shared context.
212+
*
213+
* @param remote_addr The remote address to connect to
214+
* @param thread_func Function to run in the socket's dedicated thread
215+
* @param shared_context Shared memory handle for cross-thread data
201216
* @return A pointer to the connected TCP socket, or NULL on error.
202217
* On error, ptk_get_err() provides the error code.
203218
*/
204-
ptk_sock *ptk_tcp_socket_connect(const ptk_address_t *remote_addr, ptk_duration_ms timeout_ms);
219+
ptk_sock *ptk_tcp_connect(const ptk_address_t *remote_addr,
220+
ptk_socket_thread_func thread_func,
221+
ptk_shared_handle_t shared_context);
205222

206223
/**
207-
* Write data to a TCP socket using vectored I/O (blocking).
224+
* Write data to a TCP socket (blocking).
208225
*
209226
* @param sock TCP client socket.
210-
* @param data_array Array of buffers containing data to write.
227+
* @param data Buffer containing data to write.
211228
* @param timeout_ms Timeout in milliseconds (0 for infinite).
212229
* @return PTK_OK on success, PTK_ERR_TIMEOUT on timeout, PTK_ERR_NETWORK_ERROR on error.
213230
* On error, ptk_get_err() provides the error code.
214231
*/
215-
ptk_err ptk_tcp_socket_send(ptk_sock *sock, ptk_buf_array_t *data_array, ptk_duration_ms timeout_ms);
232+
ptk_err ptk_tcp_socket_send(ptk_sock *sock, ptk_buf *data, ptk_duration_ms timeout_ms);
216233

217234
/**
218235
* Read data from a TCP socket (blocking).
219236
*
220237
* @param sock TCP client socket.
221-
* @param wait_for_data If true, wait the entire timeout period and return as much data as is available.
222238
* @param timeout_ms Timeout in milliseconds (0 for infinite).
223-
* @return Returns a buffer containing the received data on success (must be freed with ptk_buf_free()),
224-
* or NULL on error. If NULL is returned, check ptk_get_err() for
239+
* @return Returns a buffer containing the received data on success (must be freed with ptk_local_free()),
240+
* or NULL on error. If NULL is returned, check ptk_get_err() for error details.
225241
*/
226-
ptk_buf *ptk_tcp_socket_recv(ptk_sock *sock, bool wait_for_data, ptk_duration_ms timeout_ms);
242+
ptk_buf *ptk_tcp_socket_recv(ptk_sock *sock, ptk_duration_ms timeout_ms);
227243

228244
//=============================================================================
229245
// TCP Server Sockets
230246
//=============================================================================
231247

232248
/**
233-
* Listen on a local address as a TCP server.
249+
* Start a TCP server that accepts connections.
234250
*
235-
* @param local_addr Address to bind to.
236-
* @param backlog Maximum number of pending connections.
237-
* @return Valid server socket on success, NULL on failure (ptk_get_err() set).
238-
*/
239-
ptk_sock *ptk_tcp_socket_listen(const ptk_address_t *local_addr, int backlog);
240-
241-
/**
242-
* Accept a new TCP connection (blocking).
251+
* Creates a listening socket, binds it to the local address, and spawns a thread
252+
* to accept connections in a loop. For each accepted connection, spawns a dedicated
253+
* thread that runs the provided thread function with the client socket and shared context.
254+
*
255+
* Returns immediately with a server socket handle that can be used to control the server.
243256
*
244-
* @param server Listening server socket.
245-
* @param timeout_ms Timeout in milliseconds (0 for infinite).
246-
* @return Valid client socket on success, NULL on error.
247-
* On error, ptk_get_err() provides the error code.
257+
* @param local_addr Address to bind the server to
258+
* @param thread_func Function to run for each accepted client connection
259+
* @param shared_context Shared memory handle for cross-thread data
260+
* @return Server socket handle for controlling the server, or NULL on error
248261
*/
249-
ptk_sock *ptk_tcp_socket_accept(ptk_sock *server, ptk_duration_ms timeout_ms);
262+
ptk_sock *ptk_tcp_server_start(const ptk_address_t *local_addr,
263+
ptk_socket_thread_func thread_func,
264+
ptk_shared_handle_t shared_context);
250265

251266
//=============================================================================
252267
// UDP Sockets
253268
//=============================================================================
254269

255-
typedef struct ptk_udp_buf_entry_t {
256-
ptk_buf *buf; // Buffer containing UDP packet data
257-
ptk_address_t sender_addr; // Address of the sender of this packet
258-
} ptk_udp_buf_entry_t;
259-
260-
PTK_ARRAY_DECLARE(ptk_udp_buf_entry, ptk_udp_buf_entry_t);
261-
262270

263271
/**
264-
* Create a UDP socket.
272+
* Create a UDP socket with a dedicated thread.
273+
*
274+
* Creates a UDP socket and spawns a dedicated thread that will run the provided
275+
* thread function with the socket and shared context.
265276
*
266-
* @param local_addr Address to bind to (NULL for client-only).
267-
* @return Valid UDP socket on success, NULL on failure (ptk_get_err() set).
277+
* @param local_addr Address to bind to (NULL for client-only)
278+
* @param broadcast Whether to enable broadcast support
279+
* @param thread_func Function to run in the socket's dedicated thread
280+
* @param shared_context Shared memory handle for cross-thread data
281+
* @return Valid UDP socket on success, NULL on failure (ptk_get_err() set)
268282
*/
269-
ptk_sock *ptk_udp_socket_create(const ptk_address_t *local_addr, bool broadcast);
283+
ptk_sock *ptk_udp_socket_create(const ptk_address_t *local_addr,
284+
bool broadcast,
285+
ptk_socket_thread_func thread_func,
286+
ptk_shared_handle_t shared_context);
270287

271288
/**
272-
* Send UDP data to a specific address using vectored I/O (blocking).
289+
* Send UDP data to a specific address (blocking).
273290
*
274291
* @param sock UDP socket.
275292
* @param data Buffer containing data to send.
@@ -279,44 +296,20 @@ ptk_sock *ptk_udp_socket_create(const ptk_address_t *local_addr, bool broadcast)
279296
* @return PTK_OK on success, PTK_ERR_TIMEOUT on timeout, PTK_ERR_NETWORK_ERROR on error.
280297
* On error, ptk_get_err() provides the error code.
281298
*/
282-
ptk_err ptk_udp_socket_send_to(ptk_sock *sock, ptk_buf *data, const ptk_address_t *dest_addr, bool broadcast,
283-
ptk_duration_ms timeout_ms);
284-
285-
/**
286-
* Send UDP data to a specific address using vectored I/O (blocking).
287-
*
288-
* @param sock UDP socket.
289-
* @param data_array Array of buffers and addresses containing data to send.
290-
* @param broadcast Whether to send as broadcast.
291-
* @param timeout_ms Timeout in milliseconds (0 for infinite).
292-
* @return PTK_OK on success, PTK_ERR_TIMEOUT on timeout, PTK_ERR_NETWORK_ERROR on error.
293-
* On error, ptk_get_err() provides the error code.
294-
*/
295-
ptk_err ptk_udp_socket_send_many_to(ptk_sock *sock, ptk_udp_buf_entry_array_t *data_array, const ptk_address_t *dest_addr, bool broadcast, ptk_duration_ms timeout_ms);
299+
ptk_err ptk_udp_socket_send_to(ptk_sock *sock, ptk_buf *data, const ptk_address_t *dest_addr, bool broadcast, ptk_duration_ms timeout_ms);
296300

297301
/**
298-
* Receive UDP data, returning an array of buffers (blocking).
302+
* Receive UDP data (blocking).
299303
*
300304
* @param sock UDP socket.
301-
* @param sender_addr Output parameter for sender's address of last packet (can be NULL).
302-
* @param timeout_ms Timeout in milliseconds (0 for infinite).
303-
* @return Buffer containing one received packet on success (must be freed with ptk_free()),
305+
* @param sender_addr Output parameter for sender's address (can be NULL).
306+
* @param timeout_ms Timeout in milliseconds (0 to loop and get all available packets).
307+
* @return Buffer containing received packet on success (must be freed with ptk_local_free()),
304308
* NULL on error. Check ptk_get_err() for error details.
309+
* When timeout_ms is 0, this function loops internally to collect all available packets.
305310
*/
306311
ptk_buf *ptk_udp_socket_recv_from(ptk_sock *sock, ptk_address_t *sender_addr, ptk_duration_ms timeout_ms);
307312

308-
/**
309-
* Receive UDP data, returning an array of buffers (blocking).
310-
*
311-
* @param sock UDP socket.
312-
* @param wait_for_packets If true, wait the entire timeout period and return all the packets.
313-
* If false, return as soon as any packets are available (may be one or more).
314-
* @param timeout_ms Timeout in milliseconds (0 for infinite).
315-
* @return Buffer entry array containing received packets on success (must be freed with ptk_free()),
316-
* NULL on error. Check ptk_get_err() for error details.
317-
*/
318-
ptk_udp_buf_entry_array_t *ptk_udp_socket_recv_many_from(ptk_sock *sock, bool wait_for_packets, ptk_duration_ms timeout_ms);
319-
320313

321314
//=============================================================================
322315
// NETWORK DISCOVERY

0 commit comments

Comments
 (0)