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