Skip to content

Commit

Permalink
Teardown and tweaks (#232)
Browse files Browse the repository at this point in the history
* remove set arrays and just use a linked list

* tweak interface and add teardown

* dead code

* typo

* fix on_close being overwritten to null

* each socket has its own linked list of active streams

* no inits allowed post teardown

* fix bug

* can simplify

* add guard that cirbuf is inited

* slight refactor

* more re-entry protection
  • Loading branch information
mafintosh authored Dec 10, 2024
1 parent 49fa957 commit 1b74118
Show file tree
Hide file tree
Showing 28 changed files with 253 additions and 187 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ target_sources(
src/endian.c
src/queue.c
src/queue.h
src/link.h
src/io.h
src/udx.c
)
Expand Down
4 changes: 2 additions & 2 deletions examples/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ main (int argc, char **argv) {

uv_loop_init(&loop);

udx_init(&loop, &udx);
udx_init(&loop, &udx, NULL);

udx_socket_init(&udx, &sock);
udx_socket_init(&udx, &sock, NULL);

struct sockaddr_in addr;
uv_ip4_addr("0.0.0.0", 18082, &addr);
Expand Down
4 changes: 2 additions & 2 deletions examples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ main (int argc, char **argv) {
chunk.len = 16384;
chunk.base = calloc(1, chunk.len);

udx_init(&loop, &udx);
udx_init(&loop, &udx, NULL);

udx_socket_init(&udx, &sock);
udx_socket_init(&udx, &sock, NULL);

struct sockaddr_in addr;
uv_ip4_addr("0.0.0.0", 18081, &addr);
Expand Down
4 changes: 2 additions & 2 deletions examples/udxperf.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,8 @@ main (int argc, char **argv) {
}

uv_loop_init(&loop);
udx_init(&loop, &udx);
udx_socket_init(&udx, &sock);
udx_init(&loop, &udx, NULL);
udx_socket_init(&udx, &sock, NULL);

if (is_server) {
server();
Expand Down
44 changes: 27 additions & 17 deletions include/udx.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,15 @@ typedef void (*udx_interface_event_close_cb)(udx_interface_event_t *handle);
struct udx_s {
uv_loop_t *loop;

uint32_t refs;
udx_idle_cb on_idle;
int refs;
bool teardown;
bool has_streams;

uint32_t sockets_len;
uint32_t sockets_max_len;
udx_socket_t **sockets;
udx_idle_cb on_idle;

uint32_t streams_len;
uint32_t streams_max_len;
udx_stream_t **streams;
udx_socket_t *sockets;
udx_stream_t *streams;
udx_interface_event_t *listeners;

udx_cirbuf_t streams_by_id;

Expand Down Expand Up @@ -145,11 +144,14 @@ struct udx_socket_s {

udx_queue_t send_queue;

udx_socket_t *prev;
udx_socket_t *next;

udx_stream_t *streams;

udx_t *udx;
udx_cirbuf_t *streams_by_id; // for convenience

int set_id;

bool cmsg_wanted; // include a control buffer for recvmsg
int family;
int status;
Expand Down Expand Up @@ -193,7 +195,9 @@ struct udx_stream_s {
uint32_t local_id; // must be first entry, so its compat with the cirbuf
uint32_t remote_id;

int set_id;
udx_stream_t *prev;
udx_stream_t *next;

int status;
int write_wanted;
int out_of_order;
Expand Down Expand Up @@ -392,6 +396,9 @@ struct udx_interface_event_s {
uv_loop_t *loop;
udx_t *udx;

udx_interface_event_t *prev;
udx_interface_event_t *next;

uv_interface_address_t *addrs;
int addrs_len;
bool sorted;
Expand All @@ -403,13 +410,16 @@ struct udx_interface_event_s {
};

int
udx_init (uv_loop_t *loop, udx_t *udx);
udx_init (uv_loop_t *loop, udx_t *udx, udx_idle_cb on_idle);

int
udx_is_idle (udx_t *udx);

void
udx_idle (udx_t *udx, udx_idle_cb cb);
udx_teardown (udx_t *udx);

int
udx_socket_init (udx_t *udx, udx_socket_t *socket);
udx_socket_init (udx_t *udx, udx_socket_t *socket, udx_socket_close_cb cb);

int
udx_socket_get_send_buffer_size (udx_socket_t *socket, int *value);
Expand Down Expand Up @@ -460,7 +470,7 @@ int
udx_socket_recv_stop (udx_socket_t *socket);

int
udx_socket_close (udx_socket_t *socket, udx_socket_close_cb cb);
udx_socket_close (udx_socket_t *socket);

// only exposed here as a convenience / debug tool - the udx instance uses this automatically
int
Expand Down Expand Up @@ -530,7 +540,7 @@ int
udx_lookup (udx_t *udx, udx_lookup_t *req, const char *host, unsigned int flags, udx_lookup_cb cb);

int
udx_interface_event_init (udx_t *udx, udx_interface_event_t *handle);
udx_interface_event_init (udx_t *udx, udx_interface_event_t *handle, udx_interface_event_close_cb cb);

int
udx_interface_event_start (udx_interface_event_t *handle, udx_interface_event_cb cb, uint64_t frequency);
Expand All @@ -539,7 +549,7 @@ int
udx_interface_event_stop (udx_interface_event_t *handle);

int
udx_interface_event_close (udx_interface_event_t *handle, udx_interface_event_close_cb cb);
udx_interface_event_close (udx_interface_event_t *handle);

#ifdef __cplusplus
}
Expand Down
22 changes: 22 additions & 0 deletions src/link.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef UDX_LINK_H
#define UDX_LINK_H

#define udx__link_add(l, v) \
if ((l) == NULL) { \
(v)->next = (v)->prev = NULL; \
} else { \
(v)->prev = NULL; \
(v)->next = (l); \
(l)->prev = (v); \
} \
(l) = (v);

#define udx__link_remove(l, v) \
if ((v)->next != NULL) (v)->next->prev = (v)->prev; \
if ((v)->prev == NULL) (l) = (v)->next; \
else (v)->prev->next = (v)->next;

#define udx__link_foreach(l, el) \
for ((el) = (l); (el) != NULL; (el) = (el)->next)

#endif
Loading

0 comments on commit 1b74118

Please sign in to comment.