6 #include <sys/socket.h>
12 #include "serverpool.h"
19 extern ServerPool *servers;
21 UDPInput::UDPInput(const string &stream_id, const string &url)
22 : stream_id(stream_id),
26 // Should be verified by the caller.
28 bool ok = parse_url(url, &protocol, &host, &port, &path);
34 UDPInput::UDPInput(const InputProto &serialized)
35 : stream_id(serialized.stream_id()),
36 url(serialized.url()),
37 sock(serialized.sock())
39 // Should be verified by the caller.
41 bool ok = parse_url(url, &protocol, &host, &port, &path);
47 InputProto UDPInput::serialize() const
49 InputProto serialized;
50 serialized.set_stream_id(stream_id);
51 serialized.set_url(url);
52 serialized.set_sock(sock);
56 void UDPInput::close_socket()
61 } while (ret == -1 && errno == EINTR);
64 log_perror("close()");
70 void UDPInput::construct_header()
74 "Content-type: application/octet-stream\r\n"
75 "Cache-control: no-cache\r\n"
76 "Server: " SERVER_IDENTIFICATION "\r\n"
78 servers->set_header(stream_id, header);
81 void UDPInput::do_work()
83 while (!should_stop) {
85 int port_num = atoi(port.c_str());
86 sock = create_server_socket(port_num, UDP_SOCKET);
88 log(WARNING, "[%s] UDP socket creation failed. Waiting 0.2 seconds and trying again...",
95 // Since we are non-blocking, we need to wait for the right state first.
96 // Wait up to 50 ms, then check should_stop.
101 int nfds = poll(&pfd, 1, 50);
102 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
114 ret = recv(sock, buf, sizeof(buf), 0);
115 } while (ret == -1 && errno == EINTR);
123 servers->add_data(stream_id, buf, ret);