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;
25 // Serialization/deserialization.
26 Client(const ClientProto &serialized, Stream *stream);
27 ClientProto serialize() const;
29 // The file descriptor associated with this socket.
32 enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_ERROR };
35 // The HTTP request, as sent by the client. If we are in READING_REQUEST,
36 // this might not be finished.
39 // What stream we're connecting to; parsed from <request>.
40 // Not relevant for READING_REQUEST.
41 std::string stream_id;
44 // The header we want to send. This is nominally a copy of Stream::header,
45 // but since that might change on reconnects etc., we keep a local copy here.
46 // Only relevant for SENDING_HEADER or SENDING_ERROR; blank otherwise.
47 std::string header_or_error;
49 // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
51 size_t header_or_error_bytes_sent;
53 // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
58 Stream(const std::string &stream_id);
61 // Serialization/deserialization.
62 Stream(const StreamProto &serialized);
63 StreamProto serialize() const;
65 std::string stream_id;
67 // The HTTP response header, plus the video stream header (if any).
70 // The stream data itself, stored in a circular buffer.
73 // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
74 // since the buffer wraps.
78 Stream(const Stream& other);
86 // Start a new thread that handles clients.
92 CubemapStateProto serialize() const;
94 void add_client(int sock);
95 void add_client_from_serialized(const ClientProto &client);
97 void add_stream(const std::string &stream_id);
98 void add_stream_from_serialized(const StreamProto &stream);
100 void set_header(const std::string &stream_id, const std::string &header);
101 void add_data(const std::string &stream_id, const char *data, size_t bytes);
104 pthread_t worker_thread;
106 // All variables below this line are protected by the mutex.
107 pthread_mutex_t mutex;
109 // If the thread should stop or not.
112 // Map from stream ID to stream.
113 std::map<std::string, Stream *> streams;
115 // Map from file descriptor to client.
116 std::map<int, Client> clients;
118 // Used for epoll implementation (obviously).
120 epoll_event events[EPOLL_MAX_EVENTS];
122 // Clients that are in SENDING_DATA, but that we don't listen on,
123 // because we currently don't have any data for them.
124 // See put_client_to_sleep() and wake_up_all_clients().
125 std::vector<Client *> sleeping_clients;
127 // Recover the this pointer, and call do_work().
128 static void *do_work_thunk(void *arg);
130 // The actual worker thread.
133 // Process a client; read and write data as far as we can.
134 // After this call, one of these four is true:
136 // 1. The socket is closed, and the client deleted.
137 // 2. We are still waiting for more data from the client.
138 // 3. We've sent all the data we have to the client,
139 // and put it in <sleeping_clients>.
140 // 4. The socket buffer is full (which means we still have
141 // data outstanding).
143 // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
144 // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
145 // but it's cheaper than taking it in and out all the time.
146 void process_client(Client *client);
148 // Close a given client socket, and clean up after it.
149 void close_client(Client *client);
151 // Parse the HTTP request. Returns a HTTP status code (200/400/404).
152 int parse_request(Client *client);
154 // Construct the HTTP header, and set the client into
155 // the SENDING_HEADER state.
156 void construct_header(Client *client);
158 // Construct a generic error with the given line, and set the client into
159 // the SENDING_ERROR state.
160 void construct_error(Client *client, int error_code);
162 // Put client to sleep, since there is no more data for it; we will on
163 // longer listen on POLLOUT until we get more data. Also, it will be put
164 // in the list of clients to wake up when we do.
165 void put_client_to_sleep(Client *client);
167 // We have more data, so mark all clients that are sleeping as ready to go.
168 void wake_up_all_clients();
170 // TODO: This function should probably die.
171 Stream *find_stream(const std::string &stream_id);
174 #endif // !defined(_SERVER_H)