/*
- * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2010 Andre Noll <maan@systemlinux.org>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
#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.
*
struct private_udp_recv_data {
/** The socket file descriptor. */
int fd;
+ struct btr_pool *btrp;
};
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;
+ if (generic_recv_pre_select(s, t) <= 0)
+ return;
para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
}
-static int enough_space(size_t nbytes, size_t loaded)
+static int udp_check_eof(size_t sz, struct iovec iov[2])
{
- 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 (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;
+ }
+ 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;
}
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;
- int ret;
- char tmpbuf[UDP_RECV_CHUNK_SIZE];
+ struct btr_node *btrn = rn->btrn;
size_t packet_size;
+ struct iovec iov[2];
+ int ret, iovcnt;
- if (rn->output_error && *rn->output_error < 0) {
- t->error = *rn->output_error;
+ t->error = 0;
+ ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+ if (ret < 0)
+ goto err;
+ if (ret == 0)
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;
- }
- t->error = -E_RECV_EOF;
- if (!ret)
+ iovcnt = btr_pool_get_buffers(purd->btrp, iov);
+ ret = -E_UDP_OVERRUN;
+ if (iovcnt == 0)
+ goto err;
+ ret = para_readv(purd->fd, iov, iovcnt);
+ /* EAGAIN is possible even if FD_ISSET */
+ if (ret < 0 && is_errno(-ret, EAGAIN))
return;
+ if (ret == 0)
+ ret = -E_RECV_EOF;
+ if (ret < 0)
+ goto err;
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;
-}
-
-static void udp_shutdown(void)
-{
+ ret = udp_check_eof(packet_size, iov);
+ if (ret < 0)
+ goto err;
+ if (iov[0].iov_len >= packet_size)
+ btr_add_output_pool(purd->btrp, packet_size, btrn);
+ else { /* both buffers contain data */
+ btr_add_output_pool(purd->btrp, iov[0].iov_len, btrn);
+ btr_add_output_pool(purd->btrp, packet_size - iov[0].iov_len,
+ btrn);
+ }
return;
+err:
+ btr_remove_node(btrn);
+ t->error = ret;
+ close(purd->fd);
+ purd->fd = -1;
}
static void udp_recv_close(struct receiver_node *rn)
if (purd->fd >= 0)
close(purd->fd);
+ btr_pool_free(purd->btrp);
free(rn->private_data);
- free(rn->buf);
}
static void *udp_recv_parse_config(int argc, char **argv)
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;
ret = mcast_receiver_setup(purd->fd, iface);
if (ret < 0) {
close(purd->fd);
- return ret;
+ goto err;
}
ret = mark_fd_nonblocking(purd->fd);
- if (ret < 0)
+ if (ret < 0) {
+ close(purd->fd);
goto err;
- PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
+ }
+ PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
c->port_arg, purd->fd);
+ purd->btrp = btr_pool_new("udp_recv", 320 * 1024);
return purd->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.
*
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