10 #define BACKLOG_SIZE 1048576
11 #define EPOLL_MAX_EVENTS 8192
12 #define EPOLL_TIMEOUT_MS 20
13 #define MAX_CLIENT_REQUEST 16384
16 class CubemapStateProto;
23 // Serialization/deserialization.
24 Client(const ClientProto &serialized);
25 ClientProto serialize() const;
27 // The file descriptor associated with this socket.
30 enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA };
33 // The HTTP request, as sent by the client. If we are in READING_REQUEST,
34 // this might not be finished.
37 // What stream we're connecting to; parsed from <request>.
38 // Not relevant for READING_REQUEST.
39 std::string stream_id;
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.
46 // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER.
47 size_t header_bytes_sent;
49 // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
54 Stream(const std::string &stream_id);
57 // Serialization/deserialization.
58 Stream(const StreamProto &serialized);
59 StreamProto serialize() const;
61 std::string stream_id;
63 // The HTTP response header, plus the video stream header (if any).
66 // The stream data itself, stored in a circular buffer.
69 // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
70 // since the buffer wraps.
74 Stream(const Stream& other);
82 // Start a new thread that handles clients.
88 CubemapStateProto serialize() const;
90 void add_client(int sock);
91 void add_client_from_serialized(const ClientProto &client);
93 void add_stream(const std::string &stream_id);
94 void add_stream_from_serialized(const StreamProto &stream);
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);
100 pthread_t worker_thread;
102 // All variables below this line are protected by the mutex.
103 pthread_mutex_t mutex;
105 // If the thread should stop or not.
108 // Map from stream ID to stream.
109 std::map<std::string, Stream *> streams;
111 // Map from file descriptor to client.
112 std::map<int, Client> clients;
114 // Used for epoll implementation (obviously).
116 epoll_event events[EPOLL_MAX_EVENTS];
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;
123 // Recover the this pointer, and call do_work().
124 static void *do_work_thunk(void *arg);
126 // The actual worker thread.
129 void process_client(Client *client);
131 // Close a given client socket, and clean up after it.
132 void close_client(Client *client);
134 // Parse the HTTP request.
135 void parse_request(Client *client);
137 // Construct the HTTP header, and set the client into
138 // the SENDING_HEADER state.
139 void construct_header(Client *client);
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);
146 // We have more data, so mark all clients that are sleeping as ready to go.
147 void wake_up_all_clients();
149 // TODO: This function should probably die.
150 Stream *find_stream(const std::string &stream_id);
153 #endif // !defined(_SERVER_H)