]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Use unique_ptr a few places instead of explicit delete.
[cubemap] / server.cpp
index 6bac2c63feefffeaf677fd636e39886f8dc92ebd..0b4d076cd9f3fd36ac71fea0508e2e58d6d478cb 100644 (file)
@@ -69,10 +69,6 @@ Server::Server()
 
 Server::~Server()
 {
-       for (size_t i = 0; i < streams.size(); ++i) {   
-               delete streams[i];
-       }
-
        safe_close(epoll_fd);
 }
 
@@ -81,10 +77,8 @@ vector<ClientStats> Server::get_client_stats() const
        vector<ClientStats> ret;
 
        MutexLock lock(&mutex);
-       for (map<int, Client>::const_iterator client_it = clients.begin();
-            client_it != clients.end();
-            ++client_it) {
-               ret.push_back(client_it->second.get_stats());
+       for (const auto &fd_and_client : clients) {
+               ret.push_back(fd_and_client.second.get_stats());
        }
        return ret;
 }
@@ -124,11 +118,11 @@ void Server::do_work()
 
                // Process each client where its stream has new data,
                // even if there was no socket activity.
-               for (size_t i = 0; i < streams.size(); ++i) {   
+               for (unique_ptr<Stream> &stream : streams) {
                        vector<Client *> to_process;
-                       swap(streams[i]->to_process, to_process);
-                       for (size_t i = 0; i < to_process.size(); ++i) {
-                               process_client(to_process[i]);
+                       swap(stream->to_process, to_process);
+                       for (Client *client : to_process) {
+                               process_client(client);
                        }
                }
 
@@ -152,7 +146,7 @@ void Server::do_work()
 
                        // If this client doesn't exist anymore, just ignore it
                        // (it was deleted earlier).
-                       map<int, Client>::iterator client_it = clients.find(connect_time_and_fd.second);
+                       auto client_it = clients.find(connect_time_and_fd.second);
                        if (client_it == clients.end()) {
                                clients_ordered_by_connect_time.pop();
                                continue;
@@ -189,20 +183,16 @@ CubemapStateProto Server::serialize()
        //
        // TODO: Do this when clients are added back from serialized state instead;
        // it would probably be less wasteful.
-       for (map<int, Client>::iterator client_it = clients.begin();
-            client_it != clients.end();
-            ++client_it) {
-               skip_lost_data(&client_it->second);
+       for (auto &fd_and_client : clients) {
+               skip_lost_data(&fd_and_client.second);
        }
 
        CubemapStateProto serialized;
-       for (map<int, Client>::const_iterator client_it = clients.begin();
-            client_it != clients.end();
-            ++client_it) {
-               serialized.add_clients()->MergeFrom(client_it->second.serialize());
+       for (const auto &fd_and_client : clients) {
+               serialized.add_clients()->MergeFrom(fd_and_client.second.serialize());
        }
-       for (size_t i = 0; i < streams.size(); ++i) {   
-               serialized.add_streams()->MergeFrom(streams[i]->serialize());
+       for (unique_ptr<Stream> &stream : streams) {
+               serialized.add_streams()->MergeFrom(stream->serialize());
        }
        return serialized;
 }
@@ -216,10 +206,9 @@ void Server::add_client_deferred(int sock, Acceptor *acceptor)
 void Server::add_client(int sock, Acceptor *acceptor)
 {
        const bool is_tls = acceptor->is_tls();
-       pair<map<int, Client>::iterator, bool> ret =
-               clients.insert(make_pair(sock, Client(sock)));
-       assert(ret.second == true);  // Should not already exist.
-       Client *client_ptr = &ret.first->second;
+       auto inserted = clients.insert(make_pair(sock, Client(sock)));
+       assert(inserted.second == true);  // Should not already exist.
+       Client *client_ptr = &inserted.first->second;
 
        // Connection timestamps must be nondecreasing. I can't find any guarantee
        // that even the monotonic clock can't go backwards by a small amount
@@ -272,12 +261,11 @@ void Server::add_client_from_serialized(const ClientProto &client)
                assert(client.state() != Client::SENDING_DATA);
                stream = NULL;
        } else {
-               stream = streams[stream_index];
+               stream = streams[stream_index].get();
        }
-       pair<map<int, Client>::iterator, bool> ret =
-               clients.insert(make_pair(client.sock(), Client(client, stream)));
-       assert(ret.second == true);  // Should not already exist.
-       Client *client_ptr = &ret.first->second;
+       auto inserted = clients.insert(make_pair(client.sock(), Client(client, stream)));
+       assert(inserted.second == true);  // Should not already exist.
+       Client *client_ptr = &inserted.first->second;
 
        // Connection timestamps must be nondecreasing.
        assert(clients_ordered_by_connect_time.empty() ||
@@ -327,7 +315,7 @@ int Server::add_stream(const string &url, size_t backlog_size, size_t prebufferi
 {
        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;
 }
 
@@ -335,7 +323,7 @@ int Server::add_stream_from_serialized(const StreamProto &stream, int data_fd)
 {
        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;
 }
        
@@ -860,7 +848,7 @@ int Server::parse_request(Client *client)
                }
        }
 
-       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.
        }
@@ -992,13 +980,13 @@ void Server::process_queued_data()
        {
                MutexLock lock(&queued_clients_mutex);
 
-               for (size_t i = 0; i < queued_add_clients.size(); ++i) {
-                       add_client(queued_add_clients[i].first, queued_add_clients[i].second);
+               for (const pair<int, Acceptor *> &id_and_acceptor : queued_add_clients) {
+                       add_client(id_and_acceptor.first, id_and_acceptor.second);
                }
                queued_add_clients.clear();
        }
 
-       for (size_t i = 0; i < streams.size(); ++i) {   
-               streams[i]->process_queued_data();
+       for (unique_ptr<Stream> &stream : streams) {
+               stream->process_queued_data();
        }
 }