From c401bbcc12eeaa6543f847fb2466c11e42451997 Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Wed, 9 May 2018 09:58:09 +0000 Subject: [PATCH] Feature: Added listensocket_container_set_sockcount_cb() and listensocket_container_sockcount() --- src/listensocket.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/listensocket.h | 2 ++ 2 files changed, 48 insertions(+) diff --git a/src/listensocket.c b/src/listensocket.c index b7970021..9eec86fc 100644 --- a/src/listensocket.c +++ b/src/listensocket.c @@ -38,6 +38,8 @@ struct listensocket_container_tag { listensocket_t **sock; int *sockref; size_t sock_len; + void (*sockcount_cb)(size_t count, void *userdata); + void *sockcount_userdata; }; struct listensocket_tag { refobject_base_t __base; @@ -54,6 +56,14 @@ static int listensocket__select_set(listensocket_t *self, fd_set *set, int *max) static int listensocket__select_isset(listensocket_t *self, fd_set *set); #endif +static inline void __call_sockcount_cb(listensocket_container_t *self) +{ + if (self->sockcount_cb == NULL) + return; + + self->sockcount_cb(listensocket_container_sockcount(self), self->sockcount_userdata); +} + static void listensocket_container_clear_sockets(listensocket_container_t *self) { size_t i; @@ -76,6 +86,8 @@ static void listensocket_container_clear_sockets(listensocket_container_t *self) free(self->sockref); self->sock = NULL; self->sockref = NULL; + + __call_sockcount_cb(self); } @@ -94,6 +106,8 @@ listensocket_container_t * listensocket_container_new(void) self->sock = NULL; self->sock_len = 0; + self->sockcount_cb = NULL; + self->sockcount_userdata = NULL; return self; } @@ -161,10 +175,13 @@ int listensocket_container_setup(listensocket_container_ if (listensocket_refsock(self->sock[i]) == 0) { self->sockref[i] = 1; } else { + ICECAST_LOG_DEBUG("Can not ref socket."); ret = 1; } } + __call_sockcount_cb(self); + return ret; } @@ -214,6 +231,7 @@ static connection_t * listensocket_container_accept__inner(listensocket_co ICECAST_LOG_ERROR("Closing listen socket in error state."); listensocket_unrefsock(socks[i]); self->sockref[p] = 0; + __call_sockcount_cb(self); } } } @@ -264,6 +282,34 @@ connection_t * listensocket_container_accept(listensocket_container return listensocket_container_accept__inner(self, timeout); } +int listensocket_container_set_sockcount_cb(listensocket_container_t *self, void (*cb)(size_t count, void *userdata), void *userdata) +{ + if (!self) + return -1; + + self->sockcount_cb = cb; + self->sockcount_userdata = userdata; + + return 0; +} + +ssize_t listensocket_container_sockcount(listensocket_container_t *self) +{ + ssize_t count = 0; + size_t i; + + if (!self) + return -1; + + for (i = 0; i < self->sock_len; i++) { + if (self->sockref[i]) { + count++; + } + } + + return count; +} + static void __listensocket_free(refobject_t self, void **userdata) { listensocket_t *listensocket = REFOBJECT_TO_TYPE(self, listensocket_t *); diff --git a/src/listensocket.h b/src/listensocket.h index e6f4b05b..6c4576c0 100644 --- a/src/listensocket.h +++ b/src/listensocket.h @@ -16,6 +16,8 @@ listensocket_container_t * listensocket_container_new(void); int listensocket_container_configure(listensocket_container_t *self, const ice_config_t *config); int listensocket_container_setup(listensocket_container_t *self); connection_t * listensocket_container_accept(listensocket_container_t *self, int timeout); +int listensocket_container_set_sockcount_cb(listensocket_container_t *self, void (*cb)(size_t count, void *userdata), void *userdata); +ssize_t listensocket_container_sockcount(listensocket_container_t *self); int listensocket_refsock(listensocket_t *self); int listensocket_unrefsock(listensocket_t *self);