+ int stream_index = servers[i].add_stream(url, backlog_size, prebuffering_bytes, encoding);
+ assert(stream_index == num_http_streams);
+ }
+ return num_http_streams++;
+}
+
+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) {
+ int data_fd;
+ if (i < int(data_fds.size())) {
+ // Reuse one of the existing file descriptors.
+ data_fd = data_fds[i];
+ } else {
+ // Clone the first one.
+ if (contents.empty()) {
+ if (!read_tempfile(data_fds[0], &contents)) {
+ exit(1);
+ }
+ }
+ data_fd = make_tempfile(contents);
+ }
+
+ 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) {
+ safe_close(data_fds[i]); // Implicitly deletes the file.