]> git.sesse.net Git - cubemap/blob - server.h
Add a configure script, for easier override of CXXFLAGS etc.
[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 <queue>
12 #include <string>
13 #include <vector>
14
15 #include "client.h"
16 #include "stream.h"
17 #include "thread.h"
18
19 class ClientProto;
20 struct Stream;
21
22 #define EPOLL_MAX_EVENTS 8192
23 #define EPOLL_TIMEOUT_MS 20
24 #define MAX_CLIENT_REQUEST 16384
25 #define REQUEST_READ_TIMEOUT_SEC 60
26
27 class CubemapStateProto;
28 class StreamProto;
29
30 class Server : public Thread {
31 public:
32         Server();
33         ~Server();
34
35         // Get the list of all currently connected clients.     
36         std::vector<ClientStats> get_client_stats() const;
37
38         // Set header (both HTTP header and any stream headers) for the given stream.
39         void set_header(int stream_index,
40                         const std::string &http_header,
41                         const std::string &stream_header);
42
43         // Set that the given stream should use the given max pacing rate from now on.
44         // NOTE: This should be set before any clients are connected!
45         void set_pacing_rate(int stream_index, uint32_t pacing_rate);
46
47         // These will be deferred until the next time an iteration in do_work() happens,
48         // and the order between them are undefined.
49         // XXX: header should ideally be ordered with respect to data.
50         void add_client_deferred(int sock);
51         void add_data_deferred(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
52
53         // These should not be called while running, since that would violate
54         // threading assumptions (ie., that epoll is only called from one thread
55         // at the same time).
56         CubemapStateProto serialize();
57         void add_client_from_serialized(const ClientProto &client);
58         int add_stream(const std::string &url, size_t bytes_received, size_t prebuffering_bytes, Stream::Encoding encoding);
59         int add_stream_from_serialized(const StreamProto &stream, int data_fd);
60         int lookup_stream_by_url(const std::string &url) const;
61         void set_backlog_size(int stream_index, size_t new_size);
62         void set_prebuffering_bytes(int stream_index, size_t new_amount);
63         void set_encoding(int stream_index, Stream::Encoding encoding);
64         void add_gen204(const std::string &url, const std::string &allow_origin);
65
66 private:
67         // Mutex protecting queued_add_clients.
68         // Note that if you want to hold both this and <mutex> below,
69         // you will need to take <mutex> before this one.
70         mutable pthread_mutex_t queued_clients_mutex;
71
72         // Deferred commands that should be run from the do_work() thread as soon as possible.
73         // We defer these for two reasons:
74         //
75         //  - We only want to fiddle with epoll from one thread at any given time,
76         //    and doing add_client() from the acceptor thread would violate that.
77         //  - We don't want the input thread(s) hanging on <mutex> when doing
78         //    add_data(), since they want to do add_data() rather often, and <mutex>
79         //    can be taken a lot of the time.
80         //      
81         // Protected by <queued_clients_mutex>.
82         std::vector<int> queued_add_clients;
83
84         // All variables below this line are protected by the mutex.
85         mutable pthread_mutex_t mutex;
86
87         // All streams.
88         std::vector<Stream *> streams;
89
90         // Map from URL to index into <streams>.
91         std::map<std::string, int> stream_url_map;
92
93         // Map from URL to CORS Allow-Origin header (or empty string).
94         std::map<std::string, std::string> ping_url_map;
95
96         // Map from file descriptor to client.
97         std::map<int, Client> clients;
98
99         // A list of all clients, ordered by the time they connected (first element),
100         // and their file descriptor (second element). It is ordered by connection time
101         // (and thus also by read timeout time) so that we can read clients from the
102         // start and stop processing once we get to one that isn't ready to be
103         // timed out yet (which means we only have to look at each client exactly once,
104         // save for the first element of the queue, which is always checked).
105         //
106         // Note that when we delete a client, we don't update this queue.
107         // This means that when reading it, we need to check if the client it
108         // describes is still exists (ie., that the fd still exists, and that
109         // the timespec matches).
110         std::queue<std::pair<timespec, int> > clients_ordered_by_connect_time;
111
112         // Used for epoll implementation (obviously).
113         int epoll_fd;
114         epoll_event events[EPOLL_MAX_EVENTS];
115
116         // The actual worker thread.
117         virtual void do_work();
118
119         // Process a client; read and write data as far as we can.
120         // After this call, one of these four is true:
121         //
122         //  1. The socket is closed, and the client deleted.
123         //  2. We are still waiting for more data from the client.
124         //  3. We've sent all the data we have to the client,
125         //     and put it in <sleeping_clients>.
126         //  4. The socket buffer is full (which means we still have
127         //     data outstanding).
128         //
129         // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
130         // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
131         // but it's cheaper than taking it in and out all the time.
132         void process_client(Client *client);
133
134         // Close a given client socket, and clean up after it.
135         void close_client(Client *client);
136
137         // Parse the HTTP request. Returns a HTTP status code (200/204/400/404).
138         int parse_request(Client *client);
139
140         // Construct the HTTP header, and set the client into
141         // the SENDING_HEADER state.
142         void construct_header(Client *client);
143
144         // Construct a generic error with the given line, and set the client into
145         // the SENDING_SHORT_RESPONSE state.
146         void construct_error(Client *client, int error_code);
147
148         // Construct a 204, and set the client into the SENDING_SHORT_RESPONSE state.
149         void construct_204(Client *client);
150
151         void process_queued_data();
152         void skip_lost_data(Client *client);
153
154         void add_client(int sock);
155 };
156
157 #endif  // !defined(_SERVER_H)