From: Steinar H. Gunderson Date: Sun, 21 Apr 2013 11:56:03 +0000 (+0200) Subject: Merge branch 'master' of /srv/git.sesse.net/www/cubemap X-Git-Tag: 1.0.0~48 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=7b3d494100ef1063578b1ef76818baee4ab53ada;hp=-c;p=cubemap Merge branch 'master' of /srv/git.sesse.net/www/cubemap --- 7b3d494100ef1063578b1ef76818baee4ab53ada diff --combined serverpool.cpp index 9582041,f8e64aa..590e0f5 --- a/serverpool.cpp +++ b/serverpool.cpp @@@ -1,7 -1,11 +1,7 @@@ #include -#include -#include #include -#include #include "client.h" -#include "log.h" #include "server.h" #include "serverpool.h" #include "state.pb.h" @@@ -12,13 -16,18 +12,18 @@@ 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() @@@ -65,24 -74,25 +70,25 @@@ int ServerPool::lookup_stream_by_url(co int ServerPool::add_stream(const string &url, size_t backlog_size, Stream::Encoding 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, 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())) { @@@ -98,13 -108,8 +104,8 @@@ 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. @@@ -112,11 -117,30 +113,30 @@@ safe_close(data_fds[i]); // Implicitly deletes the file. } - return stream_index; + return num_http_streams++; + } + + int ServerPool::add_udpstream(const sockaddr_in6 &dst, MarkPool *mark_pool) + { + udp_streams.push_back(new UDPStream(dst, mark_pool)); + 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); } @@@ -124,6 -148,15 +144,15 @@@ void ServerPool::add_data(int stream_index, const char *data, size_t bytes) { + 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); } diff --combined stream.cpp index 425477c,b860401..6a84027 --- a/stream.cpp +++ b/stream.cpp @@@ -3,7 -3,7 +3,7 @@@ #include #include #include -#include +#include #include #include @@@ -222,7 -222,7 +222,7 @@@ void Stream::add_data_deferred(const ch queued_data.push_back(iov); } else { - assert(encoding == Stream::STREAM_ENCODING_RAW); + assert(false); } } diff --combined thread.cpp index 2aaf34f,98c743c..72bc320 --- a/thread.cpp +++ b/thread.cpp @@@ -1,5 -1,6 +1,5 @@@ #include #include -#include #include #include #include @@@ -40,6 -41,7 +40,7 @@@ void *Thread::do_work_thunk(void *arg // (This isn't strictly required, but it makes it easier to debug that indeed // SIGUSR1 was what woke us up.) sigset_t set; + sigemptyset(&set); sigaddset(&set, SIGHUP); int err = pthread_sigmask(SIG_BLOCK, &set, NULL); if (err != 0) {