X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=udp_send.c;h=3804c39c1b96571e6a6553c97ed5dd690284c47f;hp=1408ae7f251bc4fe0c4e79e67e00dc7914d0b91b;hb=f85e05c0b3951e7d3119983c118c82d71f2662a3;hpb=3bd3f7a83d4685ca18f5eeb3aafc73b615698016 diff --git a/udp_send.c b/udp_send.c index 1408ae7f..3804c39c 100644 --- a/udp_send.c +++ b/udp_send.c @@ -68,14 +68,42 @@ static void udp_delete_target(struct udp_target *ut, const char *msg) free(ut); } +/** The maximal size of the per-target chunk queue. */ +#define UDP_CQ_BYTES 40000 + +static int udp_init_session(struct udp_target *ut) +{ + int ret; + + if (ut->fd >= 0) /* nothing to do */ + return 0; + ret = create_udp_send_socket(TARGET_ADDR(ut), ut->port, + conf.udp_ttl_arg); + if (ret < 0) + return ret; + ut->fd = ret; + 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) { - if (ut->fd < 0) + 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)); @@ -105,37 +133,16 @@ static void udp_send_buf(char *buf, size_t len) } } -#define UDP_CQ_BYTES 40000 - -static int udp_init_session(struct udp_target *ut) -{ - int ret; - - if (ut->fd >= 0) /* nothing to do */ - return 0; - PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ut), ut->port); - ret = create_udp_send_socket(TARGET_ADDR(ut), ut->port, - conf.udp_ttl_arg); - if (ret < 0) - return ret; - ut->fd = ret; - 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); - return 1; -} - 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; @@ -162,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; @@ -181,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); }