]> git.sesse.net Git - cubemap/blob - server.h
Set the default number of files to 16384; the default is seemingly 1024, which can...
[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 "stream.h"
16 #include "thread.h"
17
18 class ClientProto;
19 struct Stream;
20
21 #define EPOLL_MAX_EVENTS 8192
22 #define EPOLL_TIMEOUT_MS 20
23 #define MAX_CLIENT_REQUEST 16384
24
25 class CubemapStateProto;
26 class MarkPool;
27 class StreamProto;
28
29 class Server : public Thread {
30 public:
31         Server();
32         ~Server();
33
34         // Get the list of all currently connected clients.     
35         std::vector<ClientStats> get_client_stats() const;
36
37         // Set header (both HTTP header and any stream headers) for the given stream.
38         void set_header(int stream_index,
39                         const std::string &http_header,
40                         const std::string &stream_header);
41
42         // Set that the given stream should use the given mark pool from now on.
43         // NOTE: This should be set before any clients are connected!
44         void set_mark_pool(int stream_index, MarkPool *mark_pool);
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);
54         void add_data_deferred(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
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, Stream::Encoding 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_encoding(int stream_index, Stream::Encoding encoding);
66
67 private:
68         // Mutex protecting queued_add_clients.
69         // Note that if you want to hold both this and <mutex> below,
70         // you will need to take <mutex> before this one.
71         mutable pthread_mutex_t queued_clients_mutex;
72
73         // Deferred commands that should be run from the do_work() thread as soon as possible.
74         // We defer these for two reasons:
75         //
76         //  - We only want to fiddle with epoll from one thread at any given time,
77         //    and doing add_client() from the acceptor thread would violate that.
78         //  - We don't want the input thread(s) hanging on <mutex> when doing
79         //    add_data(), since they want to do add_data() rather often, and <mutex>
80         //    can be taken a lot of the time.
81         //      
82         // Protected by <queued_clients_mutex>.
83         std::vector<int> queued_add_clients;
84
85         // All variables below this line are protected by the mutex.
86         mutable pthread_mutex_t mutex;
87
88         // All streams.
89         std::vector<Stream *> streams;
90
91         // Map from URL to index into <streams>.
92         std::map<std::string, int> url_map;
93
94         // Map from file descriptor to client.
95         std::map<int, Client> clients;
96
97         // Used for epoll implementation (obviously).
98         int epoll_fd;
99         epoll_event events[EPOLL_MAX_EVENTS];
100
101         // The actual worker thread.
102         virtual void do_work();
103
104         // Process a client; read and write data as far as we can.
105         // After this call, one of these four is true:
106         //
107         //  1. The socket is closed, and the client deleted.
108         //  2. We are still waiting for more data from the client.
109         //  3. We've sent all the data we have to the client,
110         //     and put it in <sleeping_clients>.
111         //  4. The socket buffer is full (which means we still have
112         //     data outstanding).
113         //
114         // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
115         // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
116         // but it's cheaper than taking it in and out all the time.
117         void process_client(Client *client);
118
119         // Close a given client socket, and clean up after it.
120         void close_client(Client *client);
121
122         // Parse the HTTP request. Returns a HTTP status code (200/400/404).
123         int parse_request(Client *client);
124
125         // Construct the HTTP header, and set the client into
126         // the SENDING_HEADER state.
127         void construct_header(Client *client);
128
129         // Construct a generic error with the given line, and set the client into
130         // the SENDING_ERROR state.
131         void construct_error(Client *client, int error_code);
132
133         void process_queued_data();
134         void skip_lost_data(Client *client);
135
136         void add_client(int sock);
137 };
138
139 #endif  // !defined(_SERVER_H)