diff --git a/connections/c/core_adapter.cc b/connections/c/core_adapter.cc index f8521f6d..920c7aa4 100644 --- a/connections/c/core_adapter.cc +++ b/connections/c/core_adapter.cc @@ -95,9 +95,6 @@ void StartDiscovery(connections::Core *pCore, const char *service_id, if (pCore == nullptr) { return; } - connections::DiscoveryListener discoveryListener = - std::move(*listener.GetImpl()); - connections::DiscoveryOptions discovery_options; if (discovery_options_w.strategy == StrategyW::kNone) @@ -125,7 +122,8 @@ void StartDiscovery(connections::Core *pCore, const char *service_id, discovery_options_w.allowed.wifi_hotspot; discovery_options.allowed.web_rtc = discovery_options_w.allowed.web_rtc; - pCore->StartDiscovery(service_id, discovery_options, discoveryListener, + pCore->StartDiscovery(service_id, discovery_options, + std::move(*listener.GetImpl()), std::move(*callback.GetImpl())); } diff --git a/connections/core.cc b/connections/core.cc index 2dbb44a2..cbca2a82 100644 --- a/connections/core.cc +++ b/connections/core.cc @@ -102,8 +102,8 @@ void Core::StartDiscovery(absl::string_view service_id, CheckServiceId(service_id); CHECK(discovery_options.strategy.IsValid()); - router_->StartDiscovery(&client_, service_id, discovery_options, listener, - std::move(callback)); + router_->StartDiscovery(&client_, service_id, discovery_options, + std::move(listener), std::move(callback)); } void Core::InjectEndpoint(absl::string_view service_id, @@ -302,23 +302,26 @@ void Core::StartDiscoveryV3(absl::string_view service_id, ResultCallback callback) { DiscoveryListener old_listener = { .endpoint_found_cb = - [&listener](const std::string& endpoint_id, - const ByteArray& endpoint_info, - const std::string& service_id) { + [endpoint_found_cb = std::move(listener.endpoint_found_cb)]( + const std::string& endpoint_id, const ByteArray& endpoint_info, + const std::string& service_id) mutable { auto remote_device = v3::ConnectionsDevice( endpoint_id, endpoint_info.AsStringView(), {}); - listener.endpoint_found_cb(remote_device, service_id); + endpoint_found_cb(remote_device, service_id); }, .endpoint_lost_cb = - [&listener](const std::string& endpoint_id) { + [endpoint_lost_cb = std::move(listener.endpoint_lost_cb)]( + const std::string& endpoint_id) mutable { auto remote_device = v3::ConnectionsDevice(endpoint_id, "", {}); - listener.endpoint_lost_cb(remote_device); + endpoint_lost_cb(remote_device); }, .endpoint_distance_changed_cb = - [&listener](const std::string& endpoint_id, - DistanceInfo distance_info) { + [endpoint_distance_changed_cb = + std::move(listener.endpoint_distance_changed_cb)]( + const std::string& endpoint_id, + DistanceInfo distance_info) mutable { auto remote = v3::ConnectionsDevice(endpoint_id, "", {}); - listener.endpoint_distance_changed_cb(remote, distance_info); + endpoint_distance_changed_cb(remote, distance_info); }, }; DiscoveryOptions old_discovery_options = { @@ -333,7 +336,7 @@ void Core::StartDiscoveryV3(absl::string_view service_id, discovery_options.power_level == PowerLevel::kLowPower, }; // TODO(b/291295755): Deeper refactor to use v3 options throughout. - StartDiscovery(service_id, old_discovery_options, old_listener, + StartDiscovery(service_id, old_discovery_options, std::move(old_listener), std::move(callback)); } diff --git a/connections/core_test.cc b/connections/core_test.cc index 5456963c..7fa4dfdd 100644 --- a/connections/core_test.cc +++ b/connections/core_test.cc @@ -158,8 +158,8 @@ TEST(CoreV3Test, TestDiscoveryOptionsConversionWorks) { }); EXPECT_CALL(mock, StartDiscovery) .WillOnce([](ClientProxy*, absl::string_view, - const DiscoveryOptions& options, - const DiscoveryListener& info, ResultCallback) { + const DiscoveryOptions& options, DiscoveryListener, + ResultCallback) { EXPECT_EQ(options.strategy, Strategy::kP2pCluster); EXPECT_FALSE(options.low_power); EXPECT_TRUE(options.auto_upgrade_bandwidth); @@ -411,12 +411,12 @@ TEST(CoreV3Test, TestCallbackWrapWorksStartDiscoveryV3) { MockServiceControllerRouter mock; EXPECT_CALL(mock, StartDiscovery) .WillOnce([&](ClientProxy*, absl::string_view, const DiscoveryOptions&, - const DiscoveryListener& info, const ResultCallback&) { + DiscoveryListener listener, const ResultCallback&) { // call all callbacks to make sure it all gets called correctly. NEARBY_LOGS(INFO) << "StartDiscovery called"; - info.endpoint_distance_changed_cb("FAKE", {}); - info.endpoint_found_cb("FAKE", ByteArray(), ""); - info.endpoint_lost_cb("FAKE"); + listener.endpoint_distance_changed_cb("FAKE", {}); + listener.endpoint_found_cb("FAKE", ByteArray(), ""); + listener.endpoint_lost_cb("FAKE"); }); EXPECT_CALL(mock, StopAllEndpoints) .WillOnce([&](ClientProxy* client, ResultCallback callback) { diff --git a/connections/implementation/base_pcp_handler.cc b/connections/implementation/base_pcp_handler.cc index 41218bbe..efdd7c91 100644 --- a/connections/implementation/base_pcp_handler.cc +++ b/connections/implementation/base_pcp_handler.cc @@ -22,18 +22,20 @@ #include #include "securegcm/ukey2_handshake.h" +#include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_set.h" #include "absl/strings/escaping.h" #include "absl/time/time.h" #include "absl/types/span.h" #include "connections/advertising_options.h" #include "connections/connection_options.h" -#include "connections/implementation/endpoint_channel_manager.h" #include "connections/implementation/client_proxy.h" +#include "connections/implementation/endpoint_channel_manager.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/mediums/utils.h" #include "connections/implementation/offline_frames.h" #include "connections/implementation/proto/offline_wire_formats.pb.h" +#include "connections/listeners.h" #include "connections/medium_selector.h" #include "connections/status.h" #include "connections/v3/connection_listening_options.h" @@ -353,7 +355,7 @@ BooleanMediumSelector BasePcpHandler::ComputeIntersectionOfSupportedMediums( Status BasePcpHandler::StartDiscovery(ClientProxy* client, const std::string& service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener) { + DiscoveryListener listener) { Future response; DiscoveryOptions stripped_discovery_options = discovery_options; StripOutUnavailableMediums(stripped_discovery_options); @@ -362,9 +364,9 @@ Status BasePcpHandler::StartDiscovery(ClientProxy* client, stripped_discovery_options); RunOnPcpHandlerThread( "start-discovery", - [this, client, service_id, stripped_discovery_options, &listener, - &response]() RUN_ON_PCP_HANDLER_THREAD() - ABSL_LOCKS_EXCLUDED(discovered_endpoint_mutex_) { + [this, client, service_id, stripped_discovery_options, + listener = std::move(listener), &response]() RUN_ON_PCP_HANDLER_THREAD() + ABSL_LOCKS_EXCLUDED(discovered_endpoint_mutex_) mutable { // Ask the implementation to attempt to start discovery. auto result = StartDiscoveryImpl(client, service_id, stripped_discovery_options); @@ -379,9 +381,9 @@ Status BasePcpHandler::StartDiscovery(ClientProxy* client, MutexLock lock(&discovered_endpoint_mutex_); discovered_endpoints_.clear(); } - client->StartedDiscovery(service_id, GetStrategy(), listener, - absl::MakeSpan(result.mediums), - stripped_discovery_options); + client->StartedDiscovery( + service_id, GetStrategy(), std::move(listener), + absl::MakeSpan(result.mediums), stripped_discovery_options); response.Set({Status::kSuccess}); }); return WaitForResult(absl::StrCat("StartDiscovery(", service_id, ")"), @@ -1316,8 +1318,8 @@ void BasePcpHandler::OnIncomingFrame( client->SetRemoteSafeToDisconnectVersion( endpoint_id, connection_response.safe_to_disconnect_version()); } - channel_manager_->UpdateSafeToDisconnectForEndpoint(endpoint_id, - client->IsSafeToDisconnectEnabled(endpoint_id)); + channel_manager_->UpdateSafeToDisconnectForEndpoint( + endpoint_id, client->IsSafeToDisconnectEnabled(endpoint_id)); EvaluateConnectionResult(client, endpoint_id, /* can_close_immediately= */ true); @@ -1335,21 +1337,20 @@ void BasePcpHandler::OnEndpointDisconnect(ClientProxy* client, barrier.CountDown(); return; } - RunOnPcpHandlerThread("on-endpoint-disconnect", - [this, client, endpoint_id, barrier, reason]() - RUN_ON_PCP_HANDLER_THREAD() mutable { - auto item = pending_alarms_.find(endpoint_id); - if (item != pending_alarms_.end()) { - auto& alarm = item->second; - alarm->Cancel(); - pending_alarms_.erase(item); - } - ProcessPreConnectionResultFailure( - client, endpoint_id, - /* should_call_disconnect_endpoint= */ false, - reason); - barrier.CountDown(); - }); + RunOnPcpHandlerThread( + "on-endpoint-disconnect", [this, client, endpoint_id, barrier, + reason]() RUN_ON_PCP_HANDLER_THREAD() mutable { + auto item = pending_alarms_.find(endpoint_id); + if (item != pending_alarms_.end()) { + auto& alarm = item->second; + alarm->Cancel(); + pending_alarms_.erase(item); + } + ProcessPreConnectionResultFailure( + client, endpoint_id, + /* should_call_disconnect_endpoint= */ false, reason); + barrier.CountDown(); + }); } BluetoothDevice BasePcpHandler::GetRemoteBluetoothDevice( diff --git a/connections/implementation/base_pcp_handler.h b/connections/implementation/base_pcp_handler.h index b40d6c0d..19b87e80 100644 --- a/connections/implementation/base_pcp_handler.h +++ b/connections/implementation/base_pcp_handler.h @@ -106,7 +106,7 @@ class BasePcpHandler : public PcpHandler, // DiscoveryListener will get called in case of any event. Status StartDiscovery(ClientProxy* client, const std::string& service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener) override; + DiscoveryListener listener) override; // Stops Discovery if it is active, and changes CLientProxy state, // otherwise does nothing. diff --git a/connections/implementation/base_pcp_handler_test.cc b/connections/implementation/base_pcp_handler_test.cc index 2e2831dc..535a1a74 100644 --- a/connections/implementation/base_pcp_handler_test.cc +++ b/connections/implementation/base_pcp_handler_test.cc @@ -471,7 +471,7 @@ class BasePcpHandlerTest pcp_handler->GetMediumsFromSelector(discovery_options.allowed), })); EXPECT_EQ(pcp_handler->StartDiscovery(client, service_id, discovery_options, - discovery_listener_), + GetDiscoveryListener()), Status{Status::kSuccess}); EXPECT_TRUE(client->IsDiscovering()); for (const auto& discovered_medium : @@ -812,14 +812,17 @@ class BasePcpHandlerTest .bandwidth_changed_cb = mock_connection_listener_.bandwidth_changed_cb.AsStdFunction(), }; - DiscoveryListener discovery_listener_{ - .endpoint_found_cb = - mock_discovery_listener_.endpoint_found_cb.AsStdFunction(), - .endpoint_lost_cb = - mock_discovery_listener_.endpoint_lost_cb.AsStdFunction(), - .endpoint_distance_changed_cb = - mock_discovery_listener_.endpoint_distance_changed_cb.AsStdFunction(), - }; + DiscoveryListener GetDiscoveryListener() { + return DiscoveryListener{ + .endpoint_found_cb = + mock_discovery_listener_.endpoint_found_cb.AsStdFunction(), + .endpoint_lost_cb = + mock_discovery_listener_.endpoint_lost_cb.AsStdFunction(), + .endpoint_distance_changed_cb = + mock_discovery_listener_.endpoint_distance_changed_cb + .AsStdFunction(), + }; + } SetSafeToDisconnect set_safe_to_disconnect_{true}; MediumEnvironment& env_ = MediumEnvironment::Instance(); NiceMock mock_device_; @@ -902,7 +905,7 @@ TEST_P(BasePcpHandlerTest, StartDiscoveryFails) { .mediums = {}, })); EXPECT_EQ(pcp_handler.StartDiscovery(&client, "service", discovery_options, - discovery_listener_), + GetDiscoveryListener()), Status{Status::kError}); bwu.Shutdown(); env_.Stop(); @@ -985,7 +988,7 @@ TEST_F(BasePcpHandlerTest, WifiMediumFailFallBackToBT) { })); EXPECT_EQ(pcp_handler.StartDiscovery(&client, service_id, discovery_options, - discovery_listener_), + GetDiscoveryListener()), Status{Status::kSuccess}); EXPECT_TRUE(client.IsDiscovering()); @@ -1566,7 +1569,7 @@ TEST_F(BasePcpHandlerTest, InjectEndpoint) { .mediums = allowed.GetMediums(true), })); EXPECT_EQ(pcp_handler.StartDiscovery(&client, service_id, discovery_options, - discovery_listener_), + GetDiscoveryListener()), Status{Status::kSuccess}); EXPECT_TRUE(client.IsDiscovering()); diff --git a/connections/implementation/client_proxy.cc b/connections/implementation/client_proxy.cc index 441d774d..c86ae99f 100644 --- a/connections/implementation/client_proxy.cc +++ b/connections/implementation/client_proxy.cc @@ -31,6 +31,7 @@ #include "absl/strings/escaping.h" #include "absl/strings/string_view.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h" +#include "connections/listeners.h" #include "connections/v3/bandwidth_info.h" #include "connections/v3/connection_listening_options.h" #include "connections/v3/connections_device_provider.h" @@ -282,11 +283,11 @@ ConnectionListener ClientProxy::GetAdvertisingOrIncomingConnectionListener() { void ClientProxy::StartedDiscovery( const std::string& service_id, Strategy strategy, - const DiscoveryListener& listener, + DiscoveryListener listener, absl::Span mediums, const DiscoveryOptions& discovery_options) { MutexLock lock(&mutex_); - discovery_info_ = DiscoveryInfo{service_id, listener}; + discovery_info_ = DiscoveryInfo{service_id, std::move(listener)}; discovery_options_ = discovery_options; const std::vector medium_vector( @@ -856,7 +857,6 @@ bool ClientProxy::IsPayloadReceivedAckEnabled(absl::string_view endpoint_id) { .min_nc_version_supports_payload_received_ack); } - void ClientProxy::CancelAllEndpoints() { for (const auto& item : cancellation_flags_) { CancellationFlag* cancellation_flag = item.second.get(); diff --git a/connections/implementation/client_proxy.h b/connections/implementation/client_proxy.h index e39ae058..ff6a8c4d 100644 --- a/connections/implementation/client_proxy.h +++ b/connections/implementation/client_proxy.h @@ -110,7 +110,7 @@ class ClientProxy final { // Marks this client as discovering with the given callback. void StartedDiscovery( const std::string& service_id, Strategy strategy, - const DiscoveryListener& discovery_listener, + DiscoveryListener discovery_listener, absl::Span mediums, const DiscoveryOptions& discovery_options = DiscoveryOptions{}); // Marks this client as not discovering at all. diff --git a/connections/implementation/client_proxy_test.cc b/connections/implementation/client_proxy_test.cc index 3d53bc87..e128f33c 100644 --- a/connections/implementation/client_proxy_test.cc +++ b/connections/implementation/client_proxy_test.cc @@ -210,7 +210,7 @@ class ClientProxyTest : public ::testing::TestWithParam { .info = ByteArray{"discovery endpoint name"}, .id = client->GetLocalEndpointId(), }; - client->StartedDiscovery(service_id_, strategy_, listener, + client->StartedDiscovery(service_id_, strategy_, std::move(listener), absl::MakeSpan(mediums_)); return endpoint; } @@ -363,10 +363,12 @@ class ClientProxyTest : public ::testing::TestWithParam { .bandwidth_changed_cb = mock_discovery_connection_.bandwidth_changed_cb.AsStdFunction(), }; - DiscoveryListener discovery_listener_{ - .endpoint_found_cb = mock_discovery_.endpoint_found_cb.AsStdFunction(), - .endpoint_lost_cb = mock_discovery_.endpoint_lost_cb.AsStdFunction(), - }; + DiscoveryListener GetDiscoveryListener() { + return DiscoveryListener{ + .endpoint_found_cb = mock_discovery_.endpoint_found_cb.AsStdFunction(), + .endpoint_lost_cb = mock_discovery_.endpoint_lost_cb.AsStdFunction(), + }; + } ConnectionOptions connection_options_; AdvertisingOptions advertising_options_; DiscoveryOptions discovery_options_; @@ -379,7 +381,7 @@ TEST_P(ClientProxyTest, CanCancelEndpoint) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); @@ -415,7 +417,7 @@ TEST_P(ClientProxyTest, CanCancelAllEndpoints) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); @@ -452,7 +454,7 @@ TEST_P(ClientProxyTest, CanCancelAllEndpointsWithDifferentEndpoint) { ConnectionListener advertising_connection_listener_3; ClientProxy client3; - StartDiscovery(&client1_, discovery_listener_); + StartDiscovery(&client1_, GetDiscoveryListener()); Endpoint advertising_endpoint_2 = StartAdvertising(&client2_, advertising_connection_listener_2); Endpoint advertising_endpoint_3 = @@ -558,14 +560,14 @@ TEST_F(ClientProxyTest, StartedDiscoveryChangesStateFromIdle) { TEST_F(ClientProxyTest, OnEndpointFoundFiresNotificationInDiscovery) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); } TEST_F(ClientProxyTest, OnEndpointLostFiresNotificationInDiscovery) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryEndpointLost(&client2_, advertising_endpoint); } @@ -573,7 +575,7 @@ TEST_F(ClientProxyTest, OnEndpointLostFiresNotificationInDiscovery) { TEST_F(ClientProxyTest, OnConnectionInitiatedFiresNotificationInDiscovery) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); } @@ -581,7 +583,7 @@ TEST_F(ClientProxyTest, OnConnectionInitiatedFiresNotificationInDiscovery) { TEST_F(ClientProxyTest, OnBandwidthChangedFiresNotificationInDiscovery) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionLocalAccepted(&client2_, advertising_endpoint); @@ -593,7 +595,7 @@ TEST_F(ClientProxyTest, OnBandwidthChangedFiresNotificationInDiscovery) { TEST_F(ClientProxyTest, OnDisconnectedFiresNotificationInDiscovery) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionDisconnected(&client2_, advertising_endpoint); @@ -602,7 +604,7 @@ TEST_F(ClientProxyTest, OnDisconnectedFiresNotificationInDiscovery) { TEST_F(ClientProxyTest, LocalEndpointAcceptedConnectionChangesState) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionLocalAccepted(&client2_, advertising_endpoint); @@ -611,7 +613,7 @@ TEST_F(ClientProxyTest, LocalEndpointAcceptedConnectionChangesState) { TEST_F(ClientProxyTest, LocalEndpointRejectedConnectionChangesState) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionLocalRejected(&client2_, advertising_endpoint); @@ -620,7 +622,7 @@ TEST_F(ClientProxyTest, LocalEndpointRejectedConnectionChangesState) { TEST_F(ClientProxyTest, RemoteEndpointAcceptedConnectionChangesState) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionRemoteAccepted(&client2_, advertising_endpoint); @@ -629,7 +631,7 @@ TEST_F(ClientProxyTest, RemoteEndpointAcceptedConnectionChangesState) { TEST_F(ClientProxyTest, RemoteEndpointRejectedConnectionChangesState) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionRemoteRejected(&client2_, advertising_endpoint); @@ -638,7 +640,7 @@ TEST_F(ClientProxyTest, RemoteEndpointRejectedConnectionChangesState) { TEST_F(ClientProxyTest, OnPayloadChangesState) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionLocalAccepted(&client2_, advertising_endpoint); @@ -650,7 +652,7 @@ TEST_F(ClientProxyTest, OnPayloadChangesState) { TEST_F(ClientProxyTest, OnPayloadProgressChangesState) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); OnDiscoveryConnectionLocalAccepted(&client2_, advertising_endpoint); @@ -770,7 +772,7 @@ TEST_F(ClientProxyTest, EndpointIdRotateWhenStartDiscovery) { &client1_, advertising_connection_listener_, advertising_options); StopAdvertising(&client1_); - StartDiscovery(&client1_, discovery_listener_); + StartDiscovery(&client1_, GetDiscoveryListener()); Endpoint advertising_endpoint_2 = StartAdvertising( &client1_, advertising_connection_listener_, advertising_options); @@ -835,7 +837,7 @@ TEST_F(ClientProxyTest, NotLogSessionForStoppedAdvertisingWithConnection) { StartAdvertising(&client1_, advertising_connection_listener_); OnAdvertisingConnectionInitiated(&client1_, advertising_endpoint); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); @@ -874,7 +876,7 @@ TEST_F(ClientProxyTest, NotLogSessionForStoppedDiscoveryWithConnection) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); // Before @@ -894,7 +896,7 @@ TEST_F(ClientProxyTest, Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); // Before EXPECT_FALSE(client2_.IsAdvertising()); // No Advertising @@ -911,7 +913,7 @@ TEST_F(ClientProxyTest, TEST_F(ClientProxyTest, LogSessionOnDisconnectedWithOneConnection) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); @@ -950,7 +952,7 @@ TEST_F(ClientProxyTest, NotLogSessionOnDisconnectedWhenMoreThanOneConnection) { StartAdvertising(&client1_, advertising_connection_listener_); Endpoint advertising_endpoint_2 = StartAdvertising(&client2_, advertising_connection_listener_); - StartDiscovery(&client3, discovery_listener_); + StartDiscovery(&client3, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client3, advertising_endpoint_1); OnDiscoveryConnectionInitiated(&client3, advertising_endpoint_1); @@ -976,7 +978,7 @@ TEST_F(ClientProxyTest, NotLogSessionOnDisconnectedForDiscoveringWithOnlyOneConnection) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); @@ -995,7 +997,7 @@ TEST_F(ClientProxyTest, TEST_F(ClientProxyTest, LogSessionForResetClientProxy) { Endpoint advertising_endpoint = StartAdvertising(&client1_, advertising_connection_listener_); - StartDiscovery(&client2_, discovery_listener_); + StartDiscovery(&client2_, GetDiscoveryListener()); OnDiscoveryEndpointFound(&client2_, advertising_endpoint); OnDiscoveryConnectionInitiated(&client2_, advertising_endpoint); @@ -1039,7 +1041,7 @@ TEST_F(ClientProxyTest, SetRemoteInfoCorrect) { std::int32_t nearby_connections_version = 2; client1_.SetRemoteOsInfo(advertising_endpoint.id, os_info); client1_.SetRemoteSafeToDisconnectVersion(advertising_endpoint.id, - nearby_connections_version); + nearby_connections_version); ASSERT_TRUE(client1_.GetRemoteOsInfo(advertising_endpoint.id).has_value()); EXPECT_EQ(client1_.GetRemoteOsInfo(advertising_endpoint.id).value().type(), diff --git a/connections/implementation/mock_service_controller.h b/connections/implementation/mock_service_controller.h index 564b4257..02e336c4 100644 --- a/connections/implementation/mock_service_controller.h +++ b/connections/implementation/mock_service_controller.h @@ -20,6 +20,7 @@ #include "gmock/gmock.h" #include "connections/implementation/service_controller.h" +#include "connections/listeners.h" #include "connections/v3/connection_listening_options.h" #include "internal/interop/device.h" @@ -47,7 +48,7 @@ class MockServiceController : public ServiceController { MOCK_METHOD(Status, StartDiscovery, (ClientProxy * client, const std::string& service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener), + DiscoveryListener listener), (override)); MOCK_METHOD(void, StopDiscovery, (ClientProxy * client), (override)); diff --git a/connections/implementation/mock_service_controller_router.h b/connections/implementation/mock_service_controller_router.h index 4d8ea108..14e6e9c5 100644 --- a/connections/implementation/mock_service_controller_router.h +++ b/connections/implementation/mock_service_controller_router.h @@ -17,6 +17,7 @@ #include "gmock/gmock.h" #include "connections/implementation/service_controller_router.h" +#include "connections/listeners.h" namespace nearby { namespace connections { @@ -35,7 +36,7 @@ class MockServiceControllerRouter : public ServiceControllerRouter { MOCK_METHOD(void, StartDiscovery, (ClientProxy * client, absl::string_view service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener, ResultCallback callback), + DiscoveryListener listener, ResultCallback callback), (override)); MOCK_METHOD(void, StopDiscovery, diff --git a/connections/implementation/offline_service_controller.cc b/connections/implementation/offline_service_controller.cc index 3ae1e27d..a1811cd7 100644 --- a/connections/implementation/offline_service_controller.cc +++ b/connections/implementation/offline_service_controller.cc @@ -19,6 +19,8 @@ #include #include "absl/strings/str_join.h" +#include "connections/discovery_options.h" +#include "connections/listeners.h" #include "internal/interop/device.h" namespace nearby { @@ -54,13 +56,12 @@ void OfflineServiceController::StopAdvertising(ClientProxy* client) { Status OfflineServiceController::StartDiscovery( ClientProxy* client, const std::string& service_id, - const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener) { + const DiscoveryOptions& discovery_options, DiscoveryListener listener) { if (stop_) return {Status::kOutOfOrderApiCall}; NEARBY_LOGS(INFO) << "Client " << client->GetClientId() << " requested discovery to start."; return pcp_manager_.StartDiscovery(client, service_id, discovery_options, - listener); + std::move(listener)); } void OfflineServiceController::StopDiscovery(ClientProxy* client) { diff --git a/connections/implementation/offline_service_controller.h b/connections/implementation/offline_service_controller.h index 13661961..9331aa35 100644 --- a/connections/implementation/offline_service_controller.h +++ b/connections/implementation/offline_service_controller.h @@ -49,7 +49,7 @@ class OfflineServiceController : public ServiceController { Status StartDiscovery(ClientProxy* client, const std::string& service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener) override; + DiscoveryListener listener) override; void StopDiscovery(ClientProxy* client) override; void InjectEndpoint(ClientProxy* client, const std::string& service_id, diff --git a/connections/implementation/pcp_handler.h b/connections/implementation/pcp_handler.h index d156a916..16d9273a 100644 --- a/connections/implementation/pcp_handler.h +++ b/connections/implementation/pcp_handler.h @@ -83,7 +83,7 @@ class PcpHandler { virtual Status StartDiscovery(ClientProxy* client, const std::string& service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener) = 0; + DiscoveryListener listener) = 0; // If Discovery is active, stop it, and change CLientProxy state, // otherwise do nothing. diff --git a/connections/implementation/service_controller.h b/connections/implementation/service_controller.h index 8a5acf39..e0dbbf31 100644 --- a/connections/implementation/service_controller.h +++ b/connections/implementation/service_controller.h @@ -74,7 +74,7 @@ class ServiceController { virtual Status StartDiscovery(ClientProxy* client, const std::string& service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener) = 0; + DiscoveryListener listener) = 0; virtual void StopDiscovery(ClientProxy* client) = 0; virtual void InjectEndpoint(ClientProxy* client, diff --git a/connections/implementation/service_controller_router.cc b/connections/implementation/service_controller_router.cc index b6053718..15605478 100644 --- a/connections/implementation/service_controller_router.cc +++ b/connections/implementation/service_controller_router.cc @@ -20,6 +20,7 @@ #include #include "absl/memory/memory.h" +#include "connections/discovery_options.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/offline_service_controller.h" #include "connections/listeners.h" @@ -127,19 +128,20 @@ void ServiceControllerRouter::StopAdvertising(ClientProxy* client, void ServiceControllerRouter::StartDiscovery( ClientProxy* client, absl::string_view service_id, - const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener, ResultCallback callback) { + const DiscoveryOptions& discovery_options, DiscoveryListener listener, + ResultCallback callback) { RouteToServiceController( "scr-start-discovery", [this, client, service_id = std::string(service_id), discovery_options, - listener, callback = std::move(callback)]() mutable { + listener = std::move(listener), + callback = std::move(callback)]() mutable { if (client->IsDiscovering()) { callback({Status::kAlreadyDiscovering}); return; } callback(GetServiceController()->StartDiscovery( - client, service_id, discovery_options, listener)); + client, service_id, discovery_options, std::move(listener))); }); } diff --git a/connections/implementation/service_controller_router.h b/connections/implementation/service_controller_router.h index 9b3ee470..759aea75 100644 --- a/connections/implementation/service_controller_router.h +++ b/connections/implementation/service_controller_router.h @@ -76,7 +76,7 @@ class ServiceControllerRouter { virtual void StartDiscovery(ClientProxy* client, absl::string_view service_id, const DiscoveryOptions& discovery_options, - const DiscoveryListener& listener, + DiscoveryListener listener, ResultCallback callback); virtual void StopDiscovery(ClientProxy* client, ResultCallback callback); diff --git a/connections/implementation/service_controller_router_test.cc b/connections/implementation/service_controller_router_test.cc index e636e30f..9fdd2b8f 100644 --- a/connections/implementation/service_controller_router_test.cc +++ b/connections/implementation/service_controller_router_test.cc @@ -104,20 +104,19 @@ class ServiceControllerRouterTest : public testing::Test { void StartDiscovery(ClientProxy* client, std::string service_id, DiscoveryOptions discovery_options, - const DiscoveryListener& listener, - ResultCallback callback) { + DiscoveryListener listener, ResultCallback callback) { EXPECT_CALL(*mock_, StartDiscovery) .WillOnce(Return(Status{Status::kSuccess})); { MutexLock lock(&mutex_); complete_ = false; - router_.StartDiscovery(client, kServiceId, discovery_options, listener, + router_.StartDiscovery(client, kServiceId, discovery_options, {}, std::move(callback)); while (!complete_) cond_.Wait(); EXPECT_EQ(result_, Status{Status::kSuccess}); } - client->StartedDiscovery(service_id, discovery_options.strategy, listener, - absl::MakeSpan(mediums_)); + client->StartedDiscovery(service_id, discovery_options.strategy, + std::move(listener), absl::MakeSpan(mediums_)); EXPECT_TRUE(client->IsDiscovering()); } @@ -185,8 +184,7 @@ class ServiceControllerRouterTest : public testing::Test { while (!complete_) cond_.Wait(); EXPECT_EQ(result_, Status{Status::kSuccess}); } - client->LocalEndpointAcceptedConnection(endpoint_id, - {}); + client->LocalEndpointAcceptedConnection(endpoint_id, {}); client->RemoteEndpointAcceptedConnection(endpoint_id); EXPECT_TRUE(client->IsConnectionAccepted(endpoint_id)); client->OnConnectionAccepted(endpoint_id); @@ -506,8 +504,6 @@ class ServiceControllerRouterTest : public testing::Test { .listener = ConnectionListener(), }; - DiscoveryListener discovery_listener_; - Mutex mutex_; ConditionVariable cond_{&mutex_}; Status result_ ABSL_GUARDED_BY(mutex_) = {Status::kError}; @@ -561,7 +557,7 @@ TEST_F(ServiceControllerRouterTest, StopAdvertisingCalled) { } TEST_F(ServiceControllerRouterTest, StartDiscoveryCalled) { - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -571,7 +567,7 @@ TEST_F(ServiceControllerRouterTest, StartDiscoveryCalled) { } TEST_F(ServiceControllerRouterTest, StopDiscoveryCalled) { - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -587,7 +583,7 @@ TEST_F(ServiceControllerRouterTest, StopDiscoveryCalled) { } TEST_F(ServiceControllerRouterTest, InjectEndpointCalled) { - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -611,7 +607,7 @@ TEST_F(ServiceControllerRouterTest, InjectEndpointCalled) { TEST_F(ServiceControllerRouterTest, RequestConnectionCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -629,7 +625,7 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionCalled) { TEST_F(ServiceControllerRouterTest, AcceptConnectionCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -655,7 +651,7 @@ TEST_F(ServiceControllerRouterTest, AcceptConnectionCalled) { TEST_F(ServiceControllerRouterTest, RejectConnectionCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -681,7 +677,7 @@ TEST_F(ServiceControllerRouterTest, RejectConnectionCalled) { TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -714,7 +710,7 @@ TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalled) { TEST_F(ServiceControllerRouterTest, SendPayloadCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -748,7 +744,7 @@ TEST_F(ServiceControllerRouterTest, SendPayloadCalled) { TEST_F(ServiceControllerRouterTest, CancelPayloadCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -783,7 +779,7 @@ TEST_F(ServiceControllerRouterTest, CancelPayloadCalled) { TEST_F(ServiceControllerRouterTest, DisconnectFromEndpointCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -816,7 +812,7 @@ TEST_F(ServiceControllerRouterTest, DisconnectFromEndpointCalled) { TEST_F(ServiceControllerRouterTest, RequestConnectionCalledV3) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -870,7 +866,7 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionCalledV3) { TEST_F(ServiceControllerRouterTest, RequestConnectionV3FakeDevice) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -923,7 +919,7 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionV3FakeDevice) { TEST_F(ServiceControllerRouterTest, RequestConnectionV3TwiceFails) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -967,7 +963,7 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionV3TwiceFails) { TEST_F(ServiceControllerRouterTest, AcceptConnectionCalledV3) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -1001,7 +997,7 @@ TEST_F(ServiceControllerRouterTest, AcceptConnectionCalledV3) { TEST_F(ServiceControllerRouterTest, RejectConnectionCalledV3) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -1035,7 +1031,7 @@ TEST_F(ServiceControllerRouterTest, RejectConnectionCalledV3) { TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalledV3) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -1076,7 +1072,7 @@ TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalledV3) { TEST_F(ServiceControllerRouterTest, SendPayloadCalledV3) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -1118,7 +1114,7 @@ TEST_F(ServiceControllerRouterTest, SendPayloadCalledV3) { TEST_F(ServiceControllerRouterTest, DisconnectFromDeviceCalledV3) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; @@ -1159,7 +1155,7 @@ TEST_F(ServiceControllerRouterTest, DisconnectFromDeviceCalledV3) { TEST_F(ServiceControllerRouterTest, CancelPayloadV3Called) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, DiscoveryListener{}, [this](Status status) { MutexLock lock(&mutex_); result_ = status; diff --git a/connections/listeners.h b/connections/listeners.h index 15ed72f7..493decaf 100644 --- a/connections/listeners.h +++ b/connections/listeners.h @@ -136,9 +136,9 @@ struct DiscoveryListener { // endpoint_id - The ID of the remote endpoint that was discovered. // endpoint_info - The info of the remote endpoint representd by ByteArray. // service_id - The ID of the service advertised by the remote endpoint. - std::function + absl::AnyInvocable endpoint_found_cb = [](const std::string&, const ByteArray&, const std::string&) {}; @@ -147,7 +147,7 @@ struct DiscoveryListener { // #onEndpointFound(String, DiscoveredEndpointInfo)}. // // endpoint_id - The ID of the remote endpoint that was lost. - std::function endpoint_lost_cb = + absl::AnyInvocable endpoint_lost_cb = [](const std::string&) {}; // Called when a remote endpoint is found with an updated distance. @@ -155,7 +155,7 @@ struct DiscoveryListener { // arguments: // endpoint_id - The ID of the remote endpoint that was lost. // info - The distance info, encoded as enum value. - std::function + absl::AnyInvocable endpoint_distance_changed_cb = [](const std::string&, DistanceInfo) {}; }; diff --git a/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm b/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm index ef72eded..6ed239f3 100644 --- a/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm +++ b/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm @@ -199,13 +199,14 @@ GNCStatus GNCStatusFromCppStatus(Status status) { DiscoveryOptions discovery_options = [discoveryOptions toCpp]; DiscoveryListener listener; - listener.endpoint_found_cb = ^(const std::string &endpoint_id, const ByteArray &endpoint_info, - const std::string &service_id) { + listener.endpoint_found_cb = [delegate](const std::string &endpoint_id, + const ByteArray &endpoint_info, + const std::string &service_id) { NSString *endpointID = @(endpoint_id.c_str()); NSData *info = [NSData dataWithBytes:endpoint_info.data() length:endpoint_info.size()]; [delegate foundEndpoint:endpointID withEndpointInfo:info]; }; - listener.endpoint_lost_cb = ^(const std::string &endpoint_id) { + listener.endpoint_lost_cb = [delegate](const std::string &endpoint_id) { NSString *endpointID = @(endpoint_id.c_str()); [delegate lostEndpoint:endpointID]; }; @@ -290,30 +291,30 @@ GNCStatus GNCStatusFromCppStatus(Status status) { GNCPayload *gncPayload = [GNCPayload fromCpp:std::move(payload)]; [delegate receivedPayload:gncPayload fromEndpoint:endpointID]; }; - listener.payload_progress_cb = - [delegate](absl::string_view endpoint_id, const PayloadProgressInfo &info) { - NSString *endpointID = @(std::string(endpoint_id).c_str()); - GNCPayloadStatus status; - switch (info.status) { - case PayloadProgressInfo::Status::kSuccess: - status = GNCPayloadStatusSuccess; - break; - case PayloadProgressInfo::Status::kFailure: - status = GNCPayloadStatusFailure; - break; - case PayloadProgressInfo::Status::kInProgress: - status = GNCPayloadStatusInProgress; - break; - case PayloadProgressInfo::Status::kCanceled: - status = GNCPayloadStatusCanceled; - break; - } - [delegate receivedProgressUpdateForPayload:info.payload_id - withStatus:status - fromEndpoint:endpointID - bytesTransfered:info.bytes_transferred - totalBytes:info.total_bytes]; - }; + listener.payload_progress_cb = [delegate](absl::string_view endpoint_id, + const PayloadProgressInfo &info) { + NSString *endpointID = @(std::string(endpoint_id).c_str()); + GNCPayloadStatus status; + switch (info.status) { + case PayloadProgressInfo::Status::kSuccess: + status = GNCPayloadStatusSuccess; + break; + case PayloadProgressInfo::Status::kFailure: + status = GNCPayloadStatusFailure; + break; + case PayloadProgressInfo::Status::kInProgress: + status = GNCPayloadStatusInProgress; + break; + case PayloadProgressInfo::Status::kCanceled: + status = GNCPayloadStatusCanceled; + break; + } + [delegate receivedProgressUpdateForPayload:info.payload_id + withStatus:status + fromEndpoint:endpointID + bytesTransfered:info.bytes_transferred + totalBytes:info.total_bytes]; + }; ResultListener result = [completionHandler](Status status) { NSError *err = NSErrorFromCppStatus(status);