21 #define EPOLL_MAX_EVENTS 8192
22 #define EPOLL_TIMEOUT_MS 20
23 #define MAX_CLIENT_REQUEST 16384
25 class CubemapStateProto;
29 class Server : public Thread {
34 // Get the list of all currently connected clients.
35 std::vector<ClientStats> get_client_stats() const;
37 // Set header (both HTTP header and any stream headers) for the given stream.
38 void set_header(int stream_index,
39 const std::string &http_header,
40 const std::string &stream_header);
42 // Set that the given stream should use the given mark pool from now on.
43 // NOTE: This should be set before any clients are connected!
44 void set_mark_pool(int stream_index, MarkPool *mark_pool);
46 // These will be deferred until the next time an iteration in do_work() happens,
47 // and the order between them are undefined.
48 // XXX: header should ideally be ordered with respect to data.
49 void add_client_deferred(int sock);
50 void add_data_deferred(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
52 // These should not be called while running, since that would violate
53 // threading assumptions (ie., that epoll is only called from one thread
55 CubemapStateProto serialize();
56 void add_client_from_serialized(const ClientProto &client);
57 int add_stream(const std::string &url, size_t bytes_received, Stream::Encoding encoding);
58 int add_stream_from_serialized(const StreamProto &stream, int data_fd);
59 int lookup_stream_by_url(const std::string &url) const;
60 void set_backlog_size(int stream_index, size_t new_size);
61 void set_encoding(int stream_index, Stream::Encoding encoding);
64 // Mutex protecting queued_add_clients and streams[..]->queued_data.
65 // Note that if you want to hold both this and <mutex> below,
66 // you will need to take <mutex> before this one.
67 mutable pthread_mutex_t queued_data_mutex;
69 // Deferred commands that should be run from the do_work() thread as soon as possible.
70 // We defer these for two reasons:
72 // - We only want to fiddle with epoll from one thread at any given time,
73 // and doing add_client() from the acceptor thread would violate that.
74 // - We don't want the input thread(s) hanging on <mutex> when doing
75 // add_data(), since they want to do add_data() rather often, and <mutex>
76 // can be taken a lot of the time.
78 // Protected by <queued_data_mutex>.
79 std::vector<int> queued_add_clients;
81 // All variables below this line are protected by the mutex.
82 mutable pthread_mutex_t mutex;
85 std::vector<Stream *> streams;
87 // Map from URL to index into <streams>.
88 std::map<std::string, int> url_map;
90 // Map from file descriptor to client.
91 std::map<int, Client> clients;
93 // Used for epoll implementation (obviously).
95 epoll_event events[EPOLL_MAX_EVENTS];
97 // The actual worker thread.
98 virtual void do_work();
100 // Process a client; read and write data as far as we can.
101 // After this call, one of these four is true:
103 // 1. The socket is closed, and the client deleted.
104 // 2. We are still waiting for more data from the client.
105 // 3. We've sent all the data we have to the client,
106 // and put it in <sleeping_clients>.
107 // 4. The socket buffer is full (which means we still have
108 // data outstanding).
110 // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
111 // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
112 // but it's cheaper than taking it in and out all the time.
113 void process_client(Client *client);
115 // Close a given client socket, and clean up after it.
116 void close_client(Client *client);
118 // Parse the HTTP request. Returns a HTTP status code (200/400/404).
119 int parse_request(Client *client);
121 // Construct the HTTP header, and set the client into
122 // the SENDING_HEADER state.
123 void construct_header(Client *client);
125 // Construct a generic error with the given line, and set the client into
126 // the SENDING_ERROR state.
127 void construct_error(Client *client, int error_code);
129 void process_queued_data();
130 void skip_lost_data(Client *client);
132 void add_client(int sock);
135 #endif // !defined(_SERVER_H)