Skip to content

Commit

Permalink
Allow adding external event loop in socket server
Browse files Browse the repository at this point in the history
This is hacky. Socket server and core bus listen should be refactored to
give you an fd you can poll.
  • Loading branch information
archigup committed Nov 8, 2024
1 parent fdb632d commit 9f03668
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ggl-socket/include/ggl/socket_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ GglError ggl_socket_server_listen(
void *ctx
);

extern void (*ggl_socket_server_ext_handler)(void);
extern int ggl_socket_server_ext_fd;

#endif
17 changes: 17 additions & 0 deletions ggl-socket/src/socket_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include <unistd.h>
#include <stdint.h>

void (*ggl_socket_server_ext_handler)(void) = NULL;
int ggl_socket_server_ext_fd;

static void new_client_available(
GglSocketPool *pool, int epoll_fd, int socket_fd
) {
Expand Down Expand Up @@ -171,13 +174,18 @@ typedef struct {
// server_fd's data must be out of range of handle (uint32_t)
static const uint64_t SERVER_FD_DATA = UINT64_MAX;

static const uint64_t EXT_FD_DATA = UINT64_MAX - 1;

static GglError epoll_fd_ready(void *epoll_ctx, uint64_t data) {
SocketServerCtx *server_ctx = epoll_ctx;

if (data == SERVER_FD_DATA) {
new_client_available(
server_ctx->pool, server_ctx->epoll_fd, server_ctx->server_fd
);
} else if ((ggl_socket_server_ext_handler != NULL)
&& (data == EXT_FD_DATA)) {
ggl_socket_server_ext_handler();
} else if (data <= UINT32_MAX) {
client_data_ready(
server_ctx->pool,
Expand Down Expand Up @@ -230,6 +238,15 @@ GglError ggl_socket_server_listen(
return ret;
}

if (ggl_socket_server_ext_handler != NULL) {
ret = ggl_socket_epoll_add(
epoll_fd, ggl_socket_server_ext_fd, EXT_FD_DATA
);
if (ret != GGL_ERR_OK) {
return ret;
}
}

SocketServerCtx server_ctx = {
.pool = pool,
.epoll_fd = epoll_fd,
Expand Down

0 comments on commit 9f03668

Please sign in to comment.