Refactor StunRequest and StunRequestManager classes.
* Make StunRequest::manager_ a reference, inject ref at ctor time.
* Make other member variables private.
* Mark methods that are only used for testing with "ForTest"
* Add RTC_GUARDED_BY for member variables and thread checks.
* Remove/reduce 'friend'-ness between classes.
* Use std::unique_ptr for owned and passed message pointers.
* Rename `requests_` to `request_manager_` (type: StunRequestManager)
Bug: webrtc:13892
Change-Id: I3a5d511b3c2645bb6813352d39e9fefe422dd1de
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/258620
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36529}
diff --git a/p2p/base/stun_request.h b/p2p/base/stun_request.h
index b417c70..5127602 100644
--- a/p2p/base/stun_request.h
+++ b/p2p/base/stun_request.h
@@ -15,6 +15,7 @@
#include <stdint.h>
#include <map>
+#include <memory>
#include <string>
#include "api/transport/stun.h"
@@ -47,11 +48,11 @@
// If `msg_type` is kAllRequests, sends all pending requests right away.
// Otherwise, sends those that have a matching type right away.
// Only for testing.
- void Flush(int msg_type);
+ void FlushForTest(int msg_type);
// Returns true if at least one request with `msg_type` is scheduled for
// transmission. For testing only.
- bool HasRequest(int msg_type);
+ bool HasRequestForTest(int msg_type);
// Removes a stun request that was added previously. This will happen
// automatically when a request succeeds, fails, or times out.
@@ -65,7 +66,10 @@
bool CheckResponse(StunMessage* msg);
bool CheckResponse(const char* data, size_t size);
- bool empty() { return requests_.empty(); }
+ bool empty() const;
+
+ // TODO(tommi): Use TaskQueueBase* instead of rtc::Thread.
+ rtc::Thread* network_thread() const { return thread_; }
// Raised when there are bytes to be sent.
sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
@@ -74,27 +78,26 @@
typedef std::map<std::string, StunRequest*> RequestMap;
rtc::Thread* const thread_;
- RequestMap requests_;
-
- friend class StunRequest;
+ RequestMap requests_ RTC_GUARDED_BY(thread_);
};
// Represents an individual request to be sent. The STUN message can either be
// constructed beforehand or built on demand.
class StunRequest : public rtc::MessageHandler {
public:
- StunRequest();
- explicit StunRequest(StunMessage* request);
+ explicit StunRequest(StunRequestManager& manager);
+ StunRequest(StunRequestManager& manager,
+ std::unique_ptr<StunMessage> request);
~StunRequest() override;
// Causes our wrapped StunMessage to be Prepared
void Construct();
// The manager handling this request (if it has been scheduled for sending).
- StunRequestManager* manager() { return manager_; }
+ StunRequestManager* manager() { return &manager_; }
// Returns the transaction ID of this request.
- const std::string& id() { return msg_->transaction_id(); }
+ const std::string& id() const { return msg_->transaction_id(); }
// Returns the reduced transaction ID of this request.
uint32_t reduced_transaction_id() const {
@@ -107,15 +110,11 @@
// Returns a const pointer to `msg_`.
const StunMessage* msg() const;
- // Returns a mutable pointer to `msg_`.
- StunMessage* mutable_msg();
-
// Time elapsed since last send (in ms)
int Elapsed() const;
protected:
- int count_;
- bool timeout_;
+ friend class StunRequestManager;
// Fills in a request object to be sent. Note that request's transaction ID
// will already be set and cannot be changed.
@@ -130,17 +129,21 @@
// Returns the next delay for resends.
virtual int resend_delay();
- private:
- void set_manager(StunRequestManager* manager);
+ webrtc::TaskQueueBase* network_thread() const {
+ return manager_.network_thread();
+ }
+ void set_timed_out();
+
+ private:
// Handles messages for sending and timeout.
void OnMessage(rtc::Message* pmsg) override;
- StunRequestManager* manager_;
- StunMessage* msg_;
- int64_t tstamp_;
-
- friend class StunRequestManager;
+ StunRequestManager& manager_;
+ const std::unique_ptr<StunMessage> msg_;
+ int64_t tstamp_ RTC_GUARDED_BY(network_thread());
+ int count_ RTC_GUARDED_BY(network_thread());
+ bool timeout_ RTC_GUARDED_BY(network_thread());
};
} // namespace cricket