Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

Use sigslot trampoline for signals on Connection

Bug: webrtc:42222066
Change-Id: Ica21f4c8d5c0beeac4da8ed5c39b2a7497713367
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/407721
Reviewed-by: Tomas Gunnarsson <[email protected]>
Commit-Queue: Lena Kaplan <[email protected]>
Reviewed-by: Harald Alvestrand <[email protected]>
Cr-Commit-Position: refs/heads/main@{#45591}
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index c0384f1..e8e98b9 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -228,6 +228,7 @@
     "../rtc_base:rate_tracker",
     "../rtc_base:rtc_numerics",
     "../rtc_base:safe_minmax",
+    "../rtc_base:sigslot_trampoline",
     "../rtc_base:socket",
     "../rtc_base:socket_address",
     "../rtc_base:stringutils",
diff --git a/p2p/base/connection.cc b/p2p/base/connection.cc
index d2b774d..78dbd8f 100644
--- a/p2p/base/connection.cc
+++ b/p2p/base/connection.cc
@@ -261,7 +261,11 @@
       delta_internal_unix_epoch_(Timestamp::Millis(TimeUTCMillis()) -
                                  time_created_),
       field_trials_(&kDefaultFieldTrials),
-      rtt_estimate_(kDefaultRttEstimateHalfTimeMs) {
+      rtt_estimate_(kDefaultRttEstimateHalfTimeMs),
+      state_change_trampoline_(this),
+      destroyed_trampoline_(this),
+      ready_to_send_trampoline_(this),
+      nominated_trampoline_(this) {
   RTC_DCHECK_RUN_ON(network_thread_);
   RTC_DCHECK(port_);
   RTC_LOG(LS_INFO) << ToString() << ": Connection created";
diff --git a/p2p/base/connection.h b/p2p/base/connection.h
index 1cb0c20..5071071 100644
--- a/p2p/base/connection.h
+++ b/p2p/base/connection.h
@@ -45,6 +45,7 @@
 #include "rtc_base/network/received_packet.h"
 #include "rtc_base/numerics/event_based_exponential_moving_average.h"
 #include "rtc_base/rate_tracker.h"
+#include "rtc_base/sigslot_trampoline.h"
 #include "rtc_base/system/rtc_export.h"
 #include "rtc_base/third_party/sigslot/sigslot.h"
 #include "rtc_base/thread_annotations.h"
@@ -159,10 +160,22 @@
   ConnectionInfo stats();
 
   sigslot::signal1<Connection*> SignalStateChange;
+  void SubscribeStateChange(
+      absl::AnyInvocable<void(Connection* connection)> callback) {
+    state_change_trampoline_.Subscribe(std::move(callback));
+  }
 
   // Sent when the connection has decided that it is no longer of value.  It
   // will delete itself immediately after this call.
   sigslot::signal1<Connection*> SignalDestroyed;
+  void SubscribeDestroyed(
+      void* tag,
+      absl::AnyInvocable<void(Connection* connection)> callback) {
+    destroyed_trampoline_.Subscribe(tag, std::move(callback));
+  }
+  void UnsubscribeDestroyed(void* tag) {
+    destroyed_trampoline_.Unsubscribe(tag);
+  }
 
   // The connection can send and receive packets asynchronously.  This matches
   // the interface of AsyncPacketSocket, which may use UDP or TCP under the
@@ -181,6 +194,10 @@
   void DeregisterReceivedPacketCallback();
 
   sigslot::signal1<Connection*> SignalReadyToSend;
+  void SubscribeReadyToSend(
+      absl::AnyInvocable<void(Connection* connection)> callback) {
+    ready_to_send_trampoline_.Subscribe(std::move(callback));
+  }
 
   // Called when a packet is received on this connection.
   void OnReadPacket(const ReceivedIpPacket& packet);
@@ -327,6 +344,10 @@
   // This signal will be fired if this connection is nominated by the
   // controlling side.
   sigslot::signal1<Connection*> SignalNominated;
+  void SubscribeNominated(
+      absl::AnyInvocable<void(Connection* connection)> callback) {
+    nominated_trampoline_.Subscribe(std::move(callback));
+  }
 
   IceCandidatePairState state() const;
 
@@ -617,6 +638,14 @@
       const StunMessage* msg,
       const StunRequest* original_request);
   DtlsStunPiggybackCallbacks dtls_stun_piggyback_callbacks_;
+  SignalTrampoline<Connection, &Connection::SignalStateChange>
+      state_change_trampoline_;
+  SignalTrampoline<Connection, &Connection::SignalDestroyed>
+      destroyed_trampoline_;
+  SignalTrampoline<Connection, &Connection::SignalReadyToSend>
+      ready_to_send_trampoline_;
+  SignalTrampoline<Connection, &Connection::SignalNominated>
+      nominated_trampoline_;
 };
 
 // ProxyConnection defers all the interesting work to the port.
diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc
index 466b932..2b2519e 100644
--- a/p2p/base/p2p_transport_channel.cc
+++ b/p2p/base/p2p_transport_channel.cc
@@ -241,7 +241,7 @@
   RTC_DCHECK_RUN_ON(network_thread_);
   std::vector<Connection*> copy(connections_.begin(), connections_.end());
   for (Connection* connection : copy) {
-    connection->SignalDestroyed.disconnect(this);
+    connection->UnsubscribeDestroyed(this);
     RemoveConnection(connection);
     connection->Destroy();
   }
@@ -306,13 +306,15 @@
       [&](Connection* connection, const ReceivedIpPacket& packet) {
         OnReadPacket(connection, packet);
       });
-  connection->SignalReadyToSend.connect(this,
-                                        &P2PTransportChannel::OnReadyToSend);
-  connection->SignalStateChange.connect(
-      this, &P2PTransportChannel::OnConnectionStateChange);
-  connection->SignalDestroyed.connect(
-      this, &P2PTransportChannel::OnConnectionDestroyed);
-  connection->SignalNominated.connect(this, &P2PTransportChannel::OnNominated);
+  connection->SubscribeReadyToSend(
+      [this](Connection* connection) { OnReadyToSend(connection); });
+  connection->SubscribeStateChange(
+      [this](Connection* connection) { OnConnectionStateChange(connection); });
+  connection->SubscribeDestroyed(this, [this](Connection* connection) {
+    OnConnectionDestroyed(connection);
+  });
+  connection->SubscribeNominated(
+      [this](Connection* connection) { OnNominated(connection); });
 
   had_connection_ = true;
 
@@ -1722,7 +1724,7 @@
 void P2PTransportChannel::RemoveConnectionForTest(Connection* connection) {
   RTC_DCHECK_RUN_ON(network_thread_);
   RTC_DCHECK(FindConnection(connection));
-  connection->SignalDestroyed.disconnect(this);
+  connection->UnsubscribeDestroyed(this);
   RemoveConnection(connection);
   RTC_DCHECK(!FindConnection(connection));
   if (selected_connection_ == connection)
@@ -2030,7 +2032,7 @@
       selected_connection_ = nullptr;
       update_selected_connection = true;
     }
-    connection->SignalDestroyed.disconnect(this);
+    connection->UnsubscribeDestroyed(this);
     RemoveConnection(connection);
     connection->Destroy();
   }
diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc
index ed80899..0e9ab0e 100644
--- a/p2p/base/p2p_transport_channel_unittest.cc
+++ b/p2p/base/p2p_transport_channel_unittest.cc
@@ -1003,8 +1003,8 @@
   void set_force_relay(bool relay) { force_relay_ = relay; }
 
   void ConnectSignalNominated(Connection* conn) {
-    conn->SignalNominated.connect(this,
-                                  &P2PTransportChannelTestBase::OnNominated);
+    conn->SubscribeNominated(
+        [this](Connection* connection) { OnNominated(connection); });
   }
 
   void OnNominated(Connection* conn) { nominated_ = true; }
diff --git a/p2p/base/port_unittest.cc b/p2p/base/port_unittest.cc
index d5a0f99..db45e50 100644
--- a/p2p/base/port_unittest.cc
+++ b/p2p/base/port_unittest.cc
@@ -314,11 +314,14 @@
     IceMode remote_ice_mode =
         (ice_mode_ == ICEMODE_FULL) ? ICEMODE_LITE : ICEMODE_FULL;
     conn_->set_use_candidate_attr(remote_ice_mode == ICEMODE_FULL);
-    conn_->SignalStateChange.connect(this,
-                                     &TestChannel::OnConnectionStateChange);
-    conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
-    conn_->SignalReadyToSend.connect(this,
-                                     &TestChannel::OnConnectionReadyToSend);
+    conn_->SubscribeStateChange([this](Connection* connection) {
+      OnConnectionStateChange(connection);
+    });
+    conn_->SubscribeDestroyed(
+        this, [this](Connection* connection) { OnDestroyed(connection); });
+    conn_->SubscribeReadyToSend([this](Connection* connection) {
+      OnConnectionReadyToSend(connection);
+    });
     connection_ready_to_send_ = false;
   }
 
@@ -330,14 +333,15 @@
   }
   void AcceptConnection(const Candidate& remote_candidate) {
     if (conn_) {
-      conn_->SignalDestroyed.disconnect(this);
+      conn_->UnsubscribeDestroyed(this);
       conn_ = nullptr;
     }
     ASSERT_TRUE(remote_request_.get() != nullptr);
     Candidate c = remote_candidate;
     c.set_address(remote_address_);
     conn_ = port_->CreateConnection(c, Port::ORIGIN_MESSAGE);
-    conn_->SignalDestroyed.connect(this, &TestChannel::OnDestroyed);
+    conn_->SubscribeDestroyed(
+        this, [this](Connection* connection) { OnDestroyed(connection); });
     conn_->SendStunBindingResponse(remote_request_.get());
     remote_request_.reset();
   }
@@ -4096,8 +4100,9 @@
       conn = rport_->CreateConnection(lport_->Candidates()[0],
                                       Port::ORIGIN_MESSAGE);
     }
-    conn->SignalStateChange.connect(this,
-                                    &ConnectionTest::OnConnectionStateChange);
+    conn->SubscribeStateChange([this](Connection* connection) {
+      OnConnectionStateChange(connection);
+    });
     return conn;
   }
 
diff --git a/p2p/base/tcp_port_unittest.cc b/p2p/base/tcp_port_unittest.cc
index 85175b4..6a6f4c7 100644
--- a/p2p/base/tcp_port_unittest.cc
+++ b/p2p/base/tcp_port_unittest.cc
@@ -66,13 +66,14 @@
 class ConnectionObserver : public sigslot::has_slots<> {
  public:
   explicit ConnectionObserver(Connection* conn) : conn_(conn) {
-    conn->SignalDestroyed.connect(this, &ConnectionObserver::OnDestroyed);
+    conn->SubscribeDestroyed(
+        this, [this](Connection* connection) { OnDestroyed(connection); });
   }
 
   ~ConnectionObserver() override {
     if (!connection_destroyed_) {
       RTC_DCHECK(conn_);
-      conn_->SignalDestroyed.disconnect(this);
+      conn_->UnsubscribeDestroyed(this);
     }
   }
 
diff --git a/p2p/base/turn_port_unittest.cc b/p2p/base/turn_port_unittest.cc
index 6c95558..fda57b3 100644
--- a/p2p/base/turn_port_unittest.cc
+++ b/p2p/base/turn_port_unittest.cc
@@ -185,13 +185,14 @@
 class TestConnectionWrapper : public sigslot::has_slots<> {
  public:
   explicit TestConnectionWrapper(Connection* conn) : connection_(conn) {
-    conn->SignalDestroyed.connect(
-        this, &TestConnectionWrapper::OnConnectionDestroyed);
+    conn->SubscribeDestroyed(this, [this](Connection* connection) {
+      OnConnectionDestroyed(connection);
+    });
   }
 
   ~TestConnectionWrapper() override {
     if (connection_) {
-      connection_->SignalDestroyed.disconnect(this);
+      connection_->UnsubscribeDestroyed(this);
     }
   }
 
@@ -793,15 +794,18 @@
           turn_packets_.push_back(
               Buffer(packet.payload().data(), packet.payload().size()));
         });
-    conn1->SignalDestroyed.connect(this,
-                                   &TurnPortTest::OnConnectionSignalDestroyed);
+
+    conn1->SubscribeDestroyed(this, [this](Connection* connection) {
+      OnConnectionSignalDestroyed(connection);
+    });
     conn2->RegisterReceivedPacketCallback(
         [&](Connection* connection, const ReceivedIpPacket& packet) {
           udp_packets_.push_back(
               Buffer(packet.payload().data(), packet.payload().size()));
         });
-    conn2->SignalDestroyed.connect(this,
-                                   &TurnPortTest::OnConnectionSignalDestroyed);
+    conn2->SubscribeDestroyed(this, [this](Connection* connection) {
+      OnConnectionSignalDestroyed(connection);
+    });
     conn1->Ping(0);
     EXPECT_THAT(WaitUntil([&] { return conn1->write_state(); },
                           Eq(Connection::STATE_WRITABLE),
@@ -866,15 +870,18 @@
           turn_packets_.push_back(
               Buffer(packet.payload().data(), packet.payload().size()));
         });
-    conn1->SignalDestroyed.connect(this,
-                                   &TurnPortTest::OnConnectionSignalDestroyed);
+    conn1->SubscribeDestroyed(this, [this](Connection* connection) {
+      OnConnectionSignalDestroyed(connection);
+    });
+
     conn2->RegisterReceivedPacketCallback(
         [&](Connection* connection, const ReceivedIpPacket& packet) {
           udp_packets_.push_back(
               Buffer(packet.payload().data(), packet.payload().size()));
         });
-    conn2->SignalDestroyed.connect(this,
-                                   &TurnPortTest::OnConnectionSignalDestroyed);
+    conn2->SubscribeDestroyed(this, [this](Connection* connection) {
+      OnConnectionSignalDestroyed(connection);
+    });
 
     conn1->Ping(0);
     EXPECT_THAT(WaitUntil([&] { return conn1->write_state(); },