]> git.sesse.net Git - cubemap/blobdiff - serverpool.cpp
Add support for UDP outputs.
[cubemap] / serverpool.cpp
index fb8ca451d38cf1c10f8ab30268ffc71c9ff85a9e..f8e64aaeb962e75b139e3cc3e9098161abda6990 100644 (file)
@@ -16,13 +16,18 @@ using namespace std;
 ServerPool::ServerPool(int size)
        : servers(new Server[size]),
          num_servers(size),
 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;
 {
 }
 
 ServerPool::~ServerPool()
 {
        delete[] servers;
+
+       for (size_t i = 0; i < udp_streams.size(); ++i) {
+               delete udp_streams[i];
+       }
 }
        
 CubemapStateProto ServerPool::serialize()
 }
        
 CubemapStateProto ServerPool::serialize()
@@ -69,24 +74,25 @@ int ServerPool::lookup_stream_by_url(const std::string &url) const
 
 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, 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) {
        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<int> &data_fds)
 {
 }
 
 int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vector<int> &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;
        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())) {
        for (int i = 0; i < num_servers; ++i) {
                int data_fd;
                if (i < int(data_fds.size())) {
@@ -102,13 +108,8 @@ int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vect
                        data_fd = make_tempfile(contents);
                }
 
                        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.
        }
 
        // Close and delete any leftovers, if the number of servers was reduced.
@@ -116,11 +117,30 @@ int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vect
                safe_close(data_fds[i]);  // Implicitly deletes the file.
        }
 
                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)
 {
 }
 
 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);
        }
        for (int i = 0; i < num_servers; ++i) {
                servers[i].set_header(stream_index, http_header, stream_header);
        }
@@ -128,6 +148,15 @@ void ServerPool::set_header(int stream_index, const string &http_header, const s
 
 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)
 {
+       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);
        }
        for (int i = 0; i < num_servers; ++i) {
                servers[i].add_data_deferred(stream_index, data, bytes);
        }