]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Tweak the MutexLock implementation slightly, so as to confuse Coverity less.
[cubemap] / server.cpp
index 1f5477e9baa92066bbf2d8736e27901d32d2c9eb..4442b34cee7899661bae68e15e1ed223018abdc2 100644 (file)
@@ -20,7 +20,7 @@
 #include "accesslog.h"
 #include "log.h"
 #include "markpool.h"
-#include "metacube.h"
+#include "metacube2.h"
 #include "mutexlock.h"
 #include "parse.h"
 #include "server.h"
@@ -194,8 +194,9 @@ void Server::add_client_from_serialized(const ClientProto &client)
                exit(1);
        }
 
-       if (client_ptr->state == Client::SENDING_DATA && 
-           client_ptr->stream_pos == client_ptr->stream->bytes_received) {
+       if (client_ptr->state == Client::WAITING_FOR_KEYFRAME ||
+           (client_ptr->state == Client::SENDING_DATA &&
+            client_ptr->stream_pos == client_ptr->stream->bytes_received)) {
                client_ptr->stream->put_client_to_sleep(client_ptr);
        } else {
                process_client(client_ptr);
@@ -269,11 +270,11 @@ void Server::set_mark_pool(int stream_index, MarkPool *mark_pool)
        streams[stream_index]->mark_pool = mark_pool;
 }
 
-void Server::add_data_deferred(int stream_index, const char *data, size_t bytes)
+void Server::add_data_deferred(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start)
 {
        MutexLock lock(&queued_data_mutex);
        assert(stream_index >= 0 && stream_index < ssize_t(streams.size()));
-       streams[stream_index]->add_data_deferred(data, bytes);
+       streams[stream_index]->add_data_deferred(data, bytes, suitable_for_stream_start);
 }
 
 // See the .h file for postconditions after this function.     
@@ -380,14 +381,37 @@ sending_header_or_error_again:
                        return;
                }
 
-               // Start sending from the end. In other words, we won't send any of the backlog,
-               // but we'll start sending immediately as we get data.
+               // Start sending from the first keyframe we get. In other
+               // words, we won't send any of the backlog, but we'll start
+               // sending immediately as we get the next keyframe block.
                // This is postcondition #3.
-               client->state = Client::SENDING_DATA;
-               client->stream_pos = client->stream->bytes_received;
+               if (client->stream_pos == size_t(-2)) {
+                       client->stream_pos = std::min<size_t>(
+                           client->stream->bytes_received - client->stream->backlog_size,
+                           0);
+                       client->state = Client::SENDING_DATA;
+               } else {
+                       // client->stream_pos should be -1, but it might not be,
+                       // if we have clients from an older version.
+                       client->stream_pos = client->stream->bytes_received;
+                       client->state = Client::WAITING_FOR_KEYFRAME;
+               }
                client->stream->put_client_to_sleep(client);
                return;
        }
+       case Client::WAITING_FOR_KEYFRAME: {
+               Stream *stream = client->stream;
+               if (ssize_t(client->stream_pos) > stream->last_suitable_starting_point) {
+                       // We haven't received a keyframe since this stream started waiting,
+                       // so keep on waiting for one.
+                       // This is postcondition #3.
+                       stream->put_client_to_sleep(client);
+                       return;
+               }
+               client->stream_pos = stream->last_suitable_starting_point;
+               client->state = Client::SENDING_DATA;
+               // Fall through.
+       }
        case Client::SENDING_DATA: {
                skip_lost_data(client);
                Stream *stream = client->stream;
@@ -476,7 +500,15 @@ int Server::parse_request(Client *client)
                return 400;  // Should maybe be 405 instead?
        }
 
-       map<string, int>::const_iterator url_map_it = url_map.find(request_tokens[1]);
+       string url = request_tokens[1];
+       if (url.find("?backlog") == url.size() - 8) {
+               client->stream_pos = -2;
+               url = url.substr(0, url.size() - 8);
+       } else {
+               client->stream_pos = -1;
+       }
+
+       map<string, int>::const_iterator url_map_it = url_map.find(url);
        if (url_map_it == url_map.end()) {
                return 404;  // Not found.
        }
@@ -510,10 +542,11 @@ void Server::construct_header(Client *client)
                        "Content-encoding: metacube\r\n" +
                        "\r\n";
                if (!stream->stream_header.empty()) {
-                       metacube_block_header hdr;
-                       memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
+                       metacube2_block_header hdr;
+                       memcpy(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync));
                        hdr.size = htonl(stream->stream_header.size());
-                       hdr.flags = htonl(METACUBE_FLAGS_HEADER);
+                       hdr.flags = htons(METACUBE_FLAGS_HEADER);
+                       hdr.csum = htons(metacube2_compute_crc(&hdr));
                        client->header_or_error.append(
                                string(reinterpret_cast<char *>(&hdr), sizeof(hdr)));
                }