]> git.sesse.net Git - cubemap/blob - serverpool.h
Bump version number to 1.2.1-pre.
[cubemap] / serverpool.h
1 #ifndef _SERVERPOOL_H
2 #define _SERVERPOOL_H 1
3
4 #include <stddef.h>
5 #include <string>
6 #include <vector>
7
8 #include "server.h"
9 #include "state.pb.h"
10 #include "stream.h"
11 #include "udpstream.h"
12
13 class Server;
14 class UDPStream;
15 struct ClientStats;
16 struct sockaddr_in6;
17
18 // Provides services such as load-balancing between a number of Server instances.
19 class ServerPool {
20 public:
21         ServerPool(int num_servers);
22         ~ServerPool();
23
24         // Fills streams() and clients().
25         CubemapStateProto serialize();
26
27         // Picks a server (round-robin) and allocates the given client to it.
28         void add_client(int sock);
29         void add_client_from_serialized(const ClientProto &client);
30
31         // Adds the given stream to all the servers. Returns the stream index.
32         int add_stream(const std::string &url, size_t backlog_size, size_t prebuffering_bytes, Stream::Encoding encoding);
33         int add_stream_from_serialized(const StreamProto &stream, const std::vector<int> &data_fds);
34         void delete_stream(const std::string &url);
35         int add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index);
36
37         // Returns the stream index for the given URL (e.g. /foo.ts). Returns -1 on failure.
38         int lookup_stream_by_url(const std::string &url) const;
39
40         // Adds the given data to all the servers.
41         void set_header(int stream_index,
42                         const std::string &http_header,
43                         const std::string &stream_header);
44         void add_data(int stream_index, const char *data, size_t bytes, StreamStartSuitability suitable_for_stream_start);
45
46         // Sets the max pacing rate for all the servers.
47         void set_pacing_rate(int stream_index, uint32_t pacing_rate);
48
49         // Changes the given stream's backlog size on all the servers.
50         void set_backlog_size(int stream_index, size_t new_size);
51
52         // Changes the given stream's amount of forced prebuffering on all the servers.
53         void set_prebuffering_bytes(int stream_index, size_t new_amount);
54
55         // Changes the given stream's encoding type on all the servers.
56         void set_encoding(int stream_index, Stream::Encoding encoding);
57
58         // Starts all the servers.
59         void run();
60
61         // Stops all the servers.
62         void stop();
63
64         std::vector<ClientStats> get_client_stats() const;
65
66 private:
67         Server *servers;
68         int num_servers, clients_added;
69
70         // Our indexing is currently rather primitive; every stream_index in
71         // [0, num_http_streams) maps to a HTTP stream (of which every Server
72         // has exactly one copy), and after that, it's mapping directly into
73         // <udp_streams>.
74         int num_http_streams;
75         std::vector<UDPStream *> udp_streams;
76
77         ServerPool(const ServerPool &);
78 };
79
80 #endif  // !defined(_SERVERPOOL_H)