#include <string>
#include <map>
+#define NUM_SERVERS 4
#define BACKLOG_SIZE 1048576
+#define EPOLL_MAX_EVENTS 8192
+#define EPOLL_TIMEOUT_MS 20
+#define MAX_CLIENT_REQUEST 16384
struct Client {
+ // The file descriptor associated with this socket.
+ int sock;
+
enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA };
State state;
// this might not be finished.
std::string client_request;
-#if 0
// What stream we're connecting to; parsed from client_request.
// Not relevant for READING_REQUEST.
- string stream_id;
-#endif
+ std::string stream_id;
+
+ // The header we want to send. This is nominally a copy of Stream::header,
+ // but since that might change on reconnects etc., we keep a local copy here.
+ // Only relevant for SENDING_HEADER; blank otherwise.
+ std::string header;
// Number of bytes we've sent of the header. Only relevant for SENDING_HEADER.
size_t header_bytes_sent;
void add_data(const std::string &stream_id, const char *data, size_t bytes);
private:
+ void process_client(Client *client);
+
+ // Close a given client socket, and clean up after it.
+ void close_client(Client *client);
+
+ // Parse the HTTP request, construct the header, and set the client into
+ // the SENDING_HEADER state.
+ void parse_request(Client *client);
+
pthread_mutex_t mutex;
// Map from stream ID to stream.
// Map from file descriptor to client.
std::map<int, Client> clients;
+ // Used for epoll implementation (obviously).
int epoll_fd;
+ epoll_event events[EPOLL_MAX_EVENTS];
// Recover the this pointer, and call do_work().
static void *do_work_thunk(void *arg);