From 0b786d977ded3c3922e851e5b8d60837b43469b9 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 7 Aug 2017 17:44:15 +0200 Subject: [PATCH] server: Make array of senders constant. Currently we define an array of static non-constant sender structures in vss.c with only ->init being initialized at compile time. At startup the ->init method of each sender must fill out the other function pointers of the structure. This approach is kind of pointless, as these pointers are known at compile time and never change. This patch changes the three senders to define a non-static constant sender structure instead. The vss maintains an array of pointers to these structures. Since the new sender structures are non-static, they need to be doxified. We use the opportunity to provide an overview of each sender. The patch also introduces FOR_EACH_SENDER(), a macro which is used several times to replace the open-coded loops we had before. --- command.c | 21 ++++++++++---------- dccp_send.c | 50 +++++++++++++++++++++++++++--------------------- http_send.c | 52 ++++++++++++++++++++++++++++++-------------------- send.h | 16 +++++++++------- udp_send.c | 47 +++++++++++++++++++++++++-------------------- vss.c | 55 +++++++++++++++++++---------------------------------- 6 files changed, 124 insertions(+), 117 deletions(-) diff --git a/command.c b/command.c index cba2afb4..cd24c035 100644 --- a/command.c +++ b/command.c @@ -48,7 +48,6 @@ static const char * const server_command_perms_txt[] = {LSG_SERVER_CMD_AUX_INFOS extern int mmd_mutex; extern struct misc_meta_data *mmd; -extern struct sender senders[]; int send_afs_status(struct command_context *cc, int parser_friendly); static void dummy(__a_unused int s) @@ -235,10 +234,10 @@ static int check_sender_args(struct command_context *cc, return ret; } arg = lls_input(0, lpr); - for (i = 0; senders[i].name; i++) - if (!strcmp(senders[i].name, arg)) + FOR_EACH_SENDER(i) + if (strcmp(senders[i]->name, arg) == 0) break; - if (!senders[i].name) + if (!senders[i]) return -E_COMMAND_SYNTAX; scd->sender_num = i; arg = lls_input(1, lpr); @@ -248,7 +247,7 @@ static int check_sender_args(struct command_context *cc, if (i == NUM_SENDER_CMDS) return -E_COMMAND_SYNTAX; scd->cmd_num = i; - if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) + if (!senders[scd->sender_num]->client_cmds[scd->cmd_num]) return -E_SENDER_CMD; switch (scd->cmd_num) { case SENDER_on: @@ -330,10 +329,10 @@ static int com_sender(struct command_context *cc, struct lls_parse_result *lpr) struct sender_command_data scd; if (lls_num_inputs(lpr) == 0) { - for (i = 0; senders[i].name; i++) { + FOR_EACH_SENDER(i) { char *tmp; ret = xasprintf(&tmp, "%s%s\n", msg? msg : "", - senders[i].name); + senders[i]->name); free(msg); msg = tmp; } @@ -344,17 +343,17 @@ static int com_sender(struct command_context *cc, struct lls_parse_result *lpr) if (scd.sender_num < 0) return ret; if (strcmp(lls_input(1, lpr), "status") == 0) - msg = senders[scd.sender_num].status(); + msg = senders[scd.sender_num]->status(); else - msg = senders[scd.sender_num].help(); + msg = senders[scd.sender_num]->help(); return send_sb(&cc->scc, msg, strlen(msg), SBD_OUTPUT, false); } switch (scd.cmd_num) { case SENDER_add: case SENDER_delete: - assert(senders[scd.sender_num].resolve_target); - ret = senders[scd.sender_num].resolve_target(lls_input(2, lpr), + assert(senders[scd.sender_num]->resolve_target); + ret = senders[scd.sender_num]->resolve_target(lls_input(2, lpr), &scd); if (ret < 0) return ret; diff --git a/dccp_send.c b/dccp_send.c index 6f989541..9ab21f8a 100644 --- a/dccp_send.c +++ b/dccp_send.c @@ -214,32 +214,14 @@ static char *dccp_status(void) return result; } -/** - * The init function of the dccp sender. - * - * \param s pointer to the dccp sender struct. - * - * It initializes all function pointers of \a s and starts - * listening on the given port. +/* + * Initialize the client list and the access control list and listen on the + * dccp port. */ -void dccp_send_init(struct sender *s) +static void dccp_send_init(void) { int ret; - s->status = dccp_status; - s->send = NULL; - s->pre_select = dccp_pre_select; - s->post_select = dccp_post_select; - s->shutdown_clients = dccp_shutdown_clients; - s->resolve_target = NULL; - s->help = generic_sender_help; - s->client_cmds[SENDER_on] = dccp_com_on; - s->client_cmds[SENDER_off] = dccp_com_off; - s->client_cmds[SENDER_deny] = dccp_com_deny; - s->client_cmds[SENDER_allow] = dccp_com_allow; - s->client_cmds[SENDER_add] = NULL; - s->client_cmds[SENDER_delete] = NULL; - init_sender_status(dss, OPT_RESULT(DCCP_ACCESS), OPT_UINT32_VAL(DCCP_PORT), OPT_UINT32_VAL(DCCP_MAX_CLIENTS), OPT_GIVEN(DCCP_DEFAULT_DENY)); @@ -247,3 +229,27 @@ void dccp_send_init(struct sender *s) if (ret < 0) PARA_ERROR_LOG("%s\n", para_strerror(-ret)); } + +/** + * The DCCP sender. + * + * This sender offers congestion control not available in plain TCP. Most + * methods of the sender structure are implemented as simple wrappers for the + * generic functions defined in \ref send_common.c. Like UDP streams, DCCP + * streams are sent FEC-encoded. + */ +const struct sender dccp_sender = { + .name = "dccp", + .init = dccp_send_init, + .pre_select = dccp_pre_select, + .post_select = dccp_post_select, + .shutdown_clients = dccp_shutdown_clients, + .client_cmds = { + [SENDER_on] = dccp_com_on, + [SENDER_off] = dccp_com_off, + [SENDER_deny] = dccp_com_deny, + [SENDER_allow] = dccp_com_allow, + }, + .help = generic_sender_help, + .status = dccp_status, +}; diff --git a/http_send.c b/http_send.c index dace5c23..3df694de 100644 --- a/http_send.c +++ b/http_send.c @@ -235,30 +235,13 @@ static char *http_status(void) return generic_sender_status(hss, "http"); } -/** - * The init function of the http sender. - * - * \param s Pointer to the http sender struct. - * - * It initializes all function pointers of \a s, the client list and the access - * control list. If the autostart option was given, the tcp port is opened. +/* + * Initialize the client list and the access control list, and optionally + * listen on the tcp port. */ -void http_send_init(struct sender *s) +static void http_send_init(void) { int ret; - s->status = http_status; - s->send = http_send; - s->pre_select = http_pre_select; - s->post_select = http_post_select; - s->shutdown_clients = http_shutdown_clients; - s->resolve_target = NULL; - s->help = generic_sender_help; - s->client_cmds[SENDER_on] = http_com_on; - s->client_cmds[SENDER_off] = http_com_off; - s->client_cmds[SENDER_deny] = http_com_deny; - s->client_cmds[SENDER_allow] = http_com_allow; - s->client_cmds[SENDER_add] = NULL; - s->client_cmds[SENDER_delete] = NULL; init_sender_status(hss, OPT_RESULT(HTTP_ACCESS), OPT_UINT32_VAL(HTTP_PORT), OPT_UINT32_VAL(HTTP_MAX_CLIENTS), @@ -269,3 +252,30 @@ void http_send_init(struct sender *s) if (ret < 0) PARA_ERROR_LOG("%s\n", para_strerror(-ret)); } + +/** + * The HTTP sender. + * + * This is the only sender which does not FEC-encode the stream. This is not + * necessary because HTTP sits on top of TCP, a reliable transport which + * retransmits lost packets automatically. The sender employs per-client queues + * which queue chunks of audio data if they can not be sent immediately because + * the write operation would block. Most methods of the sender are implemented + * as wrappers for the generic functions defined in \ref send_common.c. + */ +const struct sender http_sender = { + .name = "http", + .init = http_send_init, + .pre_select = http_pre_select, + .post_select = http_post_select, + .send = http_send, + .shutdown_clients = http_shutdown_clients, + .client_cmds = { + [SENDER_on] = http_com_on, + [SENDER_off] = http_com_off, + [SENDER_deny] = http_com_deny, + [SENDER_allow] = http_com_allow, + }, + .help = generic_sender_help, + .status = http_status, +}; diff --git a/send.h b/send.h index 9a6e5f74..dc9cd395 100644 --- a/send.h +++ b/send.h @@ -27,14 +27,11 @@ struct sender { /** The name of the sender. */ const char *name; /** - * The init function of this sender. - * - * It must fill in all function pointers of \a s as well as the \a - * client_cmds array, see below. It should also do all necessary - * preparations to init this sending facility, for example it could - * open a tcp port. + * Parse the command line options and initialize this sender (e.g., + * initialize target or access control lists, listen on a network + * socket, etc.). */ - void (*init)(struct sender *s); + void (*init)(void); /** * Return the help text of this sender. * @@ -107,6 +104,11 @@ struct sender { int (*resolve_target)(const char *, struct sender_command_data *); }; +/** NULL-terminated list, defined in \ref vss.c. */ +extern const struct sender * const senders[]; +/** Iterate over all senders. */ +#define FOR_EACH_SENDER(_i) for ((_i) = 0; senders[(_i)]; (_i)++) + /** Describes one client, connected to a paraslash sender. */ struct sender_client { /** The file descriptor of the client. */ diff --git a/udp_send.c b/udp_send.c index 49c17631..eea1c933 100644 --- a/udp_send.c +++ b/udp_send.c @@ -412,32 +412,37 @@ static char *udp_help(void) ); } -/** - * The init function of para_server's udp sender. - * - * \param s Pointer to the udp sender struct. - * - * It initializes all function pointers of \a s and the list of udp targets. - */ -void udp_send_init(struct sender *s) +/* Initialize the list of udp targets. */ +static void udp_send_init(void) { INIT_LIST_HEAD(&targets); - s->status = udp_status; - s->help = udp_help; - s->send = NULL; - s->pre_select = NULL; - s->post_select = NULL; - s->shutdown_clients = udp_shutdown_targets; - s->resolve_target = udp_resolve_target; - s->client_cmds[SENDER_on] = udp_com_on; - s->client_cmds[SENDER_off] = udp_com_off; - s->client_cmds[SENDER_deny] = NULL; - s->client_cmds[SENDER_allow] = NULL; - s->client_cmds[SENDER_add] = udp_com_add; - s->client_cmds[SENDER_delete] = udp_com_delete; sender_status = SENDER_off; udp_init_target_list(); if (!OPT_GIVEN(UDP_NO_AUTOSTART)) sender_status = SENDER_on; PARA_DEBUG_LOG("udp sender init complete\n"); } + +/** + * The UDP sender. + * + * In contrast to the other senders the UDP sender is active in the sense that + * it initiates the network connection according to its list of targets rather + * than passively waiting for clients to connect. Like DCCP streams, UDP + * streams are always sent FEC-encoded. The UDP sender is the only sender which + * supports IP multicasting. + */ +const struct sender udp_sender = { + .name = "udp", + .init = udp_send_init, + .shutdown_clients = udp_shutdown_targets, + .resolve_target = udp_resolve_target, + .client_cmds = { + [SENDER_on] = udp_com_on, + [SENDER_off] = udp_com_off, + [SENDER_add] = udp_com_add, + [SENDER_delete] = udp_com_delete, + }, + .help = udp_help, + .status = udp_status, +}; diff --git a/vss.c b/vss.c index f69225ea..84f5ada5 100644 --- a/vss.c +++ b/vss.c @@ -40,24 +40,9 @@ extern void dccp_send_init(struct sender *); extern void http_send_init(struct sender *); extern void udp_send_init(struct sender *); -/** The list of supported senders. */ -struct sender senders[] = { - { - .name = "http", - .init = http_send_init, - }, - { - .name = "dccp", - .init = dccp_send_init, - }, - { - .name = "udp", - .init = udp_send_init, - }, - { - .name = NULL, - } -}; +extern const struct sender udp_sender, dccp_sender, http_sender; +const struct sender * const senders[] = { + &http_sender, &dccp_sender, &udp_sender, NULL}; /** The possible states of the afs socket. */ enum afs_socket_status { @@ -918,10 +903,10 @@ static void vss_pre_select(struct sched *s, void *context) vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE; } else para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno); - for (i = 0; senders[i].name; i++) { - if (!senders[i].pre_select) + FOR_EACH_SENDER(i) { + if (!senders[i]->pre_select) continue; - senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds); + senders[i]->pre_select(&s->max_fileno, &s->rfds, &s->wfds); } vss_compute_timeout(s, vsst); } @@ -1082,10 +1067,10 @@ static void vss_send(struct vss_task *vsst) * We call ->send() even if len is zero because senders might * have data queued which can be sent now. */ - for (i = 0; senders[i].name; i++) { - if (!senders[i].send) + FOR_EACH_SENDER(i) { + if (!senders[i]->send) continue; - senders[i].send(mmd->current_chunk, mmd->chunks_sent, + senders[i]->send(mmd->current_chunk, mmd->chunks_sent, buf, len, vsst->header_buf, vsst->header_len); } } @@ -1101,9 +1086,9 @@ static int vss_post_select(struct sched *s, void *context) if (!vsst->map || vss_next() || vss_paused() || vss_repos()) { /* shut down senders and fec clients */ struct fec_client *fc, *tmp; - for (i = 0; senders[i].name; i++) - if (senders[i].shutdown_clients) - senders[i].shutdown_clients(); + FOR_EACH_SENDER(i) + if (senders[i]->shutdown_clients) + senders[i]->shutdown_clients(); list_for_each_entry_safe(fc, tmp, &fec_client_list, node) fc->state = FEC_STATE_NONE; mmd->stream_start.tv_sec = 0; @@ -1129,8 +1114,8 @@ static int vss_post_select(struct sched *s, void *context) int num = mmd->sender_cmd_data.cmd_num, sender_num = mmd->sender_cmd_data.sender_num; - if (senders[sender_num].client_cmds[num]) { - ret = senders[sender_num].client_cmds[num] + if (senders[sender_num]->client_cmds[num]) { + ret = senders[sender_num]->client_cmds[num] (&mmd->sender_cmd_data); if (ret < 0) PARA_ERROR_LOG("%s\n", para_strerror(-ret)); @@ -1147,10 +1132,10 @@ static int vss_post_select(struct sched *s, void *context) else vsst->afsss = AFS_SOCKET_AFD_PENDING; } - for (i = 0; senders[i].name; i++) { - if (!senders[i].post_select) + FOR_EACH_SENDER(i) { + if (!senders[i]->post_select) continue; - senders[i].post_select(&s->rfds, &s->wfds); + senders[i]->post_select(&s->rfds, &s->wfds); } if ((vss_playing() && !(mmd->vss_status_flags & VSS_PLAYING)) || (vss_next() && vss_playing())) @@ -1179,9 +1164,9 @@ void vss_init(int afs_socket, struct sched *s) ms2tv(announce_time, &vsst->announce_tv); PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv)); INIT_LIST_HEAD(&fec_client_list); - for (i = 0; senders[i].name; i++) { - PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name); - senders[i].init(&senders[i]); + FOR_EACH_SENDER(i) { + PARA_NOTICE_LOG("initializing %s sender\n", senders[i]->name); + senders[i]->init(); } mmd->sender_cmd_data.cmd_num = -1; if (OPT_GIVEN(AUTOPLAY)) { -- 2.39.5