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/byteorder.h" | 28 #include "webrtc/base/byteorder.h" |
29 #include "webrtc/base/bind.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 : main_(Thread::Current()), |
tommi
2017/06/08 08:02:06
one other idea for a variable name could be |const
| |
91 worker_(ThreadEntry, this, "AsyncResolver"), | |
tommi
2017/06/08 08:02:05
nit: worker_(&ThreadEntry, this, "AsyncResolver"),
nisse-webrtc
2017/06/08 09:52:33
Done.
| |
92 error_(-1) {} | |
tommi
2017/06/08 08:02:07
nit: you could initialize this value inline in the
nisse-webrtc
2017/06/08 09:52:33
Done.
| |
88 | 93 |
89 AsyncResolver::~AsyncResolver() = default; | 94 AsyncResolver::~AsyncResolver() { |
95 RTC_DCHECK(main_->IsCurrent()); | |
96 worker_.Stop(); | |
97 } | |
90 | 98 |
91 void AsyncResolver::Start(const SocketAddress& addr) { | 99 void AsyncResolver::Start(const SocketAddress& addr) { |
100 RTC_DCHECK(main_->IsCurrent()); | |
tommi
2017/06/08 08:02:06
also add:
RTC_DCHECK(!worker_.IsRunning());
| |
92 addr_ = addr; | 101 addr_ = addr; |
93 // SignalThred Start will kickoff the resolve process. | 102 worker_.Start(); |
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 { |
tommi
2017/06/08 08:02:06
on what thread does this method run?
nisse-webrtc
2017/06/08 09:52:33
Don't know. My understanding is that the applicati
| |
106 rtc::CritScope cs_(&lock_); | |
98 if (error_ != 0 || addresses_.empty()) | 107 if (error_ != 0 || addresses_.empty()) |
99 return false; | 108 return false; |
100 | 109 |
101 *addr = addr_; | 110 *addr = addr_; |
102 for (size_t i = 0; i < addresses_.size(); ++i) { | 111 for (size_t i = 0; i < addresses_.size(); ++i) { |
103 if (family == addresses_[i].family()) { | 112 if (family == addresses_[i].family()) { |
104 addr->SetResolvedIP(addresses_[i]); | 113 addr->SetResolvedIP(addresses_[i]); |
tommi
2017/06/08 08:02:09
calling out while holding a lock, would be nice to
nisse-webrtc
2017/06/08 09:52:33
SetResolvedAddress is a cheap setter, as far as I
| |
105 return true; | 114 return true; |
106 } | 115 } |
107 } | 116 } |
108 return false; | 117 return false; |
109 } | 118 } |
110 | 119 |
111 int AsyncResolver::GetError() const { | 120 int AsyncResolver::GetError() const { |
121 rtc::CritScope cs_(&lock_); | |
tommi
2017/06/08 08:02:08
this sort of pattern always feels strange to me.
nisse-webrtc
2017/06/08 09:52:33
Agreed, this use implies a pretty heavy memory bar
| |
112 return error_; | 122 return error_; |
113 } | 123 } |
114 | 124 |
115 void AsyncResolver::Destroy(bool wait) { | 125 void AsyncResolver::Destroy(bool wait) { |
116 SignalThread::Destroy(wait); | 126 RTC_DCHECK(main_->IsCurrent()); |
127 // If we don't wait here, we will nevertheless wait in the destructor. | |
128 if (wait) { | |
129 worker_.Stop(); | |
130 } | |
117 } | 131 } |
118 | 132 |
119 void AsyncResolver::DoWork() { | 133 void AsyncResolver::DoWork() { |
tommi
2017/06/08 08:02:06
would like to have a thread check for this method
nisse-webrtc
2017/06/08 09:52:33
It's private and with only a single call site. I'm
| |
120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), | 134 { |
121 &addresses_); | 135 rtc::CritScope cs_(&lock_); |
136 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), | |
tommi
2017/06/08 08:02:06
what about not touching addr_ on any other thread
nisse-webrtc
2017/06/08 09:52:33
|addr_| is written in Start (on construction_threa
| |
137 &addresses_); | |
138 } | |
139 // Ensure SignalDone is called on the main thread. | |
140 main_->Invoke<void>(RTC_FROM_HERE, | |
nisse-webrtc
2017/06/02 08:41:04
The sequence of events is slightly different from
Taylor Brandstetter
2017/06/05 22:33:25
The new sequence seems better actually. If the wor
| |
141 rtc::Bind(&AsyncResolver::OnWorkDone, this)); | |
122 } | 142 } |
123 | 143 |
124 void AsyncResolver::OnWorkDone() { | 144 void AsyncResolver::OnWorkDone() { |
Taylor Brandstetter
2017/06/05 22:33:25
nit: Rename to "SignalWorkDone"?
nisse-webrtc
2017/06/07 07:17:31
Done.
| |
145 RTC_DCHECK(main_->IsCurrent()); | |
125 SignalDone(this); | 146 SignalDone(this); |
126 } | 147 } |
127 | 148 |
149 // static | |
150 void AsyncResolver::ThreadEntry(void *p) { | |
151 AsyncResolver* resolver = static_cast<AsyncResolver*>(p); | |
152 resolver->DoWork(); | |
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 |