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