Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(598)

Unified Diff: webrtc/p2p/stunprober/stunprober.cc

Issue 1422593002: Separate StunProber::Start into Prepare and Run so we could create multiple of them and send out ST… (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/p2p/stunprober/stunprober.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/p2p/stunprober/stunprober.cc
diff --git a/webrtc/p2p/stunprober/stunprober.cc b/webrtc/p2p/stunprober/stunprober.cc
index d7d527a37bc0fdc37e9a2d6e8e7ae9cb5c32e7b9..ee9eb2258cf05c195a8b5cb4970262bbc063202a 100644
--- a/webrtc/p2p/stunprober/stunprober.cc
+++ b/webrtc/p2p/stunprober/stunprober.cc
@@ -28,7 +28,7 @@ namespace stunprober {
namespace {
-const int thread_wake_up_interval_ms = 5;
+const int THREAD_WAKE_UP_INTERVAL_MS = 5;
template <typename T>
void IncrementCounterByAddress(std::map<T, int>* counter_per_ip, const T& ip) {
@@ -143,7 +143,7 @@ void StunProber::Requester::SendStunRequest() {
rtc::scoped_ptr<rtc::ByteBuffer> request_packet(
new rtc::ByteBuffer(nullptr, kMaxUdpBufferSize));
if (!message.Write(request_packet.get())) {
- prober_->End(WRITE_FAILED);
+ prober_->ReportOnFinished(WRITE_FAILED);
return;
}
@@ -157,7 +157,7 @@ void StunProber::Requester::SendStunRequest() {
int rv = socket_->SendTo(const_cast<char*>(request_packet->Data()),
request_packet->Length(), addr, options);
if (rv < 0) {
- prober_->End(WRITE_FAILED);
+ prober_->ReportOnFinished(WRITE_FAILED);
return;
}
@@ -207,7 +207,7 @@ void StunProber::Requester::OnStunResponseReceived(
Request* request = GetRequestByAddress(addr.ipaddr());
if (!request) {
// Something is wrong, finish the test.
- prober_->End(GENERIC_FAILURE);
+ prober_->ReportOnFinished(GENERIC_FAILURE);
return;
}
@@ -255,6 +255,17 @@ bool StunProber::Start(const std::vector<rtc::SocketAddress>& servers,
int num_request_per_ip,
int timeout_ms,
const AsyncCallback callback) {
+ observer_adapter_.set_callback(callback);
+ return Prepare(servers, shared_socket_mode, interval_ms, num_request_per_ip,
+ timeout_ms, &observer_adapter_);
+}
+
+bool StunProber::Prepare(const std::vector<rtc::SocketAddress>& servers,
+ bool shared_socket_mode,
+ int interval_ms,
+ int num_request_per_ip,
+ int timeout_ms,
+ StunProber::Observer* observer) {
RTC_DCHECK(thread_checker_.CalledOnValidThread());
interval_ms_ = interval_ms;
shared_socket_mode_ = shared_socket_mode;
@@ -266,10 +277,19 @@ bool StunProber::Start(const std::vector<rtc::SocketAddress>& servers,
timeout_ms_ = timeout_ms;
servers_ = servers;
- finished_callback_ = callback;
+ observer_ = observer;
return ResolveServerName(servers_.back());
}
+bool StunProber::Start(StunProber::Observer* observer) {
+ observer_ = observer;
+ if (total_ready_sockets_ != total_socket_required()) {
+ return false;
+ }
+ MaybeScheduleStunRequests();
+ return true;
+}
+
bool StunProber::ResolveServerName(const rtc::SocketAddress& addr) {
rtc::AsyncResolverInterface* resolver =
socket_factory_->CreateAsyncResolver();
@@ -285,7 +305,7 @@ void StunProber::OnSocketReady(rtc::AsyncPacketSocket* socket,
const rtc::SocketAddress& addr) {
total_ready_sockets_++;
if (total_ready_sockets_ == total_socket_required()) {
- MaybeScheduleStunRequests();
+ ReportOnPrepared(SUCCESS);
}
}
@@ -307,13 +327,13 @@ void StunProber::OnServerResolved(rtc::AsyncResolverInterface* resolver) {
if (servers_.size()) {
if (!ResolveServerName(servers_.back())) {
- End(RESOLVE_FAILED);
+ ReportOnPrepared(RESOLVE_FAILED);
}
return;
}
if (all_servers_addrs_.size() == 0) {
- End(RESOLVE_FAILED);
+ ReportOnPrepared(RESOLVE_FAILED);
return;
}
@@ -328,7 +348,7 @@ void StunProber::OnServerResolved(rtc::AsyncResolverInterface* resolver) {
socket_factory_->CreateUdpSocket(rtc::SocketAddress(INADDR_ANY, 0), 0,
0));
if (!socket) {
- End(GENERIC_FAILURE);
+ ReportOnPrepared(GENERIC_FAILURE);
return;
}
// Chrome and WebRTC behave differently in terms of the state of a socket
@@ -374,25 +394,42 @@ bool StunProber::SendNextRequest() {
return true;
}
+bool StunProber::should_send_next_request(uint32_t now) {
+ if (interval_ms_ < THREAD_WAKE_UP_INTERVAL_MS) {
+ return now >= next_request_time_ms_;
+ } else {
+ return (now + (THREAD_WAKE_UP_INTERVAL_MS / 2)) >= next_request_time_ms_;
+ }
+}
+
+int StunProber::get_wake_up_interval_ms() {
+ if (interval_ms_ < THREAD_WAKE_UP_INTERVAL_MS) {
+ return 1;
+ } else {
+ return THREAD_WAKE_UP_INTERVAL_MS;
+ }
+}
+
void StunProber::MaybeScheduleStunRequests() {
RTC_DCHECK(thread_checker_.CalledOnValidThread());
uint32_t now = rtc::Time();
if (Done()) {
invoker_.AsyncInvokeDelayed<void>(
- thread_, rtc::Bind(&StunProber::End, this, SUCCESS), timeout_ms_);
+ thread_, rtc::Bind(&StunProber::ReportOnFinished, this, SUCCESS),
+ timeout_ms_);
return;
}
- if ((now + (thread_wake_up_interval_ms / 2)) >= next_request_time_ms_) {
+ if (should_send_next_request(now)) {
if (!SendNextRequest()) {
- End(GENERIC_FAILURE);
+ ReportOnFinished(GENERIC_FAILURE);
return;
}
next_request_time_ms_ = now + interval_ms_;
}
invoker_.AsyncInvokeDelayed<void>(
thread_, rtc::Bind(&StunProber::MaybeScheduleStunRequests, this),
- thread_wake_up_interval_ms /* ms */);
+ get_wake_up_interval_ms());
}
bool StunProber::GetStats(StunProber::Stats* prob_stats) const {
@@ -520,14 +557,15 @@ bool StunProber::GetStats(StunProber::Stats* prob_stats) const {
return true;
}
-void StunProber::End(StunProber::Status status) {
- RTC_DCHECK(thread_checker_.CalledOnValidThread());
- if (!finished_callback_.empty()) {
- AsyncCallback callback = finished_callback_;
- finished_callback_ = AsyncCallback();
+void StunProber::ReportOnPrepared(StunProber::Status status) {
+ if (observer_) {
+ observer_->OnPrepared(this, status);
+ }
+}
- // Callback at the last since the prober might be deleted in the callback.
- callback(this, status);
+void StunProber::ReportOnFinished(StunProber::Status status) {
+ if (observer_) {
+ observer_->OnFinished(this, status);
}
}
« no previous file with comments | « webrtc/p2p/stunprober/stunprober.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698