X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=udp_recv.c;h=c1f238308b58bab556434f57171be30db92944f2;hp=bb166c5147ef18603fc87e9f19b0b112084cea07;hb=4744d937c4160898d1fe151257606430750e580c;hpb=5496ca9d07ede9aaa3afd86b60cdf16ed8ccca2b diff --git a/udp_recv.c b/udp_recv.c index bb166c51..c1f23830 100644 --- a/udp_recv.c +++ b/udp_recv.c @@ -1,12 +1,11 @@ /* - * Copyright (C) 2005-2009 Andre Noll + * Copyright (C) 2005-2012 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ /** \file udp_recv.c Paraslash's udp receiver */ #include -#include #include #include @@ -18,151 +17,83 @@ #include "ggo.h" #include "recv.h" #include "udp_recv.cmdline.h" -#include "audiod.h" #include "string.h" #include "net.h" #include "fd.h" #include "buffer_tree.h" -/** The size of the receiver node buffer. */ -#define UDP_RECV_CHUNK_SIZE (128 * 1024) -/** - * Data specific to the udp receiver. - * - * \sa \ref receiver, \ref receiver_node. - */ -struct private_udp_recv_data { - /** The socket file descriptor. */ - int fd; -}; - static void udp_recv_pre_select(struct sched *s, struct task *t) { struct receiver_node *rn = container_of(t, struct receiver_node, task); - struct private_udp_recv_data *purd = rn->private_data; - int ret; - - if (rn->btrn) { - ret = generic_recv_pre_select(s, t); - if (ret <= 0) - return; - } - para_fd_set(purd->fd, &s->rfds, &s->max_fileno); -} - -static int enough_space(size_t nbytes, size_t loaded) -{ - return nbytes + loaded < UDP_RECV_CHUNK_SIZE; -} -static int add_rn_output(struct receiver_node *rn, char *buf, size_t len) -{ - if (!len) - return 1; - if (!enough_space(len, rn->loaded)) - return -E_UDP_OVERRUN; - memcpy(rn->buf + rn->loaded, buf, len); - rn->loaded += len; - return 1; + if (generic_recv_pre_select(s, t) <= 0) + return; + para_fd_set(rn->fd, &s->rfds, &s->max_fileno); } -static void udp_recv_post_select_nobtr(__a_unused struct sched *s, struct task *t) +static int udp_check_eof(size_t sz, struct iovec iov[2]) { - struct receiver_node *rn = container_of(t, struct receiver_node, task); - struct private_udp_recv_data *purd = rn->private_data; - int ret; - char tmpbuf[UDP_RECV_CHUNK_SIZE]; - size_t packet_size; - - if (rn->output_error && *rn->output_error < 0) { - t->error = *rn->output_error; - return; - } - if (!FD_ISSET(purd->fd, &s->rfds)) - return; - ret = recv_bin_buffer(purd->fd, tmpbuf, UDP_RECV_CHUNK_SIZE); - if (ret < 0) { - if (is_errno(ret, EINTR) || is_errno(ret, EAGAIN)) - goto success; - t->error = ret; - return; + if (sz < FEC_EOF_PACKET_LEN) + return 0; + if (iov[0].iov_len >= FEC_EOF_PACKET_LEN) { + if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, + FEC_EOF_PACKET_LEN) != 0) + return 0; + return -E_RECV_EOF; } - t->error = -E_RECV_EOF; - if (!ret) - return; - packet_size = ret; - if (packet_size >= FEC_EOF_PACKET_LEN) - if (!memcmp(tmpbuf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) - return; - t->error = add_rn_output(rn, tmpbuf, packet_size); - if (t->error < 0) - return; -success: - t->error = 1; + if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, iov[0].iov_len) != 0) + return 0; + if (memcmp(iov[1].iov_base, FEC_EOF_PACKET + iov[0].iov_len, + FEC_EOF_PACKET_LEN - iov[0].iov_len) != 0) + return 0; + return -E_RECV_EOF; } -#define UDP_RECV_READ_BUF_SIZE 1500 - -static void udp_recv_post_select_btr(__a_unused struct sched *s, struct task *t) +static void udp_recv_post_select(__a_unused struct sched *s, struct task *t) { struct receiver_node *rn = container_of(t, struct receiver_node, task); - struct private_udp_recv_data *purd = rn->private_data; struct btr_node *btrn = rn->btrn; - int ret; - char *buf = NULL; - size_t packet_size; + size_t num_bytes; + struct iovec iov[2]; + int ret, readv_ret, iovcnt; t->error = 0; ret = btr_node_status(btrn, 0, BTR_NT_ROOT); + if (ret <= 0) + goto out; + iovcnt = btr_pool_get_buffers(rn->btrp, iov); + ret = -E_UDP_OVERRUN; + if (iovcnt == 0) + goto out; + ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes); + if (num_bytes == 0) + goto out; + readv_ret = ret; + ret = udp_check_eof(num_bytes, iov); if (ret < 0) - goto err; - if (ret == 0) - return; - if (!FD_ISSET(purd->fd, &s->rfds)) - return; - buf = para_malloc(UDP_RECV_READ_BUF_SIZE); - ret = recv_bin_buffer(purd->fd, buf, UDP_RECV_READ_BUF_SIZE); - if (ret == 0) - ret = -E_RECV_EOF; - if (ret < 0) - goto err; - packet_size = ret; - if (packet_size >= FEC_EOF_PACKET_LEN) { - if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) { - ret = -E_RECV_EOF; - goto err; - } + goto out; + if (iov[0].iov_len >= num_bytes) + btr_add_output_pool(rn->btrp, num_bytes, btrn); + else { /* both buffers contain data */ + btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn); + btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, + btrn); } - btr_add_output(buf, ret, btrn); - return; -err: - free(buf); + ret = readv_ret; +out: + if (ret >= 0) + return; btr_remove_node(btrn); t->error = ret; -} - -static void udp_recv_post_select(__a_unused struct sched *s, struct task *t) -{ - struct receiver_node *rn = container_of(t, struct receiver_node, task); - if (rn->btrn) - return udp_recv_post_select_btr(s, t); - udp_recv_post_select_nobtr(s, t); -} - -static void udp_shutdown(void) -{ - return; + close(rn->fd); + rn->fd = -1; } static void udp_recv_close(struct receiver_node *rn) { - struct private_udp_recv_data *purd = rn->private_data; - - if (purd->fd >= 0) - close(purd->fd); - free(rn->private_data); - free(rn->buf); - udp_recv_cmdline_parser_free(rn->conf); + if (rn->fd >= 0) + close(rn->fd); + btr_pool_free(rn->btrp); } static void *udp_recv_parse_config(int argc, char **argv) @@ -194,9 +125,11 @@ static int mcast_receiver_setup(int fd, const char *iface) if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) goto err; + assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6); if (iface != NULL && id == 0) - PARA_WARNING_LOG("could not resolve interface %s, using default", iface); + PARA_WARNING_LOG("could not resolve interface %s, using default\n", + iface); switch (ss.ss_family) { case AF_INET: @@ -211,12 +144,13 @@ static int mcast_receiver_setup(int fd, const char *iface) m4.imr_interface.s_addr = INADDR_ANY; if (id != 0) - PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported."); + PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported\n"); #endif - m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr; + m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr; - if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0) + if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, + &m4, sizeof(m4)) < 0) break; } return 0; @@ -231,9 +165,6 @@ static int mcast_receiver_setup(int fd, const char *iface) break; } return 0; - default: - PARA_ERROR_LOG("address family %d not supported", ss.ss_family); - return -E_ADDRESS_LOOKUP; } err: return -ERRNO_TO_PARA_ERROR(errno); @@ -241,38 +172,40 @@ err: static int udp_recv_open(struct receiver_node *rn) { - struct private_udp_recv_data *purd; struct udp_recv_args_info *c = rn->conf; char *iface = c->iface_given ? c->iface_arg : NULL; int ret; - rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE); - rn->private_data = para_calloc(sizeof(struct private_udp_recv_data)); - purd = rn->private_data; - - ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg); + ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL); if (ret < 0) goto err; - purd->fd = ret; + rn->fd = ret; - ret = mcast_receiver_setup(purd->fd, iface); + ret = mcast_receiver_setup(rn->fd, iface); if (ret < 0) { - close(purd->fd); - return ret; + close(rn->fd); + goto err; } - ret = mark_fd_nonblocking(purd->fd); - if (ret < 0) + ret = mark_fd_nonblocking(rn->fd); + if (ret < 0) { + close(rn->fd); goto err; - PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg, - c->port_arg, purd->fd); - return purd->fd; + } + PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg, + c->port_arg, rn->fd); + rn->btrp = btr_pool_new("udp_recv", 320 * 1024); + return rn->fd; err: - free(rn->private_data); - free(rn->buf); return ret; } +static void udp_recv_free_config(void *conf) +{ + udp_recv_cmdline_parser_free(conf); + free(conf); +} + /** * The init function of the udp receiver. * @@ -285,12 +218,12 @@ void udp_recv_init(struct receiver *r) struct udp_recv_args_info dummy; udp_recv_cmdline_parser_init(&dummy); - r->shutdown = udp_shutdown; r->open = udp_recv_open; r->close = udp_recv_close; r->pre_select = udp_recv_pre_select; r->post_select = udp_recv_post_select; r->parse_config = udp_recv_parse_config; + r->free_config = udp_recv_free_config; r->help = (struct ggo_help) { .short_help = udp_recv_args_info_help, .detailed_help = udp_recv_args_info_detailed_help