]> git.sesse.net Git - cubemap/blobdiff - server.h
Support configurable BACKLOG_SIZE (per-stream). No support for changing across restar...
[cubemap] / server.h
index 79794939280cf315359f602344d3681d287054e0..7910f4c9c37ffdf190245f60edf5fbce1512dc29 100644 (file)
--- a/server.h
+++ b/server.h
@@ -9,7 +9,8 @@
 #include <map>
 #include <vector>
 
-#define BACKLOG_SIZE 1048576
+#include "thread.h"
+
 #define EPOLL_MAX_EVENTS 8192
 #define EPOLL_TIMEOUT_MS 20
 #define MAX_CLIENT_REQUEST 16384
@@ -74,12 +75,12 @@ struct Client {
 };
 
 struct Stream {
-       Stream(const std::string &stream_id);
+       Stream(const std::string &stream_id, size_t backlog_size);
        ~Stream();
 
        // Serialization/deserialization.
        Stream(const StreamProto &serialized);
-       StreamProto serialize() const;
+       StreamProto serialize();
 
        std::string stream_id;
 
@@ -87,11 +88,21 @@ struct Stream {
        std::string header;
 
        // The stream data itself, stored in a circular buffer.
-       char *data;
-
-       // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
-       // since the buffer wraps.
-       size_t data_size;
+       //
+       // We store our data in a file, so that we can send the data to the
+       // kernel only once (with write()). We then use sendfile() for each
+       // client, which effectively zero-copies it out of the kernel's buffer
+       // cache. This is significantly more efficient than doing write() from
+       // a userspace memory buffer, since the latter makes the kernel copy
+       // the same data from userspace many times.
+       int data_fd;
+
+       // How many bytes <data_fd> can hold (the buffer size).
+       size_t backlog_size;
+
+       // How many bytes this stream have received. Can very well be larger
+       // than <backlog_size>, since the buffer wraps.
+       size_t bytes_received;
        
        // Clients that are in SENDING_DATA, but that we don't listen on,
        // because we currently don't have any data for them.
@@ -117,17 +128,11 @@ private:
        Stream(const Stream& other);
 };
 
-class Server {
+class Server : public Thread {
 public:
        Server();
        ~Server();
 
-       // Start a new thread that handles clients.
-       void run();
-
-       // Stop the thread.
-       void stop();
-       
        // Get the list of all currently connected clients.     
        std::vector<ClientStats> get_client_stats() const;
 
@@ -149,12 +154,10 @@ public:
        // at the same time).
        CubemapStateProto serialize();
        void add_client_from_serialized(const ClientProto &client);
-       void add_stream(const std::string &stream_id);
+       void add_stream(const std::string &stream_id, size_t bytes_received);
        void add_stream_from_serialized(const StreamProto &stream);
 
 private:
-       pthread_t worker_thread;
-
        // Mutex protecting queued_data only. Note that if you want to hold both this
        // and <mutex> below, you will need to take <mutex> before this one.
        mutable pthread_mutex_t queued_data_mutex;
@@ -175,9 +178,6 @@ private:
        // All variables below this line are protected by the mutex.
        mutable pthread_mutex_t mutex;
 
-       // If the thread should stop or not.
-       bool should_stop;       
-
        // Map from stream ID to stream.
        std::map<std::string, Stream *> streams;
 
@@ -188,11 +188,8 @@ private:
        int epoll_fd;
        epoll_event events[EPOLL_MAX_EVENTS];
 
-       // Recover the this pointer, and call do_work().
-       static void *do_work_thunk(void *arg);
-
        // The actual worker thread.
-       void do_work();
+       virtual void do_work();
 
        // Process a client; read and write data as far as we can.
        // After this call, one of these four is true:
@@ -229,7 +226,7 @@ private:
        void process_queued_data();
 
        void add_client(int sock);
-       void add_data(const std::string &stream_id, const char *data, size_t bytes);
+       void add_data(const std::string &stream_id, const char *data, ssize_t bytes);
 };
 
 #endif  // !defined(_SERVER_H)