webrtc_m130/p2p/base/basic_async_resolver_factory.cc
Mirko Bonadei 5a40b37105 Revert "Use the new DNS resolver API in PeerConnection"
This reverts commit acf8ccb3c9f001b0ed749aca52b2d436d66f9586.

Reason for revert: Speculative revert for https://ci.chromium.org/ui/p/chromium/builders/try/win10_chromium_x64_rel_ng/b8851745102358680592/overview.

Original change's description:
> Use the new DNS resolver API in PeerConnection
>
> Bug: webrtc:12598
> Change-Id: I5a14058e7f28c993ed927749df7357c715ba83fb
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/212961
> Reviewed-by: Niels Moller <nisse@webrtc.org>
> Commit-Queue: Harald Alvestrand <hta@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#33561}

# Not skipping CQ checks because original CL landed > 1 day ago.

TBR=hta@webrtc.org

Bug: webrtc:12598
Change-Id: Idc9853cb569849c49052f9cbd865614710fff979
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/213188
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33591}
2021-03-30 08:37:01 +00:00

116 lines
3.5 KiB
C++

/*
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "p2p/base/basic_async_resolver_factory.h"
#include <memory>
#include <utility>
#include "absl/memory/memory.h"
#include "api/async_dns_resolver.h"
#include "rtc_base/async_resolver.h"
namespace webrtc {
rtc::AsyncResolverInterface* BasicAsyncResolverFactory::Create() {
return new rtc::AsyncResolver();
}
class WrappingAsyncDnsResolver;
class WrappingAsyncDnsResolverResult : public AsyncDnsResolverResult {
public:
explicit WrappingAsyncDnsResolverResult(WrappingAsyncDnsResolver* owner)
: owner_(owner) {}
~WrappingAsyncDnsResolverResult() {}
// Note: Inline declaration not possible, since it refers to
// WrappingAsyncDnsResolver.
bool GetResolvedAddress(int family, rtc::SocketAddress* addr) const override;
int GetError() const override;
private:
WrappingAsyncDnsResolver* const owner_;
};
class WrappingAsyncDnsResolver : public AsyncDnsResolverInterface,
public sigslot::has_slots<> {
public:
explicit WrappingAsyncDnsResolver(rtc::AsyncResolverInterface* wrapped)
: wrapped_(absl::WrapUnique(wrapped)), result_(this) {}
~WrappingAsyncDnsResolver() override { wrapped_.release()->Destroy(false); }
void Start(const rtc::SocketAddress& addr,
std::function<void()> callback) override {
RTC_DCHECK(state_ == State::kNotStarted);
state_ = State::kStarted;
callback_ = callback;
wrapped_->SignalDone.connect(this,
&WrappingAsyncDnsResolver::OnResolveResult);
wrapped_->Start(addr);
}
const AsyncDnsResolverResult& result() const override {
RTC_DCHECK(state_ == State::kResolved);
return result_;
}
// For use by WrappingAsyncDnsResolverResult
rtc::AsyncResolverInterface* wrapped() const { return wrapped_.get(); }
private:
enum class State { kNotStarted, kStarted, kResolved };
void OnResolveResult(rtc::AsyncResolverInterface* ref) {
RTC_DCHECK(state_ == State::kStarted);
RTC_DCHECK_EQ(ref, wrapped_.get());
state_ = State::kResolved;
callback_();
}
std::function<void()> callback_;
std::unique_ptr<rtc::AsyncResolverInterface> wrapped_;
State state_ = State::kNotStarted;
WrappingAsyncDnsResolverResult result_;
};
bool WrappingAsyncDnsResolverResult::GetResolvedAddress(
int family,
rtc::SocketAddress* addr) const {
if (!owner_->wrapped()) {
return false;
}
return owner_->wrapped()->GetResolvedAddress(family, addr);
}
int WrappingAsyncDnsResolverResult::GetError() const {
if (!owner_->wrapped()) {
return -1; // FIXME: Find a code that makes sense.
}
return owner_->wrapped()->GetError();
}
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
WrappingAsyncDnsResolverFactory::Create() {
return std::make_unique<WrappingAsyncDnsResolver>(wrapped_factory_->Create());
}
std::unique_ptr<webrtc::AsyncDnsResolverInterface>
WrappingAsyncDnsResolverFactory::CreateAndResolve(
const rtc::SocketAddress& addr,
std::function<void()> callback) {
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver = Create();
resolver->Start(addr, callback);
return resolver;
}
} // namespace webrtc