]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Support configurable BACKLOG_SIZE (per-stream). No support for changing across restar...
[cubemap] / server.cpp
index c9b942f2c686e19a5d06682eb79075f7e59defad..0c36e49363178f1ccd0a4955ef119a5ea6ebe9bd 100644 (file)
@@ -106,10 +106,11 @@ ClientStats Client::get_stats() const
        return stats;
 }
 
-Stream::Stream(const string &stream_id)
+Stream::Stream(const string &stream_id, size_t backlog_size)
        : stream_id(stream_id),
          data_fd(make_tempfile("")),
-         data_size(0),
+          backlog_size(backlog_size),
+         bytes_received(0),
          mark_pool(NULL)
 {
        if (data_fd == -1) {
@@ -134,7 +135,8 @@ Stream::Stream(const StreamProto &serialized)
        : stream_id(serialized.stream_id()),
          header(serialized.header()),
          data_fd(make_tempfile(serialized.data())),
-         data_size(serialized.data_size()),
+         backlog_size(serialized.backlog_size()),
+         bytes_received(serialized.bytes_received()),
          mark_pool(NULL)
 {
        if (data_fd == -1) {
@@ -149,7 +151,8 @@ StreamProto Stream::serialize()
        if (!read_tempfile(data_fd, serialized.mutable_data())) {  // Closes data_fd.
                exit(1);
        }
-       serialized.set_data_size(data_size);
+       serialized.set_backlog_size(backlog_size);
+       serialized.set_bytes_received(bytes_received);
        serialized.set_stream_id(stream_id);
        data_fd = -1;
        return serialized;
@@ -321,17 +324,17 @@ void Server::add_client_from_serialized(const ClientProto &client)
        }
 
        if (client_ptr->state == Client::SENDING_DATA && 
-           client_ptr->bytes_sent == client_ptr->stream->data_size) {
+           client_ptr->bytes_sent == client_ptr->stream->bytes_received) {
                client_ptr->stream->put_client_to_sleep(client_ptr);
        } else {
                process_client(client_ptr);
        }
 }
 
-void Server::add_stream(const string &stream_id)
+void Server::add_stream(const string &stream_id, size_t backlog_size)
 {
        MutexLock lock(&mutex);
-       streams.insert(make_pair(stream_id, new Stream(stream_id)));
+       streams.insert(make_pair(stream_id, new Stream(stream_id, backlog_size)));
 }
 
 void Server::add_stream_from_serialized(const StreamProto &stream)
@@ -374,11 +377,11 @@ void Server::add_data_deferred(const string &stream_id, const char *data, size_t
 void Server::add_data(const string &stream_id, const char *data, ssize_t bytes)
 {
        Stream *stream = find_stream(stream_id);
-       size_t pos = stream->data_size % BACKLOG_SIZE;
-       stream->data_size += bytes;
+       size_t pos = stream->bytes_received % stream->backlog_size;
+       stream->bytes_received += bytes;
 
-       if (pos + bytes > BACKLOG_SIZE) {
-               ssize_t to_copy = BACKLOG_SIZE - pos;
+       if (pos + bytes > stream->backlog_size) {
+               ssize_t to_copy = stream->backlog_size - pos;
                while (to_copy > 0) {
                        int ret = pwrite(stream->data_fd, data, to_copy, pos);
                        if (ret == -1 && errno == EINTR) {
@@ -523,7 +526,7 @@ sending_header_or_error_again:
                // but we'll start sending immediately as we get data.
                // This is postcondition #3.
                client->state = Client::SENDING_DATA;
-               client->bytes_sent = client->stream->data_size;
+               client->bytes_sent = client->stream->bytes_received;
                client->stream->put_client_to_sleep(client);
                return;
        }
@@ -532,28 +535,28 @@ sending_data_again:
                // See if there's some data we've lost. Ideally, we should drop to a block boundary,
                // but resync will be the mux's problem.
                Stream *stream = client->stream;
-               size_t bytes_to_send = stream->data_size - client->bytes_sent;
+               size_t bytes_to_send = stream->bytes_received - client->bytes_sent;
                if (bytes_to_send == 0) {
                        return;
                }
-               if (bytes_to_send > BACKLOG_SIZE) {
+               if (bytes_to_send > stream->backlog_size) {
                        fprintf(stderr, "WARNING: fd %d lost %lld bytes, maybe too slow connection\n",
                                client->sock,
-                               (long long int)(bytes_to_send - BACKLOG_SIZE));
-                       client->bytes_sent = stream->data_size - BACKLOG_SIZE;
-                       bytes_to_send = BACKLOG_SIZE;
+                               (long long int)(bytes_to_send - stream->backlog_size));
+                       client->bytes_sent = stream->bytes_received - stream->backlog_size;
+                       bytes_to_send = stream->backlog_size;
                }
 
                // See if we need to split across the circular buffer.
                bool more_data = false;
-               if ((client->bytes_sent % BACKLOG_SIZE) + bytes_to_send > BACKLOG_SIZE) {
-                       bytes_to_send = BACKLOG_SIZE - (client->bytes_sent % BACKLOG_SIZE);
+               if ((client->bytes_sent % stream->backlog_size) + bytes_to_send > stream->backlog_size) {
+                       bytes_to_send = stream->backlog_size - (client->bytes_sent % stream->backlog_size);
                        more_data = true;
                }
 
                ssize_t ret;
                do {
-                       loff_t offset = client->bytes_sent % BACKLOG_SIZE;
+                       loff_t offset = client->bytes_sent % stream->backlog_size;
                        ret = sendfile(client->sock, stream->data_fd, &offset, bytes_to_send);
                } while (ret == -1 && errno == EINTR);
 
@@ -571,7 +574,7 @@ sending_data_again:
                }
                client->bytes_sent += ret;
 
-               if (client->bytes_sent == stream->data_size) {
+               if (client->bytes_sent == stream->bytes_received) {
                        // We don't have any more data for this client, so put it to sleep.
                        // This is postcondition #3.
                        stream->put_client_to_sleep(client);