1
0
mirror of https://gitlab.xiph.org/xiph/icecast-server.git synced 2024-09-22 04:15:54 -04:00

Feature: Added listensocket_container_set_sockcount_cb() and listensocket_container_sockcount()

This commit is contained in:
Philipp Schafft 2018-05-09 09:58:09 +00:00
parent 5490120d4d
commit c401bbcc12
2 changed files with 48 additions and 0 deletions

View File

@ -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 *);

View File

@ -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);