6 #include <sys/socket.h>
11 #include "serverpool.h"
18 extern ServerPool *servers;
20 UDPInput::UDPInput(const string &stream_id, const string &url)
21 : stream_id(stream_id),
25 // Should be verified by the caller.
27 bool ok = parse_url(url, &protocol, &host, &port, &path);
33 UDPInput::UDPInput(const InputProto &serialized)
34 : stream_id(serialized.stream_id()),
35 url(serialized.url()),
36 sock(serialized.sock())
38 // Should be verified by the caller.
40 bool ok = parse_url(url, &protocol, &host, &port, &path);
46 InputProto UDPInput::serialize() const
48 InputProto serialized;
49 serialized.set_stream_id(stream_id);
50 serialized.set_url(url);
51 serialized.set_sock(sock);
55 void UDPInput::close_socket()
60 } while (ret == -1 && errno == EINTR);
69 void UDPInput::construct_header()
73 "Content-type: application/octet-stream\r\n"
74 "Cache-control: no-cache\r\n"
75 "Server: " SERVER_IDENTIFICATION "\r\n"
77 servers->set_header(stream_id, header);
80 void UDPInput::do_work()
82 while (!should_stop) {
84 int port_num = atoi(port.c_str());
85 sock = create_server_socket(port_num, UDP_SOCKET);
87 fprintf(stderr, "WARNING: UDP socket creation failed. Waiting 0.2 seconds and trying again...\n");
93 // Since we are non-blocking, we need to wait for the right state first.
94 // Wait up to 50 ms, then check should_stop.
99 int nfds = poll(&pfd, 1, 50);
100 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
112 ret = recv(sock, buf, sizeof(buf), 0);
113 } while (ret == -1 && errno == EINTR);
121 servers->add_data(stream_id, buf, ret);