]> git.sesse.net Git - cubemap/blobdiff - server.h
Support writing a stats file listing the number of clients currently connected.
[cubemap] / server.h
index ce9365d7a49cfb98d176d9a566358c2a5f98d937..300ec0d41115f53640068691947481cc24f44c45 100644 (file)
--- a/server.h
+++ b/server.h
@@ -4,6 +4,7 @@
 #include <stdint.h>
 #include <pthread.h>
 #include <sys/epoll.h>
+#include <time.h>
 #include <string>
 #include <map>
 #include <vector>
@@ -18,6 +19,14 @@ class CubemapStateProto;
 class Stream;
 class StreamProto;
 
+// Digested statistics for writing to logs etc.
+struct ClientStats {
+       std::string stream_id;
+       std::string remote_addr;
+       time_t connect_time;
+       size_t bytes_sent;
+};
+
 struct Client {
        Client() {}
        Client(int sock);
@@ -26,9 +35,15 @@ struct Client {
        Client(const ClientProto &serialized, Stream *stream);
        ClientProto serialize() const;
 
+       ClientStats get_stats() const;
+
        // The file descriptor associated with this socket.
        int sock;
 
+       // Some information only used for logging.
+       std::string remote_addr;
+       time_t connect_time;
+
        enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_ERROR };
        State state;
 
@@ -106,6 +121,10 @@ public:
        // Stop the thread.
        void stop();
        
+       // Get the list of all currently connected clients.     
+       std::vector<ClientStats> get_client_stats() const;
+
+       // Set header (both HTTP header and any stream headers) for the given stream.
        void set_header(const std::string &stream_id, const std::string &header);
 
        // These will be deferred until the next time an iteration in do_work() happens,
@@ -127,7 +146,7 @@ private:
 
        // 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.
-       pthread_mutex_t queued_data_mutex;
+       mutable pthread_mutex_t queued_data_mutex;
 
        // Deferred commands that should be run from the do_work() thread as soon as possible.
        // We defer these for two reasons:
@@ -143,7 +162,7 @@ private:
        std::map<std::string, std::string> queued_data;
 
        // All variables below this line are protected by the mutex.
-       pthread_mutex_t mutex;
+       mutable pthread_mutex_t mutex;
 
        // If the thread should stop or not.
        bool should_stop;