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 #ifndef WEBRTC_RTC_BASE_NETHELPERS_H_ | 11 #ifndef WEBRTC_RTC_BASE_NETHELPERS_H_ |
12 #define WEBRTC_RTC_BASE_NETHELPERS_H_ | 12 #define WEBRTC_RTC_BASE_NETHELPERS_H_ |
13 | 13 |
14 #if defined(WEBRTC_POSIX) | 14 #if defined(WEBRTC_POSIX) |
15 #include <netdb.h> | 15 #include <netdb.h> |
16 #include <stddef.h> | 16 #include <stddef.h> |
17 #elif WEBRTC_WIN | 17 #elif WEBRTC_WIN |
18 #include <winsock2.h> // NOLINT | 18 #include <winsock2.h> // NOLINT |
19 #endif | 19 #endif |
20 | 20 |
21 #include <list> | 21 #include <list> |
22 #include <memory> | |
23 | 22 |
24 #include "webrtc/rtc_base/asyncresolverinterface.h" | 23 #include "webrtc/rtc_base/asyncresolverinterface.h" |
25 #include "webrtc/rtc_base/refcount.h" | 24 #include "webrtc/rtc_base/signalthread.h" |
26 #include "webrtc/rtc_base/scoped_ref_ptr.h" | |
27 #include "webrtc/rtc_base/sigslot.h" | 25 #include "webrtc/rtc_base/sigslot.h" |
28 #include "webrtc/rtc_base/socketaddress.h" | 26 #include "webrtc/rtc_base/socketaddress.h" |
29 #include "webrtc/rtc_base/thread_checker.h" | |
30 | 27 |
31 namespace rtc { | 28 namespace rtc { |
32 | 29 |
33 class Thread; | 30 class AsyncResolverTest; |
34 class TaskQueue; | |
35 | 31 |
36 // AsyncResolver will perform async DNS resolution, signaling the result on the | 32 // AsyncResolver will perform async DNS resolution, signaling the result on |
37 // SignalDone from AsyncResolverInterface when the operation completes. | 33 // the SignalDone from AsyncResolverInterface when the operation completes. |
38 // SignalDone is fired on the same thread on which the AsyncResolver was | 34 class AsyncResolver : public SignalThread, public AsyncResolverInterface { |
39 // constructed. | |
40 class AsyncResolver : public AsyncResolverInterface { | |
41 public: | 35 public: |
42 AsyncResolver(); | 36 AsyncResolver(); |
43 ~AsyncResolver() override; | 37 ~AsyncResolver() override; |
44 | 38 |
45 void Start(const SocketAddress& addr) override; | 39 void Start(const SocketAddress& addr) override; |
46 bool GetResolvedAddress(int family, SocketAddress* addr) const override; | 40 bool GetResolvedAddress(int family, SocketAddress* addr) const override; |
47 int GetError() const override; | 41 int GetError() const override; |
48 void Destroy(bool wait) override; | 42 void Destroy(bool wait) override; |
49 | 43 |
50 const std::vector<IPAddress>& addresses() const { return addresses_; } | 44 const std::vector<IPAddress>& addresses() const { return addresses_; } |
| 45 void set_error(int error) { error_ = error; } |
| 46 |
| 47 protected: |
| 48 void DoWork() override; |
| 49 void OnWorkDone() override; |
51 | 50 |
52 private: | 51 private: |
53 void ResolveDone(int error, std::vector<IPAddress> addresses); | |
54 | |
55 class Trampoline : public RefCountInterface { | |
56 public: | |
57 Trampoline(AsyncResolver* resolver) : resolver(resolver) {} | |
58 // Points back to the resolver, as long as it is alive. Cleared | |
59 // by the AsyncResolver destructor. | |
60 AsyncResolver* resolver; | |
61 }; | |
62 | |
63 // |state_| is non-null while resolution is pending, i.e., set | |
64 // non-null by Start() and cleared by ResolveDone(). The destructor | |
65 // clears state_->resolver (assuming |state_| is non-null), to | |
66 // indicate that the resolver can no longer be accessed. | |
67 scoped_refptr<Trampoline> state_ ACCESS_ON(construction_thread_); | |
68 | |
69 Thread* const construction_thread_; | |
70 // Set to true when Destroy() can't delete the object immediately. | |
71 // Indicate that the ResolveDone method is now responsible for | |
72 // deletion. method should delete the object. | |
73 bool destroyed_ = false; | |
74 // Queue used only for a single task. | |
75 std::unique_ptr<TaskQueue> resolver_queue_; | |
76 SocketAddress addr_; | 52 SocketAddress addr_; |
77 std::vector<IPAddress> addresses_; | 53 std::vector<IPAddress> addresses_; |
78 int error_ = -1; | 54 int error_; |
79 }; | 55 }; |
80 | 56 |
81 // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid | 57 // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid |
82 // the windows-native versions of these. | 58 // the windows-native versions of these. |
83 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size); | 59 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size); |
84 int inet_pton(int af, const char* src, void *dst); | 60 int inet_pton(int af, const char* src, void *dst); |
85 | 61 |
86 bool HasIPv4Enabled(); | 62 bool HasIPv4Enabled(); |
87 bool HasIPv6Enabled(); | 63 bool HasIPv6Enabled(); |
88 } // namespace rtc | 64 } // namespace rtc |
89 | 65 |
90 #endif // WEBRTC_RTC_BASE_NETHELPERS_H_ | 66 #endif // WEBRTC_RTC_BASE_NETHELPERS_H_ |
OLD | NEW |