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 = ResolveHostname(addr_.hostname().c_str(), addr_.family(), |
| 110 &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_->PostFunctor( |
| 115 RTC_FROM_HERE, rtc::Bind(&AsyncResolver::ResolveDone, this, |
| 116 error, std::move(addresses))); |
| 117 }); |
95 } | 118 } |
96 | 119 |
97 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { | 120 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { |
98 if (error_ != 0 || addresses_.empty()) | 121 if (error_ != 0 || addresses_.empty()) |
99 return false; | 122 return false; |
100 | 123 |
101 *addr = addr_; | 124 *addr = addr_; |
102 for (size_t i = 0; i < addresses_.size(); ++i) { | 125 for (size_t i = 0; i < addresses_.size(); ++i) { |
103 if (family == addresses_[i].family()) { | 126 if (family == addresses_[i].family()) { |
104 addr->SetResolvedIP(addresses_[i]); | 127 addr->SetResolvedIP(addresses_[i]); |
105 return true; | 128 return true; |
106 } | 129 } |
107 } | 130 } |
108 return false; | 131 return false; |
109 } | 132 } |
110 | 133 |
111 int AsyncResolver::GetError() const { | 134 int AsyncResolver::GetError() const { |
112 return error_; | 135 return error_; |
113 } | 136 } |
114 | 137 |
115 void AsyncResolver::Destroy(bool wait) { | 138 void AsyncResolver::Destroy(bool wait) { |
116 SignalThread::Destroy(wait); | 139 RTC_DCHECK_RUN_ON(construction_thread_); |
| 140 RTC_DCHECK(state_ != State::DESTROYED); |
| 141 // If we don't wait here, we will nevertheless wait in the destructor. |
| 142 if (wait || state_ != State::PENDING) { |
| 143 // Destroy task queue, blocks on any currently running task. If we have a |
| 144 // pending task, it will post a call to ResolveDone before finishing, which |
| 145 // we will never handle. |
| 146 delete this; |
| 147 } else { |
| 148 state_ = State::DESTROYED; |
| 149 } |
117 } | 150 } |
118 | 151 |
119 void AsyncResolver::DoWork() { | 152 void AsyncResolver::ResolveDone(int error, std::vector<IPAddress> addresses) { |
120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), | 153 RTC_DCHECK_RUN_ON(construction_thread_); |
121 &addresses_); | 154 error_ = error; |
122 } | 155 addresses_ = std::move(addresses); |
123 | 156 switch (state_) { |
124 void AsyncResolver::OnWorkDone() { | 157 case State::PENDING: |
125 SignalDone(this); | 158 SignalDone(this); |
| 159 state_ = State::NONE; |
| 160 break; |
| 161 case State::DESTROYED: |
| 162 delete this; |
| 163 break; |
| 164 default: |
| 165 RTC_NOTREACHED(); |
| 166 } |
126 } | 167 } |
127 | 168 |
128 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { | 169 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { |
129 #if defined(WEBRTC_WIN) | 170 #if defined(WEBRTC_WIN) |
130 return win32_inet_ntop(af, src, dst, size); | 171 return win32_inet_ntop(af, src, dst, size); |
131 #else | 172 #else |
132 return ::inet_ntop(af, src, dst, size); | 173 return ::inet_ntop(af, src, dst, size); |
133 #endif | 174 #endif |
134 } | 175 } |
135 | 176 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 break; | 251 break; |
211 } | 252 } |
212 } | 253 } |
213 freeifaddrs(ifa); | 254 freeifaddrs(ifa); |
214 return has_ipv6; | 255 return has_ipv6; |
215 #else | 256 #else |
216 return true; | 257 return true; |
217 #endif | 258 #endif |
218 } | 259 } |
219 } // namespace rtc | 260 } // namespace rtc |
OLD | NEW |