11 #define BACKLOG_SIZE 1048576
12 #define EPOLL_MAX_EVENTS 8192
13 #define EPOLL_TIMEOUT_MS 20
14 #define MAX_CLIENT_REQUEST 16384
17 class CubemapStateProto;
24 // Serialization/deserialization.
25 Client(const ClientProto &serialized);
26 ClientProto serialize() const;
28 // The file descriptor associated with this socket.
31 enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_ERROR };
34 // The HTTP request, as sent by the client. If we are in READING_REQUEST,
35 // this might not be finished.
38 // What stream we're connecting to; parsed from <request>.
39 // Not relevant for READING_REQUEST.
40 std::string stream_id;
42 // The header we want to send. This is nominally a copy of Stream::header,
43 // but since that might change on reconnects etc., we keep a local copy here.
44 // Only relevant for SENDING_HEADER or SENDING_ERROR; blank otherwise.
45 std::string header_or_error;
47 // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
49 size_t header_or_error_bytes_sent;
51 // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
56 Stream(const std::string &stream_id);
59 // Serialization/deserialization.
60 Stream(const StreamProto &serialized);
61 StreamProto serialize() const;
63 std::string stream_id;
65 // The HTTP response header, plus the video stream header (if any).
68 // The stream data itself, stored in a circular buffer.
71 // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
72 // since the buffer wraps.
76 Stream(const Stream& other);
84 // Start a new thread that handles clients.
90 CubemapStateProto serialize() const;
92 void add_client(int sock);
93 void add_client_from_serialized(const ClientProto &client);
95 void add_stream(const std::string &stream_id);
96 void add_stream_from_serialized(const StreamProto &stream);
98 void set_header(const std::string &stream_id, const std::string &header);
99 void add_data(const std::string &stream_id, const char *data, size_t bytes);
102 pthread_t worker_thread;
104 // All variables below this line are protected by the mutex.
105 pthread_mutex_t mutex;
107 // If the thread should stop or not.
110 // Map from stream ID to stream.
111 std::map<std::string, Stream *> streams;
113 // Map from file descriptor to client.
114 std::map<int, Client> clients;
116 // Used for epoll implementation (obviously).
118 epoll_event events[EPOLL_MAX_EVENTS];
120 // Clients that are in SENDING_DATA, but that we don't listen on,
121 // because we currently don't have any data for them.
122 // See put_client_to_sleep() and wake_up_all_clients().
123 std::vector<Client *> sleeping_clients;
125 // Recover the this pointer, and call do_work().
126 static void *do_work_thunk(void *arg);
128 // The actual worker thread.
131 // Process a client; read and write data as far as we can.
132 // After this call, one of these four is true:
134 // 1. The socket is closed, and the client deleted.
135 // 2. We are still waiting for more data from the client.
136 // 3. We've sent all the data we have to the client,
137 // and put it in <sleeping_clients>.
138 // 4. The socket buffer is full (which means we still have
139 // data outstanding).
141 // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
142 // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
143 // but it's cheaper than taking it in and out all the time.
144 void process_client(Client *client);
146 // Close a given client socket, and clean up after it.
147 void close_client(Client *client);
149 // Parse the HTTP request. Returns a HTTP status code (200/400/404).
150 int parse_request(Client *client);
152 // Construct the HTTP header, and set the client into
153 // the SENDING_HEADER state.
154 void construct_header(Client *client);
156 // Construct a generic error with the given line, and set the client into
157 // the SENDING_ERROR state.
158 void construct_error(Client *client, int error_code);
160 // Put client to sleep, since there is no more data for it; we will on
161 // longer listen on POLLOUT until we get more data. Also, it will be put
162 // in the list of clients to wake up when we do.
163 void put_client_to_sleep(Client *client);
165 // We have more data, so mark all clients that are sleeping as ready to go.
166 void wake_up_all_clients();
168 // TODO: This function should probably die.
169 Stream *find_stream(const std::string &stream_id);
172 #endif // !defined(_SERVER_H)