Files
platform-external-webrtc/rtc_base/fake_mdns_responder.h
Qingsi Wang 3ea7b83fa3 Resolve the race condition between mDNS name registration and
cricket::Port::SignalPortComplete.

The mDNS name registration is asynchronously executed by the mDNS
responder, and a host candidate with an mDNS name is only gathered after
this completes. SignalPortComplete however is currently done
synchronously by UDPPort, and any candidate gathered by a UDPPort after
this signal is fired would be discarded.

Bug: webrtc:9964, webrtc:9605
Change-Id: If8aaf193ef26c06bd118e6418b62ba0de5e87e3c
Reviewed-on: https://webrtc-review.googlesource.com/c/109541
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Reviewed-by: Zach Stein <zstein@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Commit-Queue: Qingsi Wang <qingsi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25534}
2018-11-07 02:59:41 +00:00

63 lines
1.9 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.
*/
#ifndef RTC_BASE_FAKE_MDNS_RESPONDER_H_
#define RTC_BASE_FAKE_MDNS_RESPONDER_H_
#include <map>
#include <memory>
#include <string>
#include "rtc_base/mdns_responder_interface.h"
#include "rtc_base/helpers.h"
namespace webrtc {
class FakeMdnsResponder : public MdnsResponderInterface {
public:
explicit FakeMdnsResponder(rtc::Thread* thread) : thread_(thread) {}
~FakeMdnsResponder() = default;
void CreateNameForAddress(const rtc::IPAddress& addr,
NameCreatedCallback callback) override {
std::string name;
if (addr_name_map_.find(addr) != addr_name_map_.end()) {
name = addr_name_map_[addr];
} else {
name = std::to_string(next_available_id_++) + ".local";
addr_name_map_[addr] = name;
}
invoker_.AsyncInvoke<void>(
RTC_FROM_HERE, thread_,
[callback, addr, name]() { callback(addr, name); });
}
void RemoveNameForAddress(const rtc::IPAddress& addr,
NameRemovedCallback callback) override {
auto it = addr_name_map_.find(addr);
if (it != addr_name_map_.end()) {
addr_name_map_.erase(it);
}
bool result = it != addr_name_map_.end();
invoker_.AsyncInvoke<void>(RTC_FROM_HERE, thread_,
[callback, result]() { callback(result); });
}
private:
uint32_t next_available_id_ = 0;
std::map<rtc::IPAddress, std::string> addr_name_map_;
rtc::Thread* thread_;
rtc::AsyncInvoker invoker_;
};
} // namespace webrtc
#endif // RTC_BASE_FAKE_MDNS_RESPONDER_H_