diff --git a/p2p/base/port.cc b/p2p/base/port.cc index 035d3d4bb3..7b54c11cb8 100644 --- a/p2p/base/port.cc +++ b/p2p/base/port.cc @@ -188,6 +188,9 @@ void Port::Construct() { } Port::~Port() { + RTC_DCHECK_RUN_ON(thread_); + CancelPendingTasks(); + // Delete all of the remaining connections. We copy the list up front // because each deletion will cause it to be modified. @@ -609,6 +612,16 @@ rtc::DiffServCodePoint Port::StunDscpValue() const { return rtc::DSCP_NO_CHANGE; } +void Port::set_timeout_delay(int delay) { + RTC_DCHECK_RUN_ON(thread_); + // Although this method is meant to only be used by tests, some downstream + // projects have started using it. Ideally we should update our tests to not + // require to modify this state and instead use a testing harness that allows + // adjusting the clock and then just use the kPortTimeoutDelay constant + // directly. + timeout_delay_ = delay; +} + bool Port::ParseStunUsername(const StunMessage* stun_msg, std::string* local_ufrag, std::string* remote_ufrag) const { @@ -818,7 +831,14 @@ void Port::Prune() { thread_->Post(RTC_FROM_HERE, this, MSG_DESTROY_IF_DEAD); } +// Call to stop any currently pending operations from running. +void Port::CancelPendingTasks() { + RTC_DCHECK_RUN_ON(thread_); + thread_->Clear(this); +} + void Port::OnMessage(rtc::Message* pmsg) { + RTC_DCHECK_RUN_ON(thread_); RTC_DCHECK(pmsg->message_id == MSG_DESTROY_IF_DEAD); bool dead = (state_ == State::INIT || state_ == State::PRUNED) && diff --git a/p2p/base/port.h b/p2p/base/port.h index 1e20d13462..43196e5c03 100644 --- a/p2p/base/port.h +++ b/p2p/base/port.h @@ -160,7 +160,7 @@ typedef std::set ServerAddresses; // connections to similar mechanisms of the other client. Subclasses of this // one add support for specific mechanisms like local UDP ports. class Port : public PortInterface, - public rtc::MessageHandlerAutoCleanup, + public rtc::MessageHandler, public sigslot::has_slots<> { public: // INIT: The state when a port is just created. @@ -209,6 +209,9 @@ class Port : public PortInterface, // Allows a port to be destroyed if no connection is using it. void Prune(); + // Call to stop any currently pending operations from running. + void CancelPendingTasks(); + // The thread on which this port performs its I/O. rtc::Thread* thread() { return thread_; } @@ -322,7 +325,7 @@ class Port : public PortInterface, uint16_t max_port() { return max_port_; } // Timeout shortening function to speed up unit tests. - void set_timeout_delay(int delay) { timeout_delay_ = delay; } + void set_timeout_delay(int delay); // This method will return local and remote username fragements from the // stun username attribute if present. @@ -437,7 +440,7 @@ class Port : public PortInterface, void OnNetworkTypeChanged(const rtc::Network* network); - rtc::Thread* thread_; + rtc::Thread* const thread_; rtc::PacketSocketFactory* factory_; std::string type_; bool send_retransmit_count_attribute_;