Refactor BasicNetworkManager to use TQ methods and PendingTaskSafetyFlag.

Bug: webrtc:11988
Change-Id: I29a1023297510cc57fe81f02bc4ce06fcde614ea
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/251042
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35951}
This commit is contained in:
Niels Möller 2022-02-07 16:44:21 +01:00 committed by WebRTC LUCI CQ
parent 134995ed31
commit 27c1452fae
2 changed files with 26 additions and 29 deletions

View File

@ -34,6 +34,7 @@
#include "rtc_base/string_encode.h"
#include "rtc_base/string_utils.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/task_utils/to_queued_task.h"
#include "rtc_base/thread.h"
#include "system_wrappers/include/field_trial.h"
@ -48,9 +49,6 @@ constexpr uint8_t kVpns[2][6] = {
{0x2, 0x50, 0x41, 0x0, 0x0, 0x1},
};
const uint32_t kUpdateNetworksMessage = 1;
const uint32_t kSignalNetworksMessage = 2;
// Fetch list of networks every two seconds.
const int kNetworksUpdateIntervalMs = 2000;
@ -528,7 +526,11 @@ BasicNetworkManager::BasicNetworkManager(
bind_using_ifname_(
!webrtc::field_trial::IsDisabled("WebRTC-BindUsingInterfaceName")) {}
BasicNetworkManager::~BasicNetworkManager() {}
BasicNetworkManager::~BasicNetworkManager() {
if (task_safety_flag_) {
task_safety_flag_->SetNotAlive();
}
}
void BasicNetworkManager::OnNetworksChanged() {
RTC_DCHECK_RUN_ON(thread_);
@ -897,9 +899,17 @@ void BasicNetworkManager::StartUpdating() {
// we should trigger network signal immediately for the new clients
// to start allocating ports.
if (sent_first_update_)
thread_->Post(RTC_FROM_HERE, this, kSignalNetworksMessage);
thread_->PostTask(ToQueuedTask(task_safety_flag_, [this] {
RTC_DCHECK_RUN_ON(thread_);
SignalNetworksChanged();
}));
} else {
thread_->Post(RTC_FROM_HERE, this, kUpdateNetworksMessage);
RTC_DCHECK(task_safety_flag_ == nullptr);
task_safety_flag_ = webrtc::PendingTaskSafetyFlag::Create();
thread_->PostTask(ToQueuedTask(task_safety_flag_, [this] {
RTC_DCHECK_RUN_ON(thread_);
UpdateNetworksContinually();
}));
StartNetworkMonitor();
}
++start_count_;
@ -912,7 +922,8 @@ void BasicNetworkManager::StopUpdating() {
--start_count_;
if (!start_count_) {
thread_->Clear(this);
task_safety_flag_->SetNotAlive();
task_safety_flag_ = nullptr;
sent_first_update_ = false;
StopNetworkMonitor();
}
@ -956,22 +967,6 @@ void BasicNetworkManager::StopNetworkMonitor() {
}
}
void BasicNetworkManager::OnMessage(Message* msg) {
RTC_DCHECK_RUN_ON(thread_);
switch (msg->message_id) {
case kUpdateNetworksMessage: {
UpdateNetworksContinually();
break;
}
case kSignalNetworksMessage: {
SignalNetworksChanged();
break;
}
default:
RTC_DCHECK_NOTREACHED();
}
}
IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const {
RTC_DCHECK(family == AF_INET || family == AF_INET6);
@ -1026,8 +1021,12 @@ void BasicNetworkManager::UpdateNetworksOnce() {
void BasicNetworkManager::UpdateNetworksContinually() {
UpdateNetworksOnce();
thread_->PostDelayed(RTC_FROM_HERE, kNetworksUpdateIntervalMs, this,
kUpdateNetworksMessage);
thread_->PostDelayedTask(ToQueuedTask(task_safety_flag_,
[this] {
RTC_DCHECK_RUN_ON(thread_);
UpdateNetworksContinually();
}),
kNetworksUpdateIntervalMs);
}
void BasicNetworkManager::DumpNetworks() {

View File

@ -23,11 +23,11 @@
#include "api/sequence_checker.h"
#include "rtc_base/ip_address.h"
#include "rtc_base/mdns_responder_interface.h"
#include "rtc_base/message_handler.h"
#include "rtc_base/network_monitor.h"
#include "rtc_base/network_monitor_factory.h"
#include "rtc_base/socket_factory.h"
#include "rtc_base/system/rtc_export.h"
#include "rtc_base/task_utils/pending_task_safety_flag.h"
#include "rtc_base/third_party/sigslot/sigslot.h"
#include "rtc_base/thread_annotations.h"
@ -251,7 +251,6 @@ class RTC_EXPORT NetworkManagerBase : public NetworkManager {
// Basic implementation of the NetworkManager interface that gets list
// of networks using OS APIs.
class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
public MessageHandlerAutoCleanup,
public NetworkBinderInterface,
public sigslot::has_slots<> {
public:
@ -271,8 +270,6 @@ class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
void DumpNetworks() override;
// MessageHandler interface.
void OnMessage(Message* msg) override;
bool started() { return start_count_ > 0; }
// Sets the network ignore list, which is empty by default. Any network on the
@ -347,6 +344,7 @@ class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
bool bind_using_ifname_ RTC_GUARDED_BY(thread_) = false;
std::vector<NetworkMask> vpn_;
rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> task_safety_flag_;
};
// Represents a Unix-type network interface, with a name and single address.