}
// Start writing statistics.
- StatsThread *stats_thread = NULL;
+ unique_ptr<StatsThread> stats_thread;
if (!config.stats_file.empty()) {
- stats_thread = new StatsThread(config.stats_file, config.stats_interval);
+ stats_thread.reset(new StatsThread(config.stats_file, config.stats_interval));
stats_thread->run();
}
- InputStatsThread *input_stats_thread = NULL;
+ unique_ptr<InputStatsThread> input_stats_thread;
if (!config.input_stats_file.empty()) {
vector<Input*> inputs_no_refcount;
for (const auto &key_and_input_with_refcount : inputs) {
inputs_no_refcount.push_back(key_and_input_with_refcount.second.input);
}
- input_stats_thread = new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount);
+ input_stats_thread.reset(new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount));
input_stats_thread->run();
}
if (input_stats_thread != NULL) {
input_stats_thread->stop();
- delete input_stats_thread;
+ input_stats_thread.reset();
}
if (stats_thread != NULL) {
stats_thread->stop();
- delete stats_thread;
+ stats_thread.reset();
}
for (Acceptor *acceptor : acceptors) {
acceptor->stop();
Server::~Server()
{
- for (Stream *stream : streams) {
- delete stream;
- }
-
safe_close(epoll_fd);
}
// Process each client where its stream has new data,
// even if there was no socket activity.
- for (Stream *stream : streams) {
+ for (unique_ptr<Stream> &stream : streams) {
vector<Client *> to_process;
swap(stream->to_process, to_process);
for (Client *client : to_process) {
for (const auto &fd_and_client : clients) {
serialized.add_clients()->MergeFrom(fd_and_client.second.serialize());
}
- for (Stream *stream : streams) {
+ for (unique_ptr<Stream> &stream : streams) {
serialized.add_streams()->MergeFrom(stream->serialize());
}
return serialized;
assert(client.state() != Client::SENDING_DATA);
stream = NULL;
} else {
- stream = streams[stream_index];
+ stream = streams[stream_index].get();
}
auto inserted = clients.insert(make_pair(client.sock(), Client(client, stream)));
assert(inserted.second == true); // Should not already exist.
{
MutexLock lock(&mutex);
stream_url_map.insert(make_pair(url, streams.size()));
- streams.push_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding));
+ streams.emplace_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding));
return streams.size() - 1;
}
{
MutexLock lock(&mutex);
stream_url_map.insert(make_pair(stream.url(), streams.size()));
- streams.push_back(new Stream(stream, data_fd));
+ streams.emplace_back(new Stream(stream, data_fd));
return streams.size() - 1;
}
}
}
- Stream *stream = streams[stream_url_map_it->second];
+ Stream *stream = streams[stream_url_map_it->second].get();
if (stream->http_header.empty()) {
return 503; // Service unavailable.
}
queued_add_clients.clear();
}
- for (Stream *stream : streams) {
+ for (unique_ptr<Stream> &stream : streams) {
stream->process_queued_data();
}
}
#include <sys/types.h>
#include <time.h>
#include <map>
+#include <memory>
#include <queue>
#include <string>
#include <vector>
mutable pthread_mutex_t mutex;
// All streams.
- std::vector<Stream *> streams;
+ std::vector<std::unique_ptr<Stream>> streams;
// Map from URL to index into <streams>.
std::map<std::string, int> stream_url_map;
{
}
-ServerPool::~ServerPool()
-{
- delete[] servers;
-
- for (UDPStream *udp_stream : udp_streams) {
- delete udp_stream;
- }
-}
-
CubemapStateProto ServerPool::serialize()
{
CubemapStateProto state;
int ServerPool::add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index)
{
- udp_streams.push_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index));
+ udp_streams.emplace_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index));
return num_http_streams + udp_streams.size() - 1;
}
#define _SERVERPOOL_H 1
#include <stddef.h>
+#include <memory>
#include <string>
#include <vector>
class ServerPool {
public:
ServerPool(int num_servers);
- ~ServerPool();
// Fills streams() and clients().
CubemapStateProto serialize();
std::vector<ClientStats> get_client_stats() const;
private:
- Server *servers;
+ std::unique_ptr<Server[]> servers;
int num_servers, clients_added;
// Our indexing is currently rather primitive; every stream_index in
// has exactly one copy), and after that, it's mapping directly into
// <udp_streams>.
int num_http_streams;
- std::vector<UDPStream *> udp_streams;
+ std::vector<std::unique_ptr<UDPStream>> udp_streams;
ServerPool(const ServerPool &);
};