Migrate to AnyInvocable in platform code

PiperOrigin-RevId: 504104740
This commit is contained in:
Janusz Sobczak
2023-01-23 16:04:21 -08:00
committed by Copybara-Service
parent 7f34968b78
commit f4d85f9b90
100 changed files with 449 additions and 494 deletions
+1
View File
@@ -47,6 +47,7 @@ cc_library(
deps = [
"//internal/platform:base",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
],
+2 -2
View File
@@ -100,7 +100,7 @@ class BleServerSocket : public api::ble_v2::BleServerSocket {
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
bool Connect(std::unique_ptr<BleSocket> socket) ABSL_LOCKS_EXCLUDED(mutex_);
void SetCloseNotifier(std::function<void()> notifier) ABSL_LOCKS_EXCLUDED(mutex_);
void SetCloseNotifier(absl::AnyInvocable<void()> notifier) ABSL_LOCKS_EXCLUDED(mutex_);
private:
Exception DoClose() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -108,7 +108,7 @@ class BleServerSocket : public api::ble_v2::BleServerSocket {
mutable absl::Mutex mutex_;
absl::CondVar cond_;
absl::flat_hash_set<std::unique_ptr<BleSocket>> pending_sockets_ ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
@@ -15,7 +15,7 @@
#import "internal/platform/implementation/apple/ble.h"
#include <CoreBluetooth/CoreBluetooth.h>
#include <functional>
#include <string>
#include <utility>
@@ -283,7 +283,7 @@ bool BleServerSocket::Connect(std::unique_ptr<BleSocket> socket) {
return true;
}
void BleServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void BleServerSocket::SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
@@ -18,7 +18,6 @@
#import <Foundation/Foundation.h>
#include <functional>
#include <memory>
#include "internal/platform/implementation/scheduled_executor.h"
@@ -16,7 +16,6 @@
#define PLATFORM_IMPL_APPLE_TIMER_H_
#ifdef __cplusplus
#include <functional>
#include <utility>
#include "internal/platform/implementation/timer.h"
@@ -29,7 +28,8 @@ class Timer : public api::Timer {
Timer() = default;
~Timer() override = default;
bool Create(int delay, int interval, std::function<void()> callback) override;
bool Create(int delay, int interval,
absl::AnyInvocable<void()> callback) override;
bool Stop() override;
@@ -14,7 +14,6 @@
#import "internal/platform/implementation/apple/timer.h"
#include <functional>
#include <utility>
#include "internal/platform/implementation/timer.h"
@@ -22,7 +21,7 @@
namespace nearby {
namespace apple {
bool Timer::Create(int delay, int interval, std::function<void()> callback) { return true; }
bool Timer::Create(int delay, int interval, absl::AnyInvocable<void()> callback) { return true; }
bool Timer::Stop() { return true; }
@@ -116,7 +116,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
bool Connect(std::unique_ptr<WifiLanSocket> socket) ABSL_LOCKS_EXCLUDED(mutex_);
void SetCloseNotifier(std::function<void()> notifier) ABSL_LOCKS_EXCLUDED(mutex_);
void SetCloseNotifier(absl::AnyInvocable<void()> notifier) ABSL_LOCKS_EXCLUDED(mutex_);
private:
Exception DoClose() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -126,7 +126,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
int port_ ABSL_GUARDED_BY(mutex_);
absl::CondVar cond_;
absl::flat_hash_set<std::unique_ptr<WifiLanSocket>> pending_sockets_ ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
@@ -255,7 +255,7 @@ bool WifiLanServerSocket::Connect(std::unique_ptr<WifiLanSocket> socket) {
return true;
}
void WifiLanServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void WifiLanServerSocket::SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
@@ -361,6 +361,7 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_type,
return false;
}
}
__block DiscoveredServiceCallback client_callback = std::move(callback);
GNCMBonjourBrowser* browser = [[GNCMBonjourBrowser alloc]
initWithServiceType:serviceType
endpointFoundHandler:^GNCMEndpointLostHandler(
@@ -376,9 +377,9 @@ bool WifiLanMedium::StartDiscovery(const std::string& service_type,
nsd_service_info.SetTxtRecords(txt_records);
}
connection_requesters_.insert({CppStringFromObjCString(serviceType), requestConnection});
callback.service_discovered_cb(nsd_service_info);
client_callback.service_discovered_cb(nsd_service_info);
return ^{
callback.service_lost_cb(nsd_service_info);
client_callback.service_lost_cb(nsd_service_info);
};
}];
{
+7 -6
View File
@@ -15,9 +15,9 @@
#ifndef PLATFORM_API_BLE_H_
#define PLATFORM_API_BLE_H_
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
@@ -77,12 +77,13 @@ class BleMedium {
// Callback that is invoked when a discovered peripheral is found or lost.
struct DiscoveredPeripheralCallback {
std::function<void(BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement)>
absl::AnyInvocable<void(BlePeripheral& peripheral,
const std::string& service_id,
bool fast_advertisement)>
peripheral_discovered_cb =
DefaultCallback<BlePeripheral&, const std::string&, bool>();
std::function<void(BlePeripheral& peripheral,
const std::string& service_id)>
absl::AnyInvocable<void(BlePeripheral& peripheral,
const std::string& service_id)>
peripheral_lost_cb =
DefaultCallback<BlePeripheral&, const std::string&>();
};
@@ -99,7 +100,7 @@ class BleMedium {
// Callback that is invoked when a new connection is accepted.
struct AcceptedConnectionCallback {
std::function<void(BleSocket& socket, const std::string& service_id)>
absl::AnyInvocable<void(BleSocket& socket, const std::string& service_id)>
accepted_cb = DefaultCallback<BleSocket&, const std::string&>();
};
@@ -17,7 +17,6 @@
#include <algorithm>
#include <cstdint>
#include <functional>
#include <limits>
#include <map>
#include <memory>
@@ -103,11 +103,11 @@ class BluetoothClassicMedium {
// and at any time afterwards, until device_lost_cb() is called.
// It is not safe to use BluetoothDevice after returning from
// device_lost_cb() callback.
std::function<void(BluetoothDevice& device)> device_discovered_cb =
absl::AnyInvocable<void(BluetoothDevice& device)> device_discovered_cb =
DefaultCallback<BluetoothDevice&>();
std::function<void(BluetoothDevice& device)> device_name_changed_cb =
absl::AnyInvocable<void(BluetoothDevice& device)> device_name_changed_cb =
DefaultCallback<BluetoothDevice&>();
std::function<void(BluetoothDevice& device)> device_lost_cb =
absl::AnyInvocable<void(BluetoothDevice& device)> device_lost_cb =
DefaultCallback<BluetoothDevice&>();
};
@@ -15,7 +15,6 @@
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_CALLBACKS_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_CALLBACKS_H_
#include <functional>
#include <ostream>
#include <string>
#include <vector>
@@ -15,7 +15,6 @@
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_STORAGE_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_CREDENTIAL_STORAGE_H_
#include <functional>
#include <string>
#include <vector>
+5 -4
View File
@@ -19,11 +19,11 @@
#include <string>
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/shared/count_down_latch.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/implementation/shared/count_down_latch.h"
namespace nearby {
namespace g3 {
@@ -135,7 +135,7 @@ bool BleServerSocket::Connect(BleSocket& socket) {
return true;
}
void BleServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void BleServerSocket::SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
@@ -303,7 +303,8 @@ bool BleMedium::StartAcceptingConnections(const std::string& service_id,
NEARBY_LOGS(INFO) << "G3 Ble StartAcceptingConnections: service_id="
<< service_id;
auto& env = MediumEnvironment::Instance();
env.UpdateBleMediumForAcceptedConnection(*this, service_id, callback);
env.UpdateBleMediumForAcceptedConnection(*this, service_id,
std::move(callback));
return true;
}
+5 -5
View File
@@ -22,14 +22,14 @@
#include "absl/container/flat_hash_set.h"
#include "absl/strings/escaping.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/implementation/ble.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/bluetooth_classic.h"
#include "internal/platform/implementation/g3/multi_thread_executor.h"
#include "internal/platform/implementation/g3/pipe.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
namespace nearby {
namespace g3 {
@@ -125,7 +125,7 @@ class BleServerSocket {
// Called by the server side of a connection before passing ownership of
// BleServerSocker to user, to track validity of a pointer to this
// server socket,
void SetCloseNotifier(std::function<void()> notifier)
void SetCloseNotifier(absl::AnyInvocable<void()> notifier)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
@@ -138,7 +138,7 @@ class BleServerSocket {
absl::Mutex mutex_;
absl::CondVar cond_;
absl::flat_hash_set<BleSocket*> pending_sockets_ ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
@@ -15,7 +15,6 @@
#include "internal/platform/implementation/g3/ble_v2.h"
#include <cstdint>
#include <functional>
#include <iostream>
#include <memory>
#include <optional>
@@ -166,7 +165,7 @@ bool BleV2ServerSocket::Connect(BleV2Socket& socket) {
return true;
}
void BleV2ServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void BleV2ServerSocket::SetCloseNotifier(absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
+2 -3
View File
@@ -16,7 +16,6 @@
#define PLATFORM_IMPL_G3_BLE_V2_H_
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -133,7 +132,7 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
// Called by the server side of a connection before passing ownership of
// BleServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier)
void SetCloseNotifier(absl::AnyInvocable<void()> notifier)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
@@ -147,7 +146,7 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket {
absl::CondVar cond_;
BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only.
absl::flat_hash_set<BleV2Socket*> pending_sockets_ ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
@@ -18,11 +18,11 @@
#include <string>
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
namespace nearby {
namespace g3 {
@@ -139,7 +139,8 @@ bool BluetoothServerSocket::Connect(BluetoothSocket& socket) {
return true;
}
void BluetoothServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void BluetoothServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
@@ -21,14 +21,14 @@
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/pipe.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/listeners.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
#include "internal/platform/implementation/g3/pipe.h"
namespace nearby {
namespace g3 {
@@ -137,7 +137,7 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
// Called by the server side of a connection before passing ownership of
// BluetoothServerSocker to user, to track validity of a pointer to this
// server socket,
void SetCloseNotifier(std::function<void()> notifier)
void SetCloseNotifier(absl::AnyInvocable<void()> notifier)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
@@ -152,7 +152,7 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only.
absl::flat_hash_set<BluetoothSocket*> pending_sockets_
ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
+2 -3
View File
@@ -15,7 +15,6 @@
#ifndef PLATFORM_IMPL_G3_TIMER_H_
#define PLATFORM_IMPL_G3_TIMER_H_
#include <functional>
#include <utility>
#include "internal/platform/implementation/timer.h"
@@ -29,7 +28,7 @@ class Timer : public api::Timer {
~Timer() override = default;
bool Create(int delay, int interval,
std::function<void()> callback) override {
absl::AnyInvocable<void()> callback) override {
if (delay < 0 || interval < 0) {
return false;
}
@@ -61,7 +60,7 @@ class Timer : public api::Timer {
}
private:
std::function<void()> callback_;
absl::AnyInvocable<void()> callback_;
bool is_stopped_ = false;
};
@@ -15,6 +15,7 @@
#include "internal/platform/implementation/g3/webrtc.h"
#include <memory>
#include <utility>
#include "internal/platform/medium_environment.h"
#include "webrtc/api/task_queue/default_task_queue_factory.h"
@@ -39,8 +40,8 @@ bool WebRtcSignalingMessenger::StartReceivingMessages(
OnSignalingMessageCallback on_message_callback,
OnSignalingCompleteCallback on_complete_callback) {
auto& env = MediumEnvironment::Instance();
env.RegisterWebRtcSignalingMessenger(self_id_, on_message_callback,
on_complete_callback);
env.RegisterWebRtcSignalingMessenger(self_id_, std::move(on_message_callback),
std::move(on_complete_callback));
return true;
}
@@ -83,7 +84,7 @@ void WebRtcMedium::CreatePeerConnection(
single_thread_executor_.Execute(
[&env, callback = std::move(callback),
peer_connection = peer_connection_or_error.MoveValue()]() {
peer_connection = peer_connection_or_error.MoveValue()]() mutable {
absl::SleepFor(env.GetPeerConnectionLatency());
callback(peer_connection);
});
@@ -14,7 +14,6 @@
#include "internal/platform/implementation/g3/wifi_direct.h"
#include <functional>
#include <iostream>
#include <memory>
#include <optional>
@@ -30,7 +29,6 @@
namespace nearby {
namespace g3 {
// Code for WifiDirectSocket
WifiDirectSocket::~WifiDirectSocket() {
absl::MutexLock lock(&mutex_);
@@ -94,7 +92,7 @@ OutputStream& WifiDirectSocket::GetLocalOutputStream() {
// Code for WifiDirectServerSocket
std::string WifiDirectServerSocket::GetName(absl::string_view ip_address,
int port) {
int port) {
return absl::StrCat(ip_address, ":", port);
}
@@ -134,7 +132,8 @@ bool WifiDirectServerSocket::Connect(WifiDirectSocket& socket) {
return true;
}
void WifiDirectServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void WifiDirectServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
@@ -291,8 +290,8 @@ std::unique_ptr<api::WifiDirectSocket> WifiDirectMedium::ConnectToService(
return socket;
}
std::unique_ptr<api::WifiDirectServerSocket>
WifiDirectMedium::ListenForService(int port) {
std::unique_ptr<api::WifiDirectServerSocket> WifiDirectMedium::ListenForService(
int port) {
auto& env = MediumEnvironment::Instance();
auto server_socket = std::make_unique<WifiDirectServerSocket>();
@@ -15,7 +15,6 @@
#ifndef PLATFORM_IMPL_G3_WIFI_DIRECT_H_
#define PLATFORM_IMPL_G3_WIFI_DIRECT_H_
#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -144,7 +143,7 @@ class WifiDirectServerSocket : public api::WifiDirectServerSocket {
// Called by the server side of a connection before passing ownership of
// WifiDirectServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier)
void SetCloseNotifier(absl::AnyInvocable<void()> notifier)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
@@ -164,7 +163,7 @@ class WifiDirectServerSocket : public api::WifiDirectServerSocket {
absl::CondVar cond_;
absl::flat_hash_set<WifiDirectSocket*> pending_sockets_
ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
@@ -14,7 +14,6 @@
#include "internal/platform/implementation/g3/wifi_hotspot.h"
#include <functional>
#include <iostream>
#include <memory>
#include <optional>
@@ -23,8 +22,8 @@
#include "absl/strings/str_format.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
@@ -99,7 +98,7 @@ OutputStream& WifiHotspotSocket::GetLocalOutputStream() {
// Code for WifiHotspotServerSocket
std::string WifiHotspotServerSocket::GetName(absl::string_view ip_address,
int port) {
int port) {
return absl::StrCat(ip_address, ":", port);
}
@@ -139,7 +138,8 @@ bool WifiHotspotServerSocket::Connect(WifiHotspotSocket& socket) {
return true;
}
void WifiHotspotServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void WifiHotspotServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
@@ -186,8 +186,7 @@ bool WifiHotspotMedium::StartWifiHotspot(
HotspotCredentials* hotspot_credentials) {
absl::MutexLock lock(&mutex_);
if (!IsInterfaceValid())
return false;
if (!IsInterfaceValid()) return false;
std::string ssid = absl::StrCat("DIRECT-", Prng().NextUint32());
hotspot_credentials->SetSSID(ssid);
@@ -199,7 +198,8 @@ bool WifiHotspotMedium::StartWifiHotspot(
auto& env = MediumEnvironment::Instance();
env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials,
/*is_ap=*/true, /*enabled=*/true);
/*is_ap=*/true,
/*enabled=*/true);
return true;
}
@@ -208,13 +208,12 @@ bool WifiHotspotMedium::StopWifiHotspot() {
absl::MutexLock lock(&mutex_);
NEARBY_LOGS(INFO) << "G3 StopWifiHotspot";
if (!IsInterfaceValid())
return false;
if (!IsInterfaceValid()) return false;
auto& env = MediumEnvironment::Instance();
env.UpdateWifiHotspotMediumForStartOrConnect(*this, /*credentials*/nullptr,
/*is_ap=*/true,
/*enabled=*/false);
env.UpdateWifiHotspotMediumForStartOrConnect(*this, /*credentials*/ nullptr,
/*is_ap=*/true,
/*enabled=*/false);
return true;
}
@@ -230,13 +229,15 @@ bool WifiHotspotMedium::ConnectWifiHotspot(
auto* remote_medium = static_cast<WifiHotspotMedium*>(
env.GetWifiHotspotMedium(hotspot_credentials->GetSSID(), {}));
if (!remote_medium) {
env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials,
/*is_ap=*/false, /*enabled=*/false);
env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials,
/*is_ap=*/false,
/*enabled=*/false);
return false;
}
env.UpdateWifiHotspotMediumForStartOrConnect(*this, hotspot_credentials,
/*is_ap=*/false, /*enabled=*/true);
/*is_ap=*/false,
/*enabled=*/true);
return true;
}
@@ -246,8 +247,9 @@ bool WifiHotspotMedium::DisconnectWifiHotspot() {
NEARBY_LOGS(INFO) << "G3 DisconnectWifiHotspot";
auto& env = MediumEnvironment::Instance();
env.UpdateWifiHotspotMediumForStartOrConnect(*this, /*credentials*/nullptr,
/*is_ap=*/false, /*enabled=*/false);
env.UpdateWifiHotspotMediumForStartOrConnect(*this, /*credentials*/ nullptr,
/*is_ap=*/false,
/*enabled=*/false);
return true;
}
@@ -259,8 +261,8 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotMedium::ConnectToService(
<< ", ip address + port=" << socket_name;
// First, find an instance of remote medium, that exposed this service.
auto& env = MediumEnvironment::Instance();
auto* remote_medium = static_cast<WifiHotspotMedium*>(
env.GetWifiHotspotMedium({}, ip_address));
auto* remote_medium =
static_cast<WifiHotspotMedium*>(env.GetWifiHotspotMedium({}, ip_address));
if (remote_medium == nullptr) {
return {};
}
@@ -318,8 +320,7 @@ WifiHotspotMedium::ListenForService(int port) {
std::string dot_decimal_ip;
std::string ip_address = env.GetFakeIPAddress();
if (ip_address.empty())
return nullptr;
if (ip_address.empty()) return nullptr;
for (auto byte : ip_address) {
absl::StrAppend(&dot_decimal_ip, absl::StrFormat("%d", byte), ".");
@@ -15,19 +15,18 @@
#ifndef PLATFORM_IMPL_G3_WIFI_HOTSPOT_H_
#define PLATFORM_IMPL_G3_WIFI_HOTSPOT_H_
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/implementation/g3/multi_thread_executor.h"
#include "internal/platform/implementation/g3/pipe.h"
#include "internal/platform/implementation/wifi_hotspot.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
namespace nearby {
namespace g3 {
@@ -64,7 +63,7 @@ class WifiHotspotSocket : public api::WifiHotspotSocket {
// Returns address of a remote WifiHotspotSocket or nullptr.
WifiHotspotSocket* GetRemoteSocket() ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true if connection exists to the (possibly closed) remote socket.
// Returns true if connection exists to the (possibly closed) remote socket.
bool IsConnected() const ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true if socket is closed.
@@ -148,7 +147,7 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
// Called by the server side of a connection before passing ownership of
// WifiHotspotServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier)
void SetCloseNotifier(absl::AnyInvocable<void()> notifier)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
@@ -168,7 +167,7 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
absl::CondVar cond_;
absl::flat_hash_set<WifiHotspotSocket*> pending_sockets_
ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
@@ -184,7 +183,7 @@ class WifiHotspotMedium : public api::WifiHotspotMedium {
WifiHotspotMedium& operator=(WifiHotspotMedium&&) = delete;
// If the WiFi Adaptor supports to start a Hotspot interface.
bool IsInterfaceValid() const override { return true;}
bool IsInterfaceValid() const override { return true; }
// Discoverer connects to server socket
std::unique_ptr<api::WifiHotspotSocket> ConnectToService(
@@ -211,7 +210,7 @@ class WifiHotspotMedium : public api::WifiHotspotMedium {
private:
// Gets error message from exception pointer
// std::string GetErrorMessage(std::exception_ptr eptr);
// std::string GetErrorMessage(std::exception_ptr eptr);
// Protects to access some members
absl::Mutex mutex_;
@@ -220,7 +219,6 @@ class WifiHotspotMedium : public api::WifiHotspotMedium {
ABSL_GUARDED_BY(mutex_);
};
} // namespace g3
} // namespace nearby
@@ -22,8 +22,8 @@
#include "absl/strings/escaping.h"
#include "absl/strings/str_format.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/nsd_service_info.h"
@@ -146,7 +146,8 @@ bool WifiLanServerSocket::Connect(WifiLanSocket& socket) {
return true;
}
void WifiLanServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void WifiLanServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
absl::MutexLock lock(&mutex_);
close_notifier_ = std::move(notifier);
}
@@ -22,13 +22,13 @@
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/implementation/g3/multi_thread_executor.h"
#include "internal/platform/implementation/g3/pipe.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/nsd_service_info.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/implementation/g3/multi_thread_executor.h"
#include "internal/platform/implementation/g3/pipe.h"
namespace nearby {
namespace g3 {
@@ -144,7 +144,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
// Called by the server side of a connection before passing ownership of
// WifiLanServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier)
void SetCloseNotifier(absl::AnyInvocable<void()> notifier)
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
@@ -159,7 +159,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
int port_ ABSL_GUARDED_BY(mutex_);
absl::CondVar cond_;
absl::flat_hash_set<WifiLanSocket*> pending_sockets_ ABSL_GUARDED_BY(mutex_);
std::function<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> close_notifier_ ABSL_GUARDED_BY(mutex_);
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
};
@@ -15,12 +15,11 @@
#ifndef PLATFORM_API_LISTENABLE_FUTURE_H_
#define PLATFORM_API_LISTENABLE_FUTURE_H_
#include <functional>
#include <memory>
#include "internal/platform/exception.h"
#include "internal/platform/implementation/executor.h"
#include "internal/platform/implementation/future.h"
#include "internal/platform/exception.h"
#include "internal/platform/runnable.h"
namespace nearby {
@@ -16,7 +16,6 @@
#define PLATFORM_API_SCHEDULED_EXECUTOR_H_
#include <cstdint>
#include <functional>
#include <memory>
#include "absl/time/time.h"
@@ -15,7 +15,6 @@
#ifndef PLATFORM_API_SUBMITTABLE_EXECUTOR_H_
#define PLATFORM_API_SUBMITTABLE_EXECUTOR_H_
#include <functional>
#include <memory>
#include "internal/platform/implementation/executor.h"
@@ -28,7 +27,7 @@ namespace api {
// Main interface to be used by platform as a base class for
// - MultiThreadExecutorWrapper
// - SingleThreadExecutorWrapper
// Platform must override bool submit(std::function<void()>) method.
// Platform must override bool submit(absl::AnyInvocable<void()>) method.
class SubmittableExecutor : public Executor {
public:
~SubmittableExecutor() override = default;
+2 -2
View File
@@ -15,7 +15,7 @@
#ifndef PLATFORM_API_TIMER_H_
#define PLATFORM_API_TIMER_H_
#include <functional>
#include "absl/functional/any_invocable.h"
namespace nearby {
namespace api {
@@ -35,7 +35,7 @@ class Timer {
//
// @return return true if success, otherwise false
virtual bool Create(int delay, int interval,
std::function<void()> callback) = 0;
absl::AnyInvocable<void()> callback) = 0;
// Stops timer. No timer signal is sent after the call.
virtual bool Stop() = 0;
+4 -5
View File
@@ -15,7 +15,6 @@
#ifndef PLATFORM_API_WEBRTC_H_
#define PLATFORM_API_WEBRTC_H_
#include <functional>
#include <memory>
#include <string>
@@ -29,8 +28,8 @@ namespace api {
class WebRtcSignalingMessenger {
public:
using OnSignalingMessageCallback = std::function<void(const ByteArray&)>;
using OnSignalingCompleteCallback = std::function<void(bool)>;
using OnSignalingMessageCallback = absl::AnyInvocable<void(const ByteArray&)>;
using OnSignalingCompleteCallback = absl::AnyInvocable<void(bool)>;
virtual ~WebRtcSignalingMessenger() = default;
@@ -45,8 +44,8 @@ class WebRtcSignalingMessenger {
class WebRtcMedium {
public:
using PeerConnectionCallback =
std::function<void(rtc::scoped_refptr<webrtc::PeerConnectionInterface>)>;
using PeerConnectionCallback = absl::AnyInvocable<void(
rtc::scoped_refptr<webrtc::PeerConnectionInterface>)>;
virtual ~WebRtcMedium() = default;
+3 -3
View File
@@ -99,9 +99,9 @@ class WifiLanMedium {
// Callback that is invoked when a discovered service is found or lost.
struct DiscoveredServiceCallback {
std::function<void(NsdServiceInfo service_info)> service_discovered_cb =
DefaultCallback<NsdServiceInfo>();
std::function<void(NsdServiceInfo service_info)> service_lost_cb =
absl::AnyInvocable<void(NsdServiceInfo service_info)>
service_discovered_cb = DefaultCallback<NsdServiceInfo>();
absl::AnyInvocable<void(NsdServiceInfo service_info)> service_lost_cb =
DefaultCallback<NsdServiceInfo>();
};
@@ -17,7 +17,6 @@
#include <guiddef.h>
#include <functional>
#include <future> // NOLINT
#include <memory>
#include <string>
@@ -121,4 +120,3 @@ class BleMedium : public api::BleMedium {
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_MEDIUM_H_
@@ -229,8 +229,7 @@ TEST(BleMedium, DISABLED_StartAdvertising_ExtendedAdvertising) {
ByteArray advertising_data(advertising_data_byte_array);
EXPECT_TRUE(
ble_medium.StartAdvertising("NearbyShare", advertising_data, ""));
EXPECT_TRUE(ble_medium.StartAdvertising("NearbyShare", advertising_data, ""));
}
TEST(BleMedium, DISABLED_StopAdvertising) {
@@ -254,35 +253,30 @@ TEST(BleMedium, DISABLED_StartScanning) {
BluetoothAdapter bluetoothAdapter;
BleMedium ble_medium(bluetoothAdapter);
BleMedium::DiscoveredPeripheralCallback discovered_peripheral_callback = {
.peripheral_discovered_cb = [this](api::BlePeripheral& peripheral,
const std::string& service_id,
bool fast_advertisement) {},
.peripheral_lost_cb = [this](api::BlePeripheral& peripheral,
const std::string& service_id) {}};
EXPECT_TRUE(ble_medium.StartScanning("NearbyShare", "\xfe\xf3",
discovered_peripheral_callback));
EXPECT_TRUE(ble_medium.StartScanning(
"NearbyShare", "\xfe\xf3",
{.peripheral_discovered_cb = [](api::BlePeripheral& peripheral,
const std::string& service_id,
bool fast_advertisement) {},
.peripheral_lost_cb = [](api::BlePeripheral& peripheral,
const std::string& service_id) {}}));
}
TEST(BleMedium, DISABLED_ReceiveAdvertisement) {
absl::Notification advertisement_received_notification;
BluetoothAdapter bluetoothAdapter;
BleMedium ble_medium(bluetoothAdapter);
BleMedium::DiscoveredPeripheralCallback discovered_peripheral_callback = {
.peripheral_discovered_cb =
[this, &advertisement_received_notification](
api::BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) {
advertisement_received_notification.Notify();
},
.peripheral_lost_cb = [this](api::BlePeripheral& peripheral,
const std::string& service_id) {}};
EXPECT_TRUE(ble_medium.StartScanning(
"NearbyShare", "\xfe\xf3",
{.peripheral_discovered_cb =
[&](api::BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) {
advertisement_received_notification.Notify();
},
.peripheral_lost_cb = [](api::BlePeripheral& peripheral,
const std::string& service_id) {}}));
EXPECT_TRUE(ble_medium.StartScanning("NearbyShare", "\xfe\xf3",
discovered_peripheral_callback));
EXPECT_TRUE(
advertisement_received_notification.WaitForNotificationWithTimeout(
absl::Seconds(5)));
@@ -292,15 +286,14 @@ TEST(BleMedium, DISABLED_StopScanning) {
BluetoothAdapter bluetoothAdapter;
BleMedium ble_medium(bluetoothAdapter);
BleMedium::DiscoveredPeripheralCallback discovered_peripheral_callback = {
.peripheral_discovered_cb =
[this](api::BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) { EXPECT_TRUE(fast_advertisement); },
.peripheral_lost_cb = [this](api::BlePeripheral& peripheral,
const std::string& service_id) {}};
EXPECT_TRUE(ble_medium.StartScanning(
"NearbyShare", "\xfe\xf3",
{.peripheral_discovered_cb =
[](api::BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) { EXPECT_TRUE(fast_advertisement); },
.peripheral_lost_cb = [](api::BlePeripheral& peripheral,
const std::string& service_id) {}}));
EXPECT_TRUE(ble_medium.StartScanning("NearbyShare", "\xfe\xf3",
discovered_peripheral_callback));
EXPECT_TRUE(ble_medium.StopScanning("NearbyShare"));
}
@@ -15,7 +15,6 @@
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_
#include <functional>
#include <memory>
#include <string>
@@ -107,18 +106,20 @@ class BleV2Medium : public api::ble_v2::BleMedium {
BluetoothLEAdvertisementPublisher publisher,
winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementPublisherStatusChangedEventArgs args);
std::function<void()> publisher_started_callback_ ABSL_GUARDED_BY(mutex_);
std::function<void()> publisher_stopped_callback_ ABSL_GUARDED_BY(mutex_);
std::function<void()> publisher_error_callback_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> publisher_started_callback_
ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> publisher_stopped_callback_
ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> publisher_error_callback_ ABSL_GUARDED_BY(mutex_);
winrt::event_token watcher_token_;
void WatcherHandler(winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementWatcher watcher,
winrt::Windows::Devices::Bluetooth::Advertisement::
BluetoothLEAdvertisementWatcherStoppedEventArgs args);
std::function<void()> watcher_started_callback_ ABSL_GUARDED_BY(mutex_);
std::function<void()> watcher_stopped_callback_ ABSL_GUARDED_BY(mutex_);
std::function<void()> watcher_error_callback_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> watcher_started_callback_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> watcher_stopped_callback_ ABSL_GUARDED_BY(mutex_);
absl::AnyInvocable<void()> watcher_error_callback_ ABSL_GUARDED_BY(mutex_);
winrt::event_token advertisement_received_token_;
void AdvertisementReceivedHandler(
@@ -20,10 +20,11 @@
#include <guiddef.h>
// clang-format on
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include "absl/functional/any_invocable.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Devices.Bluetooth.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Devices.Radios.h"
@@ -52,7 +53,8 @@ class BluetoothAdapter : public api::BluetoothAdapter {
~BluetoothAdapter() override = default;
typedef std::function<void(api::BluetoothAdapter::ScanMode)> ScanModeCallback;
typedef absl::AnyInvocable<void(api::BluetoothAdapter::ScanMode)>
ScanModeCallback;
// Synchronously sets the status of the BluetoothAdapter to 'status', and
// returns true if the operation was a success.
@@ -88,8 +90,8 @@ class BluetoothAdapter : public api::BluetoothAdapter {
std::string GetNameFromComputerName() const;
void SetOnScanModeChanged(ScanModeCallback callback) {
if (scan_mode_changed_ == nullptr) {
scan_mode_changed_ = callback;
if (scan_mode_changed_) {
scan_mode_changed_ = std::move(callback);
}
}
@@ -111,7 +113,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
void find_and_replace(char *source, const char *strFind,
const char *strReplace) const;
ScanMode scan_mode_ = ScanMode::kNone;
ScanModeCallback scan_mode_changed_ = nullptr;
ScanModeCallback scan_mode_changed_;
// Used to fake the device name when the device name is longer than android
// limitation.
@@ -163,16 +163,14 @@ TEST(BluetoothAdapter, DISABLED_SetOnScanModeChanged) {
api::BluetoothAdapter::ScanMode mode =
api::BluetoothAdapter::ScanMode::kUnknown;
absl::Notification scan_mode_changed_notification;
BluetoothAdapter::ScanModeCallback callback =
[&mode, &scan_mode_changed_notification](
api::BluetoothAdapter::ScanMode scan_mode) {
mode = scan_mode;
scan_mode_changed_notification.Notify();
};
BluetoothAdapter bluetooth_adapter;
bluetooth_adapter.SetScanMode(mode);
bluetooth_adapter.SetOnScanModeChanged(callback);
bluetooth_adapter.SetOnScanModeChanged(
[&](api::BluetoothAdapter::ScanMode scan_mode) {
mode = scan_mode;
scan_mode_changed_notification.Notify();
});
bluetooth_adapter.SetScanMode(
api::BluetoothAdapter::ScanMode::kConnectableDiscoverable);
EXPECT_TRUE(scan_mode_changed_notification.WaitForNotificationWithTimeout(
@@ -154,7 +154,7 @@ bool BluetoothClassicMedium::StartDiscovery(
NEARBY_LOGS(INFO) << "StartDiscovery is called.";
bool result = false;
discovery_callback_ = discovery_callback;
discovery_callback_ = std::move(discovery_callback);
if (!IsWatcherStarted()) {
result = StartScanning();
@@ -16,7 +16,6 @@
#include <codecvt>
#include <exception>
#include <functional>
#include <locale>
#include <memory>
#include <string>
@@ -55,7 +54,8 @@ std::unique_ptr<api::BluetoothSocket> BluetoothServerSocket::Accept() {
return std::make_unique<BluetoothSocket>(bluetooth_socket);
}
void BluetoothServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void BluetoothServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
close_notifier_ = std::move(notifier);
}
@@ -17,7 +17,6 @@
#include <Windows.h>
#include <functional>
#include <memory>
#include <queue>
#include <string>
@@ -74,7 +73,7 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
// Called by the server side of a connection before passing ownership of
// WifiLanServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier);
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
bool listen();
@@ -98,7 +97,7 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
winrt::event_token listener_event_token_{};
// Close notifier
std::function<void()> close_notifier_ = nullptr;
absl::AnyInvocable<void()> close_notifier_ = nullptr;
// IP addresses of the computer. mDNS uses them to advertise.
std::vector<std::string> ip_addresses_{};
@@ -24,7 +24,7 @@ namespace windows {
// Main interface to be used by platform as a base class for
// - MultiThreadExecutorWrapper
// - SingleThreadExecutorWrapper
// Platform must override bool submit(std::function<void()>) method.
// Platform must override bool submit(absl::AnyInvocable<void()>) method.
class SubmittableExecutor : public api::SubmittableExecutor {
public:
SubmittableExecutor();
@@ -14,7 +14,6 @@
#include "internal/platform/implementation/windows/thread_pool.h"
#include <functional>
#include <vector>
#include "gtest/gtest.h"
@@ -14,7 +14,6 @@
#include "internal/platform/implementation/windows/timer.h"
#include <functional>
#include <memory>
#include "internal/platform/logging.h"
@@ -24,7 +23,8 @@ namespace windows {
Timer::~Timer() { Stop(); }
bool Timer::Create(int delay, int interval, std::function<void()> callback) {
bool Timer::Create(int delay, int interval,
absl::AnyInvocable<void()> callback) {
if ((delay < 0) || (interval < 0)) {
NEARBY_LOGS(WARNING) << "Delay and interval shouldn\'t be negative value.";
return false;
@@ -90,8 +90,8 @@ bool Timer::FireNow() {
}
void CALLBACK Timer::TimerRoutine(PVOID lpParam, BOOLEAN TimerOrWaitFired) {
std::function<void()>* callback =
reinterpret_cast<std::function<void()>*>(lpParam);
absl::AnyInvocable<void()>* callback =
reinterpret_cast<absl::AnyInvocable<void()>*>(lpParam);
if (*callback != NULL) {
(*callback)();
}
@@ -17,8 +17,6 @@
#include <windows.h>
#include <functional>
#include "internal/platform/implementation/timer.h"
namespace nearby {
@@ -29,7 +27,8 @@ class Timer : public api::Timer {
Timer() = default;
~Timer() override;
bool Create(int delay, int interval, std::function<void()> callback) override;
bool Create(int delay, int interval,
absl::AnyInvocable<void()> callback) override;
bool Stop() override;
bool FireNow() override;
@@ -38,7 +37,7 @@ class Timer : public api::Timer {
int delay_;
int interval_;
std::function<void()> callback_;
absl::AnyInvocable<void()> callback_;
HANDLE handle_ = NULL;
HANDLE timer_queue_handle_ = NULL;
};
@@ -14,8 +14,8 @@
#include "internal/platform/implementation/timer.h"
#include <chrono> // NOLINT
#include <functional> // NOLINT
#include <chrono> // NOLINT
// NOLINT
#include <memory>
#include <thread> // NOLINT
@@ -28,26 +28,24 @@ namespace {
TEST(Timer, TestCreateTimer) {
int count = 0;
std::function<void()> callback = [&count]() { ++count; };
std::unique_ptr<nearby::api::Timer> timer =
nearby::api::ImplementationPlatform::CreateTimer();
ASSERT_TRUE(timer != nullptr);
EXPECT_FALSE(timer->Create(-100, 0, callback));
EXPECT_FALSE(timer->Create(-100, 0, [&]() { ++count; }));
EXPECT_TRUE(timer->Stop());
}
// This test case cannot run on Google3
TEST(Timer, DISABLED_TestRepeatTimer) {
int count = 0;
std::function<void()> callback = [&count]() { count++; };
std::unique_ptr<nearby::api::Timer> timer =
nearby::api::ImplementationPlatform::CreateTimer();
ASSERT_TRUE(timer != nullptr);
EXPECT_TRUE(timer->Create(300, 300, callback));
EXPECT_TRUE(timer->Create(300, 300, [&]() { ++count; }));
std::this_thread::sleep_for(std::chrono::seconds(1));
EXPECT_TRUE(timer->Stop());
EXPECT_EQ(count, 3);
@@ -55,10 +53,11 @@ TEST(Timer, DISABLED_TestRepeatTimer) {
TEST(Timer, DISABLED_TestFireNow) {
int count = 0;
std::function<void()> callback = [&count]() { ++count; };
auto timer = nearby::api::ImplementationPlatform::CreateTimer();
EXPECT_TRUE(timer != nullptr);
EXPECT_TRUE(timer->Create(3000, 3000, callback));
EXPECT_TRUE(timer->Create(3000, 3000, [&]() { ++count; }));
EXPECT_TRUE(timer->FireNow());
EXPECT_TRUE(timer->Stop());
EXPECT_EQ(count, 1);
@@ -22,7 +22,6 @@
// Standard C/C++ headers
#include <deque>
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -191,7 +190,7 @@ class WifiDirectServerSocket : public api::WifiDirectServerSocket {
// Called by the server side of a connection before passing ownership of
// WifiDirectServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier);
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override;
@@ -217,7 +216,7 @@ class WifiDirectServerSocket : public api::WifiDirectServerSocket {
winrt::event_token listener_event_token_{};
// Close notifier
std::function<void()> close_notifier_ = nullptr;
absl::AnyInvocable<void()> close_notifier_ = nullptr;
// IP addresses of the computer. mDNS uses them to advertise.
std::vector<std::string> ip_addresses_{};
@@ -15,7 +15,6 @@
#include <windows.h>
#include <exception>
#include <functional>
#include <memory>
#include <string>
#include <vector>
@@ -82,7 +81,8 @@ std::unique_ptr<api::WifiDirectSocket> WifiDirectServerSocket::Accept() {
return std::make_unique<WifiDirectSocket>(wifi_direct_socket);
}
void WifiDirectServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void WifiDirectServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
close_notifier_ = std::move(notifier);
}
@@ -20,7 +20,7 @@
#include <wlanapi.h>
// Standard C/C++ headers
#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -185,7 +185,7 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
// Called by the server side of a connection before passing ownership of
// WifiHotspotServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier);
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override;
@@ -211,7 +211,7 @@ class WifiHotspotServerSocket : public api::WifiHotspotServerSocket {
winrt::event_token listener_event_token_{};
// Close notifier
std::function<void()> close_notifier_ = nullptr;
absl::AnyInvocable<void()> close_notifier_ = nullptr;
// IP addresses of the computer. mDNS uses them to advertise.
std::vector<std::string> ip_addresses_{};
@@ -15,7 +15,6 @@
#include <windows.h>
#include <exception>
#include <functional>
#include <memory>
#include <string>
#include <vector>
@@ -82,7 +81,8 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotServerSocket::Accept() {
return std::make_unique<WifiHotspotSocket>(wifi_hotspot_socket);
}
void WifiHotspotServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void WifiHotspotServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
close_notifier_ = std::move(notifier);
}
@@ -23,7 +23,6 @@
// Standard C/C++ headers
#include <exception>
#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -35,8 +34,8 @@
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/implementation/windows/scheduled_executor.h"
@@ -187,7 +186,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
// Called by the server side of a connection before passing ownership of
// WifiLanServerSocker to user, to track validity of a pointer to this
// server socket.
void SetCloseNotifier(std::function<void()> notifier);
void SetCloseNotifier(absl::AnyInvocable<void()> notifier);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override;
@@ -208,7 +207,7 @@ class WifiLanServerSocket : public api::WifiLanServerSocket {
winrt::event_token listener_event_token_{};
// Close notifier
std::function<void()> close_notifier_ = nullptr;
absl::AnyInvocable<void()> close_notifier_ = nullptr;
// IP addresses of the computer. mDNS uses them to advertise.
std::vector<std::string> ip_addresses_{};
@@ -15,7 +15,6 @@
#include <windows.h>
#include <exception>
#include <functional>
#include <memory>
#include <string>
#include <vector>
@@ -84,7 +83,8 @@ std::unique_ptr<api::WifiLanSocket> WifiLanServerSocket::Accept() {
return std::make_unique<WifiLanSocket>(wifi_lan_socket);
}
void WifiLanServerSocket::SetCloseNotifier(std::function<void()> notifier) {
void WifiLanServerSocket::SetCloseNotifier(
absl::AnyInvocable<void()> notifier) {
close_notifier_ = std::move(notifier);
}