Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2008 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2008 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/nethelpers.h" | 11 #include "webrtc/base/nethelpers.h" |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 | 14 |
| 15 #if defined(WEBRTC_WIN) | 15 #if defined(WEBRTC_WIN) |
| 16 #include <ws2spi.h> | 16 #include <ws2spi.h> |
| 17 #include <ws2tcpip.h> | 17 #include <ws2tcpip.h> |
| 18 #include "webrtc/base/win32.h" | 18 #include "webrtc/base/win32.h" |
| 19 #endif | 19 #endif |
| 20 #if defined(WEBRTC_POSIX) && !defined(__native_client__) | 20 #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 21 #if defined(WEBRTC_ANDROID) | 21 #if defined(WEBRTC_ANDROID) |
| 22 #include "webrtc/base/ifaddrs-android.h" | 22 #include "webrtc/base/ifaddrs-android.h" |
| 23 #else | 23 #else |
| 24 #include <ifaddrs.h> | 24 #include <ifaddrs.h> |
| 25 #endif | 25 #endif |
| 26 #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) | 26 #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 27 | 27 |
| 28 #include "webrtc/base/bind.h" | |
| 28 #include "webrtc/base/byteorder.h" | 29 #include "webrtc/base/byteorder.h" |
| 29 #include "webrtc/base/checks.h" | 30 #include "webrtc/base/checks.h" |
| 30 #include "webrtc/base/logging.h" | 31 #include "webrtc/base/logging.h" |
| 31 #include "webrtc/base/signalthread.h" | 32 #include "webrtc/base/ptr_util.h" |
| 33 #include "webrtc/base/task_queue.h" | |
| 34 #include "webrtc/base/thread.h" | |
| 32 | 35 |
| 33 namespace rtc { | 36 namespace rtc { |
| 34 | 37 |
| 38 namespace { | |
| 35 int ResolveHostname(const std::string& hostname, int family, | 39 int ResolveHostname(const std::string& hostname, int family, |
| 36 std::vector<IPAddress>* addresses) { | 40 std::vector<IPAddress>* addresses) { |
| 37 #ifdef __native_client__ | 41 #ifdef __native_client__ |
| 38 RTC_NOTREACHED(); | 42 RTC_NOTREACHED(); |
| 39 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; | 43 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; |
| 40 return -1; | 44 return -1; |
| 41 #else // __native_client__ | 45 #else // __native_client__ |
| 42 if (!addresses) { | 46 if (!addresses) { |
| 43 return -1; | 47 return -1; |
| 44 } | 48 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 74 IPAddress ip; | 78 IPAddress ip; |
| 75 if (IPFromAddrInfo(cursor, &ip)) { | 79 if (IPFromAddrInfo(cursor, &ip)) { |
| 76 addresses->push_back(ip); | 80 addresses->push_back(ip); |
| 77 } | 81 } |
| 78 } | 82 } |
| 79 } | 83 } |
| 80 freeaddrinfo(result); | 84 freeaddrinfo(result); |
| 81 return 0; | 85 return 0; |
| 82 #endif // !__native_client__ | 86 #endif // !__native_client__ |
| 83 } | 87 } |
| 88 } // namespace | |
| 84 | 89 |
| 85 // AsyncResolver | 90 // AsyncResolver |
| 86 AsyncResolver::AsyncResolver() | 91 AsyncResolver::AsyncResolver() |
| 87 : SignalThread(false /* use_socket_server */), error_(-1) {} | 92 : state_(State::NONE), construction_thread_(Thread::Current()) { |
| 93 RTC_DCHECK(construction_thread_); | |
| 94 } | |
| 88 | 95 |
| 89 AsyncResolver::~AsyncResolver() = default; | 96 AsyncResolver::~AsyncResolver() { |
| 97 RTC_DCHECK(construction_thread_->IsCurrent()); | |
| 98 } | |
| 90 | 99 |
| 91 void AsyncResolver::Start(const SocketAddress& addr) { | 100 void AsyncResolver::Start(const SocketAddress& addr) { |
| 101 RTC_DCHECK_RUN_ON(construction_thread_); | |
| 102 RTC_DCHECK(!resolver_queue_); | |
| 103 RTC_DCHECK(state_ == State::NONE); | |
| 104 // TODO(nisse): Support injection of task queue at construction? | |
| 105 resolver_queue_ = rtc::MakeUnique<TaskQueue>("AsyncResolverQueue"); | |
| 92 addr_ = addr; | 106 addr_ = addr; |
| 93 // SignalThred Start will kickoff the resolve process. | 107 resolver_queue_->PostTask([this]() { |
| 94 SignalThread::Start(); | 108 std::vector<IPAddress> addresses; |
| 109 int error = | |
| 110 ResolveHostname(addr_.hostname().c_str(), addr_.family(), &addresses); | |
| 111 // Ensure SignalDone is called on the main thread. | |
| 112 // TODO(nisse): Despite std::move below, we likely get a copy, due to the | |
| 113 // way rtc::Bind is implemented. | |
| 114 construction_thread_->Post( | |
| 115 RTC_FROM_HERE, rtc::Bind(&AsyncResolver::ResolveDone, this, error, | |
| 116 std::move(addresses))); | |
| 117 }); | |
| 118 state_ = State::PENDING; | |
| 95 } | 119 } |
| 96 | 120 |
| 97 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { | 121 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { |
| 98 if (error_ != 0 || addresses_.empty()) | 122 if (error_ != 0 || addresses_.empty()) |
| 99 return false; | 123 return false; |
| 100 | 124 |
| 101 *addr = addr_; | 125 *addr = addr_; |
| 102 for (size_t i = 0; i < addresses_.size(); ++i) { | 126 for (size_t i = 0; i < addresses_.size(); ++i) { |
| 103 if (family == addresses_[i].family()) { | 127 if (family == addresses_[i].family()) { |
| 104 addr->SetResolvedIP(addresses_[i]); | 128 addr->SetResolvedIP(addresses_[i]); |
| 105 return true; | 129 return true; |
| 106 } | 130 } |
| 107 } | 131 } |
| 108 return false; | 132 return false; |
| 109 } | 133 } |
| 110 | 134 |
| 111 int AsyncResolver::GetError() const { | 135 int AsyncResolver::GetError() const { |
| 112 return error_; | 136 return error_; |
| 113 } | 137 } |
| 114 | 138 |
| 115 void AsyncResolver::Destroy(bool wait) { | 139 void AsyncResolver::Destroy(bool wait) { |
| 116 SignalThread::Destroy(wait); | 140 RTC_DCHECK_RUN_ON(construction_thread_); |
| 141 RTC_DCHECK(state_ != State::DESTROYED); | |
| 142 // If we don't wait here, we will nevertheless wait in the destructor. | |
| 143 if (wait || state_ != State::PENDING) { | |
| 144 // Destroy task queue, blocks on any currently running task. If we have a | |
| 145 // pending task, it will post a call to ResolveDone before finishing, which | |
| 146 // we will never handle. | |
|
tommi
2017/06/27 12:01:14
this comment doesn't really explain why it's OK to
nisse-webrtc
2017/06/28 11:47:54
The AsyncResolverInterface is poorly documented, b
| |
| 147 delete this; | |
| 148 } else { | |
| 149 state_ = State::DESTROYED; | |
| 150 } | |
| 117 } | 151 } |
| 118 | 152 |
| 119 void AsyncResolver::DoWork() { | 153 void AsyncResolver::ResolveDone(int error, std::vector<IPAddress> addresses) { |
| 120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), | 154 RTC_DCHECK_RUN_ON(construction_thread_); |
| 121 &addresses_); | 155 error_ = error; |
| 122 } | 156 addresses_ = std::move(addresses); |
| 123 | 157 switch (state_) { |
| 124 void AsyncResolver::OnWorkDone() { | 158 case State::PENDING: |
| 125 SignalDone(this); | 159 SignalDone(this); |
| 160 state_ = State::NONE; | |
| 161 break; | |
| 162 case State::DESTROYED: | |
| 163 delete this; | |
| 164 break; | |
| 165 default: | |
| 166 RTC_NOTREACHED(); | |
| 167 } | |
| 126 } | 168 } |
| 127 | 169 |
| 128 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { | 170 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { |
| 129 #if defined(WEBRTC_WIN) | 171 #if defined(WEBRTC_WIN) |
| 130 return win32_inet_ntop(af, src, dst, size); | 172 return win32_inet_ntop(af, src, dst, size); |
| 131 #else | 173 #else |
| 132 return ::inet_ntop(af, src, dst, size); | 174 return ::inet_ntop(af, src, dst, size); |
| 133 #endif | 175 #endif |
| 134 } | 176 } |
| 135 | 177 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 break; | 252 break; |
| 211 } | 253 } |
| 212 } | 254 } |
| 213 freeifaddrs(ifa); | 255 freeifaddrs(ifa); |
| 214 return has_ipv6; | 256 return has_ipv6; |
| 215 #else | 257 #else |
| 216 return true; | 258 return true; |
| 217 #endif | 259 #endif |
| 218 } | 260 } |
| 219 } // namespace rtc | 261 } // namespace rtc |
| OLD | NEW |