Replace AcceptedConnectionCallback structs with absl::AnyInvocable

PiperOrigin-RevId: 557949490
This commit is contained in:
Janusz Sobczak
2023-08-17 15:14:25 -07:00
committed by Copybara-Service
parent 408344244f
commit e38ce96964
25 changed files with 148 additions and 216 deletions
@@ -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
+2 -10
View File
@@ -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,
+3 -1
View File
@@ -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);
}
}
});
+2 -4
View File
@@ -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<void(BleV2Socket socket, const std::string& service_id)>
accepted_cb = DefaultCallback<BleV2Socket, const std::string&>();
};
using AcceptedConnectionCallback = absl::AnyInvocable<void(
BleV2Socket socket, const std::string& service_id)>;
explicit BleV2(BluetoothRadio& bluetooth_radio);
~BleV2();
@@ -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,
@@ -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));
}
}
});
@@ -17,9 +17,9 @@
#include <cstdint>
#include <functional>
#include <string>
#include <memory>
#include <map>
#include <memory>
#include <string>
#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<void(const std::string& service_id, BluetoothSocket socket)>
accepted_cb = [](const std::string&, BluetoothSocket) {};
};
using AcceptedConnectionCallback = absl::AnyInvocable<void(
const std::string& service_id, BluetoothSocket socket)>;
explicit BluetoothClassic(BluetoothRadio& radio);
~BluetoothClassic();
@@ -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 =
@@ -31,14 +31,13 @@
namespace nearby {
namespace connections {
namespace mediums {
// Callback that is invoked when a new connection is accepted.
struct AcceptedConnectionCallback {
std::function<void(WebRtcSocketWrapper socket)> 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<void(WebRtcSocketWrapper socket)>;
WebRtc();
~WebRtc();
@@ -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));
}
}
});
@@ -29,10 +29,8 @@ namespace connections {
class WifiDirect {
public:
// Callback that is invoked when a new connection is accepted.
struct AcceptedConnectionCallback {
std::function<void(const std::string& service_id, WifiDirectSocket socket)>
accepted_cb = [](const std::string&, WifiDirectSocket) {};
};
using AcceptedConnectionCallback = absl::AnyInvocable<void(
const std::string& service_id, WifiDirectSocket socket)>;
WifiDirect() : is_go_started_(false), is_connected_to_go_(false) {}
~WifiDirect();
@@ -14,8 +14,8 @@
#include "connections/implementation/mediums/wifi_hotspot.h"
#include <utility>
#include <string>
#include <utility>
#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));
}
}
});
@@ -29,10 +29,8 @@ namespace connections {
class WifiHotspot {
public:
// Callback that is invoked when a new connection is accepted.
struct AcceptedConnectionCallback {
std::function<void(const std::string& service_id, WifiHotspotSocket socket)>
accepted_cb = [](const std::string&, WifiHotspotSocket) {};
};
using AcceptedConnectionCallback = absl::AnyInvocable<void(
const std::string& service_id, WifiHotspotSocket socket)>;
WifiHotspot() : is_hotspot_started_(false), is_connected_to_hotspot_(false) {}
~WifiHotspot();
@@ -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));
}
}
});
@@ -36,10 +36,8 @@ class WifiLan {
using DiscoveredServiceCallback = WifiLanMedium::DiscoveredServiceCallback;
// Callback that is invoked when a new connection is accepted.
struct AcceptedConnectionCallback {
std::function<void(const std::string& service_id, WifiLanSocket socket)>
accepted_cb = [](const std::string&, WifiLanSocket) {};
};
using AcceptedConnectionCallback = absl::AnyInvocable<void(
const std::string& service_id, WifiLanSocket socket)>;
WifiLan() = default;
~WifiLan();
@@ -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;
@@ -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())
@@ -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
@@ -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
@@ -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
+17 -19
View File
@@ -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<AcceptedConnectionInfo>());
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<AcceptedConnectionInfo>());
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());
}
+2 -4
View File
@@ -100,10 +100,8 @@ class BleMedium final {
BlePeripheral peripheral;
};
struct AcceptedConnectionCallback {
absl::AnyInvocable<void(BleSocket& socket, const std::string& service_id)>
accepted_cb = DefaultCallback<BleSocket&, const std::string&>();
};
using AcceptedConnectionCallback = absl::AnyInvocable<void(
BleSocket& socket, const std::string& service_id)>;
struct AcceptedConnectionInfo {
BleSocket socket;
};
+10 -16
View File
@@ -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;
+2 -4
View File
@@ -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<void(BleSocket& socket, const std::string& service_id)>
accepted_cb = DefaultCallback<BleSocket&, const std::string&>();
};
using AcceptedConnectionCallback = absl::AnyInvocable<void(
BleSocket& socket, const std::string& service_id)>;
// Returns true once BLE socket connection requests to service_id can be
// accepted.
+3 -1
View File
@@ -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);
}
});
}