diff --git a/connections/implementation/bluetooth_bwu_handler.cc b/connections/implementation/bluetooth_bwu_handler.cc index f60d607b..21328239 100644 --- a/connections/implementation/bluetooth_bwu_handler.cc +++ b/connections/implementation/bluetooth_bwu_handler.cc @@ -111,11 +111,9 @@ ByteArray BluetoothBwuHandler::HandleInitializeUpgradedMediumForEndpoint( if (!bluetooth_medium_.IsAcceptingConnections(upgrade_service_id)) { if (!bluetooth_medium_.StartAcceptingConnections( upgrade_service_id, - { - .accepted_cb = absl::bind_front( - &BluetoothBwuHandler::OnIncomingBluetoothConnection, this, - client), - })) { + absl::bind_front( + &BluetoothBwuHandler::OnIncomingBluetoothConnection, this, + client))) { NEARBY_LOGS(ERROR) << "BluetoothBwuHandler couldn't initiate the " "BLUETOOTH upgrade for endpoint " << endpoint_id diff --git a/connections/implementation/mediums/ble_test.cc b/connections/implementation/mediums/ble_test.cc index 9ec605b1..51673a77 100644 --- a/connections/implementation/mediums/ble_test.cc +++ b/connections/implementation/mediums/ble_test.cc @@ -72,11 +72,7 @@ TEST_P(BleTest, CanStartAcceptingConnectionsAndConnect) { fast_advertisement_service_uuid); ble_a.StartAcceptingConnections( service_id, - { - .accepted_cb = [&accept_latch]( - BleSocket socket, - const std::string&) { accept_latch.CountDown(); }, - }); + [&](BleSocket socket, const std::string&) { accept_latch.CountDown(); }); BlePeripheral discovered_peripheral; ble_b.StartScanning( service_id, fast_advertisement_service_uuid, @@ -126,11 +122,7 @@ TEST_P(BleTest, CanCancelConnect) { fast_advertisement_service_uuid); ble_a.StartAcceptingConnections( service_id, - { - .accepted_cb = [&accept_latch]( - BleSocket socket, - const std::string&) { accept_latch.CountDown(); }, - }); + [&](BleSocket socket, const std::string&) { accept_latch.CountDown(); }); BlePeripheral discovered_peripheral; ble_b.StartScanning( service_id, fast_advertisement_service_uuid, diff --git a/connections/implementation/mediums/ble_v2.cc b/connections/implementation/mediums/ble_v2.cc index cbdc5fc8..88131d85 100644 --- a/connections/implementation/mediums/ble_v2.cc +++ b/connections/implementation/mediums/ble_v2.cc @@ -417,7 +417,9 @@ bool BleV2::StartAcceptingConnections(const std::string& service_id, }); incoming_sockets_.insert({service_id, client_socket}); } - callback.accepted_cb(std::move(client_socket), service_id); + if (callback) { + callback(std::move(client_socket), service_id); + } } }); diff --git a/connections/implementation/mediums/ble_v2.h b/connections/implementation/mediums/ble_v2.h index 23ff3cf8..0040dbd0 100644 --- a/connections/implementation/mediums/ble_v2.h +++ b/connections/implementation/mediums/ble_v2.h @@ -48,10 +48,8 @@ class BleV2 final { using DiscoveredPeripheralCallback = mediums::DiscoveredPeripheralCallback; // Callback that is invoked when a new connection is accepted. - struct AcceptedConnectionCallback { - absl::AnyInvocable - accepted_cb = DefaultCallback(); - }; + using AcceptedConnectionCallback = absl::AnyInvocable; explicit BleV2(BluetoothRadio& bluetooth_radio); ~BleV2(); diff --git a/connections/implementation/mediums/ble_v2_test.cc b/connections/implementation/mediums/ble_v2_test.cc index 484d1471..d1d1ca5e 100644 --- a/connections/implementation/mediums/ble_v2_test.cc +++ b/connections/implementation/mediums/ble_v2_test.cc @@ -77,14 +77,10 @@ TEST_P(BleV2Test, CanConnect) { BleV2Socket socket_for_server; EXPECT_TRUE(ble_server.StartAcceptingConnections( - service_id, { - .accepted_cb = - [&socket_for_server, &accept_latch]( - BleV2Socket socket, const std::string&) { - socket_for_server = std::move(socket); - accept_latch.CountDown(); - }, - })); + service_id, [&](BleV2Socket socket, const std::string&) { + socket_for_server = std::move(socket); + accept_latch.CountDown(); + })); ble_server.StartAdvertising(service_id, advertisement_bytes, PowerLevel::kHighPower, @@ -139,14 +135,10 @@ TEST_P(BleV2Test, CanCancelConnect) { BleV2Socket socket_for_server; EXPECT_TRUE(ble_server.StartAcceptingConnections( - service_id, { - .accepted_cb = - [&socket_for_server, &accept_latch]( - BleV2Socket socket, const std::string&) { - socket_for_server = std::move(socket); - accept_latch.CountDown(); - }, - })); + service_id, [&](BleV2Socket socket, const std::string&) { + socket_for_server = std::move(socket); + accept_latch.CountDown(); + })); ble_server.StartAdvertising(service_id, advertisement_bytes, PowerLevel::kHighPower, diff --git a/connections/implementation/mediums/bluetooth_classic.cc b/connections/implementation/mediums/bluetooth_classic.cc index ccf42d62..34803fd8 100644 --- a/connections/implementation/mediums/bluetooth_classic.cc +++ b/connections/implementation/mediums/bluetooth_classic.cc @@ -298,8 +298,9 @@ bool BluetoothClassic::StartAcceptingConnections( server_socket.Close(); break; } - - callback.accepted_cb(service_id, std::move(client_socket)); + if (callback) { + callback(service_id, std::move(client_socket)); + } } }); diff --git a/connections/implementation/mediums/bluetooth_classic.h b/connections/implementation/mediums/bluetooth_classic.h index 415129ec..8c84215f 100644 --- a/connections/implementation/mediums/bluetooth_classic.h +++ b/connections/implementation/mediums/bluetooth_classic.h @@ -17,9 +17,9 @@ #include #include -#include -#include #include +#include +#include #include "absl/container/flat_hash_map.h" #include "connections/implementation/mediums/bluetooth_radio.h" @@ -40,10 +40,8 @@ class BluetoothClassic { using ScanMode = BluetoothAdapter::ScanMode; // Callback that is invoked when a new connection is accepted. - struct AcceptedConnectionCallback { - std::function - accepted_cb = [](const std::string&, BluetoothSocket) {}; - }; + using AcceptedConnectionCallback = absl::AnyInvocable; explicit BluetoothClassic(BluetoothRadio& radio); ~BluetoothClassic(); diff --git a/connections/implementation/mediums/bluetooth_classic_test.cc b/connections/implementation/mediums/bluetooth_classic_test.cc index 8a1e771e..0c618524 100644 --- a/connections/implementation/mediums/bluetooth_classic_test.cc +++ b/connections/implementation/mediums/bluetooth_classic_test.cc @@ -162,13 +162,9 @@ TEST_P(BluetoothClassicTest, CanConnect) { CountDownLatch accept_latch(1); EXPECT_TRUE(bt_server.StartAcceptingConnections( std::string(kServiceName1), - { - .accepted_cb = - [&socket_for_server, &accept_latch](const std::string& service_id, - BluetoothSocket socket) { - socket_for_server = std::move(socket); - accept_latch.CountDown(); - }, + [&](const std::string& service_id, BluetoothSocket socket) { + socket_for_server = std::move(socket); + accept_latch.CountDown(); })); CancellationFlag flag; BluetoothSocket socket_for_client = @@ -217,13 +213,9 @@ TEST_P(BluetoothClassicTest, CanCancelBeforeConnect) { CountDownLatch accept_latch(1); EXPECT_TRUE(bt_server.StartAcceptingConnections( std::string(kServiceName1), - { - .accepted_cb = - [&socket_for_server, &accept_latch](const std::string& service_id, - BluetoothSocket socket) { - socket_for_server = std::move(socket); - accept_latch.CountDown(); - }, + [&](const std::string& service_id, BluetoothSocket socket) { + socket_for_server = std::move(socket); + accept_latch.CountDown(); })); CancellationFlag flag(true); BluetoothSocket socket_for_client = @@ -288,13 +280,9 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect) { CountDownLatch accept_latch(1); EXPECT_TRUE(bt_server.StartAcceptingConnections( std::string(kServiceName1), - { - .accepted_cb = - [&socket_for_server, &accept_latch](const std::string& service_id, - BluetoothSocket socket) { - socket_for_server = std::move(socket); - accept_latch.CountDown(); - }, + [&](const std::string& service_id, BluetoothSocket socket) { + socket_for_server = std::move(socket); + accept_latch.CountDown(); })); CancellationFlag flag; BluetoothSocket socket_for_client = @@ -361,13 +349,9 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) { EXPECT_TRUE(bt_server.StartAcceptingConnections( std::string(kServiceName1), - { - .accepted_cb = - [&socket_for_server1, &accept_latch]( - const std::string& service_id, BluetoothSocket socket) { - socket_for_server1 = std::move(socket); - accept_latch.CountDown(); - }, + [&](const std::string& service_id, BluetoothSocket socket) { + socket_for_server1 = std::move(socket); + accept_latch.CountDown(); })); CancellationFlag flag; BluetoothSocket socket_for_client1 = @@ -378,13 +362,9 @@ TEST_P(BluetoothClassicTest, CanCancelDuringConnect_MultipleEndpoints) { medium_a_->CancelDuringConnectToService(); EXPECT_TRUE(bt_server.StartAcceptingConnections( std::string(kServiceName2), - { - .accepted_cb = - [&socket_for_server2, &accept_latch]( - const std::string& service_id, BluetoothSocket socket) { - socket_for_server2 = std::move(socket); - accept_latch.CountDown(); - }, + [&](const std::string& service_id, BluetoothSocket socket) { + socket_for_server2 = std::move(socket); + accept_latch.CountDown(); })); BluetoothSocket socket_for_client2 = diff --git a/connections/implementation/mediums/webrtc_stub.h b/connections/implementation/mediums/webrtc_stub.h index a0609d95..3bc01fe7 100644 --- a/connections/implementation/mediums/webrtc_stub.h +++ b/connections/implementation/mediums/webrtc_stub.h @@ -31,14 +31,13 @@ namespace nearby { namespace connections { namespace mediums { -// Callback that is invoked when a new connection is accepted. -struct AcceptedConnectionCallback { - std::function accepted_cb = - [](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/wifi_direct.cc b/connections/implementation/mediums/wifi_direct.cc index aa03650d..81020856 100644 --- a/connections/implementation/mediums/wifi_direct.cc +++ b/connections/implementation/mediums/wifi_direct.cc @@ -185,7 +185,9 @@ bool WifiDirect::StartAcceptingConnections( server_socket.Close(); break; } - callback.accepted_cb(service_id, std::move(client_socket)); + if (callback) { + callback(service_id, std::move(client_socket)); + } } }); diff --git a/connections/implementation/mediums/wifi_direct.h b/connections/implementation/mediums/wifi_direct.h index f4ef22af..899b7d71 100644 --- a/connections/implementation/mediums/wifi_direct.h +++ b/connections/implementation/mediums/wifi_direct.h @@ -29,10 +29,8 @@ namespace connections { class WifiDirect { public: // Callback that is invoked when a new connection is accepted. - struct AcceptedConnectionCallback { - std::function - accepted_cb = [](const std::string&, WifiDirectSocket) {}; - }; + using AcceptedConnectionCallback = absl::AnyInvocable; WifiDirect() : is_go_started_(false), is_connected_to_go_(false) {} ~WifiDirect(); diff --git a/connections/implementation/mediums/wifi_hotspot.cc b/connections/implementation/mediums/wifi_hotspot.cc index 27b8256e..8605ad1d 100644 --- a/connections/implementation/mediums/wifi_hotspot.cc +++ b/connections/implementation/mediums/wifi_hotspot.cc @@ -14,8 +14,8 @@ #include "connections/implementation/mediums/wifi_hotspot.h" -#include #include +#include #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" @@ -188,7 +188,9 @@ bool WifiHotspot::StartAcceptingConnections( server_socket.Close(); break; } - callback.accepted_cb(service_id, std::move(client_socket)); + if (callback) { + callback(service_id, std::move(client_socket)); + } } }); diff --git a/connections/implementation/mediums/wifi_hotspot.h b/connections/implementation/mediums/wifi_hotspot.h index 7ca8e210..6b63eac7 100644 --- a/connections/implementation/mediums/wifi_hotspot.h +++ b/connections/implementation/mediums/wifi_hotspot.h @@ -29,10 +29,8 @@ namespace connections { class WifiHotspot { public: // Callback that is invoked when a new connection is accepted. - struct AcceptedConnectionCallback { - std::function - accepted_cb = [](const std::string&, WifiHotspotSocket) {}; - }; + using AcceptedConnectionCallback = absl::AnyInvocable; WifiHotspot() : is_hotspot_started_(false), is_connected_to_hotspot_(false) {} ~WifiHotspot(); diff --git a/connections/implementation/mediums/wifi_lan.cc b/connections/implementation/mediums/wifi_lan.cc index 44b18caa..2c3e262e 100644 --- a/connections/implementation/mediums/wifi_lan.cc +++ b/connections/implementation/mediums/wifi_lan.cc @@ -264,7 +264,9 @@ bool WifiLan::StartAcceptingConnections(const std::string& service_id, server_socket.Close(); break; } - callback.accepted_cb(service_id, std::move(client_socket)); + if (callback) { + callback(service_id, std::move(client_socket)); + } } }); diff --git a/connections/implementation/mediums/wifi_lan.h b/connections/implementation/mediums/wifi_lan.h index 9c0dd271..b9d53f22 100644 --- a/connections/implementation/mediums/wifi_lan.h +++ b/connections/implementation/mediums/wifi_lan.h @@ -36,10 +36,8 @@ class WifiLan { using DiscoveredServiceCallback = WifiLanMedium::DiscoveredServiceCallback; // Callback that is invoked when a new connection is accepted. - struct AcceptedConnectionCallback { - std::function - accepted_cb = [](const std::string&, WifiLanSocket) {}; - }; + using AcceptedConnectionCallback = absl::AnyInvocable; WifiLan() = default; ~WifiLan(); diff --git a/connections/implementation/mediums/wifi_lan_test.cc b/connections/implementation/mediums/wifi_lan_test.cc index fd23f51a..acd1df81 100644 --- a/connections/implementation/mediums/wifi_lan_test.cc +++ b/connections/implementation/mediums/wifi_lan_test.cc @@ -68,14 +68,9 @@ TEST_P(WifiLanTest, CanConnect) { WifiLanSocket socket_for_server; EXPECT_TRUE(wifi_lan_server.StartAcceptingConnections( - service_id, - { - .accepted_cb = - [&socket_for_server, &accept_latch](const std::string& service_id, - WifiLanSocket socket) { - socket_for_server = std::move(socket); - accept_latch.CountDown(); - }, + service_id, [&](const std::string& service_id, WifiLanSocket socket) { + socket_for_server = std::move(socket); + accept_latch.CountDown(); })); NsdServiceInfo nsd_service_info; @@ -125,14 +120,9 @@ TEST_P(WifiLanTest, CanCancelConnect) { WifiLanSocket socket_for_server; EXPECT_TRUE(wifi_lan_server.StartAcceptingConnections( - service_id, - { - .accepted_cb = - [&socket_for_server, &accept_latch](const std::string& service_id, - WifiLanSocket socket) { - socket_for_server = std::move(socket); - accept_latch.CountDown(); - }, + service_id, [&](const std::string& service_id, WifiLanSocket socket) { + socket_for_server = std::move(socket); + accept_latch.CountDown(); })); NsdServiceInfo nsd_service_info; diff --git a/connections/implementation/p2p_cluster_pcp_handler.cc b/connections/implementation/p2p_cluster_pcp_handler.cc index ab6491f8..da91affa 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.cc +++ b/connections/implementation/p2p_cluster_pcp_handler.cc @@ -1087,10 +1087,10 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( !bluetooth_medium_.IsAcceptingConnections(std::string(service_id))) { if (!bluetooth_medium_.StartAcceptingConnections( std::string(service_id), - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, - this, client_proxy, local_endpoint_id, - options.listening_endpoint_type)})) { + absl::bind_front( + &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, this, + client_proxy, local_endpoint_id, + options.listening_endpoint_type))) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on Bluetooth"; } else { @@ -1106,10 +1106,10 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( !ble_v2_medium_.IsAcceptingConnections(std::string(service_id))) { if (!ble_v2_medium_.StartAcceptingConnections( std::string(service_id), - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this, - client_proxy, local_endpoint_id, - options.listening_endpoint_type)})) { + absl::bind_front( + &P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this, + client_proxy, local_endpoint_id, + options.listening_endpoint_type))) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on ble_v2"; } else { @@ -1122,10 +1122,10 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( !ble_medium_.IsAcceptingConnections(std::string(service_id))) { if (!ble_medium_.StartAcceptingConnections( std::string(service_id), - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BleConnectionAcceptedHandler, this, - client_proxy, local_endpoint_id, - options.listening_endpoint_type)})) { + absl::bind_front( + &P2pClusterPcpHandler::BleConnectionAcceptedHandler, this, + client_proxy, local_endpoint_id, + options.listening_endpoint_type))) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on ble"; } else { @@ -1137,10 +1137,10 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( !wifi_lan_medium_.IsAcceptingConnections(std::string(service_id))) { if (!wifi_lan_medium_.StartAcceptingConnections( std::string(service_id), - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler, this, - client_proxy, local_endpoint_id, "", - options.listening_endpoint_type)})) { + absl::bind_front( + &P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler, this, + client_proxy, local_endpoint_id, "", + options.listening_endpoint_type))) { NEARBY_LOGS(WARNING) << "Failed to start listening for incoming connections on wifi_lan"; } else { @@ -1458,10 +1458,10 @@ Medium P2pClusterPcpHandler::StartBluetoothAdvertising( if (!bluetooth_radio_.Enable() || !bluetooth_medium_.StartAcceptingConnections( service_id, - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, - this, client, local_endpoint_info.AsStringView(), - NearbyDevice::Type::kConnectionsDevice)})) { + absl::bind_front( + &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, this, + client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice))) { NEARBY_LOGS(WARNING) << "In StartBluetoothAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1635,11 +1635,10 @@ Medium P2pClusterPcpHandler::StartBleAdvertising( if (!ble_medium_.IsAcceptingConnections(service_id)) { if (!bluetooth_radio_.Enable() || !ble_medium_.StartAcceptingConnections( - service_id, - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BleConnectionAcceptedHandler, this, - client, local_endpoint_info.AsStringView(), - NearbyDevice::Type::kConnectionsDevice)})) { + service_id, absl::bind_front( + &P2pClusterPcpHandler::BleConnectionAcceptedHandler, + this, client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice))) { NEARBY_LOGS(WARNING) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1664,10 +1663,10 @@ Medium P2pClusterPcpHandler::StartBleAdvertising( if (!bluetooth_radio_.Enable() || !bluetooth_medium_.StartAcceptingConnections( service_id, - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, - this, client, local_endpoint_info.AsStringView(), - NearbyDevice::Type::kConnectionsDevice)})) { + absl::bind_front( + &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, + this, client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice))) { NEARBY_LOGS(WARNING) << "In BT StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1845,10 +1844,10 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising( if (!bluetooth_radio_.Enable() || !ble_v2_medium_.StartAcceptingConnections( service_id, - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this, - client, local_endpoint_info.AsStringView(), - NearbyDevice::Type::kConnectionsDevice)})) { + absl::bind_front( + &P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this, + client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice))) { NEARBY_LOGS(WARNING) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -1876,10 +1875,10 @@ Medium P2pClusterPcpHandler::StartBleV2Advertising( if (!bluetooth_radio_.Enable() || !bluetooth_medium_.StartAcceptingConnections( service_id, - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, - this, client, local_endpoint_info.AsStringView(), - NearbyDevice::Type::kConnectionsDevice)})) { + absl::bind_front( + &P2pClusterPcpHandler::BluetoothConnectionAcceptedHandler, + this, client, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice))) { NEARBY_LOGS(WARNING) << "In BT StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) @@ -2057,10 +2056,10 @@ Medium P2pClusterPcpHandler::StartWifiLanAdvertising( if (!wifi_lan_medium_.IsAcceptingConnections(service_id)) { if (!wifi_lan_medium_.StartAcceptingConnections( service_id, - {.accepted_cb = absl::bind_front( - &P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler, this, - client, local_endpoint_id, local_endpoint_info.AsStringView(), - NearbyDevice::Type::kConnectionsDevice)})) { + absl::bind_front( + &P2pClusterPcpHandler::WifiLanConnectionAcceptedHandler, this, + client, local_endpoint_id, local_endpoint_info.AsStringView(), + NearbyDevice::Type::kConnectionsDevice))) { NEARBY_LOGS(WARNING) << "In StartWifiLanAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) diff --git a/connections/implementation/wifi_direct_bwu_handler.cc b/connections/implementation/wifi_direct_bwu_handler.cc index daa5d04b..0ff387c1 100644 --- a/connections/implementation/wifi_direct_bwu_handler.cc +++ b/connections/implementation/wifi_direct_bwu_handler.cc @@ -45,11 +45,9 @@ ByteArray WifiDirectBwuHandler::HandleInitializeUpgradedMediumForEndpoint( if (!wifi_direct_medium_.IsAcceptingConnections(upgrade_service_id)) { if (!wifi_direct_medium_.StartAcceptingConnections( upgrade_service_id, - { - .accepted_cb = absl::bind_front( - &WifiDirectBwuHandler::OnIncomingWifiDirectConnection, this, - client), - })) { + absl::bind_front( + &WifiDirectBwuHandler::OnIncomingWifiDirectConnection, this, + client))) { NEARBY_LOGS(ERROR) << "WifiDirectBwuHandler couldn't initiate WifiDirect upgrade for " << "service " << upgrade_service_id << " and endpoint " << endpoint_id diff --git a/connections/implementation/wifi_hotspot_bwu_handler.cc b/connections/implementation/wifi_hotspot_bwu_handler.cc index 4bf7f215..9479924d 100644 --- a/connections/implementation/wifi_hotspot_bwu_handler.cc +++ b/connections/implementation/wifi_hotspot_bwu_handler.cc @@ -49,11 +49,9 @@ ByteArray WifiHotspotBwuHandler::HandleInitializeUpgradedMediumForEndpoint( if (!wifi_hotspot_medium_.IsAcceptingConnections(upgrade_service_id)) { if (!wifi_hotspot_medium_.StartAcceptingConnections( upgrade_service_id, - { - .accepted_cb = absl::bind_front( - &WifiHotspotBwuHandler::OnIncomingWifiHotspotConnection, - this, client), - })) { + absl::bind_front( + &WifiHotspotBwuHandler::OnIncomingWifiHotspotConnection, this, + client))) { NEARBY_LOGS(ERROR) << "WifiHotspotBwuHandler couldn't initiate WifiHotspot upgrade for " << "service " << upgrade_service_id << " and endpoint " << endpoint_id diff --git a/connections/implementation/wifi_lan_bwu_handler.cc b/connections/implementation/wifi_lan_bwu_handler.cc index 04a0ea50..2a455e0c 100644 --- a/connections/implementation/wifi_lan_bwu_handler.cc +++ b/connections/implementation/wifi_lan_bwu_handler.cc @@ -95,11 +95,8 @@ ByteArray WifiLanBwuHandler::HandleInitializeUpgradedMediumForEndpoint( if (!wifi_lan_medium_.IsAcceptingConnections(upgrade_service_id)) { if (!wifi_lan_medium_.StartAcceptingConnections( upgrade_service_id, - { - .accepted_cb = absl::bind_front( - &WifiLanBwuHandler::OnIncomingWifiLanConnection, this, - client), - })) { + absl::bind_front(&WifiLanBwuHandler::OnIncomingWifiLanConnection, + this, client))) { NEARBY_LOGS(ERROR) << "WifiLanBwuHandler couldn't initiate the WifiLan upgrade for " << "service " << upgrade_service_id << " and endpoint " << endpoint_id diff --git a/internal/platform/ble.cc b/internal/platform/ble.cc index a47f1852..d6792c22 100644 --- a/internal/platform/ble.cc +++ b/internal/platform/ble.cc @@ -90,31 +90,29 @@ bool BleMedium::StartAcceptingConnections(const std::string& service_id, } return impl_->StartAcceptingConnections( service_id, - { - .accepted_cb = - [this](api::BleSocket& socket, const std::string& service_id) { - MutexLock lock(&mutex_); - auto pair = sockets_.emplace( - &socket, absl::make_unique()); - auto& context = *pair.first->second; - if (!pair.second) { - NEARBY_LOG(INFO, "Accepting (again) socket=%p, impl=%p", - &context.socket, &socket); - } else { - context.socket = BleSocket(&socket); - NEARBY_LOG(INFO, "Accepting socket=%p, impl=%p", - &context.socket, &socket); - } - accepted_connection_callback_.accepted_cb(context.socket, - service_id); - }, + [this](api::BleSocket& socket, const std::string& service_id) { + MutexLock lock(&mutex_); + auto pair = sockets_.emplace( + &socket, std::make_unique()); + auto& context = *pair.first->second; + if (!pair.second) { + NEARBY_LOG(INFO, "Accepting (again) socket=%p, impl=%p", + &context.socket, &socket); + } else { + context.socket = BleSocket(&socket); + NEARBY_LOG(INFO, "Accepting socket=%p, impl=%p", &context.socket, + &socket); + } + if (accepted_connection_callback_) { + accepted_connection_callback_(context.socket, service_id); + } }); } bool BleMedium::StopAcceptingConnections(const std::string& service_id) { { MutexLock lock(&mutex_); - accepted_connection_callback_ = {}; + accepted_connection_callback_ = nullptr; sockets_.clear(); NEARBY_LOG(INFO, "Ble accepted connection disabled: impl=%p", &GetImpl()); } diff --git a/internal/platform/ble.h b/internal/platform/ble.h index df28b38f..cf1f4fab 100644 --- a/internal/platform/ble.h +++ b/internal/platform/ble.h @@ -100,10 +100,8 @@ class BleMedium final { BlePeripheral peripheral; }; - struct AcceptedConnectionCallback { - absl::AnyInvocable - accepted_cb = DefaultCallback(); - }; + using AcceptedConnectionCallback = absl::AnyInvocable; struct AcceptedConnectionInfo { BleSocket socket; }; diff --git a/internal/platform/ble_test.cc b/internal/platform/ble_test.cc index d526e3df..8f48b908 100644 --- a/internal/platform/ble_test.cc +++ b/internal/platform/ble_test.cc @@ -86,14 +86,11 @@ TEST_P(BleMediumTest, CanStartAcceptingConnectionsAndConnect) { ble_b.StartAdvertising(service_id, advertisement_bytes, fast_advertisement_service_uuid); ble_b.StartAcceptingConnections( - service_id, - AcceptedConnectionCallback{ - .accepted_cb = [&accepted_latch](BleSocket socket, - const std::string& service_id) { - NEARBY_LOG(INFO, "Connection accepted: socket=%p, service_id=%s", - &socket, service_id.c_str()); - accepted_latch.CountDown(); - }}); + service_id, [&](BleSocket socket, const std::string& service_id) { + NEARBY_LOG(INFO, "Connection accepted: socket=%p, service_id=%s", + &socket, service_id.c_str()); + accepted_latch.CountDown(); + }); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); BleSocket socket_a; @@ -148,14 +145,11 @@ TEST_P(BleMediumTest, CanCancelConnect) { ble_b.StartAdvertising(service_id, advertisement_bytes, fast_advertisement_service_uuid); ble_b.StartAcceptingConnections( - service_id, - AcceptedConnectionCallback{ - .accepted_cb = [&accepted_latch](BleSocket socket, - const std::string& service_id) { - NEARBY_LOG(INFO, "Connection accepted: socket=%p, service_id=%s", - &socket, service_id.c_str()); - accepted_latch.CountDown(); - }}); + service_id, [&](BleSocket socket, const std::string& service_id) { + NEARBY_LOG(INFO, "Connection accepted: socket=%p, service_id=%s", + &socket, service_id.c_str()); + accepted_latch.CountDown(); + }); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); BleSocket socket_a; diff --git a/internal/platform/implementation/ble.h b/internal/platform/implementation/ble.h index c97b17de..b8be0f52 100644 --- a/internal/platform/implementation/ble.h +++ b/internal/platform/implementation/ble.h @@ -99,10 +99,8 @@ class BleMedium { virtual bool StopScanning(const std::string& service_id) = 0; // Callback that is invoked when a new connection is accepted. - struct AcceptedConnectionCallback { - absl::AnyInvocable - accepted_cb = DefaultCallback(); - }; + using AcceptedConnectionCallback = absl::AnyInvocable; // Returns true once BLE socket connection requests to service_id can be // accepted. diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index a5831ccc..8ed0d104 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -576,7 +576,9 @@ void MediumEnvironment::CallBleAcceptedConnectionCallback( return; } auto& info = item->second; - info.accepted_connection_callback.accepted_cb(socket, service_id); + if (info.accepted_connection_callback) { + info.accepted_connection_callback(socket, service_id); + } }); }