]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Move Server:add_data() into Stream, where it more logically belongs.
[cubemap] / server.cpp
index 64819f447ffeec0fbf6de8187e2de1d7705c240c..266d41809765075caaa750be0fd06809d707481d 100644 (file)
@@ -1,29 +1,26 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-#include <unistd.h>
 #include <assert.h>
-#include <arpa/inet.h>
-#include <sys/socket.h>
+#include <errno.h>
 #include <pthread.h>
-#include <sys/types.h>
-#include <sys/ioctl.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <sys/epoll.h>
 #include <sys/sendfile.h>
+#include <sys/socket.h>
+#include <sys/types.h>
 #include <time.h>
-#include <signal.h>
-#include <errno.h>
-#include <vector>
-#include <string>
-#include <map>
+#include <unistd.h>
 #include <algorithm>
+#include <map>
+#include <string>
+#include <utility>
+#include <vector>
 
 #include "markpool.h"
+#include "mutexlock.h"
 #include "parse.h"
 #include "server.h"
-#include "stream.h"
-#include "mutexlock.h"
 #include "state.pb.h"
+#include "stream.h"
 
 using namespace std;
 
@@ -228,50 +225,6 @@ void Server::add_data_deferred(const string &stream_id, const char *data, size_t
        queued_data[stream_id].append(string(data, data + bytes));
 }
 
-void Server::add_data(const string &stream_id, const char *data, ssize_t bytes)
-{
-       Stream *stream = find_stream(stream_id);
-       size_t pos = stream->bytes_received % stream->backlog_size;
-       stream->bytes_received += bytes;
-
-       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) {
-                               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(stream->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;
-       }
-
-       stream->wake_up_all_clients();
-}
-
 // See the .h file for postconditions after this function.     
 void Server::process_client(Client *client)
 {
@@ -572,7 +525,8 @@ void Server::process_queued_data()
        for (map<string, string>::iterator queued_it = queued_data.begin();
             queued_it != queued_data.end();
             ++queued_it) {
-               add_data(queued_it->first, queued_it->second.data(), queued_it->second.size());
+               Stream *stream = find_stream(queued_it->first);
+               stream->add_data(queued_it->second.data(), queued_it->second.size());
        }
        queued_data.clear();
 }