Fix an error where clients in asleep state would hang after a HUP.
[cubemap] / server.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <assert.h>
5 #include <arpa/inet.h>
6 #include <curl/curl.h>
7 #include <sys/socket.h>
8 #include <pthread.h>
9 #include <sys/types.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
12 #include <errno.h>
13 #include <vector>
14 #include <string>
15 #include <map>
16 #include <algorithm>
17
18 #include "metacube.h"
19 #include "server.h"
20 #include "mutexlock.h"
21 #include "parse.h"
22 #include "state.pb.h"
23
24 using namespace std;
25
26 Client::Client(int sock)
27         : sock(sock),
28           state(Client::READING_REQUEST),
29           stream(NULL),
30           header_or_error_bytes_sent(0),
31           bytes_sent(0)
32 {
33         request.reserve(1024);
34 }
35         
36 Client::Client(const ClientProto &serialized, Stream *stream)
37         : sock(serialized.sock()),
38           state(State(serialized.state())),
39           request(serialized.request()),
40           stream_id(serialized.stream_id()),
41           stream(stream),
42           header_or_error(serialized.header_or_error()),
43           header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
44           bytes_sent(serialized.bytes_sent())
45 {
46 }
47
48 ClientProto Client::serialize() const
49 {
50         ClientProto serialized;
51         serialized.set_sock(sock);
52         serialized.set_state(state);
53         serialized.set_request(request);
54         serialized.set_stream_id(stream_id);
55         serialized.set_header_or_error(header_or_error);
56         serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
57         serialized.set_bytes_sent(bytes_sent);
58         return serialized;
59 }
60
61 Stream::Stream(const string &stream_id)
62         : stream_id(stream_id),
63           data(new char[BACKLOG_SIZE]),
64           data_size(0)
65 {
66         memset(data, 0, BACKLOG_SIZE);
67 }
68
69 Stream::~Stream()
70 {
71         delete[] data;
72 }
73
74 Stream::Stream(const StreamProto &serialized)
75         : stream_id(serialized.stream_id()),
76           header(serialized.header()),
77           data(new char[BACKLOG_SIZE]),
78           data_size(serialized.data_size())
79 {
80         assert(serialized.data().size() == BACKLOG_SIZE);
81         memcpy(data, serialized.data().data(), BACKLOG_SIZE);
82 }
83
84 StreamProto Stream::serialize() const
85 {
86         StreamProto serialized;
87         serialized.set_header(header);
88         serialized.set_data(string(data, data + BACKLOG_SIZE));
89         serialized.set_data_size(data_size);
90         serialized.set_stream_id(stream_id);
91         return serialized;
92 }
93
94 Server::Server()
95 {
96         pthread_mutex_init(&mutex, NULL);
97
98         epoll_fd = epoll_create(1024);  // Size argument is ignored.
99         if (epoll_fd == -1) {
100                 perror("epoll_fd");
101                 exit(1);
102         }
103 }
104
105 Server::~Server()
106 {
107         int ret;
108         do {
109                 ret = close(epoll_fd);
110         } while (ret == -1 && errno == EINTR);
111
112         if (ret == -1) {
113                 perror("close(epoll_fd)");
114         }
115 }
116
117 void Server::run()
118 {
119         should_stop = false;
120         
121         // Joinable is already the default, but it's good to be certain.
122         pthread_attr_t attr;
123         pthread_attr_init(&attr);
124         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
125         pthread_create(&worker_thread, &attr, Server::do_work_thunk, this);
126 }
127         
128 void Server::stop()
129 {
130         {
131                 MutexLock lock(&mutex);
132                 should_stop = true;
133         }
134
135         if (pthread_join(worker_thread, NULL) == -1) {
136                 perror("pthread_join");
137                 exit(1);
138         }
139 }
140
141 void *Server::do_work_thunk(void *arg)
142 {
143         Server *server = static_cast<Server *>(arg);
144         server->do_work();
145         return NULL;
146 }
147
148 void Server::do_work()
149 {
150         for ( ;; ) {
151                 int nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, EPOLL_TIMEOUT_MS);
152                 if (nfds == -1 && errno == EINTR) {
153                         continue;
154                 }
155                 if (nfds == -1) {
156                         perror("epoll_wait");
157                         exit(1);
158                 }
159
160                 MutexLock lock(&mutex);  // We release the mutex between iterations.
161         
162                 if (should_stop) {
163                         return;
164                 }
165         
166                 for (int i = 0; i < nfds; ++i) {
167                         int fd = events[i].data.fd;
168                         assert(clients.count(fd) != 0);
169                         Client *client = &clients[fd];
170
171                         if (events[i].events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) {
172                                 close_client(client);
173                                 continue;
174                         }
175
176                         process_client(client);
177                 }
178         }
179 }
180
181 CubemapStateProto Server::serialize() const
182 {
183         CubemapStateProto serialized;
184         for (map<int, Client>::const_iterator client_it = clients.begin();
185              client_it != clients.end();
186              ++client_it) {
187                 serialized.add_clients()->MergeFrom(client_it->second.serialize());
188         }
189         for (map<string, Stream *>::const_iterator stream_it = streams.begin();
190              stream_it != streams.end();
191              ++stream_it) {
192                 serialized.add_streams()->MergeFrom(stream_it->second->serialize());
193         }
194         return serialized;
195 }
196
197 void Server::add_client(int sock)
198 {
199         MutexLock lock(&mutex);
200         clients.insert(make_pair(sock, Client(sock)));
201
202         // Start listening on data from this socket.
203         epoll_event ev;
204         ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
205         ev.data.u64 = 0;  // Keep Valgrind happy.
206         ev.data.fd = sock;
207         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
208                 perror("epoll_ctl(EPOLL_CTL_ADD)");
209                 exit(1);
210         }
211
212         process_client(&clients[sock]);
213 }
214
215 void Server::add_client_from_serialized(const ClientProto &client)
216 {
217         MutexLock lock(&mutex);
218         Stream *stream = find_stream(client.stream_id());
219         clients.insert(make_pair(client.sock(), Client(client, stream)));
220         Client *client_ptr = &clients[client.sock()];
221
222         // Start listening on data from this socket.
223         epoll_event ev;
224         if (client.state() == Client::READING_REQUEST) {
225                 ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
226         } else {
227                 // If we don't have more data for this client, we'll be putting it into
228                 // the sleeping array again soon.
229                 ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
230         }
231         ev.data.u64 = 0;  // Keep Valgrind happy.
232         ev.data.fd = client.sock();
233         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client.sock(), &ev) == -1) {
234                 perror("epoll_ctl(EPOLL_CTL_ADD)");
235                 exit(1);
236         }
237
238         if (client_ptr->state == Client::SENDING_DATA && 
239             client_ptr->bytes_sent == client_ptr->stream->data_size) {
240                 put_client_to_sleep(client_ptr);
241         } else {
242                 process_client(client_ptr);
243         }
244 }
245
246 void Server::add_stream(const string &stream_id)
247 {
248         MutexLock lock(&mutex);
249         streams.insert(make_pair(stream_id, new Stream(stream_id)));
250 }
251
252 void Server::add_stream_from_serialized(const StreamProto &stream)
253 {
254         MutexLock lock(&mutex);
255         streams.insert(make_pair(stream.stream_id(), new Stream(stream)));
256 }
257         
258 void Server::set_header(const string &stream_id, const string &header)
259 {
260         MutexLock lock(&mutex);
261         find_stream(stream_id)->header = header;
262
263         // If there are clients we haven't sent anything to yet, we should give
264         // them the header, so push back into the SENDING_HEADER state.
265         for (map<int, Client>::iterator client_it = clients.begin();
266              client_it != clients.end();
267              ++client_it) {
268                 Client *client = &client_it->second;
269                 if (client->state == Client::SENDING_DATA &&
270                     client->bytes_sent == 0) {
271                         construct_header(client);
272                 }
273         }
274 }
275         
276 void Server::add_data(const string &stream_id, const char *data, size_t bytes)
277 {
278         if (bytes == 0) {
279                 return;
280         }
281
282         MutexLock lock(&mutex);
283         Stream *stream = find_stream(stream_id);
284         size_t pos = stream->data_size % BACKLOG_SIZE;
285         stream->data_size += bytes;
286
287         if (pos + bytes > BACKLOG_SIZE) {
288                 size_t to_copy = BACKLOG_SIZE - pos;
289                 memcpy(stream->data + pos, data, to_copy);
290                 data += to_copy;
291                 bytes -= to_copy;
292                 pos = 0;
293         }
294
295         memcpy(stream->data + pos, data, bytes);
296         wake_up_all_clients();
297 }
298
299 // See the .h file for postconditions after this function.      
300 void Server::process_client(Client *client)
301 {
302         switch (client->state) {
303         case Client::READING_REQUEST: {
304 read_request_again:
305                 // Try to read more of the request.
306                 char buf[1024];
307                 int ret;
308                 do {
309                         ret = read(client->sock, buf, sizeof(buf));
310                 } while (ret == -1 && errno == EINTR);
311
312                 if (ret == -1 && errno == EAGAIN) {
313                         // No more data right now. Nothing to do.
314                         // This is postcondition #2.
315                         return;
316                 }
317                 if (ret == -1) {
318                         perror("read");
319                         close_client(client);
320                         return;
321                 }
322                 if (ret == 0) {
323                         // OK, the socket is closed.
324                         close_client(client);
325                         return;
326                 }
327
328                 // Guard against overlong requests gobbling up all of our space.
329                 if (client->request.size() + ret > MAX_CLIENT_REQUEST) {
330                         fprintf(stderr, "WARNING: fd %d sent overlong request!\n", client->sock);
331                         close_client(client);
332                         return;
333                 }       
334
335                 // See if we have \r\n\r\n anywhere in the request. We start three bytes
336                 // before what we just appended, in case we just got the final character.
337                 size_t existing_req_bytes = client->request.size();
338                 client->request.append(string(buf, buf + ret));
339         
340                 size_t start_at = (existing_req_bytes >= 3 ? existing_req_bytes - 3 : 0);
341                 const char *ptr = reinterpret_cast<char *>(
342                         memmem(client->request.data() + start_at, client->request.size() - start_at,
343                                "\r\n\r\n", 4));
344                 if (ptr == NULL) {
345                         // OK, we don't have the entire header yet. Fine; we'll get it later.
346                         // See if there's more data for us.
347                         goto read_request_again;
348                 }
349
350                 if (ptr != client->request.data() + client->request.size() - 4) {
351                         fprintf(stderr, "WARNING: fd %d had junk data after request!\n", client->sock);
352                         close_client(client);
353                         return;
354                 }
355
356                 int error_code = parse_request(client);
357                 if (error_code == 200) {
358                         construct_header(client);
359                 } else {
360                         construct_error(client, error_code);
361                 }
362
363                 // We've changed states, so fall through.
364                 assert(client->state == Client::SENDING_ERROR ||
365                        client->state == Client::SENDING_HEADER);
366         }
367         case Client::SENDING_ERROR:
368         case Client::SENDING_HEADER: {
369 sending_header_or_error_again:
370                 int ret;
371                 do {
372                         ret = write(client->sock,
373                                     client->header_or_error.data() + client->header_or_error_bytes_sent,
374                                     client->header_or_error.size() - client->header_or_error_bytes_sent);
375                 } while (ret == -1 && errno == EINTR);
376
377                 if (ret == -1 && errno == EAGAIN) {
378                         // We're out of socket space, so now we're at the “low edge” of epoll's
379                         // edge triggering. epoll will tell us when there is more room, so for now,
380                         // just return.
381                         // This is postcondition #4.
382                         return;
383                 }
384
385                 if (ret == -1) {
386                         // Error! Postcondition #1.
387                         perror("write");
388                         close_client(client);
389                         return;
390                 }
391                 
392                 client->header_or_error_bytes_sent += ret;
393                 assert(client->header_or_error_bytes_sent <= client->header_or_error.size());
394
395                 if (client->header_or_error_bytes_sent < client->header_or_error.size()) {
396                         // We haven't sent all yet. Fine; go another round.
397                         goto sending_header_or_error_again;
398                 }
399
400                 // We're done sending the header or error! Clear it to release some memory.
401                 client->header_or_error.clear();
402
403                 if (client->state == Client::SENDING_ERROR) {
404                         // We're done sending the error, so now close.  
405                         // This is postcondition #1.
406                         close_client(client);
407                         return;
408                 }
409
410                 // Start sending from the end. In other words, we won't send any of the backlog,
411                 // but we'll start sending immediately as we get data.
412                 // This is postcondition #3.
413                 client->state = Client::SENDING_DATA;
414                 client->bytes_sent = client->stream->data_size;
415                 put_client_to_sleep(client);
416                 return;
417         }
418         case Client::SENDING_DATA: {
419                 // See if there's some data we've lost. Ideally, we should drop to a block boundary,
420                 // but resync will be the mux's problem.
421                 const Stream *stream = client->stream;
422                 size_t bytes_to_send = stream->data_size - client->bytes_sent;
423                 if (bytes_to_send == 0) {
424                         return;
425                 }
426                 if (bytes_to_send > BACKLOG_SIZE) {
427                         fprintf(stderr, "WARNING: fd %d lost %lld bytes, maybe too slow connection\n",
428                                 client->sock,
429                                 (long long int)(bytes_to_send - BACKLOG_SIZE));
430                         client->bytes_sent = stream->data_size - BACKLOG_SIZE;
431                         bytes_to_send = BACKLOG_SIZE;
432                 }
433
434                 // See if we need to split across the circular buffer.
435                 ssize_t ret;
436                 if ((client->bytes_sent % BACKLOG_SIZE) + bytes_to_send > BACKLOG_SIZE) {
437                         size_t bytes_first_part = BACKLOG_SIZE - (client->bytes_sent % BACKLOG_SIZE);
438
439                         iovec iov[2];
440                         iov[0].iov_base = const_cast<char *>(stream->data + (client->bytes_sent % BACKLOG_SIZE));
441                         iov[0].iov_len = bytes_first_part;
442
443                         iov[1].iov_base = const_cast<char *>(stream->data);
444                         iov[1].iov_len = bytes_to_send - bytes_first_part;
445
446                         do {
447                                 ret = writev(client->sock, iov, 2);
448                         } while (ret == -1 && errno == EINTR);
449                 } else {
450                         do {
451                                 ret = write(client->sock,
452                                             stream->data + (client->bytes_sent % BACKLOG_SIZE),
453                                             bytes_to_send);
454                         } while (ret == -1 && errno == EINTR);
455                 }
456                 if (ret == -1 && errno == EAGAIN) {
457                         // We're out of socket space, so return; epoll will wake us up
458                         // when there is more room.
459                         // This is postcondition #4.
460                         return;
461                 }
462                 if (ret == -1) {
463                         // Error, close; postcondition #1.
464                         perror("write/writev");
465                         close_client(client);
466                         return;
467                 }
468                 client->bytes_sent += ret;
469
470                 if (client->bytes_sent == stream->data_size) {
471                         // We don't have any more data for this client, so put it to sleep.
472                         // This is postcondition #3.
473                         put_client_to_sleep(client);
474                 } else {
475                         // XXX: Do we need to go another round here to explicitly
476                         // get the EAGAIN?
477                 }
478                 break;
479         }
480         default:
481                 assert(false);
482         }
483 }
484
485 int Server::parse_request(Client *client)
486 {
487         vector<string> lines = split_lines(client->request);
488         if (lines.empty()) {
489                 return 400;  // Bad request (empty).
490         }
491
492         vector<string> request_tokens = split_tokens(lines[0]);
493         if (request_tokens.size() < 2) {
494                 return 400;  // Bad request (empty).
495         }
496         if (request_tokens[0] != "GET") {
497                 return 400;  // Should maybe be 405 instead?
498         }
499         if (streams.count(request_tokens[1]) == 0) {
500                 return 404;  // Not found.
501         }
502
503         client->stream_id = request_tokens[1];
504         client->stream = find_stream(client->stream_id);
505         client->request.clear();
506
507         return 200;  // OK!
508 }
509
510 void Server::construct_header(Client *client)
511 {
512         client->header_or_error = "HTTP/1.0 200 OK\r\nContent-type: video/x-flv\r\nCache-Control: no-cache\r\n\r\n" +
513                 find_stream(client->stream_id)->header;
514
515         // Switch states.
516         client->state = Client::SENDING_HEADER;
517
518         epoll_event ev;
519         ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
520         ev.data.u64 = 0;  // Keep Valgrind happy.
521         ev.data.fd = client->sock;
522
523         if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
524                 perror("epoll_ctl(EPOLL_CTL_MOD)");
525                 exit(1);
526         }
527 }
528         
529 void Server::construct_error(Client *client, int error_code)
530 {
531         char error[256];
532         snprintf(error, 256, "HTTP/1.0 %d Error\r\nContent-type: text/plain\r\n\r\nSomething went wrong. Sorry.\r\n",
533                 error_code);
534         client->header_or_error = error;
535
536         // Switch states.
537         client->state = Client::SENDING_ERROR;
538
539         epoll_event ev;
540         ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
541         ev.data.u64 = 0;  // Keep Valgrind happy.
542         ev.data.fd = client->sock;
543
544         if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
545                 perror("epoll_ctl(EPOLL_CTL_MOD)");
546                 exit(1);
547         }
548 }
549         
550 void Server::close_client(Client *client)
551 {
552         if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
553                 perror("epoll_ctl(EPOLL_CTL_DEL)");
554                 exit(1);
555         }
556
557         // This client could be sleeping, so we'll need to fix that. (Argh, O(n).)
558         vector<Client *>::iterator new_end =
559                 remove(sleeping_clients.begin(), sleeping_clients.end(), client);
560         sleeping_clients.erase(new_end, sleeping_clients.end());
561         
562         // Bye-bye!
563         int ret;
564         do {
565                 ret = close(client->sock);
566         } while (ret == -1 && errno == EINTR);
567
568         if (ret == -1) {
569                 perror("close");
570         }
571
572         clients.erase(client->sock);
573 }
574         
575 void Server::put_client_to_sleep(Client *client)
576 {
577         sleeping_clients.push_back(client);
578 }
579
580 void Server::wake_up_all_clients()
581 {
582         vector<Client *> to_process;
583         swap(sleeping_clients, to_process);
584         for (unsigned i = 0; i < to_process.size(); ++i) {
585                 process_client(to_process[i]);
586         }
587 }
588         
589 Stream *Server::find_stream(const string &stream_id)
590 {
591         map<string, Stream *>::iterator it = streams.find(stream_id);
592         assert(it != streams.end());
593         return it->second;
594 }