]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Use nullptr instead of NULL everywhere.
[cubemap] / server.cpp
index 6bac2c63feefffeaf677fd636e39886f8dc92ebd..2ec2fe0c2cdc1e9b60bfa029ddbfda457eb7f873 100644 (file)
@@ -57,8 +57,8 @@ inline bool is_earlier(timespec a, timespec b)
 
 Server::Server()
 {
-       pthread_mutex_init(&mutex, NULL);
-       pthread_mutex_init(&queued_clients_mutex, NULL);
+       pthread_mutex_init(&mutex, nullptr);
+       pthread_mutex_init(&queued_clients_mutex, nullptr);
 
        epoll_fd = epoll_create(1024);  // Size argument is ignored.
        if (epoll_fd == -1) {
@@ -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
@@ -252,7 +241,7 @@ void Server::add_client(int sock, Acceptor *acceptor)
        if (is_tls) {
                assert(tls_server_contexts.count(acceptor));
                client_ptr->tls_context = tls_accept(tls_server_contexts[acceptor]);
-               if (client_ptr->tls_context == NULL) {
+               if (client_ptr->tls_context == nullptr) {
                        log(ERROR, "tls_accept() failed");
                        close_client(client_ptr);
                        return;
@@ -270,14 +259,13 @@ void Server::add_client_from_serialized(const ClientProto &client)
        int stream_index = lookup_stream_by_url(client.url());
        if (stream_index == -1) {
                assert(client.state() != Client::SENDING_DATA);
-               stream = NULL;
+               stream = nullptr;
        } 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;
 }
        
@@ -430,7 +418,7 @@ void Server::process_client(Client *client)
 {
        switch (client->state) {
        case Client::READING_REQUEST: {
-               if (client->tls_context != NULL) {
+               if (client->tls_context != nullptr) {
                        if (send_pending_tls_data(client)) {
                                // send_pending_tls_data() hit postconditions #1 or #4.
                                return;
@@ -441,7 +429,7 @@ read_request_again:
                // Try to read more of the request.
                char buf[1024];
                int ret;
-               if (client->tls_context == NULL) {
+               if (client->tls_context == nullptr) {
                        ret = read_nontls_data(client, buf, sizeof(buf));
                        if (ret == -1) {
                                // read_nontls_data() hit postconditions #1 or #2.
@@ -673,9 +661,9 @@ sending_data_again:
 bool Server::send_pending_tls_data(Client *client)
 {
        // See if there's data from the TLS library to write.
-       if (client->tls_data_to_send == NULL) {
+       if (client->tls_data_to_send == nullptr) {
                client->tls_data_to_send = tls_get_write_buffer(client->tls_context, &client->tls_data_left_to_send);
-               if (client->tls_data_to_send == NULL) {
+               if (client->tls_data_to_send == nullptr) {
                        // Really no data to send.
                        return false;
                }
@@ -704,7 +692,7 @@ send_data_again:
        if (ret > 0 && size_t(ret) == client->tls_data_left_to_send) {
                // All data has been sent, so we don't need to go to sleep.
                tls_buffer_clear(client->tls_context);
-               client->tls_data_to_send = NULL;
+               client->tls_data_to_send = nullptr;
                return false;
        }
 
@@ -802,7 +790,7 @@ read_again:
 void Server::skip_lost_data(Client *client)
 {
        Stream *stream = client->stream;
-       if (stream == NULL) {
+       if (stream == nullptr) {
                return;
        }
        size_t bytes_to_send = stream->bytes_received - client->stream_pos;
@@ -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.
        }
@@ -951,13 +939,13 @@ void delete_from(vector<T> *v, T elem)
        
 void Server::close_client(Client *client)
 {
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
+       if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, nullptr) == -1) {
                log_perror("epoll_ctl(EPOLL_CTL_DEL)");
                exit(1);
        }
 
        // This client could be sleeping, so we'll need to fix that. (Argh, O(n).)
-       if (client->stream != NULL) {
+       if (client->stream != nullptr) {
                delete_from(&client->stream->sleeping_clients, client);
                delete_from(&client->stream->to_process, client);
        }
@@ -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();
        }
 }