From ae226f65c896d309fbdfd9e83cc28b5b5db9f0ef Mon Sep 17 00:00:00 2001 From: Steve Anton Date: Tue, 29 Jan 2019 12:47:38 -0800 Subject: [PATCH] Use Abseil container algorithms in p2p/ Bug: None Change-Id: I02dd19efa201bd9d55d0f7c2e1496693017a6848 Reviewed-on: https://webrtc-review.googlesource.com/c/120001 Commit-Queue: Steve Anton Reviewed-by: Qingsi Wang Cr-Commit-Position: refs/heads/master@{#26455} --- p2p/BUILD.gn | 3 + p2p/base/fake_ice_transport.h | 4 +- p2p/base/p2p_transport_channel.cc | 108 ++++++++---------- p2p/base/p2p_transport_channel_unittest.cc | 22 ++-- p2p/base/port.cc | 5 +- p2p/base/relay_server.cc | 15 +-- p2p/base/tcp_port.cc | 23 ++-- p2p/base/transport_description.h | 5 +- .../transport_description_factory_unittest.cc | 29 ++--- p2p/base/turn_port.cc | 34 +++--- p2p/base/turn_server.cc | 6 +- p2p/client/basic_port_allocator.cc | 54 +++++---- p2p/client/basic_port_allocator_unittest.cc | 54 ++++----- 13 files changed, 165 insertions(+), 197 deletions(-) diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn index 633dc95c5f..94aab36541 100644 --- a/p2p/BUILD.gn +++ b/p2p/BUILD.gn @@ -141,6 +141,7 @@ if (rtc_include_tests) { "../rtc_base:rtc_base_tests_utils", "../rtc_base/third_party/sigslot", "../test:test_support", + "//third_party/abseil-cpp/absl/algorithm:container", "//third_party/abseil-cpp/absl/memory", "//third_party/abseil-cpp/absl/types:optional", ] @@ -191,6 +192,7 @@ if (rtc_include_tests) { "../system_wrappers:metrics", "../test:test_support", "//testing/gtest", + "//third_party/abseil-cpp/absl/algorithm:container", "//third_party/abseil-cpp/absl/memory", ] if (!build_with_chromium && is_clang) { @@ -216,6 +218,7 @@ rtc_source_set("p2p_server_utils") { "../rtc_base:rtc_base", "../rtc_base:rtc_base_tests_utils", "../rtc_base/third_party/sigslot", + "//third_party/abseil-cpp/absl/algorithm:container", "//third_party/abseil-cpp/absl/memory", ] } diff --git a/p2p/base/fake_ice_transport.h b/p2p/base/fake_ice_transport.h index 45fe6f352c..1b95a55c42 100644 --- a/p2p/base/fake_ice_transport.h +++ b/p2p/base/fake_ice_transport.h @@ -15,6 +15,7 @@ #include #include +#include "absl/algorithm/container.h" #include "absl/types/optional.h" #include "p2p/base/ice_transport_internal.h" #include "rtc_base/async_invoker.h" @@ -181,8 +182,7 @@ class FakeIceTransport : public IceTransportInternal { remote_candidates_.push_back(candidate); } void RemoveRemoteCandidate(const Candidate& candidate) override { - auto it = std::find(remote_candidates_.begin(), remote_candidates_.end(), - candidate); + auto it = absl::c_find(remote_candidates_, candidate); if (it == remote_candidates_.end()) { RTC_LOG(LS_INFO) << "Trying to remove a candidate which doesn't exist."; return; diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc index 89f919913e..5d1b73f92b 100644 --- a/p2p/base/p2p_transport_channel.cc +++ b/p2p/base/p2p_transport_channel.cc @@ -1095,10 +1095,10 @@ P2PTransportChannel::CandidateAndResolver::~CandidateAndResolver() {} void P2PTransportChannel::OnCandidateResolved( rtc::AsyncResolverInterface* resolver) { - auto p = std::find_if(resolvers_.begin(), resolvers_.end(), - [resolver](const CandidateAndResolver& cr) { - return cr.resolver_ == resolver; - }); + auto p = + absl::c_find_if(resolvers_, [resolver](const CandidateAndResolver& cr) { + return cr.resolver_ == resolver; + }); if (p == resolvers_.end()) { RTC_LOG(LS_ERROR) << "Unexpected AsyncResolver signal"; RTC_NOTREACHED(); @@ -1211,8 +1211,7 @@ bool P2PTransportChannel::CreateConnections(const Candidate& remote_candidate, } } - if ((origin_port != NULL) && - std::find(ports_.begin(), ports_.end(), origin_port) == ports_.end()) { + if ((origin_port != NULL) && !absl::c_linear_search(ports_, origin_port)) { if (CreateConnection(origin_port, remote_candidate, origin_port)) created = true; } @@ -1268,9 +1267,7 @@ bool P2PTransportChannel::CreateConnection(PortInterface* port, } bool P2PTransportChannel::FindConnection(Connection* connection) const { - std::vector::const_iterator citer = - std::find(connections_.begin(), connections_.end(), connection); - return citer != connections_.end(); + return absl::c_linear_search(connections_, connection); } uint32_t P2PTransportChannel::GetRemoteCandidateGeneration( @@ -1621,12 +1618,11 @@ int P2PTransportChannel::CompareConnectionCandidates( } bool P2PTransportChannel::IsPortPruned(const Port* port) const { - return std::find(ports_.begin(), ports_.end(), port) == ports_.end(); + return !absl::c_linear_search(ports_, port); } bool P2PTransportChannel::IsRemoteCandidatePruned(const Candidate& cand) const { - return std::find(remote_candidates_.begin(), remote_candidates_.end(), cand) - == remote_candidates_.end(); + return !absl::c_linear_search(remote_candidates_, cand); } int P2PTransportChannel::CompareConnections( @@ -1694,15 +1690,15 @@ void P2PTransportChannel::SortConnectionsAndUpdateState( // one whose estimated latency is lowest. So it is the only one that we // need to consider switching to. // TODO(honghaiz): Don't sort; Just use std::max_element in the right places. - std::stable_sort(connections_.begin(), connections_.end(), - [this](const Connection* a, const Connection* b) { - int cmp = CompareConnections(a, b, absl::nullopt, nullptr); - if (cmp != 0) { - return cmp > 0; - } - // Otherwise, sort based on latency estimate. - return a->rtt() < b->rtt(); - }); + absl::c_stable_sort( + connections_, [this](const Connection* a, const Connection* b) { + int cmp = CompareConnections(a, b, absl::nullopt, nullptr); + if (cmp != 0) { + return cmp > 0; + } + // Otherwise, sort based on latency estimate. + return a->rtt() < b->rtt(); + }); RTC_LOG(LS_VERBOSE) << "Sorting " << connections_.size() << " available connections"; @@ -2124,19 +2120,17 @@ Connection* P2PTransportChannel::FindNextPingableConnection() { // Rule 2.1: Among such connections, pick the one with the earliest // last-ping-sent time. if (weak()) { - auto selectable_connections = GetBestWritableConnectionPerNetwork(); std::vector pingable_selectable_connections; - std::copy_if(selectable_connections.begin(), selectable_connections.end(), - std::back_inserter(pingable_selectable_connections), - [this, now](Connection* conn) { - return WritableConnectionPastPingInterval(conn, now); - }); - auto iter = std::min_element(pingable_selectable_connections.begin(), - pingable_selectable_connections.end(), - [](Connection* conn1, Connection* conn2) { - return conn1->last_ping_sent() < - conn2->last_ping_sent(); - }); + absl::c_copy_if(GetBestWritableConnectionPerNetwork(), + std::back_inserter(pingable_selectable_connections), + [this, now](Connection* conn) { + return WritableConnectionPastPingInterval(conn, now); + }); + auto iter = absl::c_min_element(pingable_selectable_connections, + [](Connection* conn1, Connection* conn2) { + return conn1->last_ping_sent() < + conn2->last_ping_sent(); + }); if (iter != pingable_selectable_connections.end()) { return *iter; } @@ -2157,10 +2151,9 @@ Connection* P2PTransportChannel::FindNextPingableConnection() { // Otherwise, treat everything as unpinged. // TODO(honghaiz): Instead of adding two separate vectors, we can add a state // "pinged" to filter out unpinged connections. - if (std::find_if(unpinged_connections_.begin(), unpinged_connections_.end(), - [this, now](Connection* conn) { - return this->IsPingable(conn, now); - }) == unpinged_connections_.end()) { + if (absl::c_none_of(unpinged_connections_, [this, now](Connection* conn) { + return this->IsPingable(conn, now); + })) { unpinged_connections_.insert(pinged_connections_.begin(), pinged_connections_.end()); pinged_connections_.clear(); @@ -2168,19 +2161,18 @@ Connection* P2PTransportChannel::FindNextPingableConnection() { // Among un-pinged pingable connections, "more pingable" takes precedence. std::vector pingable_connections; - std::copy_if(unpinged_connections_.begin(), unpinged_connections_.end(), - std::back_inserter(pingable_connections), - [this, now](Connection* conn) { return IsPingable(conn, now); }); - auto iter = - std::max_element(pingable_connections.begin(), pingable_connections.end(), - [this](Connection* conn1, Connection* conn2) { - // Some implementations of max_element compare an - // element with itself. - if (conn1 == conn2) { - return false; - } - return MorePingable(conn1, conn2) == conn2; - }); + absl::c_copy_if( + unpinged_connections_, std::back_inserter(pingable_connections), + [this, now](Connection* conn) { return IsPingable(conn, now); }); + auto iter = absl::c_max_element(pingable_connections, + [this](Connection* conn1, Connection* conn2) { + // Some implementations of max_element + // compare an element with itself. + if (conn1 == conn2) { + return false; + } + return MorePingable(conn1, conn2) == conn2; + }); if (iter != pingable_connections.end()) { return *iter; } @@ -2289,11 +2281,10 @@ void P2PTransportChannel::OnConnectionDestroyed(Connection* connection) { // use it. // Remove this connection from the list. - std::vector::iterator iter = - std::find(connections_.begin(), connections_.end(), connection); + auto iter = absl::c_find(connections_, connection); RTC_DCHECK(iter != connections_.end()); - pinged_connections_.erase(*iter); - unpinged_connections_.erase(*iter); + pinged_connections_.erase(connection); + unpinged_connections_.erase(connection); connections_.erase(iter); RTC_LOG(LS_INFO) << ToString() << ": Removed connection " << connection @@ -2367,7 +2358,7 @@ void P2PTransportChannel::PruneAllPorts() { } bool P2PTransportChannel::PrunePort(PortInterface* port) { - auto it = std::find(ports_.begin(), ports_.end(), port); + auto it = absl::c_find(ports_, port); // Don't need to do anything if the port has been deleted from the port list. if (it == ports_.end()) { return false; @@ -2487,10 +2478,9 @@ Connection* P2PTransportChannel::MorePingable(Connection* conn1, // During the initial state when nothing has been pinged yet, return the first // one in the ordered |connections_|. - return *(std::find_if(connections_.begin(), connections_.end(), - [conn1, conn2](Connection* conn) { - return conn == conn1 || conn == conn2; - })); + return *(absl::c_find_if(connections_, [conn1, conn2](Connection* conn) { + return conn == conn1 || conn == conn2; + })); } void P2PTransportChannel::SetWritable(bool writable) { diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc index 10b07b149a..acd66c196d 100644 --- a/p2p/base/p2p_transport_channel_unittest.cc +++ b/p2p/base/p2p_transport_channel_unittest.cc @@ -8,7 +8,6 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include #include #include @@ -44,6 +43,7 @@ namespace { using rtc::SocketAddress; using ::testing::_; using ::testing::Assign; +using ::testing::Contains; using ::testing::DoAll; using ::testing::InSequence; using ::testing::InvokeWithoutArgs; @@ -1989,12 +1989,10 @@ TEST_F(P2PTransportChannelTest, TestUsingPooledSessionBeforeDoneGathering) { // pooled sessions. auto pooled_ports_1 = pooled_session_1->ReadyPorts(); auto pooled_ports_2 = pooled_session_2->ReadyPorts(); - EXPECT_NE(pooled_ports_1.end(), - std::find(pooled_ports_1.begin(), pooled_ports_1.end(), - ep1_ch1()->selected_connection()->port())); - EXPECT_NE(pooled_ports_2.end(), - std::find(pooled_ports_2.begin(), pooled_ports_2.end(), - ep2_ch1()->selected_connection()->port())); + EXPECT_THAT(pooled_ports_1, + Contains(ep1_ch1()->selected_connection()->port())); + EXPECT_THAT(pooled_ports_2, + Contains(ep2_ch1()->selected_connection()->port())); } // Test that a connection succeeds when the P2PTransportChannel uses a pooled @@ -2034,12 +2032,10 @@ TEST_F(P2PTransportChannelTest, TestUsingPooledSessionAfterDoneGathering) { // pooled sessions. auto pooled_ports_1 = pooled_session_1->ReadyPorts(); auto pooled_ports_2 = pooled_session_2->ReadyPorts(); - EXPECT_NE(pooled_ports_1.end(), - std::find(pooled_ports_1.begin(), pooled_ports_1.end(), - ep1_ch1()->selected_connection()->port())); - EXPECT_NE(pooled_ports_2.end(), - std::find(pooled_ports_2.begin(), pooled_ports_2.end(), - ep2_ch1()->selected_connection()->port())); + EXPECT_THAT(pooled_ports_1, + Contains(ep1_ch1()->selected_connection()->port())); + EXPECT_THAT(pooled_ports_2, + Contains(ep2_ch1()->selected_connection()->port())); } // Test that when the "presume_writable_when_fully_relayed" flag is set to diff --git a/p2p/base/port.cc b/p2p/base/port.cc index 1977408d29..238becbf7b 100644 --- a/p2p/base/port.cc +++ b/p2p/base/port.cc @@ -16,6 +16,7 @@ #include #include +#include "absl/algorithm/container.h" #include "absl/memory/memory.h" #include "absl/strings/match.h" #include "p2p/base/port_allocator.h" @@ -1526,8 +1527,8 @@ void Connection::ReceivedPingResponse(int rtt, const std::string& request_id) { // So if we're not already, become writable. We may be bringing a pruned // connection back to life, but if we don't really want it, we can always // prune it again. - auto iter = std::find_if( - pings_since_last_response_.begin(), pings_since_last_response_.end(), + auto iter = absl::c_find_if( + pings_since_last_response_, [request_id](const SentPing& ping) { return ping.id == request_id; }); if (iter != pings_since_last_response_.end() && iter->nomination > acked_nomination_) { diff --git a/p2p/base/relay_server.cc b/p2p/base/relay_server.cc index 80b51b228b..7214fa1022 100644 --- a/p2p/base/relay_server.cc +++ b/p2p/base/relay_server.cc @@ -17,6 +17,7 @@ #include #include +#include "absl/algorithm/container.h" #include "rtc_base/async_tcp_socket.h" #include "rtc_base/checks.h" #include "rtc_base/helpers.h" @@ -107,16 +108,13 @@ RelayServer::~RelayServer() { } void RelayServer::AddInternalSocket(rtc::AsyncPacketSocket* socket) { - RTC_DCHECK(internal_sockets_.end() == std::find(internal_sockets_.begin(), - internal_sockets_.end(), - socket)); + RTC_DCHECK(!absl::c_linear_search(internal_sockets_, socket)); internal_sockets_.push_back(socket); socket->SignalReadPacket.connect(this, &RelayServer::OnInternalPacket); } void RelayServer::RemoveInternalSocket(rtc::AsyncPacketSocket* socket) { - auto iter = - std::find(internal_sockets_.begin(), internal_sockets_.end(), socket); + auto iter = absl::c_find(internal_sockets_, socket); RTC_DCHECK(iter != internal_sockets_.end()); internal_sockets_.erase(iter); removed_sockets_.push_back(socket); @@ -124,16 +122,13 @@ void RelayServer::RemoveInternalSocket(rtc::AsyncPacketSocket* socket) { } void RelayServer::AddExternalSocket(rtc::AsyncPacketSocket* socket) { - RTC_DCHECK(external_sockets_.end() == std::find(external_sockets_.begin(), - external_sockets_.end(), - socket)); + RTC_DCHECK(!absl::c_linear_search(external_sockets_, socket)); external_sockets_.push_back(socket); socket->SignalReadPacket.connect(this, &RelayServer::OnExternalPacket); } void RelayServer::RemoveExternalSocket(rtc::AsyncPacketSocket* socket) { - auto iter = - std::find(external_sockets_.begin(), external_sockets_.end(), socket); + auto iter = absl::c_find(external_sockets_, socket); RTC_DCHECK(iter != external_sockets_.end()); external_sockets_.erase(iter); removed_sockets_.push_back(socket); diff --git a/p2p/base/tcp_port.cc b/p2p/base/tcp_port.cc index af49389364..f98322a9d9 100644 --- a/p2p/base/tcp_port.cc +++ b/p2p/base/tcp_port.cc @@ -67,9 +67,9 @@ #include "p2p/base/tcp_port.h" #include -#include #include +#include "absl/algorithm/container.h" #include "p2p/base/p2p_constants.h" #include "rtc_base/checks.h" #include "rtc_base/ip_address.h" @@ -357,13 +357,10 @@ TCPConnection::TCPConnection(TCPPort* port, RTC_LOG(LS_VERBOSE) << ToString() << ": socket ipaddr: " << socket_->GetLocalAddress().ToString() << ", port() Network:" << port->Network()->ToString(); - const std::vector& desired_addresses = - port_->Network()->GetIPs(); - RTC_DCHECK(std::find_if(desired_addresses.begin(), desired_addresses.end(), - [this](const rtc::InterfaceAddress& addr) { - return socket_->GetLocalAddress().ipaddr() == - addr; - }) != desired_addresses.end()); + RTC_DCHECK(absl::c_any_of( + port_->Network()->GetIPs(), [this](const rtc::InterfaceAddress& addr) { + return socket_->GetLocalAddress().ipaddr() == addr; + })); ConnectSocketSignals(socket); } } @@ -444,12 +441,10 @@ void TCPConnection::OnConnect(rtc::AsyncPacketSocket* socket) { // Note that, aside from minor differences in log statements, this logic is // identical to that in TurnPort. const rtc::SocketAddress& socket_address = socket->GetLocalAddress(); - const std::vector& desired_addresses = - port_->Network()->GetIPs(); - if (std::find_if(desired_addresses.begin(), desired_addresses.end(), - [socket_address](const rtc::InterfaceAddress& addr) { - return socket_address.ipaddr() == addr; - }) != desired_addresses.end()) { + if (absl::c_any_of(port_->Network()->GetIPs(), + [socket_address](const rtc::InterfaceAddress& addr) { + return socket_address.ipaddr() == addr; + })) { RTC_LOG(LS_VERBOSE) << ToString() << ": Connection established to " << socket->GetRemoteAddress().ToSensitiveString(); } else { diff --git a/p2p/base/transport_description.h b/p2p/base/transport_description.h index c3c14a6077..b13defbc2b 100644 --- a/p2p/base/transport_description.h +++ b/p2p/base/transport_description.h @@ -11,11 +11,11 @@ #ifndef P2P_BASE_TRANSPORT_DESCRIPTION_H_ #define P2P_BASE_TRANSPORT_DESCRIPTION_H_ -#include #include #include #include +#include "absl/algorithm/container.h" #include "p2p/base/p2p_constants.h" #include "rtc_base/ssl_fingerprint.h" @@ -104,8 +104,7 @@ struct TransportDescription { // TODO(deadbeef): Rename to HasIceOption, etc. bool HasOption(const std::string& option) const { - return (std::find(transport_options.begin(), transport_options.end(), - option) != transport_options.end()); + return absl::c_linear_search(transport_options, option); } void AddOption(const std::string& option) { transport_options.push_back(option); diff --git a/p2p/base/transport_description_factory_unittest.cc b/p2p/base/transport_description_factory_unittest.cc index af91a212a3..cf04964e14 100644 --- a/p2p/base/transport_description_factory_unittest.cc +++ b/p2p/base/transport_description_factory_unittest.cc @@ -9,7 +9,6 @@ */ #include -#include #include #include #include @@ -22,11 +21,14 @@ #include "rtc_base/ssl_certificate.h" #include "rtc_base/ssl_fingerprint.h" #include "rtc_base/ssl_identity.h" +#include "test/gmock.h" #include "test/gtest.h" -using cricket::TransportDescriptionFactory; using cricket::TransportDescription; +using cricket::TransportDescriptionFactory; using cricket::TransportOptions; +using ::testing::Contains; +using ::testing::Not; class TransportDescriptionFactoryTest : public testing::Test { public: @@ -116,31 +118,30 @@ class TransportDescriptionFactoryTest : public testing::Test { // The initial offer / answer exchange. std::unique_ptr offer = f1_.CreateOffer(options, nullptr, &ice_credentials_); + ASSERT_TRUE(offer); + EXPECT_THAT(offer->transport_options, Not(Contains("renomination"))); + std::unique_ptr answer = f2_.CreateAnswer( offer.get(), options, true, nullptr, &ice_credentials_); - VerifyRenomination(offer.get(), false); - VerifyRenomination(answer.get(), false); + ASSERT_TRUE(answer); + EXPECT_THAT(answer->transport_options, Not(Contains("renomination"))); options.enable_ice_renomination = true; std::unique_ptr renomination_offer = f1_.CreateOffer(options, offer.get(), &ice_credentials_); - VerifyRenomination(renomination_offer.get(), true); + ASSERT_TRUE(renomination_offer); + EXPECT_THAT(renomination_offer->transport_options, + Contains("renomination")); std::unique_ptr renomination_answer = f2_.CreateAnswer(renomination_offer.get(), options, true, answer.get(), &ice_credentials_); - VerifyRenomination(renomination_answer.get(), true); + ASSERT_TRUE(renomination_answer); + EXPECT_THAT(renomination_answer->transport_options, + Contains("renomination")); } protected: - void VerifyRenomination(TransportDescription* desc, - bool renomination_expected) { - ASSERT_TRUE(desc != nullptr); - std::vector& options = desc->transport_options; - auto iter = std::find(options.begin(), options.end(), "renomination"); - EXPECT_EQ(renomination_expected, iter != options.end()); - } - void SetDtls(bool dtls) { if (dtls) { f1_.set_secure(cricket::SEC_ENABLED); diff --git a/p2p/base/turn_port.cc b/p2p/base/turn_port.cc index bfaf45b5e8..6a93090d77 100644 --- a/p2p/base/turn_port.cc +++ b/p2p/base/turn_port.cc @@ -10,11 +10,11 @@ #include "p2p/base/turn_port.h" -#include #include #include #include +#include "absl/algorithm/container.h" #include "absl/memory/memory.h" #include "absl/types/optional.h" #include "p2p/base/stun.h" @@ -434,12 +434,10 @@ void TurnPort::OnSocketConnect(rtc::AsyncPacketSocket* socket) { // Note that, aside from minor differences in log statements, this logic is // identical to that in TcpPort. const rtc::SocketAddress& socket_address = socket->GetLocalAddress(); - const std::vector& desired_addresses = - Network()->GetIPs(); - if (std::find_if(desired_addresses.begin(), desired_addresses.end(), - [socket_address](const rtc::InterfaceAddress& addr) { - return socket_address.ipaddr() == addr; - }) == desired_addresses.end()) { + if (absl::c_none_of(Network()->GetIPs(), + [socket_address](const rtc::InterfaceAddress& addr) { + return socket_address.ipaddr() == addr; + })) { if (socket->GetLocalAddress().IsLoopbackIP()) { RTC_LOG(LS_WARNING) << "Socket is bound to the address:" << socket_address.ipaddr().ToString() @@ -1121,30 +1119,26 @@ void TurnPort::ResetNonce() { } bool TurnPort::HasPermission(const rtc::IPAddress& ipaddr) const { - return (std::find_if(entries_.begin(), entries_.end(), - [&ipaddr](const TurnEntry* e) { - return e->address().ipaddr() == ipaddr; - }) != entries_.end()); + return absl::c_any_of(entries_, [&ipaddr](const TurnEntry* e) { + return e->address().ipaddr() == ipaddr; + }); } TurnEntry* TurnPort::FindEntry(const rtc::SocketAddress& addr) const { - auto it = std::find_if( - entries_.begin(), entries_.end(), - [&addr](const TurnEntry* e) { return e->address() == addr; }); + auto it = absl::c_find_if( + entries_, [&addr](const TurnEntry* e) { return e->address() == addr; }); return (it != entries_.end()) ? *it : NULL; } TurnEntry* TurnPort::FindEntry(int channel_id) const { - auto it = std::find_if(entries_.begin(), entries_.end(), - [&channel_id](const TurnEntry* e) { - return e->channel_id() == channel_id; - }); + auto it = absl::c_find_if(entries_, [&channel_id](const TurnEntry* e) { + return e->channel_id() == channel_id; + }); return (it != entries_.end()) ? *it : NULL; } bool TurnPort::EntryExists(TurnEntry* e) { - auto it = std::find(entries_.begin(), entries_.end(), e); - return it != entries_.end(); + return absl::c_linear_search(entries_, e); } bool TurnPort::CreateOrRefreshEntry(const rtc::SocketAddress& addr, diff --git a/p2p/base/turn_server.cc b/p2p/base/turn_server.cc index 62ef5d98ee..1b2903d342 100644 --- a/p2p/base/turn_server.cc +++ b/p2p/base/turn_server.cc @@ -13,6 +13,7 @@ #include // for std::tie #include +#include "absl/algorithm/container.h" #include "absl/memory/memory.h" #include "p2p/base/async_stun_tcp_socket.h" #include "p2p/base/packet_socket_factory.h" @@ -959,14 +960,13 @@ void TurnServerAllocation::OnMessage(rtc::Message* msg) { } void TurnServerAllocation::OnPermissionDestroyed(Permission* perm) { - PermissionList::iterator it = std::find(perms_.begin(), perms_.end(), perm); + auto it = absl::c_find(perms_, perm); RTC_DCHECK(it != perms_.end()); perms_.erase(it); } void TurnServerAllocation::OnChannelDestroyed(Channel* channel) { - ChannelList::iterator it = - std::find(channels_.begin(), channels_.end(), channel); + auto it = absl::c_find(channels_, channel); RTC_DCHECK(it != channels_.end()); channels_.erase(it); } diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc index 6dc5dfc318..83c8bf2b95 100644 --- a/p2p/client/basic_port_allocator.cc +++ b/p2p/client/basic_port_allocator.cc @@ -16,6 +16,7 @@ #include #include +#include "absl/algorithm/container.h" #include "p2p/base/basic_packet_socket_factory.h" #include "p2p/base/port.h" #include "p2p/base/relay_port.h" @@ -313,13 +314,12 @@ void BasicPortAllocatorSession::SetCandidateFilter(uint32_t filter) { if (!port.has_pairable_candidate()) { continue; } - const auto& candidates = port.port()->Candidates(); // Setting a filter may cause a ready port to become non-ready // if it no longer has any pairable candidates. - if (!std::any_of(candidates.begin(), candidates.end(), - [this, &port](const Candidate& candidate) { - return CandidatePairable(candidate, port.port()); - })) { + if (absl::c_none_of(port.port()->Candidates(), + [this, &port](const Candidate& candidate) { + return CandidatePairable(candidate, port.port()); + })) { port.set_has_pairable_candidate(false); } } @@ -417,8 +417,7 @@ void BasicPortAllocatorSession::RegatherOnFailedNetworks() { // regathers ports and candidates. for (AllocationSequence* sequence : sequences_) { if (!sequence->network_failed() && - std::find(failed_networks.begin(), failed_networks.end(), - sequence->network()) != failed_networks.end()) { + absl::c_linear_search(failed_networks, sequence->network())) { sequence->set_network_failed(); } } @@ -553,18 +552,17 @@ bool BasicPortAllocatorSession::CandidatesAllocationDone() const { } // Check that all port allocation sequences are complete (not running). - if (std::any_of(sequences_.begin(), sequences_.end(), - [](const AllocationSequence* sequence) { - return sequence->state() == AllocationSequence::kRunning; - })) { + if (absl::c_any_of(sequences_, [](const AllocationSequence* sequence) { + return sequence->state() == AllocationSequence::kRunning; + })) { return false; } // If all allocated ports are no longer gathering, session must have got all // expected candidates. Session will trigger candidates allocation complete // signal. - return std::none_of(ports_.begin(), ports_.end(), - [](const PortData& port) { return port.inprogress(); }); + return absl::c_none_of( + ports_, [](const PortData& port) { return port.inprogress(); }); } void BasicPortAllocatorSession::OnMessage(rtc::Message* message) { @@ -828,8 +826,7 @@ void BasicPortAllocatorSession::OnNetworksChanged() { // Mark the sequence as "network failed" if its network is not in // |networks|. if (!sequence->network_failed() && - std::find(networks.begin(), networks.end(), sequence->network()) == - networks.end()) { + !absl::c_linear_search(networks, sequence->network())) { sequence->OnNetworkFailed(); failed_networks.push_back(sequence->network()); } @@ -1159,8 +1156,7 @@ BasicPortAllocatorSession::GetUnprunedPorts( std::vector unpruned_ports; for (PortData& port : ports_) { if (!port.pruned() && - std::find(networks.begin(), networks.end(), - port.sequence()->network()) != networks.end()) { + absl::c_linear_search(networks, port.sequence()->network())) { unpruned_ports.push_back(&port); } } @@ -1263,18 +1259,20 @@ void AllocationSequence::DisableEquivalentPhases(rtc::Network* network, // This can happen if, say, there's a network change event right before an // application-triggered ICE restart. Hopefully this problem will just go // away if we get rid of the gathering "phases" though, which is planned. - if (std::any_of(session_->ports_.begin(), session_->ports_.end(), - [this](const BasicPortAllocatorSession::PortData& p) { - return p.port()->Network() == network_ && - p.port()->GetProtocol() == PROTO_UDP && !p.error(); - })) { + if (absl::c_any_of(session_->ports_, + [this](const BasicPortAllocatorSession::PortData& p) { + return p.port()->Network() == network_ && + p.port()->GetProtocol() == PROTO_UDP && + !p.error(); + })) { *flags |= PORTALLOCATOR_DISABLE_UDP; } - if (std::any_of(session_->ports_.begin(), session_->ports_.end(), - [this](const BasicPortAllocatorSession::PortData& p) { - return p.port()->Network() == network_ && - p.port()->GetProtocol() == PROTO_TCP && !p.error(); - })) { + if (absl::c_any_of(session_->ports_, + [this](const BasicPortAllocatorSession::PortData& p) { + return p.port()->Network() == network_ && + p.port()->GetProtocol() == PROTO_TCP && + !p.error(); + })) { *flags |= PORTALLOCATOR_DISABLE_TCP; } @@ -1619,7 +1617,7 @@ void AllocationSequence::OnPortDestroyed(PortInterface* port) { return; } - auto it = std::find(relay_ports_.begin(), relay_ports_.end(), port); + auto it = absl::c_find(relay_ports_, port); if (it != relay_ports_.end()) { relay_ports_.erase(it); } else { diff --git a/p2p/client/basic_port_allocator_unittest.cc b/p2p/client/basic_port_allocator_unittest.cc index 5ecb81bb4d..e9fd5060b5 100644 --- a/p2p/client/basic_port_allocator_unittest.cc +++ b/p2p/client/basic_port_allocator_unittest.cc @@ -8,10 +8,10 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include #include #include // no-presubmit-check TODO(webrtc:8982) +#include "absl/algorithm/container.h" #include "p2p/base/basic_packet_socket_factory.h" #include "p2p/base/p2p_constants.h" #include "p2p/base/stun_port.h" @@ -41,10 +41,13 @@ #include "rtc_base/thread.h" #include "rtc_base/virtual_socket_server.h" #include "system_wrappers/include/metrics.h" +#include "test/gmock.h" #include "test/gtest.h" using rtc::IPAddress; using rtc::SocketAddress; +using ::testing::Contains; +using ::testing::Not; #define MAYBE_SKIP_IPV4 \ if (!rtc::HasIPv4Enabled()) { \ @@ -310,9 +313,8 @@ class BasicPortAllocatorTestBase : public testing::Test, const std::string& type, ProtocolType protocol, const SocketAddress& client_addr) { - return std::count_if( - ports.begin(), ports.end(), - [type, protocol, client_addr](PortInterface* port) { + return absl::c_count_if( + ports, [type, protocol, client_addr](PortInterface* port) { return port->Type() == type && port->GetProtocol() == protocol && port->Network()->GetBestIP() == client_addr.ipaddr(); }); @@ -322,11 +324,11 @@ class BasicPortAllocatorTestBase : public testing::Test, const std::string& type, const std::string& proto, const SocketAddress& addr) { - return std::count_if(candidates.begin(), candidates.end(), - [type, proto, addr](const Candidate& c) { - return c.type() == type && c.protocol() == proto && - AddressMatch(c.address(), addr); - }); + return absl::c_count_if( + candidates, [type, proto, addr](const Candidate& c) { + return c.type() == type && c.protocol() == proto && + AddressMatch(c.address(), addr); + }); } // Find a candidate and return it. @@ -335,11 +337,11 @@ class BasicPortAllocatorTestBase : public testing::Test, const std::string& proto, const SocketAddress& addr, Candidate* found) { - auto it = std::find_if(candidates.begin(), candidates.end(), - [type, proto, addr](const Candidate& c) { - return c.type() == type && c.protocol() == proto && - AddressMatch(c.address(), addr); - }); + auto it = + absl::c_find_if(candidates, [type, proto, addr](const Candidate& c) { + return c.type() == type && c.protocol() == proto && + AddressMatch(c.address(), addr); + }); if (it != candidates.end() && found) { *found = *it; } @@ -361,14 +363,12 @@ class BasicPortAllocatorTestBase : public testing::Test, const std::string& proto, const SocketAddress& addr, const SocketAddress& related_addr) { - auto it = - std::find_if(candidates.begin(), candidates.end(), - [type, proto, addr, related_addr](const Candidate& c) { - return c.type() == type && c.protocol() == proto && - AddressMatch(c.address(), addr) && - AddressMatch(c.related_address(), related_addr); - }); - return it != candidates.end(); + return absl::c_any_of( + candidates, [type, proto, addr, related_addr](const Candidate& c) { + return c.type() == type && c.protocol() == proto && + AddressMatch(c.address(), addr) && + AddressMatch(c.related_address(), related_addr); + }); } static bool CheckPort(const rtc::SocketAddress& addr, @@ -414,8 +414,7 @@ class BasicPortAllocatorTestBase : public testing::Test, ports_.push_back(port); // Make sure the new port is added to ReadyPorts. auto ready_ports = ses->ReadyPorts(); - EXPECT_NE(ready_ports.end(), - std::find(ready_ports.begin(), ready_ports.end(), port)); + EXPECT_THAT(ready_ports, Contains(port)); } void OnPortsPruned(PortAllocatorSession* ses, const std::vector& pruned_ports) { @@ -425,8 +424,7 @@ class BasicPortAllocatorTestBase : public testing::Test, for (PortInterface* port : pruned_ports) { new_end = std::remove(ports_.begin(), new_end, port); // Make sure the pruned port is not in ReadyPorts. - EXPECT_EQ(ready_ports.end(), - std::find(ready_ports.begin(), ready_ports.end(), port)); + EXPECT_THAT(ready_ports, Not(Contains(port))); } ports_.erase(new_end, ports_.end()); } @@ -442,9 +440,7 @@ class BasicPortAllocatorTestBase : public testing::Test, // Make sure the new candidates are added to Candidates. auto ses_candidates = ses->ReadyCandidates(); for (const Candidate& candidate : candidates) { - EXPECT_NE( - ses_candidates.end(), - std::find(ses_candidates.begin(), ses_candidates.end(), candidate)); + EXPECT_THAT(ses_candidates, Contains(candidate)); } }