3 #include <sys/socket.h>
11 #include "serverpool.h"
17 extern ServerPool *servers;
19 UDPInput::UDPInput(const string &stream_id, const string &url)
20 : stream_id(stream_id),
24 // Should be verified by the caller.
26 bool ok = parse_url(url, &protocol, &host, &port, &path);
32 UDPInput::UDPInput(const InputProto &serialized)
33 : stream_id(serialized.stream_id()),
34 url(serialized.url()),
35 sock(serialized.sock())
37 // Should be verified by the caller.
39 bool ok = parse_url(url, &protocol, &host, &port, &path);
45 InputProto UDPInput::serialize() const
47 InputProto serialized;
48 serialized.set_stream_id(stream_id);
49 serialized.set_url(url);
50 serialized.set_sock(sock);
54 void UDPInput::close_socket()
59 } while (ret == -1 && errno == EINTR);
68 void UDPInput::construct_header()
72 "Content-type: application/octet-stream\r\n"
73 "Cache-control: no-cache\r\n"
74 "Server: " SERVER_IDENTIFICATION "\r\n"
76 servers->set_header(stream_id, header);
79 void UDPInput::do_work()
81 while (!should_stop) {
83 int port_num = atoi(port.c_str());
84 sock = create_server_socket(port_num, UDP_SOCKET);
86 fprintf(stderr, "WARNING: UDP socket creation failed. Waiting 0.2 seconds and trying again...\n");
92 // Since we are non-blocking, we need to wait for the right state first.
93 // Wait up to 50 ms, then check should_stop.
98 int nfds = poll(&pfd, 1, 50);
99 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
111 ret = recv(sock, buf, sizeof(buf), 0);
112 } while (ret == -1 && errno == EINTR);
120 servers->add_data(stream_id, buf, ret);