Add flag to control connection timeout feature

PiperOrigin-RevId: 500822176
This commit is contained in:
Guogang Li
2023-01-09 15:26:59 -08:00
committed by Copybara-Service
parent 0d58411896
commit 350e76e690
4 changed files with 40 additions and 17 deletions
+6
View File
@@ -49,6 +49,12 @@ class FeatureFlags {
bool support_ble_v2 = false;
// Allows the code to change the bluetooth radio state
bool enable_set_radio_state = false;
// If the feature is enabled, medium connection will timeout when cannot
// create connection with remote device in a duration.
bool enable_connection_timeout = false;
// Controls to enable or disable to track the status of Bluetooth classic
// conncetion.
bool enable_bluetooth_connection_status_track = false;
};
static const FeatureFlags& GetInstance() {
@@ -18,6 +18,7 @@
#include <memory>
#include <utility>
#include "internal/platform/feature_flags.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Devices.Bluetooth.h"
#include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h"
#include "internal/platform/implementation/windows/generated/winrt/base.h"
@@ -35,9 +36,14 @@ BluetoothSocket::BluetoothSocket(StreamSocket streamSocket)
winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync(
windows_socket_.Information().RemoteHostName())
.get();
connection_status_changed_token_ =
native_bluetooth_device_.ConnectionStatusChanged(
{this, &BluetoothSocket::Listener_ConnectionStatusChanged});
if (FeatureFlags::GetInstance()
.GetFlags()
.enable_bluetooth_connection_status_track) {
connection_status_changed_token_ =
native_bluetooth_device_.ConnectionStatusChanged(
{this, &BluetoothSocket::Listener_ConnectionStatusChanged});
}
bluetooth_device_ =
std::make_unique<BluetoothDevice>(native_bluetooth_device_);
input_stream_ = BluetoothInputStream(windows_socket_.InputStream());
@@ -48,8 +54,13 @@ BluetoothSocket::BluetoothSocket() {}
BluetoothSocket::~BluetoothSocket() {
if (native_bluetooth_device_ != nullptr) {
native_bluetooth_device_.ConnectionStatusChanged(
connection_status_changed_token_);
if (FeatureFlags::GetInstance()
.GetFlags()
.enable_bluetooth_connection_status_track) {
native_bluetooth_device_.ConnectionStatusChanged(
connection_status_changed_token_);
}
native_bluetooth_device_ = nullptr;
}
}
@@ -19,6 +19,7 @@
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "internal/platform/feature_flags.h"
#include "internal/platform/implementation/windows/wifi_hotspot.h"
// Nearby connections headers
@@ -125,12 +126,14 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotMedium::ConnectToService(
});
}
connection_timeout_ = scheduled_executor_.Schedule(
[socket]() {
NEARBY_LOGS(WARNING) << "connect is closed due to timeout.";
socket.Close();
},
kWifiHotspotClientSocketConnectTimeoutMillis);
if (FeatureFlags::GetInstance().GetFlags().enable_connection_timeout) {
connection_timeout_ = scheduled_executor_.Schedule(
[socket]() {
NEARBY_LOGS(WARNING) << "connect is closed due to timeout.";
socket.Close();
},
kWifiHotspotClientSocketConnectTimeoutMillis);
}
socket.ConnectAsync(host_name, service_name).get();
if (connection_cancellation_listener_ != nullptr) {
@@ -38,6 +38,7 @@
#include "absl/time/time.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/exception.h"
#include "internal/platform/feature_flags.h"
#include "internal/platform/implementation/windows/utils.h"
#include "internal/platform/logging.h"
#include "internal/platform/runnable.h"
@@ -373,12 +374,14 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::ConnectToService(
// connection to the service
try {
connection_timeout_ = scheduled_executor_.Schedule(
[socket]() {
NEARBY_LOGS(WARNING) << "connect is closed due to timeout.";
socket.Close();
},
kConnectServiceTimeout);
if (FeatureFlags::GetInstance().GetFlags().enable_connection_timeout) {
connection_timeout_ = scheduled_executor_.Schedule(
[socket]() {
NEARBY_LOGS(WARNING) << "connect is closed due to timeout.";
socket.Close();
},
kConnectServiceTimeout);
}
socket.ConnectAsync(host_name, service_name).get();
if (connection_cancellation_listener_ != nullptr) {