]> git.sesse.net Git - cubemap/blob - udpinput.cpp
bd83381b563dc9b897b70f8d89b66a39a3da0ef1
[cubemap] / udpinput.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <poll.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/socket.h>
7 #include <unistd.h>
8 #include <string>
9
10 #include "acceptor.h"
11 #include "log.h"
12 #include "serverpool.h"
13 #include "state.pb.h"
14 #include "udpinput.h"
15 #include "version.h"
16
17 using namespace std;
18
19 extern ServerPool *servers;
20
21 UDPInput::UDPInput(const string &url)
22         : url(url),
23           sock(-1)
24 {
25         // Should be verified by the caller.
26         string protocol;
27         bool ok = parse_url(url, &protocol, &host, &port, &path);
28         assert(ok);
29
30         construct_header();
31 }
32
33 UDPInput::UDPInput(const InputProto &serialized)
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                 log_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         for (size_t i = 0; i < stream_ids.size(); ++i) {
76                 servers->set_header(stream_ids[i], header);
77         }
78 }
79
80 void UDPInput::do_work()
81 {
82         while (!should_stop) {
83                 if (sock == -1) {
84                         int port_num = atoi(port.c_str());
85                         sock = create_server_socket(port_num, UDP_SOCKET);
86                         if (sock == -1) {
87                                 log(WARNING, "[%s] UDP socket creation failed. Waiting 0.2 seconds and trying again...",
88                                              url.c_str());
89                                 usleep(200000);
90                                 continue;
91                         }
92                 }
93
94                 // Since we are non-blocking, we need to wait for the right state first.
95                 // Wait up to 50 ms, then check should_stop.
96                 pollfd pfd;
97                 pfd.fd = sock;
98                 pfd.events = POLLIN;
99
100                 int nfds = poll(&pfd, 1, 50);
101                 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
102                         continue;
103                 }
104                 if (nfds == -1) {
105                         log_perror("poll");
106                         close_socket();
107                         continue;       
108                 }
109
110                 char buf[4096];
111                 int ret;
112                 do {
113                         ret = recv(sock, buf, sizeof(buf), 0);
114                 } while (ret == -1 && errno == EINTR);
115
116                 if (ret <= 0) {
117                         log_perror("recv");
118                         close_socket();
119                         continue;
120                 }
121                 
122                 for (size_t i = 0; i < stream_ids.size(); ++i) {
123                         servers->add_data(stream_ids[i], buf, ret);
124                 }
125         }
126 }