Support UDP input. Also fix some issues with socket closing.
[cubemap] / udpinput.cpp
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <sys/poll.h>
5 #include <arpa/inet.h>
6 #include <errno.h>
7 #include <string>
8
9 #include "acceptor.h"
10 #include "udpinput.h"
11 #include "serverpool.h"
12 #include "version.h"
13 #include "state.pb.h"
14
15 using namespace std;
16
17 extern ServerPool *servers;
18
19 UDPInput::UDPInput(const string &stream_id, const string &url)
20         : stream_id(stream_id),
21           url(url),
22           sock(-1)
23 {
24         // Should be verified by the caller.
25         string protocol;
26         bool ok = parse_url(url, &protocol, &host, &port, &path);
27         assert(ok);
28
29         construct_header();
30 }
31
32 UDPInput::UDPInput(const InputProto &serialized)
33         : stream_id(serialized.stream_id()),
34           url(serialized.url()),
35           sock(serialized.sock())
36 {
37         // Should be verified by the caller.
38         string protocol;
39         bool ok = parse_url(url, &protocol, &host, &port, &path);
40         assert(ok);
41
42         construct_header();
43 }
44
45 InputProto UDPInput::serialize() const
46 {
47         InputProto serialized;
48         serialized.set_url(url);
49         serialized.set_sock(sock);
50         return serialized;
51 }
52
53 void UDPInput::close_socket()
54 {
55         int ret;
56         do {
57                 ret = close(sock);
58         } while (ret == -1 && errno == EINTR);
59
60         if (ret == -1) {
61                 perror("close()");
62         }
63
64         sock = -1;
65 }
66         
67 void UDPInput::construct_header()
68 {
69         string header =
70                 "HTTP/1.0 200 OK\r\n"
71                 "Content-type: application/octet-stream\r\n"
72                 "Cache-control: no-cache\r\n"
73                 "Server: " SERVER_IDENTIFICATION "\r\n"
74                 "\r\n";
75         servers->set_header(stream_id, header);
76 }
77
78 void UDPInput::do_work()
79 {
80         while (!should_stop) {
81                 if (sock == -1) {
82                         int port_num = atoi(port.c_str());
83                         sock = create_server_socket(port_num, UDP_SOCKET);
84                         if (sock == -1) {
85                                 fprintf(stderr, "WARNING: UDP socket creation failed. Waiting 0.2 seconds and trying again...\n");
86                                 usleep(200000);
87                                 continue;
88                         }
89                 }
90
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.
93                 pollfd pfd;
94                 pfd.fd = sock;
95                 pfd.events = POLLIN;
96
97                 int nfds = poll(&pfd, 1, 50);
98                 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
99                         continue;
100                 }
101                 if (nfds == -1) {
102                         perror("poll");
103                         close_socket();
104                         continue;       
105                 }
106
107                 char buf[4096];
108                 int ret;
109                 do {
110                         ret = recv(sock, buf, sizeof(buf), 0);
111                 } while (ret == -1 && errno == EINTR);
112
113                 if (ret <= 0) {
114                         perror("recv");
115                         close_socket();
116                         continue;
117                 }
118                 
119                 servers->add_data(stream_id, buf, ret);
120         }
121 }