X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=udp_send.c;h=3804c39c1b96571e6a6553c97ed5dd690284c47f;hp=61cab430c04dca75f1e89c5e4821be3b24328dfe;hb=002731cd3938f3be6b71651e56c062af1adcdec0;hpb=9692b4241bc316665bd5b6810ea8af83057a0a89 diff --git a/udp_send.c b/udp_send.c index 61cab430..3804c39c 100644 --- a/udp_send.c +++ b/udp_send.c @@ -25,7 +25,8 @@ #include "net.h" #include "fd.h" #include "sched.h" - +#include "close_on_fork.h" +#include "chunk_queue.h" /** Convert in_addr to ascii. */ #define TARGET_ADDR(oc) inet_ntoa((oc)->addr) @@ -40,37 +41,35 @@ struct udp_target { int port; /** The socket fd. */ int fd; + /** The list of queued chunks for this fd. */ + struct chunk_queue *cq; }; static struct list_head targets; static int sender_status; +static void udp_close_target(struct udp_target *ut) +{ + if (ut->fd < 0) + return; + close(ut->fd); + del_close_on_fork_list(ut->fd); + cq_destroy(ut->cq); + ut->cq = NULL; + ut->fd = -1; +} + static void udp_delete_target(struct udp_target *ut, const char *msg) { PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ut), ut->port, msg); - if (ut->fd >= 0) - close(ut->fd); + udp_close_target(ut); list_del(&ut->node); free(ut); } -static void udp_send_buf(char *buf, size_t len) -{ - struct udp_target *ut, *tmp; - int ret; - - list_for_each_entry_safe(ut, tmp, &targets, node) { - size_t written = len; - if (ut->fd < 0) - continue; - ret = write_all(ut->fd, buf, &written); - if (ret < 0) /* TODO: Use chunk queueing */ - return udp_delete_target(ut, "send error"); - if (written != len) - PARA_WARNING_LOG("short write %zu/%zu\n", written, len); - } -} +/** The maximal size of the per-target chunk queue. */ +#define UDP_CQ_BYTES 40000 static int udp_init_session(struct udp_target *ut) { @@ -78,28 +77,77 @@ static int udp_init_session(struct udp_target *ut) if (ut->fd >= 0) /* nothing to do */ return 0; - PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ut), ut->port); - /* TODO: Make ttl configurable. */ - ret = create_udp_send_socket(TARGET_ADDR(ut), ut->port, 10); + ret = create_udp_send_socket(TARGET_ADDR(ut), ut->port, + conf.udp_ttl_arg); if (ret < 0) return ret; ut->fd = ret; - return mark_fd_nonblocking(ut->fd); + ret = mark_fd_nonblocking(ut->fd); + if (ret < 0) { + close(ut->fd); + return ret; + } + add_close_on_fork_list(ut->fd); + ut->cq = cq_new(UDP_CQ_BYTES); + PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ut), ut->port); + return 1; +} + +static void udp_send_buf(char *buf, size_t len) +{ + struct udp_target *ut, *tmp; + int ret; + + list_for_each_entry_safe(ut, tmp, &targets, node) { + ret = udp_init_session(ut); + if (ret < 0) { + udp_delete_target(ut, para_strerror(-ret)); + continue; + } + ret = send_queued_chunks(ut->fd, ut->cq, 0); + if (ret < 0) { + udp_delete_target(ut, para_strerror(-ret)); + continue; + } + if (!len) + continue; + if (!ret) { /* still data left in the queue */ + ret = cq_enqueue(ut->cq, buf, len); + if (ret < 0) { + udp_delete_target(ut, para_strerror(-ret)); + continue; + } + } + ret = write_nonblock(ut->fd, buf, len, 0); + if (ret < 0) { + udp_delete_target(ut, para_strerror(-ret)); + continue; + } + if (ret != len) { + ret = cq_enqueue(ut->cq, buf + ret, len - ret); + if (ret < 0) { + udp_delete_target(ut, para_strerror(-ret)); + continue; + } + } + } } static void udp_shutdown_targets(void) { char buf[UDP_AUDIO_HEADER_LEN]; struct udp_target *ut, *tmp; + struct udp_audio_header uah = { + .stream_type = UDP_UNKNOWN_STREAM, + .packet_type = UDP_EOF_PACKET, + }; - udp_write_packet_type(buf, UDP_EOF_PACKET); - udp_write_magic(buf); + write_udp_audio_header(buf, &uah); list_for_each_entry_safe(ut, tmp, &targets, node) { if (ut->fd < 0) continue; write(ut->fd, buf, UDP_AUDIO_HEADER_LEN); - close(ut->fd); - ut->fd = -1; + udp_close_target(ut); } } @@ -121,16 +169,12 @@ static void udp_send(long unsigned current_chunk, __a_unused long unsigned chunk const char *buf, size_t len, const char *header_buf, size_t header_len) { - struct udp_target *ut, *tmp; - size_t sendbuf_len; - uint8_t packet_type = UDP_DATA_PACKET; - int ret; char *sendbuf; + size_t sendbuf_len; struct timeval *chunk_tv; - uint8_t stream_type = header_len? UDP_HEADER_STREAM : UDP_PLAIN_STREAM; + struct udp_audio_header uah; -// PARA_NOTICE_LOG("header_len: %zd, header_buf: %p\n", header_len, -// header_buf); +// PARA_NOTICE_LOG("len: %zd, header_len: %zd\n", len, header_len); if (sender_status != SENDER_ON) return; @@ -140,27 +184,22 @@ static void udp_send(long unsigned current_chunk, __a_unused long unsigned chunk return; if (list_empty(&targets)) return; - list_for_each_entry_safe(ut, tmp, &targets, node) { - ret = udp_init_session(ut); - if (ret < 0) - udp_delete_target(ut, para_strerror(-ret)); - } - if (!need_extra_header(current_chunk)) - header_len = 0; + uah.stream_type = header_len? UDP_HEADER_STREAM : UDP_PLAIN_STREAM; + uah.header_len = need_extra_header(current_chunk)? header_len : 0; if (!current_chunk) - packet_type = UDP_BOF_PACKET; - else if (header_len) - packet_type = UDP_HEADER_PACKET; - sendbuf_len = UDP_AUDIO_HEADER_LEN + header_len + len; + uah.packet_type = UDP_BOF_PACKET; + else if (uah.header_len) + uah.packet_type = UDP_HEADER_PACKET; + else + uah.packet_type = UDP_DATA_PACKET; + uah.payload_len = uah.header_len + len; + sendbuf_len = UDP_AUDIO_HEADER_LEN + uah.payload_len; sendbuf = para_malloc(sendbuf_len); - udp_write_magic(sendbuf); - udp_write_stream_type(sendbuf, stream_type); - udp_write_packet_type(sendbuf, packet_type); - udp_write_header_len(sendbuf, header_len); - if (header_len) + write_udp_audio_header(sendbuf, &uah); + if (uah.header_len) memcpy(sendbuf + UDP_AUDIO_HEADER_LEN, header_buf, - header_len); - memcpy(sendbuf + UDP_AUDIO_HEADER_LEN + header_len, buf, len); + uah.header_len); + memcpy(sendbuf + UDP_AUDIO_HEADER_LEN + uah.header_len, buf, len); udp_send_buf(sendbuf, sendbuf_len); free(sendbuf); }