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_url(url);
49 serialized.set_sock(sock);
53 void UDPInput::close_socket()
58 } while (ret == -1 && errno == EINTR);
67 void UDPInput::construct_header()
71 "Content-type: application/octet-stream\r\n"
72 "Cache-control: no-cache\r\n"
73 "Server: " SERVER_IDENTIFICATION "\r\n"
75 servers->set_header(stream_id, header);
78 void UDPInput::do_work()
80 while (!should_stop) {
82 int port_num = atoi(port.c_str());
83 sock = create_server_socket(port_num, UDP_SOCKET);
85 fprintf(stderr, "WARNING: UDP socket creation failed. Waiting 0.2 seconds and trying again...\n");
91 // Since we are non-blocking, we need to wait for the right state first.
92 // Wait up to 50 ms, then check should_stop.
97 int nfds = poll(&pfd, 1, 50);
98 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
110 ret = recv(sock, buf, sizeof(buf), 0);
111 } while (ret == -1 && errno == EINTR);
119 servers->add_data(stream_id, buf, ret);