]> git.sesse.net Git - cubemap/blob - server.h
Capitalize HTTP header names after dashes.
[cubemap] / server.h
1 #ifndef _SERVER_H
2 #define _SERVER_H 1
3
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <sys/epoll.h>
7 #include <sys/types.h>
8 #include <time.h>
9 #include <memory>
10 #include <mutex>
11 #include <queue>
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15
16 #include "tlse.h"
17
18 #include "client.h"
19 #include "stream.h"
20 #include "thread.h"
21
22 class Acceptor;
23 class ClientProto;
24 struct Stream;
25
26 #define EPOLL_MAX_EVENTS 8192
27 #define EPOLL_TIMEOUT_MS 20
28 #define MAX_CLIENT_REQUEST 16384
29 #define REQUEST_READ_TIMEOUT_SEC 60
30
31 class CubemapStateProto;
32 class StreamProto;
33
34 class Server : public Thread {
35 public:
36         Server();
37         ~Server();
38
39         // Get the list of all currently connected clients.     
40         std::vector<ClientStats> get_client_stats() const;
41
42         // Set header (both HTTP header and any stream headers) for the given stream.
43         void set_header(int stream_index,
44                         const std::string &http_header,
45                         const std::string &stream_header);
46
47         // Set that the given stream should use the given max pacing rate from now on.
48         // NOTE: This should be set before any clients are connected!
49         void set_pacing_rate(int stream_index, uint32_t pacing_rate);
50
51         // These will be deferred until the next time an iteration in do_work() happens,
52         // and the order between them are undefined.
53         // XXX: header should ideally be ordered with respect to data.
54         void add_client_deferred(int sock, Acceptor *acceptor);
55         void add_data_deferred(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts);
56
57         // These should not be called while running, since that would violate
58         // threading assumptions (ie., that epoll is only called from one thread
59         // at the same time).
60         CubemapStateProto serialize(std::unordered_map<const std::string *, size_t> *short_response_pool);
61         void add_client_from_serialized(const ClientProto &client, const std::vector<std::shared_ptr<const std::string>> &short_responses);
62         int add_stream(const std::string &url,
63                        const std::string &hls_url,
64                        size_t bytes_received,
65                        size_t prebuffering_bytes,
66                        Stream::Encoding encoding,
67                        Stream::Encoding src_encoding,
68                        unsigned hls_frag_duration,
69                        size_t hls_backlog_margin,
70                        const std::string &allow_origin);
71         int add_stream_from_serialized(const StreamProto &stream, int data_fd);
72         int lookup_stream_by_url(const std::string &url) const;
73         void set_backlog_size(int stream_index, size_t new_size);
74         void set_prebuffering_bytes(int stream_index, size_t new_amount);
75         void set_encoding(int stream_index, Stream::Encoding encoding);
76         void set_src_encoding(int stream_index, Stream::Encoding encoding);
77         void set_hls_frag_duration(int stream_index, unsigned hls_frag_duration);
78         void set_hls_backlog_margin(int stream_index, size_t hls_backlog_margin);
79         void set_allow_origin(int stream_index, const std::string &allow_origin);
80         void register_hls_url(int stream_index, const std::string &hls_url);
81         void add_gen204(const std::string &url, const std::string &allow_origin);
82         void create_tls_context_for_acceptor(const Acceptor *acceptor);
83
84 private:
85         // Mutex protecting queued_add_clients.
86         // Note that if you want to hold both this and <mu> below,
87         // you will need to take <mu> before this one.
88         mutable std::mutex queued_clients_mutex;
89
90         // Deferred commands that should be run from the do_work() thread as soon as possible.
91         // We defer these for two reasons:
92         //
93         //  - We only want to fiddle with epoll from one thread at any given time,
94         //    and doing add_client() from the acceptor thread would violate that.
95         //  - We don't want the input thread(s) hanging on <mu> when doing
96         //    add_data(), since they want to do add_data() rather often, and <mu>
97         //    can be taken a lot of the time.
98         //      
99         // Protected by <queued_clients_mutex>.
100         std::vector<std::pair<int, Acceptor *>> queued_add_clients;
101
102         // All variables below this line are protected by the mutex.
103         mutable std::mutex mu;
104
105         // All streams.
106         std::vector<std::unique_ptr<Stream>> streams;
107
108         // Map from URL to index into <streams>.
109         std::unordered_map<std::string, int> stream_url_map, stream_hls_url_map;
110
111         // Map from URL to CORS Allow-Origin header (or empty string).
112         std::unordered_map<std::string, std::string> ping_url_map;
113
114         // Map from file descriptor to client.
115         std::unordered_map<int, Client> clients;
116
117         // A list of all clients, ordered by the time they connected (first element),
118         // and their file descriptor (second element). It is ordered by connection time
119         // (and thus also by read timeout time) so that we can read clients from the
120         // start and stop processing once we get to one that isn't ready to be
121         // timed out yet (which means we only have to look at each client exactly once,
122         // save for the first element of the queue, which is always checked).
123         //
124         // Note that when we delete a client, we don't update this queue.
125         // This means that when reading it, we need to check if the client it
126         // describes is still exists (ie., that the fd still exists, and that
127         // the timespec matches).
128         std::queue<std::pair<timespec, int>> clients_ordered_by_connect_time;
129
130         // Used for epoll implementation (obviously).
131         int epoll_fd;
132         epoll_event events[EPOLL_MAX_EVENTS];
133
134         // For each TLS-enabled acceptor, our private server context for its key pair.
135         std::unordered_map<const Acceptor *, TLSContext *> tls_server_contexts;
136
137         // The actual worker thread.
138         virtual void do_work();
139
140         // Process a client; read and write data as far as we can.
141         // After this call, one of these four is true:
142         //
143         //  1. The socket is closed, and the client deleted.
144         //  2. We are still waiting for more data from the client.
145         //  3. We've sent all the data we have to the client,
146         //     and put it in <sleeping_clients>.
147         //  4. The socket buffer is full (which means we still have
148         //     data outstanding).
149         //
150         // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
151         // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
152         // but it's cheaper than taking it in and out all the time.
153         void process_client(Client *client);
154
155         // If the TLS library wants to write anything to this client,
156         // output it. Returns true if the processing should go to sleep
157         // (an error, or lack of outgoing buffer space).
158         bool send_pending_tls_data(Client *client);
159
160         // Reads regular data fro ma socket. Returns -1 if the processing
161         // should go to sleep (an error, or no data available yet), otherwise
162         // the number of bytes read.
163         int read_nontls_data(Client *client, char *buf, size_t max_size);
164
165         // Reads (decrypted) data from a TLS socket. Returns -1 if the processing
166         // should go to sleep (an error, or no data available yet), otherwise
167         // the number of bytes read. The buffer will be used as scratch space
168         // for TLS data, so it can be overwritten by more bytes than what is returned.
169         int read_tls_data(Client *client, char *buf, size_t max_size);
170
171         // Close a given client socket, and clean up after it.
172         void close_client(Client *client);
173
174         // Listen for a different set of epoll events.
175         void change_epoll_events(Client *client, uint32_t events);
176
177         // If we're supposed to listen for more requests (persistent HTTP connections),
178         // puts the client back into READING_REQUEST, changes its epoll flags and returns
179         // true.
180         bool more_requests(Client *client);
181
182         // Parse the HTTP request. Returns a HTTP status code (200/204/400/404).
183         int parse_request(Client *client);
184
185         // Construct the HTTP header for a regular stream, and set the client into
186         // the SENDING_HEADER state.
187         void construct_stream_header(Client *client);
188
189         // Construct a HLS playlist (or get it from the cache), and set the client into
190         // the SENDING_HEADER state.
191         void construct_hls_playlist(Client *client);
192
193         // Construct a generic error with the given line, and set the client into
194         // the SENDING_SHORT_RESPONSE state.
195         void construct_error(Client *client, int error_code);
196
197         // Construct a 204, and set the client into the SENDING_SHORT_RESPONSE state.
198         void construct_204(Client *client);
199
200         void process_queued_data();
201         void skip_lost_data(Client *client);
202
203         void add_client(int sock, Acceptor *acceptor);
204 };
205
206 #endif  // !defined(_SERVER_H)