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