X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=serverpool.cpp;h=2c663fbce37dd3d4e851a05de866bc7ceb9fb7e6;hp=fb8ca451d38cf1c10f8ab30268ffc71c9ff85a9e;hb=6544fa0ec3f3a501bcb89ea977756911bd7f3ebd;hpb=9abb89bcf7940e2ada9d708f86a218a56334f68d diff --git a/serverpool.cpp b/serverpool.cpp index fb8ca45..2c663fb 100644 --- a/serverpool.cpp +++ b/serverpool.cpp @@ -1,28 +1,34 @@ #include -#include -#include #include -#include +#include #include "client.h" #include "log.h" #include "server.h" #include "serverpool.h" #include "state.pb.h" +#include "udpstream.h" #include "util.h" +struct sockaddr_in6; + using namespace std; ServerPool::ServerPool(int size) : servers(new Server[size]), num_servers(size), - clients_added(0) + clients_added(0), + num_http_streams(0) { } ServerPool::~ServerPool() { delete[] servers; + + for (size_t i = 0; i < udp_streams.size(); ++i) { + delete udp_streams[i]; + } } CubemapStateProto ServerPool::serialize() @@ -61,32 +67,33 @@ void ServerPool::add_client_from_serialized(const ClientProto &client) servers[clients_added++ % num_servers].add_client_from_serialized(client); } -int ServerPool::lookup_stream_by_url(const std::string &url) const +int ServerPool::lookup_stream_by_url(const string &url) const { assert(servers != NULL); return servers[0].lookup_stream_by_url(url); } -int ServerPool::add_stream(const string &url, size_t backlog_size, Stream::Encoding encoding) +int ServerPool::add_stream(const string &url, size_t backlog_size, size_t prebuffering_bytes, Stream::Encoding encoding, Stream::Encoding src_encoding) { - int stream_index = -1; + // Adding more HTTP streams after UDP streams would cause the UDP stream + // indices to move around, which is obviously not good. + assert(udp_streams.empty()); + for (int i = 0; i < num_servers; ++i) { - int stream_index2 = servers[i].add_stream(url, backlog_size, encoding); - if (i == 0) { - stream_index = stream_index2; - } else { - // Verify that all servers have this under the same stream index. - assert(stream_index == stream_index2); - } + int stream_index = servers[i].add_stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding); + assert(stream_index == num_http_streams); } - return stream_index; + return num_http_streams++; } int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vector &data_fds) { + // Adding more HTTP streams after UDP streams would cause the UDP stream + // indices to move around, which is obviously not good. + assert(udp_streams.empty()); + assert(!data_fds.empty()); string contents; - int stream_index = -1; for (int i = 0; i < num_servers; ++i) { int data_fd; if (i < int(data_fds.size())) { @@ -102,13 +109,8 @@ int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vect data_fd = make_tempfile(contents); } - int stream_index2 = servers[i].add_stream_from_serialized(stream, data_fd); - if (i == 0) { - stream_index = stream_index2; - } else { - // Verify that all servers have this under the same stream index. - assert(stream_index == stream_index2); - } + int stream_index = servers[i].add_stream_from_serialized(stream, data_fd); + assert(stream_index == num_http_streams); } // Close and delete any leftovers, if the number of servers was reduced. @@ -116,20 +118,55 @@ int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vect safe_close(data_fds[i]); // Implicitly deletes the file. } - return stream_index; + return num_http_streams++; +} + +int ServerPool::add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index) +{ + udp_streams.push_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index)); + return num_http_streams + udp_streams.size() - 1; } void ServerPool::set_header(int stream_index, const string &http_header, const string &stream_header) { + assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size())); + + if (stream_index >= num_http_streams) { + // UDP stream. TODO: Log which stream this is. + if (!stream_header.empty()) { + log(WARNING, "Trying to send stream format with headers to a UDP destination. This is unlikely to work well."); + } + + // Ignore the HTTP header. + return; + } + + // HTTP stream. for (int i = 0; i < num_servers; ++i) { servers[i].set_header(stream_index, http_header, stream_header); } } -void ServerPool::add_data(int stream_index, const char *data, size_t bytes) +void ServerPool::add_data(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start) { + assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size())); + + if (stream_index >= num_http_streams) { + // UDP stream. + udp_streams[stream_index - num_http_streams]->send(data, bytes); + return; + } + + // HTTP stream. for (int i = 0; i < num_servers; ++i) { - servers[i].add_data_deferred(stream_index, data, bytes); + servers[i].add_data_deferred(stream_index, data, bytes, suitable_for_stream_start); + } +} + +void ServerPool::add_gen204(const std::string &url, const std::string &allow_origin) +{ + for (int i = 0; i < num_servers; ++i) { + servers[i].add_gen204(url, allow_origin); } } @@ -157,10 +194,10 @@ vector ServerPool::get_client_stats() const return ret; } -void ServerPool::set_mark_pool(int stream_index, MarkPool *mark_pool) +void ServerPool::set_pacing_rate(int stream_index, uint32_t pacing_rate) { for (int i = 0; i < num_servers; ++i) { - servers[i].set_mark_pool(stream_index, mark_pool); + servers[i].set_pacing_rate(stream_index, pacing_rate); } } @@ -171,9 +208,23 @@ void ServerPool::set_backlog_size(int stream_index, size_t new_size) } } +void ServerPool::set_prebuffering_bytes(int stream_index, size_t new_amount) +{ + for (int i = 0; i < num_servers; ++i) { + servers[i].set_prebuffering_bytes(stream_index, new_amount); + } +} + void ServerPool::set_encoding(int stream_index, Stream::Encoding encoding) { for (int i = 0; i < num_servers; ++i) { servers[i].set_encoding(stream_index, encoding); } } + +void ServerPool::set_src_encoding(int stream_index, Stream::Encoding encoding) +{ + for (int i = 0; i < num_servers; ++i) { + servers[i].set_src_encoding(stream_index, encoding); + } +}