Skip to content

Commit

Permalink
Add pre_open event
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jan 22, 2024
1 parent 825fc86 commit 2c1c6b5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/context.c
Expand Up @@ -225,6 +225,9 @@ struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *
context->long_timestamp = 0;
context->global_tick = 0;

/* Some new events must be set to null for backwards compatibility */
context->on_pre_open = 0;

us_internal_loop_link(loop, context);

/* If we are called from within SSL code, SSL code will make further changes to us */
Expand Down Expand Up @@ -414,6 +417,12 @@ struct us_socket_t *us_socket_context_adopt_socket(int ssl, struct us_socket_con
return new_s;
}

/* For backwards compatibility, this function will be set to nullptr by default. */
void us_socket_context_on_pre_open(int ssl, struct us_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR (*on_pre_open)(LIBUS_SOCKET_DESCRIPTOR fd)) {
/* For this event, there is no difference between SSL and non-SSL */
context->on_pre_open = on_pre_open;
}

void us_socket_context_on_open(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_open)(struct us_socket_t *s, int is_client, char *ip, int ip_length)) {
#ifndef LIBUS_NO_SSL
if (ssl) {
Expand Down
1 change: 1 addition & 0 deletions src/internal/internal.h
Expand Up @@ -130,6 +130,7 @@ struct us_socket_context_t {
struct us_socket_t *iterator;
struct us_socket_context_t *prev, *next;

LIBUS_SOCKET_DESCRIPTOR (*on_pre_open)(LIBUS_SOCKET_DESCRIPTOR fd);
struct us_socket_t *(*on_open)(struct us_socket_t *, int is_client, char *ip, int ip_length);
struct us_socket_t *(*on_data)(struct us_socket_t *, char *data, int length);
struct us_socket_t *(*on_writable)(struct us_socket_t *);
Expand Down
2 changes: 2 additions & 0 deletions src/libusockets.h
Expand Up @@ -155,6 +155,8 @@ struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *
void us_socket_context_free(int ssl, struct us_socket_context_t *context);

/* Setters of various async callbacks */
void us_socket_context_on_pre_open(int ssl, struct us_socket_context_t *context,
LIBUS_SOCKET_DESCRIPTOR (*on_pre_open)(LIBUS_SOCKET_DESCRIPTOR fd));
void us_socket_context_on_open(int ssl, struct us_socket_context_t *context,
struct us_socket_t *(*on_open)(struct us_socket_t *s, int is_client, char *ip, int ip_length));
void us_socket_context_on_close(int ssl, struct us_socket_context_t *context,
Expand Down
18 changes: 12 additions & 6 deletions src/loop.c
Expand Up @@ -275,13 +275,19 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events)
/* Todo: stop timer if any */

do {
/* adopt the newly accepted socket */
us_adopt_accepted_socket(0, us_socket_context(0, &listen_socket->s),
client_fd, listen_socket->socket_ext_size, bsd_addr_get_ip(&addr), bsd_addr_get_ip_length(&addr));
struct us_socket_context_t *context = us_socket_context(0, &listen_socket->s);
/* See if we want to export the FD or keep it here (this event can be unset) */
if (context->on_pre_open == 0 || context->on_pre_open(client_fd) == client_fd) {

/* Adopt the newly accepted socket */
us_adopt_accepted_socket(0, context,
client_fd, listen_socket->socket_ext_size, bsd_addr_get_ip(&addr), bsd_addr_get_ip_length(&addr));

/* Exit accept loop if listen socket was closed in on_open handler */
if (us_socket_is_closed(0, &listen_socket->s)) {
break;
}

/* Exit accept loop if listen socket was closed in on_open handler */
if (us_socket_is_closed(0, &listen_socket->s)) {
break;
}

} while ((client_fd = bsd_accept_socket(us_poll_fd(p), &addr)) != LIBUS_SOCKET_ERROR);
Expand Down

0 comments on commit 2c1c6b5

Please sign in to comment.