]> git.sesse.net Git - cubemap/blobdiff - stream.cpp
Add Metacube headers in add_data_deferred(), not add_data().
[cubemap] / stream.cpp
index 109d2fd4a90116398e8b638816872cabf06b22ce..4be673c864410e9ddfda17675e9d9137b672da71 100644 (file)
@@ -1,14 +1,15 @@
-#include <arpa/inet.h>
+#include <assert.h>
 #include <errno.h>
-#include <stdio.h>
+#include <netinet/in.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <string>
 #include <vector>
 
-#include "state.pb.h"
 #include "log.h"
 #include "metacube.h"
+#include "state.pb.h"
 #include "stream.h"
 #include "util.h"
 
@@ -30,13 +31,7 @@ Stream::Stream(const string &stream_id, size_t backlog_size, Encoding encoding)
 Stream::~Stream()
 {
        if (data_fd != -1) {
-               int ret;
-               do {
-                       ret = close(data_fd);
-               } while (ret == -1 && errno == EINTR);
-               if (ret == -1) {
-                       log_perror("close");
-               }
+               safe_close(data_fd);
        }
 }
 
@@ -109,11 +104,14 @@ void Stream::set_backlog_size(size_t new_size)
 
        // Create a new, empty data file.
        data_fd = make_tempfile("");
+       if (data_fd == -1) {
+               exit(1);
+       }
        backlog_size = new_size;
 
        // Now cheat a bit by rewinding, and adding all the old data back.
        bytes_received -= existing_data.size();
-       add_data(existing_data.data(), existing_data.size());
+       add_data_raw(existing_data.data(), existing_data.size());
 }
 
 void Stream::put_client_to_sleep(Client *client)
@@ -121,26 +119,6 @@ void Stream::put_client_to_sleep(Client *client)
        sleeping_clients.push_back(client);
 }
 
-void Stream::add_data(const char *data, ssize_t bytes)
-{
-       if (encoding == Stream::STREAM_ENCODING_RAW) {
-               add_data_raw(data, bytes);
-       } else if (encoding == STREAM_ENCODING_METACUBE) {
-               metacube_block_header hdr;
-               memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
-               hdr.size = htonl(bytes);
-               hdr.flags = htonl(0);
-
-               char *block = new char[bytes + sizeof(hdr)];
-               memcpy(block, &hdr, sizeof(hdr));
-               memcpy(block + sizeof(hdr), data, bytes);
-               add_data_raw(block, bytes + sizeof(hdr));
-               delete[] block;
-       } else {
-               assert(false);
-       }
-}
-
 void Stream::add_data_raw(const char *data, ssize_t bytes)
 {
        size_t pos = bytes_received % backlog_size;
@@ -182,8 +160,36 @@ void Stream::add_data_raw(const char *data, ssize_t bytes)
        }
 }
 
-void Stream::wake_up_all_clients()
+void Stream::add_data_deferred(const char *data, size_t bytes)
+{
+       if (encoding == Stream::STREAM_ENCODING_RAW) {
+               queued_data.append(string(data, data + bytes));
+       } else if (encoding == STREAM_ENCODING_METACUBE) {
+               metacube_block_header hdr;
+               memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
+               hdr.size = htonl(bytes);
+               hdr.flags = htonl(0);
+
+               char *block = new char[bytes + sizeof(hdr)];
+               memcpy(block, &hdr, sizeof(hdr));
+               memcpy(block + sizeof(hdr), data, bytes);
+               queued_data.append(string(block, block + bytes + sizeof(hdr)));
+               delete[] block;
+       } else {
+               assert(false);
+       }
+}
+
+void Stream::process_queued_data()
 {
+       if (queued_data.empty()) {
+               return;
+       }
+
+       add_data_raw(queued_data.data(), queued_data.size());
+       queued_data.clear();
+
+       // We have more data, so wake up all clients.
        if (to_process.empty()) {
                swap(sleeping_clients, to_process);
        } else {