]> git.sesse.net Git - cubemap/blob - server.h
Support deserialization of most state (curl input is not really good yet).
[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         ~Server();
81
82         // Start a new thread that handles clients.
83         void run();
84
85         // Stop the thread.
86         void stop();
87
88         CubemapStateProto serialize() const;
89
90         void add_client(int sock);
91         void add_client_from_serialized(const ClientProto &client);
92
93         void add_stream(const std::string &stream_id);
94         void add_stream_from_serialized(const StreamProto &stream);
95
96         void set_header(const std::string &stream_id, const std::string &header);
97         void add_data(const std::string &stream_id, const char *data, size_t bytes);
98
99 private:
100         pthread_t worker_thread;
101
102         // All variables below this line are protected by the mutex.
103         pthread_mutex_t mutex;
104
105         // If the thread should stop or not.
106         bool should_stop;       
107
108         // Map from stream ID to stream.
109         std::map<std::string, Stream *> streams;
110
111         // Map from file descriptor to client.
112         std::map<int, Client> clients;
113
114         // Used for epoll implementation (obviously).
115         int epoll_fd;
116         epoll_event events[EPOLL_MAX_EVENTS];
117
118         // Clients that are in SENDING_DATA, but that we don't listen on,
119         // because we currently don't have any data for them.
120         // See put_client_to_sleep() and wake_up_all_clients().
121         std::vector<int> sleeping_clients;
122
123         // Recover the this pointer, and call do_work().
124         static void *do_work_thunk(void *arg);
125
126         // The actual worker thread.
127         void do_work();
128
129         void process_client(Client *client);
130
131         // Close a given client socket, and clean up after it.
132         void close_client(Client *client);
133
134         // Parse the HTTP request.
135         void parse_request(Client *client);
136
137         // Construct the HTTP header, and set the client into
138         // the SENDING_HEADER state.
139         void construct_header(Client *client);
140
141         // Put client to sleep, since there is no more data for it; we will on
142         // longer listen on POLLOUT until we get more data. Also, it will be put
143         // in the list of clients to wake up when we do.
144         void put_client_to_sleep(Client *client);
145
146         // We have more data, so mark all clients that are sleeping as ready to go.
147         void wake_up_all_clients();
148
149         // TODO: This function should probably die.
150         Stream *find_stream(const std::string &stream_id);
151 };
152
153 #endif  // !defined(_SERVER_H)