X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=udp_send.c;h=1408ae7f251bc4fe0c4e79e67e00dc7914d0b91b;hb=3bd3f7a83d4685ca18f5eeb3aafc73b615698016;hp=8bbcbae7fa611473d1abd67a65d70506380aa8a5;hpb=a6cbab95272b31c55b2b41e1d3dd55743c7c774f;p=paraslash.git diff --git a/udp_send.c b/udp_send.c index 8bbcbae7..1408ae7f 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,17 +41,29 @@ 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); } @@ -63,14 +76,37 @@ static void udp_send_buf(char *buf, size_t len) list_for_each_entry_safe(ut, tmp, &targets, node) { if (ut->fd < 0) continue; - ret = write_nonblock(ut->fd, buf, len, len); - if (ret < 0) /* TODO: Use chunk queueing */ - return udp_delete_target(ut, "send error"); - if (ret != len) - PARA_WARNING_LOG("short write %zu/%zu\n", ret, len); + 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; + } + } } } +#define UDP_CQ_BYTES 40000 + static int udp_init_session(struct udp_target *ut) { int ret; @@ -84,9 +120,13 @@ static int udp_init_session(struct udp_target *ut) return ret; ut->fd = ret; ret = mark_fd_nonblocking(ut->fd); - if (ret < 0) + if (ret < 0) { close(ut->fd); - return ret; + return ret; + } + add_close_on_fork_list(ut->fd); + ut->cq = cq_new(UDP_CQ_BYTES); + return 1; } static void udp_shutdown_targets(void) @@ -100,8 +140,7 @@ static void udp_shutdown_targets(void) if (ut->fd < 0) continue; write(ut->fd, buf, UDP_AUDIO_HEADER_LEN); - close(ut->fd); - ut->fd = -1; + udp_close_target(ut); } }