From b9201b0ef42597ba224c2479f8e5a5dad183fef3 Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Wed, 17 Aug 2022 17:12:29 +0200 Subject: [PATCH] Remove usage of rtc::MessageHandler in peer_connection example Bug: webrtc:9702 Change-Id: I2d688461858d9b6f06a0f7928f3cff7c6a7b7db9 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/272080 Auto-Submit: Danil Chapovalov Reviewed-by: Tomas Gunnarsson Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#37813} --- examples/BUILD.gn | 2 ++ .../client/peer_connection_client.cc | 18 ++++++------------ .../client/peer_connection_client.h | 8 +++----- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/examples/BUILD.gn b/examples/BUILD.gn index 17cfc7b35a..e683c192dc 100644 --- a/examples/BUILD.gn +++ b/examples/BUILD.gn @@ -693,6 +693,8 @@ if (is_linux || is_chromeos || is_win) { "../api:scoped_refptr", "../api/audio:audio_mixer_api", "../api/audio_codecs:audio_codecs_api", + "../api/task_queue:pending_task_safety_flag", + "../api/units:time_delta", "../api/video:video_frame", "../api/video:video_rtp_headers", "../api/video_codecs:video_codecs_api", diff --git a/examples/peerconnection/client/peer_connection_client.cc b/examples/peerconnection/client/peer_connection_client.cc index c0de4ffb5d..2746752d80 100644 --- a/examples/peerconnection/client/peer_connection_client.cc +++ b/examples/peerconnection/client/peer_connection_client.cc @@ -10,6 +10,7 @@ #include "examples/peerconnection/client/peer_connection_client.h" +#include "api/units/time_delta.h" #include "examples/peerconnection/client/defaults.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" @@ -18,9 +19,9 @@ namespace { // This is our magical hangup signal. -const char kByeMessage[] = "BYE"; +constexpr char kByeMessage[] = "BYE"; // Delay between server connection retries, in milliseconds -const int kReconnectDelay = 2000; +constexpr webrtc::TimeDelta kReconnectDelay = webrtc::TimeDelta::Seconds(2); rtc::Socket* CreateClientSocket(int family) { rtc::Thread* thread = rtc::Thread::Current(); @@ -33,9 +34,7 @@ rtc::Socket* CreateClientSocket(int family) { PeerConnectionClient::PeerConnectionClient() : callback_(NULL), resolver_(NULL), state_(NOT_CONNECTED), my_id_(-1) {} -PeerConnectionClient::~PeerConnectionClient() { - rtc::Thread::Current()->Clear(this); -} +PeerConnectionClient::~PeerConnectionClient() = default; void PeerConnectionClient::InitSocketSignals() { RTC_DCHECK(control_socket_.get() != NULL); @@ -480,16 +479,11 @@ void PeerConnectionClient::OnClose(rtc::Socket* socket, int err) { } else { if (socket == control_socket_.get()) { RTC_LOG(LS_WARNING) << "Connection refused; retrying in 2 seconds"; - rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, kReconnectDelay, this, - 0); + rtc::Thread::Current()->PostDelayedTask( + SafeTask(safety_.flag(), [this] { DoConnect(); }), kReconnectDelay); } else { Close(); callback_->OnDisconnected(); } } } - -void PeerConnectionClient::OnMessage(rtc::Message* msg) { - // ignore msg; there is currently only one supported message ("retry") - DoConnect(); -} diff --git a/examples/peerconnection/client/peer_connection_client.h b/examples/peerconnection/client/peer_connection_client.h index 00d2192681..8f9c5b6a75 100644 --- a/examples/peerconnection/client/peer_connection_client.h +++ b/examples/peerconnection/client/peer_connection_client.h @@ -15,6 +15,7 @@ #include #include +#include "api/task_queue/pending_task_safety_flag.h" #include "rtc_base/net_helpers.h" #include "rtc_base/physical_socket_server.h" #include "rtc_base/third_party/sigslot/sigslot.h" @@ -34,8 +35,7 @@ struct PeerConnectionClientObserver { virtual ~PeerConnectionClientObserver() {} }; -class PeerConnectionClient : public sigslot::has_slots<>, - public rtc::MessageHandler { +class PeerConnectionClient : public sigslot::has_slots<> { public: enum State { NOT_CONNECTED, @@ -65,9 +65,6 @@ class PeerConnectionClient : public sigslot::has_slots<>, bool SignOut(); - // implements the MessageHandler interface - void OnMessage(rtc::Message* msg); - protected: void DoConnect(); void Close(); @@ -126,6 +123,7 @@ class PeerConnectionClient : public sigslot::has_slots<>, Peers peers_; State state_; int my_id_; + webrtc::ScopedTaskSafety safety_; }; #endif // EXAMPLES_PEERCONNECTION_CLIENT_PEER_CONNECTION_CLIENT_H_