]> git.sesse.net Git - cubemap/blob - server.h
Run include-what-you-use.
[cubemap] / server.h
1 #ifndef _SERVER_H
2 #define _SERVER_H 1
3
4 #include <pthread.h>
5 #include <stddef.h>
6 #include <stdint.h>
7 #include <sys/epoll.h>
8 #include <sys/types.h>
9 #include <time.h>
10 #include <map>
11 #include <string>
12 #include <vector>
13
14 #include "client.h"
15 #include "thread.h"
16
17 class ClientProto;
18 struct Stream;
19
20 #define EPOLL_MAX_EVENTS 8192
21 #define EPOLL_TIMEOUT_MS 20
22 #define MAX_CLIENT_REQUEST 16384
23
24 class CubemapStateProto;
25 class MarkPool;
26 class StreamProto;
27
28 class Server : public Thread {
29 public:
30         Server();
31         ~Server();
32
33         // Get the list of all currently connected clients.     
34         std::vector<ClientStats> get_client_stats() const;
35
36         // Set header (both HTTP header and any stream headers) for the given stream.
37         void set_header(const std::string &stream_id, const std::string &header);
38
39         // Set that the given stream should use the given mark pool from now on.
40         // NOTE: This should be set before any clients are connected!
41         void set_mark_pool(const std::string &stream_id, MarkPool *mark_pool);
42
43         // These will be deferred until the next time an iteration in do_work() happens,
44         // and the order between them are undefined.
45         // XXX: header should ideally be ordered with respect to data.
46         void add_client_deferred(int sock);
47         void add_data_deferred(const std::string &stream_id, const char *data, size_t bytes);
48
49         // These should not be called while running, since that would violate
50         // threading assumptions (ie., that epoll is only called from one thread
51         // at the same time).
52         CubemapStateProto serialize();
53         void add_client_from_serialized(const ClientProto &client);
54         void add_stream(const std::string &stream_id, size_t bytes_received);
55         void add_stream_from_serialized(const StreamProto &stream);
56
57 private:
58         // Mutex protecting queued_data only. Note that if you want to hold both this
59         // and <mutex> below, you will need to take <mutex> before this one.
60         mutable pthread_mutex_t queued_data_mutex;
61
62         // Deferred commands that should be run from the do_work() thread as soon as possible.
63         // We defer these for two reasons:
64         //
65         //  - We only want to fiddle with epoll from one thread at any given time,
66         //    and doing add_client() from the acceptor thread would violate that.
67         //  - We don't want the input thread(s) hanging on <mutex> when doing
68         //    add_data(), since they want to do add_data() rather often, and <mutex>
69         //    can be taken a lot of the time.
70         //      
71         // Protected by <queued_data_mutex>.
72         std::vector<int> queued_add_clients;
73         std::map<std::string, std::string> queued_data;
74
75         // All variables below this line are protected by the mutex.
76         mutable pthread_mutex_t mutex;
77
78         // Map from stream ID to stream.
79         std::map<std::string, Stream *> streams;
80
81         // Map from file descriptor to client.
82         std::map<int, Client> clients;
83
84         // Used for epoll implementation (obviously).
85         int epoll_fd;
86         epoll_event events[EPOLL_MAX_EVENTS];
87
88         // The actual worker thread.
89         virtual void do_work();
90
91         // Process a client; read and write data as far as we can.
92         // After this call, one of these four is true:
93         //
94         //  1. The socket is closed, and the client deleted.
95         //  2. We are still waiting for more data from the client.
96         //  3. We've sent all the data we have to the client,
97         //     and put it in <sleeping_clients>.
98         //  4. The socket buffer is full (which means we still have
99         //     data outstanding).
100         //
101         // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
102         // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
103         // but it's cheaper than taking it in and out all the time.
104         void process_client(Client *client);
105
106         // Close a given client socket, and clean up after it.
107         void close_client(Client *client);
108
109         // Parse the HTTP request. Returns a HTTP status code (200/400/404).
110         int parse_request(Client *client);
111
112         // Construct the HTTP header, and set the client into
113         // the SENDING_HEADER state.
114         void construct_header(Client *client);
115
116         // Construct a generic error with the given line, and set the client into
117         // the SENDING_ERROR state.
118         void construct_error(Client *client, int error_code);
119
120         // TODO: This function should probably die.
121         Stream *find_stream(const std::string &stream_id);
122
123         void process_queued_data();
124
125         void add_client(int sock);
126         void add_data(const std::string &stream_id, const char *data, ssize_t bytes);
127 };
128
129 #endif  // !defined(_SERVER_H)