]> git.sesse.net Git - cubemap/blob - server.h
24e2fcbac3a737a91211bed9eea590d9d1c1a86a
[cubemap] / server.h
1 #ifndef _SERVER_H
2 #define _SERVER_H 1
3
4 #include <stdint.h>
5 #include <pthread.h>
6 #include <sys/epoll.h>
7 #include <string>
8 #include <map>
9 #include <vector>
10
11 #define BACKLOG_SIZE 1048576
12 #define EPOLL_MAX_EVENTS 8192
13 #define EPOLL_TIMEOUT_MS 20
14 #define MAX_CLIENT_REQUEST 16384
15
16 class ClientProto;
17 class CubemapStateProto;
18 class StreamProto;
19
20 struct Client {
21         Client() {}
22         Client(int sock);
23
24         // Serialization/deserialization.
25         Client(const ClientProto &serialized);
26         ClientProto serialize() const;
27
28         // The file descriptor associated with this socket.
29         int sock;
30
31         enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_ERROR };
32         State state;
33
34         // The HTTP request, as sent by the client. If we are in READING_REQUEST,
35         // this might not be finished.
36         std::string request;
37
38         // What stream we're connecting to; parsed from <request>.
39         // Not relevant for READING_REQUEST.
40         std::string stream_id;
41
42         // The header we want to send. This is nominally a copy of Stream::header,
43         // but since that might change on reconnects etc., we keep a local copy here.
44         // Only relevant for SENDING_HEADER or SENDING_ERROR; blank otherwise.
45         std::string header_or_error;
46
47         // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
48         // or SENDING_ERROR.
49         size_t header_or_error_bytes_sent;
50
51         // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
52         size_t bytes_sent;
53 };
54
55 struct Stream {
56         Stream(const std::string &stream_id);
57         ~Stream();
58
59         // Serialization/deserialization.
60         Stream(const StreamProto &serialized);
61         StreamProto serialize() const;
62
63         std::string stream_id;
64
65         // The HTTP response header, plus the video stream header (if any).
66         std::string header;
67
68         // The stream data itself, stored in a circular buffer.
69         char *data;
70
71         // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
72         // since the buffer wraps.
73         size_t data_size;
74
75 private:
76         Stream(const Stream& other);
77 };
78
79 class Server {
80 public:
81         Server();
82         ~Server();
83
84         // Start a new thread that handles clients.
85         void run();
86
87         // Stop the thread.
88         void stop();
89
90         CubemapStateProto serialize() const;
91
92         void add_client(int sock);
93         void add_client_from_serialized(const ClientProto &client);
94
95         void add_stream(const std::string &stream_id);
96         void add_stream_from_serialized(const StreamProto &stream);
97
98         void set_header(const std::string &stream_id, const std::string &header);
99         void add_data(const std::string &stream_id, const char *data, size_t bytes);
100
101 private:
102         pthread_t worker_thread;
103
104         // All variables below this line are protected by the mutex.
105         pthread_mutex_t mutex;
106
107         // If the thread should stop or not.
108         bool should_stop;       
109
110         // Map from stream ID to stream.
111         std::map<std::string, Stream *> streams;
112
113         // Map from file descriptor to client.
114         std::map<int, Client> clients;
115
116         // Used for epoll implementation (obviously).
117         int epoll_fd;
118         epoll_event events[EPOLL_MAX_EVENTS];
119
120         // Clients that are in SENDING_DATA, but that we don't listen on,
121         // because we currently don't have any data for them.
122         // See put_client_to_sleep() and wake_up_all_clients().
123         std::vector<Client *> sleeping_clients;
124
125         // Recover the this pointer, and call do_work().
126         static void *do_work_thunk(void *arg);
127
128         // The actual worker thread.
129         void do_work();
130
131         // Process a client; read and write data as far as we can.
132         // After this call, one of these four is true:
133         //
134         //  1. The socket is closed, and the client deleted.
135         //  2. We are still waiting for more data from the client.
136         //  3. We've sent all the data we have to the client,
137         //     and put it in <sleeping_clients>.
138         //  4. The socket buffer is full (which means we still have
139         //     data outstanding).
140         //
141         // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
142         // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
143         // but it's cheaper than taking it in and out all the time.
144         void process_client(Client *client);
145
146         // Close a given client socket, and clean up after it.
147         void close_client(Client *client);
148
149         // Parse the HTTP request. Returns a HTTP status code (200/400/404).
150         int parse_request(Client *client);
151
152         // Construct the HTTP header, and set the client into
153         // the SENDING_HEADER state.
154         void construct_header(Client *client);
155
156         // Construct a generic error with the given line, and set the client into
157         // the SENDING_ERROR state.
158         void construct_error(Client *client, int error_code);
159
160         // Put client to sleep, since there is no more data for it; we will on
161         // longer listen on POLLOUT until we get more data. Also, it will be put
162         // in the list of clients to wake up when we do.
163         void put_client_to_sleep(Client *client);
164
165         // We have more data, so mark all clients that are sleeping as ready to go.
166         void wake_up_all_clients();
167
168         // TODO: This function should probably die.
169         Stream *find_stream(const std::string &stream_id);
170 };
171
172 #endif  // !defined(_SERVER_H)