8 #include "serverpool.h"
10 #include "udpstream.h"
17 ServerPool::ServerPool(int size)
18 : servers(new Server[size]),
23 CubemapStateProto ServerPool::serialize()
25 CubemapStateProto state;
27 unordered_map<const string *, size_t> short_response_pool;
29 for (int i = 0; i < num_servers; ++i) {
30 CubemapStateProto local_state = servers[i].serialize(&short_response_pool);
32 // The stream state should be identical between the servers, so we only store it once,
33 // save for the fds, which we keep around to distribute to the servers after re-exec.
35 state.mutable_streams()->MergeFrom(local_state.streams());
37 assert(state.streams_size() == local_state.streams_size());
38 for (int j = 0; j < local_state.streams_size(); ++j) {
39 assert(local_state.streams(j).data_fds_size() == 1);
40 state.mutable_streams(j)->add_data_fds(local_state.streams(j).data_fds(0));
43 for (const ClientProto &client : local_state.clients()) {
44 state.add_clients()->MergeFrom(client);
48 for (size_t i = 0; i < short_response_pool.size(); ++i) {
49 state.mutable_short_response_pool()->Add();
51 for (const auto &string_and_index : short_response_pool) {
52 state.mutable_short_response_pool(string_and_index.second)->set_header_or_short_response(*string_and_index.first);
58 void ServerPool::add_client(int sock, Acceptor *acceptor)
60 servers[clients_added++ % num_servers].add_client_deferred(sock, acceptor);
63 void ServerPool::add_client_from_serialized(const ClientProto &client, const std::vector<std::shared_ptr<const std::string>> &short_responses)
65 servers[clients_added++ % num_servers].add_client_from_serialized(client, short_responses);
68 int ServerPool::lookup_stream_by_url(const string &url) const
70 assert(servers != nullptr);
71 return servers[0].lookup_stream_by_url(url);
74 int ServerPool::add_stream(const string &url,
75 const string &hls_url,
77 size_t prebuffering_bytes,
78 Stream::Encoding encoding,
79 Stream::Encoding src_encoding,
80 unsigned hls_frag_duration,
81 size_t hls_backlog_margin,
82 const string &allow_origin)
84 // Adding more HTTP streams after UDP streams would cause the UDP stream
85 // indices to move around, which is obviously not good.
86 assert(udp_streams.empty());
88 for (int i = 0; i < num_servers; ++i) {
89 int stream_index = servers[i].add_stream(url, hls_url, backlog_size, prebuffering_bytes, encoding, src_encoding, hls_frag_duration, hls_backlog_margin, allow_origin);
90 assert(stream_index == num_http_streams);
92 return num_http_streams++;
95 int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vector<int> &data_fds)
97 // Adding more HTTP streams after UDP streams would cause the UDP stream
98 // indices to move around, which is obviously not good.
99 assert(udp_streams.empty());
101 assert(!data_fds.empty());
103 for (int i = 0; i < num_servers; ++i) {
105 if (i < int(data_fds.size())) {
106 // Reuse one of the existing file descriptors.
107 data_fd = data_fds[i];
109 // Clone the first one.
110 if (contents.empty()) {
111 if (!read_tempfile(data_fds[0], &contents)) {
115 data_fd = make_tempfile(contents);
118 int stream_index = servers[i].add_stream_from_serialized(stream, data_fd);
119 assert(stream_index == num_http_streams);
122 // Close and delete any leftovers, if the number of servers was reduced.
123 for (size_t i = num_servers; i < data_fds.size(); ++i) {
124 safe_close(data_fds[i]); // Implicitly deletes the file.
127 return num_http_streams++;
130 int ServerPool::add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index)
132 udp_streams.emplace_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index));
133 return num_http_streams + udp_streams.size() - 1;
136 void ServerPool::set_header(int stream_index, const string &http_header, const string &stream_header)
138 assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size()));
140 if (stream_index >= num_http_streams) {
141 // UDP stream. TODO: Log which stream this is.
142 if (!stream_header.empty()) {
143 log(WARNING, "Trying to send stream format with headers to a UDP destination. This is unlikely to work well.");
146 // Ignore the HTTP header.
151 for (int i = 0; i < num_servers; ++i) {
152 servers[i].set_header(stream_index, http_header, stream_header);
156 void ServerPool::add_data(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts)
158 assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size()));
160 if (stream_index >= num_http_streams) {
162 udp_streams[stream_index - num_http_streams]->send(data, bytes);
167 for (int i = 0; i < num_servers; ++i) {
168 servers[i].add_data_deferred(stream_index, data, bytes, metacube_flags, pts);
172 void ServerPool::add_gen204(const std::string &url, const std::string &allow_origin)
174 for (int i = 0; i < num_servers; ++i) {
175 servers[i].add_gen204(url, allow_origin);
179 void ServerPool::create_tls_context_for_acceptor(const Acceptor *acceptor)
181 for (int i = 0; i < num_servers; ++i) {
182 servers[i].create_tls_context_for_acceptor(acceptor);
186 void ServerPool::run()
188 for (int i = 0; i < num_servers; ++i) {
193 void ServerPool::stop()
195 for (int i = 0; i < num_servers; ++i) {
200 vector<ClientStats> ServerPool::get_client_stats() const
202 vector<ClientStats> ret;
203 for (int i = 0; i < num_servers; ++i) {
204 vector<ClientStats> stats = servers[i].get_client_stats();
205 ret.insert(ret.end(), stats.begin(), stats.end());
210 void ServerPool::set_pacing_rate(int stream_index, uint32_t pacing_rate)
212 for (int i = 0; i < num_servers; ++i) {
213 servers[i].set_pacing_rate(stream_index, pacing_rate);
217 void ServerPool::set_backlog_size(int stream_index, size_t new_size)
219 for (int i = 0; i < num_servers; ++i) {
220 servers[i].set_backlog_size(stream_index, new_size);
224 void ServerPool::set_prebuffering_bytes(int stream_index, size_t new_amount)
226 for (int i = 0; i < num_servers; ++i) {
227 servers[i].set_prebuffering_bytes(stream_index, new_amount);
231 void ServerPool::set_encoding(int stream_index, Stream::Encoding encoding)
233 for (int i = 0; i < num_servers; ++i) {
234 servers[i].set_encoding(stream_index, encoding);
238 void ServerPool::set_src_encoding(int stream_index, Stream::Encoding encoding)
240 for (int i = 0; i < num_servers; ++i) {
241 servers[i].set_src_encoding(stream_index, encoding);
245 void ServerPool::set_hls_frag_duration(int stream_index, unsigned hls_frag_duration)
247 for (int i = 0; i < num_servers; ++i) {
248 servers[i].set_hls_frag_duration(stream_index, hls_frag_duration);
252 void ServerPool::set_hls_backlog_margin(int stream_index, size_t hls_backlog_margin)
254 for (int i = 0; i < num_servers; ++i) {
255 servers[i].set_hls_backlog_margin(stream_index, hls_backlog_margin);
259 void ServerPool::set_allow_origin(int stream_index, const std::string &allow_origin)
261 for (int i = 0; i < num_servers; ++i) {
262 servers[i].set_allow_origin(stream_index, allow_origin);
266 void ServerPool::register_hls_url(int stream_index, const string &hls_url)
268 for (int i = 0; i < num_servers; ++i) {
269 servers[i].register_hls_url(stream_index, hls_url);