X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=udpinput.cpp;h=a2915e8333dff36c1b3cc1fa2efcb1cfbf1d27cf;hp=5e39d6632c9b72ca6916816283df44ffa8ef1820;hb=HEAD;hpb=70c47a998c5aa2eb536c3c8f71f3178cd217a14d diff --git a/udpinput.cpp b/udpinput.cpp index 5e39d66..824c3bc 100644 --- a/udpinput.cpp +++ b/udpinput.cpp @@ -1,16 +1,17 @@ #include #include +#include #include #include #include #include #include #include +#include #include #include "acceptor.h" #include "log.h" -#include "mutexlock.h" #include "serverpool.h" #include "state.pb.h" #include "stream.h" @@ -40,8 +41,9 @@ bool parse_ip_address(const string &ip, sockaddr_storage *addr) ip.c_str()); return false; } - if (inet_pton(AF_INET6, ip.c_str(), &addr6->sin6_addr) != 1) { - log(ERROR, "'%s' is not a valid IPv6 address"); + string raw_ip(ip.begin() + 1, ip.end() - 1); + if (inet_pton(AF_INET6, raw_ip.c_str(), &addr6->sin6_addr) != 1) { + log(ERROR, "'%s' is not a valid IPv6 address", raw_ip.c_str()); return false; } } else { @@ -111,17 +113,18 @@ UDPInput::UDPInput(const string &url) construct_header(); - pthread_mutex_init(&stats_mutex, NULL); stats.url = url; - stats.bytes_received = 0; - stats.data_bytes_received = 0; - stats.connect_time = time(NULL); + stats.connect_time = time(nullptr); } UDPInput::UDPInput(const InputProto &serialized) : url(serialized.url()), sock(serialized.sock()) { + // Set back the close-on-exec flag for the socket. + // (This can't leak into a child, since we haven't been started yet.) + fcntl(sock, F_SETFD, FD_CLOEXEC); + // Should be verified by the caller. string protocol; bool ok = parse_url(url, &protocol, &user, &host, &port, &path); @@ -129,25 +132,29 @@ UDPInput::UDPInput(const InputProto &serialized) construct_header(); - pthread_mutex_init(&stats_mutex, NULL); stats.url = url; stats.bytes_received = serialized.bytes_received(); stats.data_bytes_received = serialized.data_bytes_received(); if (serialized.has_connect_time()) { stats.connect_time = serialized.connect_time(); } else { - stats.connect_time = time(NULL); + stats.connect_time = time(nullptr); } } InputProto UDPInput::serialize() const { + // Unset the close-on-exec flag for the socket. + // (This can't leak into a child, since there's only one thread left.) + fcntl(sock, F_SETFD, 0); + InputProto serialized; serialized.set_url(url); serialized.set_sock(sock); serialized.set_bytes_received(stats.bytes_received); serialized.set_data_bytes_received(stats.data_bytes_received); serialized.set_connect_time(stats.connect_time); + serialized.set_is_metacube_encoded(false); return serialized; } @@ -178,7 +185,7 @@ void UDPInput::do_work() while (!should_stop()) { if (sock == -1) { int port_num = atoi(port.c_str()); - sockaddr_in6 addr = CreateAnyAddress(port_num); + sockaddr_in6 addr = create_any_address(port_num); sock = create_server_socket(addr, UDP_SOCKET); if (sock == -1) { log(WARNING, "[%s] UDP socket creation failed. Waiting 0.2 seconds and trying again...", @@ -201,7 +208,7 @@ void UDPInput::do_work() } // Wait for a packet, or a wakeup. - bool activity = wait_for_activity(sock, POLLIN, NULL); + bool activity = wait_for_activity(sock, POLLIN, nullptr); if (!activity) { // Most likely, should_stop was set. continue; @@ -212,26 +219,26 @@ void UDPInput::do_work() ret = recv(sock, packet_buf, sizeof(packet_buf), 0); } while (ret == -1 && errno == EINTR); - if (ret <= 0) { + if (ret < 0) { // Note that zero-byte packets are legal. log_perror("recv"); close_socket(); continue; } { - MutexLock lock(&stats_mutex); + lock_guard lock(stats_mutex); stats.bytes_received += ret; stats.data_bytes_received += ret; } - - for (size_t i = 0; i < stream_indices.size(); ++i) { - servers->add_data(stream_indices[i], packet_buf, ret, SUITABLE_FOR_STREAM_START); + + for (size_t stream_index : stream_indices) { + servers->add_data(stream_index, packet_buf, ret, /*metacube_flags=*/0, /*pts=*/RationalPTS()); } } } InputStats UDPInput::get_stats() const { - MutexLock lock(&stats_mutex); + lock_guard lock(stats_mutex); return stats; }