mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 07:06:11 -04:00
Roll forward to cl/318180324
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I71bb49fe74104964088d392b12775ec2a253b1c9
This commit is contained in:
@@ -164,6 +164,15 @@ class BasePcpHandler : public PcpHandler,
|
||||
// instance (but it can if implementation desires to do so).
|
||||
// BasePcpHandler will hold on to the shared_ptr<DiscoveredEndpoint>.
|
||||
struct DiscoveredEndpoint {
|
||||
DiscoveredEndpoint(std::string endpoint_id, std::string endpoint_name,
|
||||
std::string service_id,
|
||||
proto::connections::Medium medium)
|
||||
: endpoint_id(std::move(endpoint_id)),
|
||||
endpoint_name(std::move(endpoint_name)),
|
||||
service_id(std::move(service_id)),
|
||||
medium(medium) {}
|
||||
virtual ~DiscoveredEndpoint() = default;
|
||||
|
||||
std::string endpoint_id;
|
||||
std::string endpoint_name;
|
||||
std::string service_id;
|
||||
|
||||
@@ -124,6 +124,10 @@ class MockContext {
|
||||
};
|
||||
|
||||
struct MockDiscoveredEndpoint : public MockPcpHandler::DiscoveredEndpoint {
|
||||
MockDiscoveredEndpoint(DiscoveredEndpoint endpoint, MockContext context)
|
||||
: DiscoveredEndpoint(std::move(endpoint)),
|
||||
context(std::move(context)) {}
|
||||
|
||||
MockContext context;
|
||||
};
|
||||
|
||||
@@ -262,10 +266,10 @@ class BasePcpHandlerTest : public ::testing::Test {
|
||||
pcp_handler->OnEndpointFound(
|
||||
client, std::make_shared<MockDiscoveredEndpoint>(MockDiscoveredEndpoint{
|
||||
{
|
||||
.endpoint_id = endpoint_id,
|
||||
.endpoint_name = info.name,
|
||||
.service_id = "service",
|
||||
.medium = Medium::BLE,
|
||||
endpoint_id,
|
||||
info.name,
|
||||
"service",
|
||||
Medium::BLE,
|
||||
},
|
||||
MockContext{flag},
|
||||
}));
|
||||
|
||||
@@ -34,9 +34,9 @@ BluetoothDeviceName::BluetoothDeviceName(Version version, Pcp pcp,
|
||||
|
||||
version_ = version;
|
||||
pcp_ = pcp;
|
||||
endpoint_id_ = endpoint_id;
|
||||
endpoint_id_ = std::string(endpoint_id);
|
||||
service_id_hash_ = service_id_hash;
|
||||
endpoint_name_ = endpoint_name;
|
||||
endpoint_name_ = std::string(endpoint_name);
|
||||
}
|
||||
|
||||
BluetoothDeviceName::BluetoothDeviceName(
|
||||
|
||||
@@ -16,6 +16,8 @@ WifiLan& Mediums::GetWifiLan() {
|
||||
return wifi_lan_;
|
||||
}
|
||||
|
||||
mediums::WebRtc& Mediums::GetWebRtc() { return webrtc_; }
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
#include "core_v2/internal/mediums/bluetooth_classic.h"
|
||||
#include "core_v2/internal/mediums/bluetooth_radio.h"
|
||||
#include "core_v2/internal/mediums/webrtc.h"
|
||||
#include "core_v2/internal/mediums/wifi_lan.h"
|
||||
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
@@ -25,6 +25,9 @@ class Mediums {
|
||||
// Returns a handle to the Wifi-Lan medium.
|
||||
WifiLan& GetWifiLan();
|
||||
|
||||
// Returns a handle to the WebRtc medium.
|
||||
mediums::WebRtc& GetWebRtc();
|
||||
|
||||
private:
|
||||
// The order of declaration is critical for both construction and
|
||||
// destruction.
|
||||
@@ -37,6 +40,7 @@ class Mediums {
|
||||
BluetoothRadio bluetooth_radio_;
|
||||
BluetoothClassic bluetooth_classic_{bluetooth_radio_};
|
||||
WifiLan wifi_lan_;
|
||||
mediums::WebRtc webrtc_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -111,14 +111,13 @@ WebRtcSocketWrapper WebRtc::Connect(const PeerId& peer_id) {
|
||||
NEARBY_LOG(INFO, "Attempting to make a WebRTC connection to %s.",
|
||||
peer_id.GetId().c_str());
|
||||
|
||||
std::shared_ptr<Future<WebRtcSocketWrapper>> socket_future =
|
||||
ListenForWebRtcSocketFuture(connection_flow_->GetDataChannel(),
|
||||
AcceptedConnectionCallback());
|
||||
Future<WebRtcSocketWrapper> socket_future = ListenForWebRtcSocketFuture(
|
||||
connection_flow_->GetDataChannel(), AcceptedConnectionCallback());
|
||||
|
||||
// The two devices have discovered each other, hence we have a timeout for
|
||||
// establishing the transport channel.
|
||||
ExceptionOr<WebRtcSocketWrapper> result =
|
||||
socket_future->Get(kDataChannelTimeout);
|
||||
socket_future.Get(kDataChannelTimeout);
|
||||
if (result.ok()) return result.result();
|
||||
|
||||
Disconnect();
|
||||
@@ -149,18 +148,17 @@ void WebRtc::StopAcceptingConnections() {
|
||||
NEARBY_LOG(INFO, "Stopped accepting WebRTC connections");
|
||||
}
|
||||
|
||||
std::shared_ptr<Future<WebRtcSocketWrapper>>
|
||||
WebRtc::ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>*
|
||||
Future<WebRtcSocketWrapper> WebRtc::ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
data_channel_future,
|
||||
AcceptedConnectionCallback callback) {
|
||||
auto socket_future = std::make_shared<Future<WebRtcSocketWrapper>>();
|
||||
Future<WebRtcSocketWrapper> socket_future;
|
||||
auto data_channel_runnable = [this, socket_future, data_channel_future,
|
||||
callback{std::move(callback)}]() {
|
||||
callback{std::move(callback)}]() mutable {
|
||||
// The overall timeout of creating the socket and data channel is controlled
|
||||
// by the caller of this function.
|
||||
ExceptionOr<rtc::scoped_refptr<webrtc::DataChannelInterface>> res =
|
||||
data_channel_future->Get();
|
||||
data_channel_future.Get();
|
||||
if (res.ok()) {
|
||||
WebRtcSocketWrapper wrapper = CreateWebRtcSocketWrapper(res.result());
|
||||
callback.accepted_cb(wrapper);
|
||||
@@ -168,15 +166,15 @@ WebRtc::ListenForWebRtcSocketFuture(
|
||||
MutexLock lock(&mutex_);
|
||||
socket_ = wrapper;
|
||||
}
|
||||
socket_future->Set(wrapper);
|
||||
socket_future.Set(wrapper);
|
||||
} else {
|
||||
NEARBY_LOG(WARNING, "Failed to get WebRtcSocket.");
|
||||
socket_future->Set(WebRtcSocketWrapper());
|
||||
socket_future.Set(WebRtcSocketWrapper());
|
||||
}
|
||||
};
|
||||
|
||||
data_channel_future->AddListener(std::move(data_channel_runnable),
|
||||
&single_thread_executor_);
|
||||
data_channel_future.AddListener(std::move(data_channel_runnable),
|
||||
&single_thread_executor_);
|
||||
|
||||
return socket_future;
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ class WebRtc {
|
||||
bool InitWebRtcFlow(Role role, const PeerId& self_id)
|
||||
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
||||
|
||||
std::shared_ptr<Future<WebRtcSocketWrapper>> ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>*
|
||||
Future<WebRtcSocketWrapper> ListenForWebRtcSocketFuture(
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
data_channel_future,
|
||||
AcceptedConnectionCallback callback);
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
constexpr absl::Duration ConnectionFlow::kTimeout;
|
||||
|
||||
namespace {
|
||||
// This is the same as the nearby data channel name.
|
||||
const char kDataChannelName[] = "dataChannel";
|
||||
@@ -217,9 +219,9 @@ bool ConnectionFlow::OnRemoteIceCandidatesReceived(
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>*
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
ConnectionFlow::GetDataChannel() {
|
||||
return &data_channel_future_;
|
||||
return data_channel_future_;
|
||||
}
|
||||
|
||||
bool ConnectionFlow::Close() {
|
||||
|
||||
@@ -87,7 +87,7 @@ class ConnectionFlow {
|
||||
std::vector<std::unique_ptr<webrtc::IceCandidateInterface>>
|
||||
ice_candidates) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
// Get a future for the data channel.
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>>* GetDataChannel();
|
||||
Future<rtc::scoped_refptr<webrtc::DataChannelInterface>> GetDataChannel();
|
||||
// Close the peer connection and data channel.
|
||||
bool Close() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
|
||||
@@ -80,10 +80,10 @@ TEST(ConnectionFlowTest, SuccessfulOfferAnswerFlow) {
|
||||
|
||||
// Retrieve Data Channels
|
||||
ExceptionOr<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
offerer_channel = offerer->GetDataChannel()->Get(absl::Seconds(1));
|
||||
offerer_channel = offerer->GetDataChannel().Get(absl::Seconds(1));
|
||||
EXPECT_TRUE(offerer_channel.ok());
|
||||
ExceptionOr<rtc::scoped_refptr<webrtc::DataChannelInterface>>
|
||||
answerer_channel = answerer->GetDataChannel()->Get(absl::Seconds(1));
|
||||
answerer_channel = answerer->GetDataChannel().Get(absl::Seconds(1));
|
||||
EXPECT_TRUE(answerer_channel.ok());
|
||||
|
||||
// Send message on data channel
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
#include "core_v2/internal/p2p_cluster_pcp_handler.h"
|
||||
|
||||
#include "core_v2/internal/base_pcp_handler.h"
|
||||
#include "core_v2/internal/bluetooth_endpoint_channel.h"
|
||||
#include "core_v2/internal/mediums/webrtc/webrtc_socket_wrapper.h"
|
||||
#include "core_v2/internal/webrtc_endpoint_channel.h"
|
||||
#include "core_v2/internal/wifi_lan_endpoint_channel.h"
|
||||
#include "platform_v2/base/types.h"
|
||||
#include "platform_v2/public/crypto.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
|
||||
@@ -23,22 +27,24 @@ P2pClusterPcpHandler::P2pClusterPcpHandler(
|
||||
: BasePcpHandler(endpoint_manager, endpoint_channel_manager, pcp),
|
||||
bluetooth_radio_(mediums.GetBluetoothRadio()),
|
||||
bluetooth_medium_(mediums.GetBluetoothClassic()),
|
||||
wifi_lan_medium_(mediums.GetWifiLan()) {}
|
||||
wifi_lan_medium_(mediums.GetWifiLan()),
|
||||
webrtc_medium_(mediums.GetWebRtc()) {}
|
||||
|
||||
// Returns a vector or mediums sorted in order or decreasing priority for
|
||||
// all the supported mediums.
|
||||
// NOTE: currently we only have BT, but eventually it will be more, and items
|
||||
// will have to be sorted in the order of decreasing traffic bandwidth.
|
||||
// Example: WiFi_LAN, BT, BLE
|
||||
// Example: WiFi_LAN, WEB_RTC, BT, BLE
|
||||
std::vector<proto::connections::Medium>
|
||||
P2pClusterPcpHandler::GetConnectionMediumsByPriority() {
|
||||
std::vector<proto::connections::Medium> mediums;
|
||||
if (bluetooth_medium_.IsAvailable()) {
|
||||
mediums.push_back(proto::connections::BLUETOOTH);
|
||||
}
|
||||
if (wifi_lan_medium_.IsAvailable()) {
|
||||
mediums.push_back(proto::connections::WIFI_LAN);
|
||||
}
|
||||
if (webrtc_medium_.IsAvailable()) {
|
||||
mediums.push_back(proto::connections::WEB_RTC);
|
||||
}
|
||||
if (bluetooth_medium_.IsAvailable()) {
|
||||
mediums.push_back(proto::connections::BLUETOOTH);
|
||||
}
|
||||
return mediums;
|
||||
}
|
||||
|
||||
@@ -52,16 +58,6 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
|
||||
const std::string& local_endpoint_name, const ConnectionOptions& options) {
|
||||
std::vector<proto::connections::Medium> mediums_started_successfully;
|
||||
|
||||
const ByteArray bluetooth_hash =
|
||||
GenerateHash(service_id, BluetoothDeviceName::kServiceIdHashLength);
|
||||
proto::connections::Medium bluetooth_medium =
|
||||
StartBluetoothAdvertising(client, service_id, bluetooth_hash,
|
||||
local_endpoint_id, local_endpoint_name);
|
||||
if (bluetooth_medium != proto::connections::UNKNOWN_MEDIUM) {
|
||||
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: BT added");
|
||||
mediums_started_successfully.push_back(bluetooth_medium);
|
||||
}
|
||||
|
||||
const ByteArray wifi_lan_hash =
|
||||
GenerateHash(service_id, WifiLanServiceInfo::kServiceIdHashLength);
|
||||
proto::connections::Medium wifi_lan_medium =
|
||||
@@ -73,6 +69,22 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
|
||||
mediums_started_successfully.push_back(wifi_lan_medium);
|
||||
}
|
||||
|
||||
proto::connections::Medium webrtc_medium = StartListeningForWebRtcConnections(
|
||||
client, service_id, local_endpoint_id, local_endpoint_name);
|
||||
if (webrtc_medium != proto::connections::UNKNOWN_MEDIUM) {
|
||||
mediums_started_successfully.push_back(webrtc_medium);
|
||||
}
|
||||
|
||||
const ByteArray bluetooth_hash =
|
||||
GenerateHash(service_id, BluetoothDeviceName::kServiceIdHashLength);
|
||||
proto::connections::Medium bluetooth_medium =
|
||||
StartBluetoothAdvertising(client, service_id, bluetooth_hash,
|
||||
local_endpoint_id, local_endpoint_name);
|
||||
if (bluetooth_medium != proto::connections::UNKNOWN_MEDIUM) {
|
||||
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: BT added");
|
||||
mediums_started_successfully.push_back(bluetooth_medium);
|
||||
}
|
||||
|
||||
if (mediums_started_successfully.empty()) {
|
||||
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartAdvertisingImpl: not started");
|
||||
return {
|
||||
@@ -91,9 +103,13 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl(
|
||||
}
|
||||
|
||||
Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) {
|
||||
wifi_lan_medium_.StopAdvertising(client->GetAdvertisingServiceId());
|
||||
bluetooth_medium_.TurnOffDiscoverability();
|
||||
bluetooth_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId());
|
||||
|
||||
webrtc_medium_.StopAcceptingConnections();
|
||||
|
||||
wifi_lan_medium_.StopAdvertising(client->GetAdvertisingServiceId());
|
||||
|
||||
return {Status::kSuccess};
|
||||
}
|
||||
|
||||
@@ -163,10 +179,10 @@ P2pClusterPcpHandler::MakeBluetoothDeviceDiscoveredHandler(
|
||||
OnEndpointFound(client,
|
||||
std::make_shared<BluetoothEndpoint>(BluetoothEndpoint{
|
||||
{
|
||||
.endpoint_id = device_name.GetEndpointId(),
|
||||
.endpoint_name = device_name.GetEndpointName(),
|
||||
.service_id = service_id,
|
||||
.medium = proto::connections::Medium::BLUETOOTH,
|
||||
device_name.GetEndpointId(),
|
||||
device_name.GetEndpointName(),
|
||||
service_id,
|
||||
proto::connections::Medium::BLUETOOTH,
|
||||
},
|
||||
device,
|
||||
}));
|
||||
@@ -203,16 +219,15 @@ P2pClusterPcpHandler::MakeBluetoothDeviceLostHandler(
|
||||
"BT discovery handler (LOST) [client=%p, service=%s]: report "
|
||||
"to client",
|
||||
client, service_id.c_str());
|
||||
OnEndpointLost(client,
|
||||
BluetoothEndpoint{
|
||||
{
|
||||
.endpoint_id = device_name.GetEndpointId(),
|
||||
.endpoint_name = device_name.GetEndpointName(),
|
||||
.service_id = service_id,
|
||||
.medium = proto::connections::Medium::BLUETOOTH,
|
||||
},
|
||||
device,
|
||||
});
|
||||
OnEndpointLost(client, BluetoothEndpoint{
|
||||
{
|
||||
device_name.GetEndpointId(),
|
||||
device_name.GetEndpointName(),
|
||||
service_id,
|
||||
proto::connections::Medium::BLUETOOTH,
|
||||
},
|
||||
device,
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -282,16 +297,15 @@ P2pClusterPcpHandler::MakeWifiLanServiceDiscoveredHandler(
|
||||
"service=%s; id=%s; name=%s",
|
||||
service_id.c_str(), service_name.GetEndpointId().c_str(),
|
||||
service_name.GetEndpointName().c_str());
|
||||
OnEndpointFound(client,
|
||||
std::make_shared<WifiLanEndpoint>(WifiLanEndpoint{
|
||||
{
|
||||
.endpoint_id = service_name.GetEndpointId(),
|
||||
.endpoint_name = service_name.GetEndpointName(),
|
||||
.service_id = service_id,
|
||||
.medium = proto::connections::Medium::WIFI_LAN,
|
||||
},
|
||||
service,
|
||||
}));
|
||||
OnEndpointFound(client, std::make_shared<WifiLanEndpoint>(WifiLanEndpoint{
|
||||
{
|
||||
service_name.GetEndpointId(),
|
||||
service_name.GetEndpointName(),
|
||||
service_id,
|
||||
proto::connections::Medium::WIFI_LAN,
|
||||
},
|
||||
service,
|
||||
}));
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -328,16 +342,15 @@ P2pClusterPcpHandler::MakeWifiLanServiceLostHandler(
|
||||
"WifiLan discovery handler (LOST) [client=%p, service=%s]: report "
|
||||
"to client",
|
||||
client, service_id.c_str());
|
||||
OnEndpointLost(client,
|
||||
WifiLanEndpoint{
|
||||
{
|
||||
.endpoint_id = service_name.GetEndpointId(),
|
||||
.endpoint_name = service_name.GetEndpointName(),
|
||||
.service_id = service_id,
|
||||
.medium = proto::connections::Medium::WIFI_LAN,
|
||||
},
|
||||
service,
|
||||
});
|
||||
OnEndpointLost(client, WifiLanEndpoint{
|
||||
{
|
||||
service_name.GetEndpointId(),
|
||||
service_name.GetEndpointName(),
|
||||
service_id,
|
||||
proto::connections::Medium::WIFI_LAN,
|
||||
},
|
||||
service,
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -347,18 +360,6 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
|
||||
const ConnectionOptions& options) {
|
||||
std::vector<proto::connections::Medium> mediums_started_successfully;
|
||||
|
||||
proto::connections::Medium bluetooth_medium = StartBluetoothDiscovery(
|
||||
{
|
||||
.device_discovered_cb =
|
||||
MakeBluetoothDeviceDiscoveredHandler(client, service_id),
|
||||
.device_lost_cb = MakeBluetoothDeviceLostHandler(client, service_id),
|
||||
},
|
||||
client, service_id);
|
||||
if (bluetooth_medium != proto::connections::UNKNOWN_MEDIUM) {
|
||||
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartDiscoveryImpl: BT added");
|
||||
mediums_started_successfully.push_back(bluetooth_medium);
|
||||
}
|
||||
|
||||
proto::connections::Medium wifi_lan_medium = StartWifiLanDiscovery(
|
||||
{
|
||||
.service_discovered_cb =
|
||||
@@ -371,6 +372,18 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl(
|
||||
mediums_started_successfully.push_back(wifi_lan_medium);
|
||||
}
|
||||
|
||||
proto::connections::Medium bluetooth_medium = StartBluetoothDiscovery(
|
||||
{
|
||||
.device_discovered_cb =
|
||||
MakeBluetoothDeviceDiscoveredHandler(client, service_id),
|
||||
.device_lost_cb = MakeBluetoothDeviceLostHandler(client, service_id),
|
||||
},
|
||||
client, service_id);
|
||||
if (bluetooth_medium != proto::connections::UNKNOWN_MEDIUM) {
|
||||
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartDiscoveryImpl: BT added");
|
||||
mediums_started_successfully.push_back(bluetooth_medium);
|
||||
}
|
||||
|
||||
if (mediums_started_successfully.empty()) {
|
||||
NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartDiscoveryImpl: nothing added");
|
||||
return {
|
||||
@@ -392,15 +405,35 @@ Status P2pClusterPcpHandler::StopDiscoveryImpl(ClientProxy* client) {
|
||||
|
||||
BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::ConnectImpl(
|
||||
ClientProxy* client, BasePcpHandler::DiscoveredEndpoint* endpoint) {
|
||||
BluetoothEndpoint* bluetooth_endpoint =
|
||||
static_cast<BluetoothEndpoint*>(endpoint);
|
||||
if (bluetooth_endpoint) {
|
||||
return BluetoothConnectImpl(client, bluetooth_endpoint);
|
||||
if (!endpoint) {
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.status = {Status::kError},
|
||||
};
|
||||
}
|
||||
|
||||
WifiLanEndpoint* wifi_lan_endpoint = static_cast<WifiLanEndpoint*>(endpoint);
|
||||
if (wifi_lan_endpoint) {
|
||||
return WifiLanConnectImpl(client, wifi_lan_endpoint);
|
||||
switch (endpoint->medium) {
|
||||
case proto::connections::Medium::BLUETOOTH: {
|
||||
auto* bluetooth_endpoint = down_cast<BluetoothEndpoint*>(endpoint);
|
||||
if (bluetooth_endpoint) {
|
||||
return BluetoothConnectImpl(client, bluetooth_endpoint);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case proto::connections::Medium::WIFI_LAN: {
|
||||
auto* wifi_lan_endpoint = down_cast<WifiLanEndpoint*>(endpoint);
|
||||
if (wifi_lan_endpoint) {
|
||||
return WifiLanConnectImpl(client, wifi_lan_endpoint);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case proto::connections::Medium::WEB_RTC: {
|
||||
auto* webrtc_endpoint = down_cast<WebRtcEndpoint*>(endpoint);
|
||||
if (webrtc_endpoint) {
|
||||
return WebRtcConnectImpl(client, webrtc_endpoint);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
@@ -654,6 +687,74 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WifiLanConnectImpl(
|
||||
};
|
||||
}
|
||||
|
||||
proto::connections::Medium
|
||||
P2pClusterPcpHandler::StartListeningForWebRtcConnections(
|
||||
ClientProxy* client, const string& service_id,
|
||||
const string& local_endpoint_id, const string& local_endpoint_name) {
|
||||
if (!webrtc_medium_.IsAvailable()) {
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
}
|
||||
|
||||
if (!webrtc_medium_.IsAcceptingConnections()) {
|
||||
mediums::PeerId self_id = CreatePeerIdFromAdvertisement(
|
||||
service_id, local_endpoint_id, local_endpoint_name);
|
||||
if (!webrtc_medium_.StartAcceptingConnections(
|
||||
self_id, {[this, client, local_endpoint_name](
|
||||
mediums::WebRtcSocketWrapper socket) {
|
||||
if (!socket.IsValid()) {
|
||||
NEARBY_LOG(ERROR, "Invalid socket in accept callback: name=%s",
|
||||
local_endpoint_name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
RunOnPcpHandlerThread(
|
||||
[this, client, socket = std::move(socket)]() {
|
||||
string remote_device_name = "WebRtcSocket";
|
||||
auto channel = absl::make_unique<WebRtcEndpointChannel>(
|
||||
remote_device_name, socket);
|
||||
|
||||
OnIncomingConnection(client, remote_device_name,
|
||||
std::move(channel),
|
||||
proto::connections::WEB_RTC);
|
||||
});
|
||||
}})) {
|
||||
return proto::connections::UNKNOWN_MEDIUM;
|
||||
}
|
||||
}
|
||||
|
||||
return proto::connections::WEB_RTC;
|
||||
}
|
||||
|
||||
BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::WebRtcConnectImpl(
|
||||
ClientProxy* client, WebRtcEndpoint* webrtc_endpoint) {
|
||||
mediums::WebRtcSocketWrapper socket_wrapper =
|
||||
webrtc_medium_.Connect(webrtc_endpoint->peer_id);
|
||||
|
||||
if (!socket_wrapper.IsValid()) {
|
||||
return BasePcpHandler::ConnectImplResult{.status = {Status::kError}};
|
||||
}
|
||||
|
||||
auto channel = absl::make_unique<WebRtcEndpointChannel>(
|
||||
webrtc_endpoint->endpoint_id, socket_wrapper);
|
||||
|
||||
if (!channel) {
|
||||
socket_wrapper.Close();
|
||||
return BasePcpHandler::ConnectImplResult{.status = {Status::kError}};
|
||||
}
|
||||
|
||||
return BasePcpHandler::ConnectImplResult{
|
||||
.medium = proto::connections::Medium::WEB_RTC,
|
||||
.status = {Status::kSuccess},
|
||||
.endpoint_channel = std::move(channel)};
|
||||
}
|
||||
|
||||
mediums::PeerId P2pClusterPcpHandler::CreatePeerIdFromAdvertisement(
|
||||
const std::string& service_id, const std::string& endpoint_id,
|
||||
const std::string& endpoint_name) {
|
||||
std::string seed = absl::StrCat(service_id, endpoint_id, endpoint_name);
|
||||
return mediums::PeerId::FromSeed(ByteArray(seed));
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include "core_v2/internal/endpoint_manager.h"
|
||||
#include "core_v2/internal/mediums/bluetooth_classic.h"
|
||||
#include "core_v2/internal/mediums/mediums.h"
|
||||
#include "core_v2/internal/mediums/webrtc.h"
|
||||
#include "core_v2/internal/mediums/webrtc/peer_id.h"
|
||||
#include "core_v2/internal/pcp.h"
|
||||
#include "core_v2/internal/wifi_lan_service_info.h"
|
||||
#include "core_v2/options.h"
|
||||
@@ -69,11 +71,24 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
|
||||
private:
|
||||
struct BluetoothEndpoint : public BasePcpHandler::DiscoveredEndpoint {
|
||||
BluetoothEndpoint(DiscoveredEndpoint endpoint, BluetoothDevice device)
|
||||
: DiscoveredEndpoint(std::move(endpoint)),
|
||||
bluetooth_device(std::move(device)) {}
|
||||
|
||||
BluetoothDevice bluetooth_device;
|
||||
};
|
||||
struct WifiLanEndpoint : public BasePcpHandler::DiscoveredEndpoint {
|
||||
WifiLanEndpoint(DiscoveredEndpoint endpoint, WifiLanService service)
|
||||
: DiscoveredEndpoint(std::move(endpoint)),
|
||||
wifi_lan_service(std::move(service)) {}
|
||||
WifiLanService wifi_lan_service;
|
||||
};
|
||||
struct WebRtcEndpoint : public BasePcpHandler::DiscoveredEndpoint {
|
||||
WebRtcEndpoint(DiscoveredEndpoint endpoint, mediums::PeerId peer_id)
|
||||
: DiscoveredEndpoint(std::move(endpoint)),
|
||||
peer_id(std::move(peer_id)) {}
|
||||
mediums::PeerId peer_id;
|
||||
};
|
||||
|
||||
using BluetoothDiscoveredDeviceCallback =
|
||||
BluetoothClassic::DiscoveredDeviceCallback;
|
||||
@@ -124,9 +139,21 @@ class P2pClusterPcpHandler : public BasePcpHandler {
|
||||
BasePcpHandler::ConnectImplResult WifiLanConnectImpl(
|
||||
ClientProxy* client, WifiLanEndpoint* endpoint);
|
||||
|
||||
// WebRtc
|
||||
proto::connections::Medium StartListeningForWebRtcConnections(
|
||||
ClientProxy* client, const std::string& service_id,
|
||||
const std::string& local_endpoint_id,
|
||||
const std::string& local_endpoint_name);
|
||||
BasePcpHandler::ConnectImplResult WebRtcConnectImpl(
|
||||
ClientProxy* client, WebRtcEndpoint* webrtc_endpoint);
|
||||
mediums::PeerId CreatePeerIdFromAdvertisement(const string& service_id,
|
||||
const string& endpoint_id,
|
||||
const string& endpoint_name);
|
||||
|
||||
BluetoothRadio& bluetooth_radio_;
|
||||
BluetoothClassic& bluetooth_medium_;
|
||||
WifiLan& wifi_lan_medium_;
|
||||
mediums::WebRtc& webrtc_medium_;
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
@@ -35,8 +35,8 @@ WifiLanServiceInfo::WifiLanServiceInfo(Version version, Pcp pcp,
|
||||
version_ = version;
|
||||
pcp_ = pcp;
|
||||
service_id_hash_ = service_id_hash;
|
||||
endpoint_id_ = endpoint_id;
|
||||
endpoint_name_ = endpoint_name;
|
||||
endpoint_id_ = std::string(endpoint_id);
|
||||
endpoint_name_ = std::string(endpoint_name);
|
||||
}
|
||||
|
||||
WifiLanServiceInfo::WifiLanServiceInfo(absl::string_view service_info_string) {
|
||||
|
||||
@@ -19,7 +19,8 @@ namespace nearby {
|
||||
template <typename Derived, typename Base>
|
||||
inline Derived down_cast(Base* value) {
|
||||
using DerivedType = typename std::remove_pointer<Derived>::type;
|
||||
static_assert(std::is_base_of<Base, DerivedType>::value);
|
||||
static_assert(std::is_base_of<Base, DerivedType>::value,
|
||||
"incompatible casting");
|
||||
return static_cast<Derived>(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
#include "platform_v2/api/platform.h"
|
||||
#include "platform_v2/public/mutex.h"
|
||||
#include "platform_v2/public/mutex_lock.h"
|
||||
#include "absl/types/any.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -20,8 +19,7 @@ class AtomicReference;
|
||||
// Platform-based atomic type, for something convertible to std::uint32_t.
|
||||
template <typename T>
|
||||
class AtomicReference<T, std::enable_if_t<sizeof(T) <= sizeof(std::uint32_t) &&
|
||||
std::is_trivially_copyable_v<T>,
|
||||
void>>
|
||||
std::is_trivially_copyable<T>::value>>
|
||||
final {
|
||||
public:
|
||||
using Platform = api::ImplementationPlatform;
|
||||
@@ -42,9 +40,9 @@ class AtomicReference<T, std::enable_if_t<sizeof(T) <= sizeof(std::uint32_t) &&
|
||||
// Atomic type that is using Platform mutex to provide atomicity.
|
||||
// Supports any copyable type.
|
||||
template <typename T>
|
||||
class AtomicReference<T, std::enable_if_t<(sizeof(T) > sizeof(std::uint32_t) ||
|
||||
!std::is_trivially_copyable_v<T>),
|
||||
void>>
|
||||
class AtomicReference<T,
|
||||
std::enable_if_t<(sizeof(T) > sizeof(std::uint32_t) ||
|
||||
!std::is_trivially_copyable<T>::value)>>
|
||||
final {
|
||||
public:
|
||||
explicit AtomicReference(T value) {
|
||||
|
||||
Reference in New Issue
Block a user