Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: webrtc/rtc_base/nethelpers.cc

Issue 2915253002: Delete SignalThread class. (Closed)
Patch Set: Another TODO on moving to QueuedTask. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/rtc_base/nethelpers.h ('k') | webrtc/rtc_base/signalthread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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() : construction_thread_(Thread::Current()) {
87 : SignalThread(false /* use_socket_server */), error_(-1) {} 92 RTC_DCHECK(construction_thread_);
93 }
88 94
89 AsyncResolver::~AsyncResolver() = default; 95 AsyncResolver::~AsyncResolver() {
96 RTC_DCHECK(construction_thread_->IsCurrent());
97 if (state_)
98 // It's possible that we have a posted message waiting on the MessageQueue
99 // refering to this object. Indirection via the ref-counted state_ object
100 // ensure it doesn't access us after deletion.
101
102 // TODO(nisse): An alternative approach to solve this problem would be to
103 // extend MessageQueue::Clear in some way to let us selectively cancel posts
104 // directed to this object. Then we wouldn't need any ref count, but its a
105 // larger change to the MessageQueue.
106 state_->resolver = nullptr;
107 }
90 108
91 void AsyncResolver::Start(const SocketAddress& addr) { 109 void AsyncResolver::Start(const SocketAddress& addr) {
110 RTC_DCHECK_RUN_ON(construction_thread_);
111 RTC_DCHECK(!resolver_queue_);
112 RTC_DCHECK(!state_);
113 // TODO(nisse): Support injection of task queue at construction?
114 resolver_queue_ = rtc::MakeUnique<TaskQueue>("AsyncResolverQueue");
92 addr_ = addr; 115 addr_ = addr;
93 // SignalThred Start will kickoff the resolve process. 116 state_ = new RefCountedObject<Trampoline>(this);
94 SignalThread::Start(); 117
118 // These member variables need to be copied to local variables to make it
119 // possible to capture them, even for capture-by-copy.
120 scoped_refptr<Trampoline> state = state_;
121 rtc::Thread* construction_thread = construction_thread_;
122 resolver_queue_->PostTask([state, addr, construction_thread]() {
123 std::vector<IPAddress> addresses;
124 int error =
125 ResolveHostname(addr.hostname().c_str(), addr.family(), &addresses);
126 // Ensure SignalDone is called on the main thread.
127 // TODO(nisse): Should use move of the address list, but not easy until
128 // C++17. Since this code isn't performance critical, copy should be fine
129 // for now.
130 construction_thread->Post(RTC_FROM_HERE, [state, error, addresses]() {
131 if (!state->resolver)
132 return;
133 state->resolver->ResolveDone(error, std::move(addresses));
134 });
135 });
95 } 136 }
96 137
97 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { 138 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
98 if (error_ != 0 || addresses_.empty()) 139 if (error_ != 0 || addresses_.empty())
99 return false; 140 return false;
100 141
101 *addr = addr_; 142 *addr = addr_;
102 for (size_t i = 0; i < addresses_.size(); ++i) { 143 for (size_t i = 0; i < addresses_.size(); ++i) {
103 if (family == addresses_[i].family()) { 144 if (family == addresses_[i].family()) {
104 addr->SetResolvedIP(addresses_[i]); 145 addr->SetResolvedIP(addresses_[i]);
105 return true; 146 return true;
106 } 147 }
107 } 148 }
108 return false; 149 return false;
109 } 150 }
110 151
111 int AsyncResolver::GetError() const { 152 int AsyncResolver::GetError() const {
112 return error_; 153 return error_;
113 } 154 }
114 155
115 void AsyncResolver::Destroy(bool wait) { 156 void AsyncResolver::Destroy(bool wait) {
116 SignalThread::Destroy(wait); 157 RTC_DCHECK_RUN_ON(construction_thread_);
158 RTC_DCHECK(!state_ || state_->resolver);
159 // If we don't wait here, we will nevertheless wait in the destructor.
160 if (wait || !state_) {
161 // Destroy task queue, blocks on any currently running task. If we have a
162 // pending task, it will post a call to attempt to call ResolveDone before
163 // finishing, which we will never handle.
164 delete this;
165 } else {
166 destroyed_ = true;
167 }
117 } 168 }
118 169
119 void AsyncResolver::DoWork() { 170 void AsyncResolver::ResolveDone(int error, std::vector<IPAddress> addresses) {
120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), 171 RTC_DCHECK_RUN_ON(construction_thread_);
121 &addresses_); 172 error_ = error;
122 } 173 addresses_ = std::move(addresses);
174 if (destroyed_) {
175 delete this;
176 return;
177 } else {
178 // Beware that SignalDone may call Destroy.
123 179
124 void AsyncResolver::OnWorkDone() { 180 // TODO(nisse): Currently allows only Destroy(false) in this case,
125 SignalDone(this); 181 // and that's what all webrtc code is using. With Destroy(true),
182 // this object would be destructed immediately, and the access
183 // both to |destroyed_| below as well as the sigslot machinery
184 // involved in SignalDone implies invalid use-after-free.
185 SignalDone(this);
186 if (destroyed_) {
187 delete this;
188 return;
189 }
190 }
191 state_ = nullptr;
126 } 192 }
127 193
128 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { 194 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) {
129 #if defined(WEBRTC_WIN) 195 #if defined(WEBRTC_WIN)
130 return win32_inet_ntop(af, src, dst, size); 196 return win32_inet_ntop(af, src, dst, size);
131 #else 197 #else
132 return ::inet_ntop(af, src, dst, size); 198 return ::inet_ntop(af, src, dst, size);
133 #endif 199 #endif
134 } 200 }
135 201
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 break; 276 break;
211 } 277 }
212 } 278 }
213 freeifaddrs(ifa); 279 freeifaddrs(ifa);
214 return has_ipv6; 280 return has_ipv6;
215 #else 281 #else
216 return true; 282 return true;
217 #endif 283 #endif
218 } 284 }
219 } // namespace rtc 285 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/rtc_base/nethelpers.h ('k') | webrtc/rtc_base/signalthread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698