]> git.sesse.net Git - cubemap/blobdiff - serverpool.cpp
Run include-what-you-use.
[cubemap] / serverpool.cpp
index 68281996847fa5686158d50e33b588f198d330d6..be47aa99139c17e4048cc82a9b6becac369e8348 100644 (file)
@@ -1,25 +1,34 @@
-#include <unistd.h>
-#include <errno.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <sys/types.h>
 
 #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()
@@ -58,15 +67,31 @@ void ServerPool::add_client_from_serialized(const ClientProto &client)
        servers[clients_added++ % num_servers].add_client_from_serialized(client);
 }
 
-void ServerPool::add_stream(const string &stream_id, size_t backlog_size, Stream::Encoding encoding)
+int ServerPool::lookup_stream_by_url(const std::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)
 {
+       // 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) {
-               servers[i].add_stream(stream_id, backlog_size, encoding);
+               int stream_index = servers[i].add_stream(url, backlog_size, encoding);
+               assert(stream_index == num_http_streams);
        }
+       return num_http_streams++;
 }
 
-void 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;
        for (int i = 0; i < num_servers; ++i) {
@@ -84,34 +109,57 @@ void ServerPool::add_stream_from_serialized(const StreamProto &stream, const vec
                        data_fd = make_tempfile(contents);
                }
 
-               servers[i].add_stream_from_serialized(stream, data_fd);
+               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.
        for (size_t i = num_servers; i < data_fds.size(); ++i) {
-               int ret;
-               do {
-                       ret = close(data_fds[i]);  // Implicitly deletes the file.
-               } while (ret == -1 && errno == EINTR);
-
-               if (ret == -1) {
-                       log_perror("close");
-                       // Can still continue.
-               }
+               safe_close(data_fds[i]);  // Implicitly deletes the file.
        }
+
+       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(const string &stream_id, 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_id, http_header, stream_header);
+               servers[i].set_header(stream_index, http_header, stream_header);
        }
 }
 
-void ServerPool::add_data(const string &stream_id, 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_id, data, bytes);
+               servers[i].add_data_deferred(stream_index, data, bytes);
        }
 }
 
@@ -139,23 +187,23 @@ vector<ClientStats> ServerPool::get_client_stats() const
        return ret;
 }
        
-void ServerPool::set_mark_pool(const string &stream_id, MarkPool *mark_pool)
+void ServerPool::set_mark_pool(int stream_index, MarkPool *mark_pool)
 {
        for (int i = 0; i < num_servers; ++i) {
-               servers[i].set_mark_pool(stream_id, mark_pool);
+               servers[i].set_mark_pool(stream_index, mark_pool);
        }       
 }
 
-void ServerPool::set_backlog_size(const string &stream_id, size_t new_size)
+void ServerPool::set_backlog_size(int stream_index, size_t new_size)
 {
        for (int i = 0; i < num_servers; ++i) {
-               servers[i].set_backlog_size(stream_id, new_size);
+               servers[i].set_backlog_size(stream_index, new_size);
        }       
 }
 
-void ServerPool::set_encoding(const string &stream_id, Stream::Encoding encoding)
+void ServerPool::set_encoding(int stream_index, Stream::Encoding encoding)
 {
        for (int i = 0; i < num_servers; ++i) {
-               servers[i].set_encoding(stream_id, encoding);
+               servers[i].set_encoding(stream_index, encoding);
        }       
 }