7 #include <sys/socket.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
12 #include <sys/sendfile.h>
24 #include "mutexlock.h"
31 Client::Client(int sock)
34 connect_time(time(NULL)),
35 state(Client::READING_REQUEST),
37 header_or_error_bytes_sent(0),
40 request.reserve(1024);
42 // Find the remote address, and convert it to ASCII.
44 socklen_t addr_len = sizeof(addr);
46 if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
47 perror("getpeername");
50 char buf[INET6_ADDRSTRLEN];
51 if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
60 Client::Client(const ClientProto &serialized, Stream *stream)
61 : sock(serialized.sock()),
62 remote_addr(serialized.remote_addr()),
63 connect_time(serialized.connect_time()),
64 state(State(serialized.state())),
65 request(serialized.request()),
66 stream_id(serialized.stream_id()),
68 header_or_error(serialized.header_or_error()),
69 header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
70 bytes_sent(serialized.bytes_sent())
72 if (stream->mark_pool != NULL) {
73 fwmark = stream->mark_pool->get_mark();
75 fwmark = 0; // No mark.
77 if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
79 perror("setsockopt(SO_MARK)");
84 ClientProto Client::serialize() const
86 ClientProto serialized;
87 serialized.set_sock(sock);
88 serialized.set_remote_addr(remote_addr);
89 serialized.set_connect_time(connect_time);
90 serialized.set_state(state);
91 serialized.set_request(request);
92 serialized.set_stream_id(stream_id);
93 serialized.set_header_or_error(header_or_error);
94 serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent());
95 serialized.set_bytes_sent(bytes_sent);
99 ClientStats Client::get_stats() const
102 stats.stream_id = stream_id;
103 stats.remote_addr = remote_addr;
104 stats.connect_time = connect_time;
105 stats.bytes_sent = bytes_sent;
109 Stream::Stream(const string &stream_id, size_t backlog_size)
110 : stream_id(stream_id),
111 data_fd(make_tempfile("")),
112 backlog_size(backlog_size),
126 ret = close(data_fd);
127 } while (ret == -1 && errno == EINTR);
134 Stream::Stream(const StreamProto &serialized)
135 : stream_id(serialized.stream_id()),
136 header(serialized.header()),
137 data_fd(make_tempfile(serialized.data())),
138 backlog_size(serialized.backlog_size()),
139 bytes_received(serialized.bytes_received()),
147 StreamProto Stream::serialize()
149 StreamProto serialized;
150 serialized.set_header(header);
151 if (!read_tempfile(data_fd, serialized.mutable_data())) { // Closes data_fd.
154 serialized.set_backlog_size(backlog_size);
155 serialized.set_bytes_received(bytes_received);
156 serialized.set_stream_id(stream_id);
161 void Stream::put_client_to_sleep(Client *client)
163 sleeping_clients.push_back(client);
166 void Stream::wake_up_all_clients()
168 if (to_process.empty()) {
169 swap(sleeping_clients, to_process);
171 to_process.insert(to_process.end(), sleeping_clients.begin(), sleeping_clients.end());
172 sleeping_clients.clear();
178 pthread_mutex_init(&mutex, NULL);
179 pthread_mutex_init(&queued_data_mutex, NULL);
181 epoll_fd = epoll_create(1024); // Size argument is ignored.
182 if (epoll_fd == -1) {
192 ret = close(epoll_fd);
193 } while (ret == -1 && errno == EINTR);
196 perror("close(epoll_fd)");
200 vector<ClientStats> Server::get_client_stats() const
202 vector<ClientStats> ret;
204 MutexLock lock(&mutex);
205 for (map<int, Client>::const_iterator client_it = clients.begin();
206 client_it != clients.end();
208 ret.push_back(client_it->second.get_stats());
213 void Server::do_work()
216 int nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, EPOLL_TIMEOUT_MS);
217 if (nfds == -1 && errno == EINTR) {
224 perror("epoll_wait");
228 MutexLock lock(&mutex); // We release the mutex between iterations.
230 process_queued_data();
232 for (int i = 0; i < nfds; ++i) {
233 int fd = events[i].data.fd;
234 assert(clients.count(fd) != 0);
235 Client *client = &clients[fd];
237 if (events[i].events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) {
238 close_client(client);
242 process_client(client);
245 for (map<string, Stream *>::iterator stream_it = streams.begin();
246 stream_it != streams.end();
248 vector<Client *> to_process;
249 swap(stream_it->second->to_process, to_process);
250 for (size_t i = 0; i < to_process.size(); ++i) {
251 process_client(to_process[i]);
261 CubemapStateProto Server::serialize()
263 // We don't serialize anything queued, so empty the queues.
264 process_queued_data();
266 CubemapStateProto serialized;
267 for (map<int, Client>::const_iterator client_it = clients.begin();
268 client_it != clients.end();
270 serialized.add_clients()->MergeFrom(client_it->second.serialize());
272 for (map<string, Stream *>::const_iterator stream_it = streams.begin();
273 stream_it != streams.end();
275 serialized.add_streams()->MergeFrom(stream_it->second->serialize());
280 void Server::add_client_deferred(int sock)
282 MutexLock lock(&queued_data_mutex);
283 queued_add_clients.push_back(sock);
286 void Server::add_client(int sock)
288 clients.insert(make_pair(sock, Client(sock)));
290 // Start listening on data from this socket.
292 ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
293 ev.data.u64 = 0; // Keep Valgrind happy.
295 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
296 perror("epoll_ctl(EPOLL_CTL_ADD)");
300 process_client(&clients[sock]);
303 void Server::add_client_from_serialized(const ClientProto &client)
305 MutexLock lock(&mutex);
306 Stream *stream = find_stream(client.stream_id());
307 clients.insert(make_pair(client.sock(), Client(client, stream)));
308 Client *client_ptr = &clients[client.sock()];
310 // Start listening on data from this socket.
312 if (client.state() == Client::READING_REQUEST) {
313 ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
315 // If we don't have more data for this client, we'll be putting it into
316 // the sleeping array again soon.
317 ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
319 ev.data.u64 = 0; // Keep Valgrind happy.
320 ev.data.fd = client.sock();
321 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client.sock(), &ev) == -1) {
322 perror("epoll_ctl(EPOLL_CTL_ADD)");
326 if (client_ptr->state == Client::SENDING_DATA &&
327 client_ptr->bytes_sent == client_ptr->stream->bytes_received) {
328 client_ptr->stream->put_client_to_sleep(client_ptr);
330 process_client(client_ptr);
334 void Server::add_stream(const string &stream_id, size_t backlog_size)
336 MutexLock lock(&mutex);
337 streams.insert(make_pair(stream_id, new Stream(stream_id, backlog_size)));
340 void Server::add_stream_from_serialized(const StreamProto &stream)
342 MutexLock lock(&mutex);
343 streams.insert(make_pair(stream.stream_id(), new Stream(stream)));
346 void Server::set_header(const string &stream_id, const string &header)
348 MutexLock lock(&mutex);
349 find_stream(stream_id)->header = header;
351 // If there are clients we haven't sent anything to yet, we should give
352 // them the header, so push back into the SENDING_HEADER state.
353 for (map<int, Client>::iterator client_it = clients.begin();
354 client_it != clients.end();
356 Client *client = &client_it->second;
357 if (client->state == Client::SENDING_DATA &&
358 client->bytes_sent == 0) {
359 construct_header(client);
364 void Server::set_mark_pool(const std::string &stream_id, MarkPool *mark_pool)
366 MutexLock lock(&mutex);
367 assert(clients.empty());
368 find_stream(stream_id)->mark_pool = mark_pool;
371 void Server::add_data_deferred(const string &stream_id, const char *data, size_t bytes)
373 MutexLock lock(&queued_data_mutex);
374 queued_data[stream_id].append(string(data, data + bytes));
377 void Server::add_data(const string &stream_id, const char *data, ssize_t bytes)
379 Stream *stream = find_stream(stream_id);
380 size_t pos = stream->bytes_received % stream->backlog_size;
381 stream->bytes_received += bytes;
383 if (pos + bytes > stream->backlog_size) {
384 ssize_t to_copy = stream->backlog_size - pos;
385 while (to_copy > 0) {
386 int ret = pwrite(stream->data_fd, data, to_copy, pos);
387 if (ret == -1 && errno == EINTR) {
392 // Dazed and confused, but trying to continue...
404 int ret = pwrite(stream->data_fd, data, bytes, pos);
405 if (ret == -1 && errno == EINTR) {
410 // Dazed and confused, but trying to continue...
418 stream->wake_up_all_clients();
421 // See the .h file for postconditions after this function.
422 void Server::process_client(Client *client)
424 switch (client->state) {
425 case Client::READING_REQUEST: {
427 // Try to read more of the request.
431 ret = read(client->sock, buf, sizeof(buf));
432 } while (ret == -1 && errno == EINTR);
434 if (ret == -1 && errno == EAGAIN) {
435 // No more data right now. Nothing to do.
436 // This is postcondition #2.
441 close_client(client);
445 // OK, the socket is closed.
446 close_client(client);
450 RequestParseStatus status = wait_for_double_newline(&client->request, buf, ret);
453 case RP_OUT_OF_SPACE:
454 fprintf(stderr, "WARNING: fd %d sent overlong request!\n", client->sock);
455 close_client(client);
457 case RP_NOT_FINISHED_YET:
458 // OK, we don't have the entire header yet. Fine; we'll get it later.
459 // See if there's more data for us.
460 goto read_request_again;
462 fprintf(stderr, "WARNING: fd %d had junk data after request!\n", client->sock);
463 close_client(client);
469 assert(status == RP_FINISHED);
471 int error_code = parse_request(client);
472 if (error_code == 200) {
473 construct_header(client);
475 construct_error(client, error_code);
478 // We've changed states, so fall through.
479 assert(client->state == Client::SENDING_ERROR ||
480 client->state == Client::SENDING_HEADER);
482 case Client::SENDING_ERROR:
483 case Client::SENDING_HEADER: {
484 sending_header_or_error_again:
487 ret = write(client->sock,
488 client->header_or_error.data() + client->header_or_error_bytes_sent,
489 client->header_or_error.size() - client->header_or_error_bytes_sent);
490 } while (ret == -1 && errno == EINTR);
492 if (ret == -1 && errno == EAGAIN) {
493 // We're out of socket space, so now we're at the “low edge” of epoll's
494 // edge triggering. epoll will tell us when there is more room, so for now,
496 // This is postcondition #4.
501 // Error! Postcondition #1.
503 close_client(client);
507 client->header_or_error_bytes_sent += ret;
508 assert(client->header_or_error_bytes_sent <= client->header_or_error.size());
510 if (client->header_or_error_bytes_sent < client->header_or_error.size()) {
511 // We haven't sent all yet. Fine; go another round.
512 goto sending_header_or_error_again;
515 // We're done sending the header or error! Clear it to release some memory.
516 client->header_or_error.clear();
518 if (client->state == Client::SENDING_ERROR) {
519 // We're done sending the error, so now close.
520 // This is postcondition #1.
521 close_client(client);
525 // Start sending from the end. In other words, we won't send any of the backlog,
526 // but we'll start sending immediately as we get data.
527 // This is postcondition #3.
528 client->state = Client::SENDING_DATA;
529 client->bytes_sent = client->stream->bytes_received;
530 client->stream->put_client_to_sleep(client);
533 case Client::SENDING_DATA: {
535 // See if there's some data we've lost. Ideally, we should drop to a block boundary,
536 // but resync will be the mux's problem.
537 Stream *stream = client->stream;
538 size_t bytes_to_send = stream->bytes_received - client->bytes_sent;
539 if (bytes_to_send == 0) {
542 if (bytes_to_send > stream->backlog_size) {
543 fprintf(stderr, "WARNING: fd %d lost %lld bytes, maybe too slow connection\n",
545 (long long int)(bytes_to_send - stream->backlog_size));
546 client->bytes_sent = stream->bytes_received - stream->backlog_size;
547 bytes_to_send = stream->backlog_size;
550 // See if we need to split across the circular buffer.
551 bool more_data = false;
552 if ((client->bytes_sent % stream->backlog_size) + bytes_to_send > stream->backlog_size) {
553 bytes_to_send = stream->backlog_size - (client->bytes_sent % stream->backlog_size);
559 loff_t offset = client->bytes_sent % stream->backlog_size;
560 ret = sendfile(client->sock, stream->data_fd, &offset, bytes_to_send);
561 } while (ret == -1 && errno == EINTR);
563 if (ret == -1 && errno == EAGAIN) {
564 // We're out of socket space, so return; epoll will wake us up
565 // when there is more room.
566 // This is postcondition #4.
570 // Error, close; postcondition #1.
572 close_client(client);
575 client->bytes_sent += ret;
577 if (client->bytes_sent == stream->bytes_received) {
578 // We don't have any more data for this client, so put it to sleep.
579 // This is postcondition #3.
580 stream->put_client_to_sleep(client);
581 } else if (more_data) {
582 goto sending_data_again;
591 int Server::parse_request(Client *client)
593 vector<string> lines = split_lines(client->request);
595 return 400; // Bad request (empty).
598 vector<string> request_tokens = split_tokens(lines[0]);
599 if (request_tokens.size() < 2) {
600 return 400; // Bad request (empty).
602 if (request_tokens[0] != "GET") {
603 return 400; // Should maybe be 405 instead?
605 if (streams.count(request_tokens[1]) == 0) {
606 return 404; // Not found.
609 client->stream_id = request_tokens[1];
610 client->stream = find_stream(client->stream_id);
611 if (client->stream->mark_pool != NULL) {
612 client->fwmark = client->stream->mark_pool->get_mark();
614 client->fwmark = 0; // No mark.
616 if (setsockopt(client->sock, SOL_SOCKET, SO_MARK, &client->fwmark, sizeof(client->fwmark)) == -1) {
617 if (client->fwmark != 0) {
618 perror("setsockopt(SO_MARK)");
621 client->request.clear();
626 void Server::construct_header(Client *client)
628 client->header_or_error = find_stream(client->stream_id)->header;
631 client->state = Client::SENDING_HEADER;
634 ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
635 ev.data.u64 = 0; // Keep Valgrind happy.
636 ev.data.fd = client->sock;
638 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
639 perror("epoll_ctl(EPOLL_CTL_MOD)");
644 void Server::construct_error(Client *client, int error_code)
647 snprintf(error, 256, "HTTP/1.0 %d Error\r\nContent-type: text/plain\r\n\r\nSomething went wrong. Sorry.\r\n",
649 client->header_or_error = error;
652 client->state = Client::SENDING_ERROR;
655 ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
656 ev.data.u64 = 0; // Keep Valgrind happy.
657 ev.data.fd = client->sock;
659 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
660 perror("epoll_ctl(EPOLL_CTL_MOD)");
666 void delete_from(vector<T> *v, T elem)
668 typename vector<T>::iterator new_end = remove(v->begin(), v->end(), elem);
669 v->erase(new_end, v->end());
672 void Server::close_client(Client *client)
674 if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
675 perror("epoll_ctl(EPOLL_CTL_DEL)");
679 // This client could be sleeping, so we'll need to fix that. (Argh, O(n).)
680 if (client->stream != NULL) {
681 delete_from(&client->stream->sleeping_clients, client);
682 delete_from(&client->stream->to_process, client);
683 if (client->stream->mark_pool != NULL) {
684 int fwmark = client->fwmark;
685 client->stream->mark_pool->release_mark(fwmark);
692 ret = close(client->sock);
693 } while (ret == -1 && errno == EINTR);
699 clients.erase(client->sock);
702 Stream *Server::find_stream(const string &stream_id)
704 map<string, Stream *>::iterator it = streams.find(stream_id);
705 assert(it != streams.end());
709 void Server::process_queued_data()
711 MutexLock lock(&queued_data_mutex);
713 for (size_t i = 0; i < queued_add_clients.size(); ++i) {
714 add_client(queued_add_clients[i]);
716 queued_add_clients.clear();
718 for (map<string, string>::iterator queued_it = queued_data.begin();
719 queued_it != queued_data.end();
721 add_data(queued_it->first, queued_it->second.data(), queued_it->second.size());