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