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/thread.h" |
32 | 33 |
33 namespace rtc { | 34 namespace rtc { |
34 | 35 |
36 namespace { | |
35 int ResolveHostname(const std::string& hostname, int family, | 37 int ResolveHostname(const std::string& hostname, int family, |
36 std::vector<IPAddress>* addresses) { | 38 std::vector<IPAddress>* addresses) { |
37 #ifdef __native_client__ | 39 #ifdef __native_client__ |
38 RTC_NOTREACHED(); | 40 RTC_NOTREACHED(); |
39 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; | 41 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; |
40 return -1; | 42 return -1; |
41 #else // __native_client__ | 43 #else // __native_client__ |
42 if (!addresses) { | 44 if (!addresses) { |
43 return -1; | 45 return -1; |
44 } | 46 } |
(...skipping 29 matching lines...) Expand all Loading... | |
74 IPAddress ip; | 76 IPAddress ip; |
75 if (IPFromAddrInfo(cursor, &ip)) { | 77 if (IPFromAddrInfo(cursor, &ip)) { |
76 addresses->push_back(ip); | 78 addresses->push_back(ip); |
77 } | 79 } |
78 } | 80 } |
79 } | 81 } |
80 freeaddrinfo(result); | 82 freeaddrinfo(result); |
81 return 0; | 83 return 0; |
82 #endif // !__native_client__ | 84 #endif // !__native_client__ |
83 } | 85 } |
86 } // namespace | |
84 | 87 |
85 // AsyncResolver | 88 // AsyncResolver |
86 AsyncResolver::AsyncResolver() | 89 AsyncResolver::AsyncResolver() |
87 : SignalThread(false /* use_socket_server */), error_(-1) {} | 90 : construction_thread_(Thread::Current()), |
91 worker_(&ThreadEntry_w, this, "AsyncResolver") {} | |
88 | 92 |
89 AsyncResolver::~AsyncResolver() = default; | 93 AsyncResolver::~AsyncResolver() { |
94 RTC_DCHECK(construction_thread_->IsCurrent()); | |
95 worker_.Stop(); | |
96 } | |
90 | 97 |
91 void AsyncResolver::Start(const SocketAddress& addr) { | 98 void AsyncResolver::Start(const SocketAddress& addr) { |
99 RTC_DCHECK(construction_thread_->IsCurrent()); | |
100 RTC_DCHECK(!worker_.IsRunning()); | |
92 addr_ = addr; | 101 addr_ = addr; |
93 // SignalThred Start will kickoff the resolve process. | 102 worker_.Start(); |
tommi
2017/06/15 10:20:32
implementing a custom QueuedTask that handles the
nisse-webrtc
2017/06/15 11:33:47
I'll give it a try.
| |
94 SignalThread::Start(); | |
95 } | 103 } |
96 | 104 |
97 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { | 105 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { |
98 if (error_ != 0 || addresses_.empty()) | 106 if (error_ != 0 || addresses_.empty()) |
99 return false; | 107 return false; |
100 | 108 |
101 *addr = addr_; | 109 *addr = addr_; |
102 for (size_t i = 0; i < addresses_.size(); ++i) { | 110 for (size_t i = 0; i < addresses_.size(); ++i) { |
103 if (family == addresses_[i].family()) { | 111 if (family == addresses_[i].family()) { |
104 addr->SetResolvedIP(addresses_[i]); | 112 addr->SetResolvedIP(addresses_[i]); |
105 return true; | 113 return true; |
106 } | 114 } |
107 } | 115 } |
108 return false; | 116 return false; |
109 } | 117 } |
110 | 118 |
111 int AsyncResolver::GetError() const { | 119 int AsyncResolver::GetError() const { |
112 return error_; | 120 return error_; |
113 } | 121 } |
114 | 122 |
115 void AsyncResolver::Destroy(bool wait) { | 123 void AsyncResolver::Destroy(bool wait) { |
116 SignalThread::Destroy(wait); | 124 RTC_DCHECK(construction_thread_->IsCurrent()); |
125 // If we don't wait here, we will nevertheless wait in the destructor. | |
126 if (wait) { | |
tommi
2017/06/15 10:20:32
why is wait == false supported?
nisse-webrtc
2017/06/15 11:33:47
It's part of the AsyncResolverInterface, which I d
| |
127 worker_.Stop(); | |
128 } | |
117 } | 129 } |
118 | 130 |
119 void AsyncResolver::DoWork() { | 131 void AsyncResolver::ResolveAddress_w() { |
120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), | 132 RTC_DCHECK(IsThreadRefEqual(worker_.GetThreadRef(), CurrentThreadRef())); |
121 &addresses_); | 133 std::vector<IPAddress> addresses; |
134 int error = ResolveHostname(addr_.hostname().c_str(), addr_.family(), | |
135 &addresses); | |
136 // Ensure SignalDone is called on the main thread. | |
137 construction_thread_->Invoke<void>( | |
nisse-webrtc
2017/06/09 09:44:52
Actually, I don't think there's any need for this
Taylor Brandstetter
2017/06/09 18:02:58
Using an AsyncInvoker is the most convenient way I
| |
138 RTC_FROM_HERE, rtc::Bind(&AsyncResolver::ResolveDone, this, | |
139 error, addresses)); | |
tommi
2017/06/15 10:20:32
std::move? (or will address be implicitly moved?)
nisse-webrtc
2017/06/15 11:33:47
Done.
| |
122 } | 140 } |
123 | 141 |
124 void AsyncResolver::OnWorkDone() { | 142 void AsyncResolver::ResolveDone(int error, std::vector<IPAddress> addresses) { |
143 RTC_DCHECK(construction_thread_->IsCurrent()); | |
144 error_ = error; | |
145 addresses_ = addresses; | |
tommi
2017/06/15 10:20:32
std::move
| |
125 SignalDone(this); | 146 SignalDone(this); |
126 } | 147 } |
127 | 148 |
149 // static | |
150 void AsyncResolver::ThreadEntry_w(void* p) { | |
tommi
2017/06/15 10:20:32
What about using a TaskQueue?
| |
151 AsyncResolver* resolver = static_cast<AsyncResolver*>(p); | |
152 resolver->ResolveAddress_w(); | |
153 } | |
154 | |
128 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { | 155 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { |
129 #if defined(WEBRTC_WIN) | 156 #if defined(WEBRTC_WIN) |
130 return win32_inet_ntop(af, src, dst, size); | 157 return win32_inet_ntop(af, src, dst, size); |
131 #else | 158 #else |
132 return ::inet_ntop(af, src, dst, size); | 159 return ::inet_ntop(af, src, dst, size); |
133 #endif | 160 #endif |
134 } | 161 } |
135 | 162 |
136 int inet_pton(int af, const char* src, void *dst) { | 163 int inet_pton(int af, const char* src, void *dst) { |
137 #if defined(WEBRTC_WIN) | 164 #if defined(WEBRTC_WIN) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 break; | 237 break; |
211 } | 238 } |
212 } | 239 } |
213 freeifaddrs(ifa); | 240 freeifaddrs(ifa); |
214 return has_ipv6; | 241 return has_ipv6; |
215 #else | 242 #else |
216 return true; | 243 return true; |
217 #endif | 244 #endif |
218 } | 245 } |
219 } // namespace rtc | 246 } // namespace rtc |
OLD | NEW |