]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Create $(libdir) on make install.
[cubemap] / server.cpp
index 81109d658dbf196ba1c308bd2034f188d097b175..8025fd1b4cb257ee71aa6a280a98c1913e007e73 100644 (file)
@@ -60,7 +60,7 @@ inline bool is_earlier(timespec a, timespec b)
 
 Server::Server()
 {
-       epoll_fd = epoll_create(1024);  // Size argument is ignored.
+       epoll_fd = epoll_create1(EPOLL_CLOEXEC);
        if (epoll_fd == -1) {
                log_perror("epoll_fd");
                exit(1);
@@ -70,6 +70,11 @@ Server::Server()
 Server::~Server()
 {
        safe_close(epoll_fd);
+
+       // We're going to die soon anyway, but clean this up to keep leak checking happy.
+       for (const auto &acceptor_and_context : tls_server_contexts) {
+               tls_destroy_context(acceptor_and_context.second);
+       }
 }
 
 vector<ClientStats> Server::get_client_stats() const
@@ -458,26 +463,14 @@ void Server::set_header(int stream_index, const string &http_header, const strin
 {
        lock_guard<mutex> lock(mu);
        assert(stream_index >= 0 && stream_index < ssize_t(streams.size()));
-       Stream *stream = streams[stream_index].get();
-       stream->http_header = http_header;
-
-       if (stream_header != stream->stream_header) {
-               // We cannot start at any of the older starting points anymore,
-               // since they'd get the wrong header for the stream (not to mention
-               // that a changed header probably means the stream restarted,
-               // which means any client starting on the old one would probably
-               // stop playing properly at the change point). Next block
-               // should be a suitable starting point (if not, something is
-               // pretty strange), so it will fill up again soon enough.
-               stream->suitable_starting_points.clear();
-
-               if (!stream->fragments.empty()) {
-                       stream->fragments.clear();
-                       ++stream->discontinuity_counter;
-                       stream->clear_hls_playlist_cache();
-               }
-       }
-       stream->stream_header = stream_header;
+       streams[stream_index]->set_header(http_header, stream_header);
+}
+
+void Server::set_unavailable(int stream_index)
+{
+       lock_guard<mutex> lock(mu);
+       assert(stream_index >= 0 && stream_index < ssize_t(streams.size()));
+       streams[stream_index]->set_unavailable();
 }
        
 void Server::set_pacing_rate(int stream_index, uint32_t pacing_rate)
@@ -649,7 +642,7 @@ sending_header_or_short_response_again:
                } else if (client->stream_pos_end != Client::STREAM_POS_NO_END) {
                        // We're sending a fragment, and should have all of it,
                        // so start sending right away.
-                       assert(client->stream_pos >= 0);
+                       assert(ssize_t(client->stream_pos) >= 0);
                        client->state = Client::SENDING_DATA;
                        goto sending_data;
                } else if (stream->prebuffering_bytes == 0) {
@@ -1116,9 +1109,6 @@ int Server::parse_request(Client *client)
        }
 
        Stream *stream = client->stream;
-       if (stream->http_header.empty()) {
-               return 503;  // Service unavailable.
-       }
 
        if (client->serving_hls_playlist) {
                if (stream->encoding == Stream::STREAM_ENCODING_METACUBE) {
@@ -1130,6 +1120,10 @@ int Server::parse_request(Client *client)
        }
 
        if (client->stream_pos_end == Client::STREAM_POS_NO_END) {
+               if (stream->unavailable) {
+                       return 503;  // Service unavailable.
+               }
+
                // This stream won't end, so we don't have a content-length,
                // and can just as well tell the client it's Connection: close
                // (otherwise, we'd have to implement chunking TE for no good reason).