Server::~Server()
{
- for (size_t i = 0; i < streams.size(); ++i) {
- delete streams[i];
- }
-
safe_close(epoll_fd);
}
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;
}
// 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);
}
}
// 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;
//
// 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;
}
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
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() ||
{
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.
}
{
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();
}
}