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