From cce7cf59b95ff6db2d30462be175666e57627ebc Mon Sep 17 00:00:00 2001 From: Janusz Sobczak Date: Thu, 10 Aug 2023 14:16:19 -0700 Subject: [PATCH] Replace std::function with AnyInvocable PiperOrigin-RevId: 555650115 --- connections/implementation/mediums/BUILD | 1 + connections/implementation/mediums/webrtc.cc | 7 +- connections/implementation/mediums/webrtc.h | 13 ++-- .../implementation/mediums/webrtc_test.cc | 66 +++++++++---------- .../implementation/webrtc_bwu_handler.cc | 11 ++-- 5 files changed, 47 insertions(+), 51 deletions(-) diff --git a/connections/implementation/mediums/BUILD b/connections/implementation/mediums/BUILD index 2ef5be09..554b8288 100644 --- a/connections/implementation/mediums/BUILD +++ b/connections/implementation/mediums/BUILD @@ -62,6 +62,7 @@ cc_library( "@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/functional:bind_front", "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", diff --git a/connections/implementation/mediums/webrtc.cc b/connections/implementation/mediums/webrtc.cc index e02a81c2..21ad6e8c 100644 --- a/connections/implementation/mediums/webrtc.cc +++ b/connections/implementation/mediums/webrtc.cc @@ -107,7 +107,7 @@ bool WebRtc::StartAcceptingConnections(const std::string& service_id, // who may be also using WebRTC. AcceptingConnectionsInfo info = AcceptingConnectionsInfo(); info.self_peer_id = self_peer_id; - info.accepted_connection_callback = callback; + info.accepted_connection_callback = std::move(callback); // Create a new SignalingMessenger so that we can communicate w/ Tachyon. info.signaling_messenger = @@ -688,8 +688,9 @@ void WebRtc::ProcessDataChannelOpen(const std::string& service_id, const auto& accepting_connection_entry = accepting_connections_info_.find(service_id); - if (accepting_connection_entry != accepting_connections_info_.end()) { - accepting_connection_entry->second.accepted_connection_callback.accepted_cb( + if (accepting_connection_entry != accepting_connections_info_.end() && + accepting_connection_entry->second.accepted_connection_callback) { + accepting_connection_entry->second.accepted_connection_callback( service_id, socket_wrapper); return; } diff --git a/connections/implementation/mediums/webrtc.h b/connections/implementation/mediums/webrtc.h index 091cafb1..8e8eef39 100644 --- a/connections/implementation/mediums/webrtc.h +++ b/connections/implementation/mediums/webrtc.h @@ -17,13 +17,12 @@ #ifndef NO_WEBRTC -#include -#include #include #include #include #include "absl/container/flat_hash_map.h" +#include "absl/functional/any_invocable.h" #include "connections/implementation/mediums/webrtc/connection_flow.h" #include "connections/implementation/mediums/webrtc_peer_id.h" #include "connections/implementation/mediums/webrtc_socket.h" @@ -42,15 +41,13 @@ namespace nearby { namespace connections { namespace mediums { -// Callback that is invoked when a new connection is accepted. -struct AcceptedConnectionCallback { - std::function - accepted_cb = [](const std::string&, WebRtcSocketWrapper) {}; -}; - // Entry point for connecting a data channel between two devices via WebRtc. class WebRtc { public: + // Callback that is invoked when a new connection is accepted. + using AcceptedConnectionCallback = absl::AnyInvocable; + WebRtc(); ~WebRtc(); diff --git a/connections/implementation/mediums/webrtc_test.cc b/connections/implementation/mediums/webrtc_test.cc index 3b6bb0ca..3ab47b00 100644 --- a/connections/implementation/mediums/webrtc_test.cc +++ b/connections/implementation/mediums/webrtc_test.cc @@ -80,11 +80,11 @@ TEST_P(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) { receiver.StartAcceptingConnections( service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); CancellationFlag flag; sender_socket = sender.Connect(service_id, self_id, location_hint, &flag); @@ -119,11 +119,11 @@ TEST_P(WebRtcTest, CanCancelConnect) { receiver.StartAcceptingConnections( service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); CancellationFlag flag(true); sender_socket = sender.Connect(service_id, self_id, location_hint, &flag); @@ -173,10 +173,10 @@ TEST_F(WebRtcTest, StartAcceptingConnectionTwice) { ASSERT_TRUE(webrtc.IsAvailable()); ASSERT_TRUE(webrtc.StartAcceptingConnections( service_id, self_id, location_hint, - {mock_accepted_callback_.AsStdFunction()})); + mock_accepted_callback_.AsStdFunction())); EXPECT_FALSE(webrtc.StartAcceptingConnections( service_id, self_id, location_hint, - {mock_accepted_callback_.AsStdFunction()})); + mock_accepted_callback_.AsStdFunction())); EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id)); EXPECT_FALSE(webrtc.IsAcceptingConnections(std::string{})); env_.Stop(); @@ -197,8 +197,8 @@ TEST_F(WebRtcTest, Connect_NoPeer) { webrtc.Connect(service_id, peer_id, location_hint, &flag); EXPECT_FALSE(wrapper_1.IsValid()); - EXPECT_TRUE(webrtc.StartAcceptingConnections( - service_id, peer_id, location_hint, AcceptedConnectionCallback())); + EXPECT_TRUE(webrtc.StartAcceptingConnections(service_id, peer_id, + location_hint, nullptr)); env_.Stop(); } @@ -215,7 +215,7 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) { ASSERT_TRUE(webrtc.IsAvailable()); ASSERT_TRUE(webrtc.StartAcceptingConnections( service_id, self_id, location_hint, - {mock_accepted_callback_.AsStdFunction()})); + mock_accepted_callback_.AsStdFunction())); CancellationFlag flag; WebRtcSocketWrapper wrapper = webrtc.Connect( service_id, WebrtcPeerId("random_peer_id"), location_hint, &flag); @@ -223,7 +223,7 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) { EXPECT_FALSE(wrapper.IsValid()); EXPECT_FALSE(webrtc.StartAcceptingConnections( service_id, self_id, location_hint, - {mock_accepted_callback_.AsStdFunction()})); + mock_accepted_callback_.AsStdFunction())); env_.Stop(); } @@ -240,7 +240,7 @@ TEST_F(WebRtcTest, StartAndStopAcceptingConnections) { ASSERT_TRUE(webrtc.IsAvailable()); ASSERT_TRUE(webrtc.StartAcceptingConnections( service_id, self_id, location_hint, - {mock_accepted_callback_.AsStdFunction()})); + mock_accepted_callback_.AsStdFunction())); EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id)); webrtc.StopAcceptingConnections(service_id); EXPECT_FALSE(webrtc.IsAcceptingConnections(service_id)); @@ -261,15 +261,15 @@ TEST_F(WebRtcTest, ConnectTwice) { receiver.StartAcceptingConnections( service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); device_c.StartAcceptingConnections( service_id, other_id, location_hint, - {[](const std::string& service_id, WebRtcSocketWrapper wrapper) {}}); + [](const std::string& service_id, WebRtcSocketWrapper wrapper) {}); CancellationFlag flag; sender_socket = sender.Connect(service_id, self_id, location_hint, &flag); @@ -311,11 +311,11 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) { receiver.StartAcceptingConnections( service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); CancellationFlag flag; sender_socket = sender.Connect(service_id, self_id, location_hint, &flag); @@ -343,11 +343,11 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) { receiver.StartAcceptingConnections( service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); CancellationFlag flag; sender_socket = sender.Connect(service_id, self_id, location_hint, &flag); @@ -399,7 +399,7 @@ TEST_F(WebRtcTest, ContinueAcceptingConnectionsOnComplete) { ASSERT_TRUE(webrtc.IsAvailable()); ASSERT_TRUE(webrtc.StartAcceptingConnections( service_id, self_id, location_hint, - {mock_accepted_callback_.AsStdFunction()})); + mock_accepted_callback_.AsStdFunction())); EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id)); // Simulate a failure in receiving messages stream, WebRtc should restart @@ -450,11 +450,11 @@ TEST_F(WebRtcTest, CancelDuringConnect) { receiver->StartAcceptingConnections( service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); sender_socket = sender->Connect(service_id, self_id, location_hint, &sender_flag); @@ -496,11 +496,11 @@ TEST_F(WebRtcTest, CancelBeforeConnect) { receiver->StartAcceptingConnections( service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); sender_socket = sender->Connect(service_id, self_id, location_hint, &sender_flag); @@ -541,11 +541,11 @@ TEST_F(WebRtcTest, CancelDuringConnect_MultipleConnect) { receiver->StartAcceptingConnections( ns_service_id, self_id, location_hint, - {[&receiver_socket, connected](const std::string& ns_service_id, - WebRtcSocketWrapper wrapper) mutable { + [&receiver_socket, connected](const std::string& ns_service_id, + WebRtcSocketWrapper wrapper) mutable { receiver_socket = wrapper; connected.Set(receiver_socket.IsValid()); - }}); + }); // Simulate a successful connect for the endpoint of NearbySharing. sender_socket = sender->Connect(ns_service_id, self_id, location_hint, &flag); diff --git a/connections/implementation/webrtc_bwu_handler.cc b/connections/implementation/webrtc_bwu_handler.cc index 535d2bc1..1941abca 100644 --- a/connections/implementation/webrtc_bwu_handler.cc +++ b/connections/implementation/webrtc_bwu_handler.cc @@ -20,6 +20,7 @@ #include #include "absl/functional/bind_front.h" +#include "connections/implementation/base_bwu_handler.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/mediums/utils.h" #include "connections/implementation/mediums/webrtc_peer_id.h" @@ -41,8 +42,7 @@ std::string WebrtcBwuHandler::WebrtcIncomingSocket::ToString() { return name_; } WebrtcBwuHandler::WebrtcBwuHandler(Mediums& mediums, BwuNotifications notifications) - : BaseBwuHandler(std::move(notifications)), - mediums_(mediums) {} + : BaseBwuHandler(std::move(notifications)), mediums_(mediums) {} // Called by BWU target. Retrieves a new medium info from incoming message, // and establishes connection over WebRTC using this info. @@ -114,11 +114,8 @@ ByteArray WebrtcBwuHandler::HandleInitializeUpgradedMediumForEndpoint( if (!webrtc_.IsAcceptingConnections(upgrade_service_id)) { if (!webrtc_.StartAcceptingConnections( upgrade_service_id, self_id, location_hint, - { - .accepted_cb = absl::bind_front( - &WebrtcBwuHandler::OnIncomingWebrtcConnection, this, - client), - })) { + absl::bind_front(&WebrtcBwuHandler::OnIncomingWebrtcConnection, + this, client))) { NEARBY_LOG(ERROR, "WebRtcBwuHandler couldn't initiate the WEB_RTC upgrade for " "endpoint %s because it failed to start listening for "