14 #define BACKLOG_SIZE 1048576
15 #define EPOLL_MAX_EVENTS 8192
16 #define EPOLL_TIMEOUT_MS 20
17 #define MAX_CLIENT_REQUEST 16384
20 class CubemapStateProto;
25 // Digested statistics for writing to logs etc.
27 std::string stream_id;
28 std::string remote_addr;
37 // Serialization/deserialization.
38 Client(const ClientProto &serialized, Stream *stream);
39 ClientProto serialize() const;
41 ClientStats get_stats() const;
43 // The file descriptor associated with this socket.
46 // The fwmark associated with this socket (or 0).
49 // Some information only used for logging.
50 std::string remote_addr;
53 enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_ERROR };
56 // The HTTP request, as sent by the client. If we are in READING_REQUEST,
57 // this might not be finished.
60 // What stream we're connecting to; parsed from <request>.
61 // Not relevant for READING_REQUEST.
62 std::string stream_id;
65 // The header we want to send. This is nominally a copy of Stream::header,
66 // but since that might change on reconnects etc., we keep a local copy here.
67 // Only relevant for SENDING_HEADER or SENDING_ERROR; blank otherwise.
68 std::string header_or_error;
70 // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
72 size_t header_or_error_bytes_sent;
74 // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
79 Stream(const std::string &stream_id);
82 // Serialization/deserialization.
83 Stream(const StreamProto &serialized);
84 StreamProto serialize() const;
86 std::string stream_id;
88 // The HTTP response header, plus the video stream header (if any).
91 // The stream data itself, stored in a circular buffer.
94 // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
95 // since the buffer wraps.
98 // Clients that are in SENDING_DATA, but that we don't listen on,
99 // because we currently don't have any data for them.
100 // See put_client_to_sleep() and wake_up_all_clients().
101 std::vector<Client *> sleeping_clients;
103 // Clients that we recently got data for (when they were in
104 // <sleeping_clients>).
105 std::vector<Client *> to_process;
107 // What pool to fetch marks from, or NULL.
110 // Put client to sleep, since there is no more data for it; we will on
111 // longer listen on POLLOUT until we get more data. Also, it will be put
112 // in the list of clients to wake up when we do.
113 void put_client_to_sleep(Client *client);
115 // We have more data, so mark all clients that are sleeping as ready to go.
116 void wake_up_all_clients();
119 Stream(const Stream& other);
122 class Server : public Thread {
127 // Get the list of all currently connected clients.
128 std::vector<ClientStats> get_client_stats() const;
130 // Set header (both HTTP header and any stream headers) for the given stream.
131 void set_header(const std::string &stream_id, const std::string &header);
133 // Set that the given stream should use the given mark pool from now on.
134 // NOTE: This should be set before any clients are connected!
135 void set_mark_pool(const std::string &stream_id, MarkPool *mark_pool);
137 // These will be deferred until the next time an iteration in do_work() happens,
138 // and the order between them are undefined.
139 // XXX: header should ideally be ordered with respect to data.
140 void add_client_deferred(int sock);
141 void add_data_deferred(const std::string &stream_id, const char *data, size_t bytes);
143 // These should not be called while running, since that would violate
144 // threading assumptions (ie., that epoll is only called from one thread
145 // at the same time).
146 CubemapStateProto serialize();
147 void add_client_from_serialized(const ClientProto &client);
148 void add_stream(const std::string &stream_id);
149 void add_stream_from_serialized(const StreamProto &stream);
152 // Mutex protecting queued_data only. Note that if you want to hold both this
153 // and <mutex> below, you will need to take <mutex> before this one.
154 mutable pthread_mutex_t queued_data_mutex;
156 // Deferred commands that should be run from the do_work() thread as soon as possible.
157 // We defer these for two reasons:
159 // - We only want to fiddle with epoll from one thread at any given time,
160 // and doing add_client() from the acceptor thread would violate that.
161 // - We don't want the input thread(s) hanging on <mutex> when doing
162 // add_data(), since they want to do add_data() rather often, and <mutex>
163 // can be taken a lot of the time.
165 // Protected by <queued_data_mutex>.
166 std::vector<int> queued_add_clients;
167 std::map<std::string, std::string> queued_data;
169 // All variables below this line are protected by the mutex.
170 mutable pthread_mutex_t mutex;
172 // Map from stream ID to stream.
173 std::map<std::string, Stream *> streams;
175 // Map from file descriptor to client.
176 std::map<int, Client> clients;
178 // Used for epoll implementation (obviously).
180 epoll_event events[EPOLL_MAX_EVENTS];
182 // The actual worker thread.
183 virtual void do_work();
185 // Process a client; read and write data as far as we can.
186 // After this call, one of these four is true:
188 // 1. The socket is closed, and the client deleted.
189 // 2. We are still waiting for more data from the client.
190 // 3. We've sent all the data we have to the client,
191 // and put it in <sleeping_clients>.
192 // 4. The socket buffer is full (which means we still have
193 // data outstanding).
195 // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
196 // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
197 // but it's cheaper than taking it in and out all the time.
198 void process_client(Client *client);
200 // Close a given client socket, and clean up after it.
201 void close_client(Client *client);
203 // Parse the HTTP request. Returns a HTTP status code (200/400/404).
204 int parse_request(Client *client);
206 // Construct the HTTP header, and set the client into
207 // the SENDING_HEADER state.
208 void construct_header(Client *client);
210 // Construct a generic error with the given line, and set the client into
211 // the SENDING_ERROR state.
212 void construct_error(Client *client, int error_code);
214 // TODO: This function should probably die.
215 Stream *find_stream(const std::string &stream_id);
217 void process_queued_data();
219 void add_client(int sock);
220 void add_data(const std::string &stream_id, const char *data, size_t bytes);
223 #endif // !defined(_SERVER_H)