mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Replace std::function with AnyInvocable
PiperOrigin-RevId: 555650115
This commit is contained in:
committed by
Copybara-Service
parent
5589166b3b
commit
cce7cf59b9
@@ -62,6 +62,7 @@ cc_library(
|
||||
"@com_google_absl//absl/container:btree",
|
||||
"@com_google_absl//absl/container:flat_hash_map",
|
||||
"@com_google_absl//absl/container:flat_hash_set",
|
||||
"@com_google_absl//absl/functional:any_invocable",
|
||||
"@com_google_absl//absl/functional:bind_front",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/strings:str_format",
|
||||
|
||||
@@ -107,7 +107,7 @@ bool WebRtc::StartAcceptingConnections(const std::string& service_id,
|
||||
// who may be also using WebRTC.
|
||||
AcceptingConnectionsInfo info = AcceptingConnectionsInfo();
|
||||
info.self_peer_id = self_peer_id;
|
||||
info.accepted_connection_callback = callback;
|
||||
info.accepted_connection_callback = std::move(callback);
|
||||
|
||||
// Create a new SignalingMessenger so that we can communicate w/ Tachyon.
|
||||
info.signaling_messenger =
|
||||
@@ -688,8 +688,9 @@ void WebRtc::ProcessDataChannelOpen(const std::string& service_id,
|
||||
|
||||
const auto& accepting_connection_entry =
|
||||
accepting_connections_info_.find(service_id);
|
||||
if (accepting_connection_entry != accepting_connections_info_.end()) {
|
||||
accepting_connection_entry->second.accepted_connection_callback.accepted_cb(
|
||||
if (accepting_connection_entry != accepting_connections_info_.end() &&
|
||||
accepting_connection_entry->second.accepted_connection_callback) {
|
||||
accepting_connection_entry->second.accepted_connection_callback(
|
||||
service_id, socket_wrapper);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -17,13 +17,12 @@
|
||||
|
||||
#ifndef NO_WEBRTC
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/functional/any_invocable.h"
|
||||
#include "connections/implementation/mediums/webrtc/connection_flow.h"
|
||||
#include "connections/implementation/mediums/webrtc_peer_id.h"
|
||||
#include "connections/implementation/mediums/webrtc_socket.h"
|
||||
@@ -42,15 +41,13 @@ namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
// Callback that is invoked when a new connection is accepted.
|
||||
struct AcceptedConnectionCallback {
|
||||
std::function<void(const std::string& service_id, WebRtcSocketWrapper socket)>
|
||||
accepted_cb = [](const std::string&, 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(
|
||||
const std::string& service_id, WebRtcSocketWrapper socket)>;
|
||||
|
||||
WebRtc();
|
||||
~WebRtc();
|
||||
|
||||
|
||||
@@ -80,11 +80,11 @@ TEST_P(WebRtcTest, ConnectBothDevices_ShutdownSignaling_SendData) {
|
||||
|
||||
receiver.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
@@ -119,11 +119,11 @@ TEST_P(WebRtcTest, CanCancelConnect) {
|
||||
|
||||
receiver.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
CancellationFlag flag(true);
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
@@ -173,10 +173,10 @@ TEST_F(WebRtcTest, StartAcceptingConnectionTwice) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{mock_accepted_callback_.AsStdFunction()}));
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
EXPECT_FALSE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{mock_accepted_callback_.AsStdFunction()}));
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id));
|
||||
EXPECT_FALSE(webrtc.IsAcceptingConnections(std::string{}));
|
||||
env_.Stop();
|
||||
@@ -197,8 +197,8 @@ TEST_F(WebRtcTest, Connect_NoPeer) {
|
||||
webrtc.Connect(service_id, peer_id, location_hint, &flag);
|
||||
EXPECT_FALSE(wrapper_1.IsValid());
|
||||
|
||||
EXPECT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, peer_id, location_hint, AcceptedConnectionCallback()));
|
||||
EXPECT_TRUE(webrtc.StartAcceptingConnections(service_id, peer_id,
|
||||
location_hint, nullptr));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -215,7 +215,7 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{mock_accepted_callback_.AsStdFunction()}));
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
CancellationFlag flag;
|
||||
WebRtcSocketWrapper wrapper = webrtc.Connect(
|
||||
service_id, WebrtcPeerId("random_peer_id"), location_hint, &flag);
|
||||
@@ -223,7 +223,7 @@ TEST_F(WebRtcTest, StartAcceptingConnection_ThenConnect) {
|
||||
EXPECT_FALSE(wrapper.IsValid());
|
||||
EXPECT_FALSE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{mock_accepted_callback_.AsStdFunction()}));
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -240,7 +240,7 @@ TEST_F(WebRtcTest, StartAndStopAcceptingConnections) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{mock_accepted_callback_.AsStdFunction()}));
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id));
|
||||
webrtc.StopAcceptingConnections(service_id);
|
||||
EXPECT_FALSE(webrtc.IsAcceptingConnections(service_id));
|
||||
@@ -261,15 +261,15 @@ TEST_F(WebRtcTest, ConnectTwice) {
|
||||
|
||||
receiver.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
device_c.StartAcceptingConnections(
|
||||
service_id, other_id, location_hint,
|
||||
{[](const std::string& service_id, WebRtcSocketWrapper wrapper) {}});
|
||||
[](const std::string& service_id, WebRtcSocketWrapper wrapper) {});
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
@@ -311,11 +311,11 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndAbort) {
|
||||
|
||||
receiver.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
@@ -343,11 +343,11 @@ TEST_F(WebRtcTest, ConnectBothDevicesAndSendData) {
|
||||
|
||||
receiver.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
CancellationFlag flag;
|
||||
sender_socket = sender.Connect(service_id, self_id, location_hint, &flag);
|
||||
@@ -399,7 +399,7 @@ TEST_F(WebRtcTest, ContinueAcceptingConnectionsOnComplete) {
|
||||
ASSERT_TRUE(webrtc.IsAvailable());
|
||||
ASSERT_TRUE(webrtc.StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{mock_accepted_callback_.AsStdFunction()}));
|
||||
mock_accepted_callback_.AsStdFunction()));
|
||||
EXPECT_TRUE(webrtc.IsAcceptingConnections(service_id));
|
||||
|
||||
// Simulate a failure in receiving messages stream, WebRtc should restart
|
||||
@@ -450,11 +450,11 @@ TEST_F(WebRtcTest, CancelDuringConnect) {
|
||||
|
||||
receiver->StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
sender_socket =
|
||||
sender->Connect(service_id, self_id, location_hint, &sender_flag);
|
||||
@@ -496,11 +496,11 @@ TEST_F(WebRtcTest, CancelBeforeConnect) {
|
||||
|
||||
receiver->StartAcceptingConnections(
|
||||
service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
sender_socket =
|
||||
sender->Connect(service_id, self_id, location_hint, &sender_flag);
|
||||
@@ -541,11 +541,11 @@ TEST_F(WebRtcTest, CancelDuringConnect_MultipleConnect) {
|
||||
|
||||
receiver->StartAcceptingConnections(
|
||||
ns_service_id, self_id, location_hint,
|
||||
{[&receiver_socket, connected](const std::string& ns_service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
[&receiver_socket, connected](const std::string& ns_service_id,
|
||||
WebRtcSocketWrapper wrapper) mutable {
|
||||
receiver_socket = wrapper;
|
||||
connected.Set(receiver_socket.IsValid());
|
||||
}});
|
||||
});
|
||||
|
||||
// Simulate a successful connect for the endpoint of NearbySharing.
|
||||
sender_socket = sender->Connect(ns_service_id, self_id, location_hint, &flag);
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include "absl/functional/bind_front.h"
|
||||
#include "connections/implementation/base_bwu_handler.h"
|
||||
#include "connections/implementation/client_proxy.h"
|
||||
#include "connections/implementation/mediums/utils.h"
|
||||
#include "connections/implementation/mediums/webrtc_peer_id.h"
|
||||
@@ -41,8 +42,7 @@ std::string WebrtcBwuHandler::WebrtcIncomingSocket::ToString() { return name_; }
|
||||
|
||||
WebrtcBwuHandler::WebrtcBwuHandler(Mediums& mediums,
|
||||
BwuNotifications notifications)
|
||||
: BaseBwuHandler(std::move(notifications)),
|
||||
mediums_(mediums) {}
|
||||
: BaseBwuHandler(std::move(notifications)), mediums_(mediums) {}
|
||||
|
||||
// Called by BWU target. Retrieves a new medium info from incoming message,
|
||||
// and establishes connection over WebRTC using this info.
|
||||
@@ -114,11 +114,8 @@ ByteArray WebrtcBwuHandler::HandleInitializeUpgradedMediumForEndpoint(
|
||||
if (!webrtc_.IsAcceptingConnections(upgrade_service_id)) {
|
||||
if (!webrtc_.StartAcceptingConnections(
|
||||
upgrade_service_id, self_id, location_hint,
|
||||
{
|
||||
.accepted_cb = absl::bind_front(
|
||||
&WebrtcBwuHandler::OnIncomingWebrtcConnection, this,
|
||||
client),
|
||||
})) {
|
||||
absl::bind_front(&WebrtcBwuHandler::OnIncomingWebrtcConnection,
|
||||
this, client))) {
|
||||
NEARBY_LOG(ERROR,
|
||||
"WebRtcBwuHandler couldn't initiate the WEB_RTC upgrade for "
|
||||
"endpoint %s because it failed to start listening for "
|
||||
|
||||
Reference in New Issue
Block a user