]> git.sesse.net Git - cubemap/blobdiff - stream.cpp
Move Server:add_data() into Stream, where it more logically belongs.
[cubemap] / stream.cpp
index 322391ef5c2158dfc04c440301a11b9cc4e792fb..d57157822aa93939c9250940bec1cf4132f212cb 100644 (file)
@@ -1,13 +1,13 @@
+#include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
-#include <errno.h>
-#include <algorithm>
 #include <string>
 #include <vector>
 
+#include "state.pb.h"
 #include "stream.h"
 #include "util.h"
-#include "state.pb.h"
 
 using namespace std;
 
@@ -68,6 +68,49 @@ void Stream::put_client_to_sleep(Client *client)
        sleeping_clients.push_back(client);
 }
 
+void Stream::add_data(const char *data, ssize_t bytes)
+{
+       size_t pos = bytes_received % backlog_size;
+       bytes_received += bytes;
+
+       if (pos + bytes > backlog_size) {
+               ssize_t to_copy = backlog_size - pos;
+               while (to_copy > 0) {
+                       int ret = pwrite(data_fd, data, to_copy, pos);
+                       if (ret == -1 && errno == EINTR) {
+                               continue;
+                       }
+                       if (ret == -1) {
+                               perror("pwrite");
+                               // Dazed and confused, but trying to continue...
+                               break;
+                       }
+                       pos += ret;
+                       data += ret;
+                       to_copy -= ret;
+                       bytes -= ret;
+               }
+               pos = 0;
+       }
+
+       while (bytes > 0) {
+               int ret = pwrite(data_fd, data, bytes, pos);
+               if (ret == -1 && errno == EINTR) {
+                       continue;
+               }
+               if (ret == -1) {
+                       perror("pwrite");
+                       // Dazed and confused, but trying to continue...
+                       break;
+               }
+               pos += ret;
+               data += ret;
+               bytes -= ret;
+       }
+
+       wake_up_all_clients();
+}
+
 void Stream::wake_up_all_clients()
 {
        if (to_process.empty()) {