| 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> |
| 22 | 23 |
| 23 #include "webrtc/base/asyncresolverinterface.h" | 24 #include "webrtc/base/asyncresolverinterface.h" |
| 24 #include "webrtc/base/signalthread.h" | 25 #include "webrtc/base/refcount.h" |
| 26 #include "webrtc/base/scoped_ref_ptr.h" |
| 25 #include "webrtc/base/sigslot.h" | 27 #include "webrtc/base/sigslot.h" |
| 26 #include "webrtc/base/socketaddress.h" | 28 #include "webrtc/base/socketaddress.h" |
| 29 #include "webrtc/base/thread_checker.h" |
| 27 | 30 |
| 28 namespace rtc { | 31 namespace rtc { |
| 29 | 32 |
| 30 class AsyncResolverTest; | 33 class Thread; |
| 34 class TaskQueue; |
| 31 | 35 |
| 32 // AsyncResolver will perform async DNS resolution, signaling the result on | 36 // AsyncResolver will perform async DNS resolution, signaling the result on the |
| 33 // the SignalDone from AsyncResolverInterface when the operation completes. | 37 // SignalDone from AsyncResolverInterface when the operation completes. |
| 34 class AsyncResolver : public SignalThread, public AsyncResolverInterface { | 38 // SignalDone is fired on the same thread on which the AsyncResolver was |
| 39 // constructed. |
| 40 class AsyncResolver : public AsyncResolverInterface { |
| 35 public: | 41 public: |
| 36 AsyncResolver(); | 42 AsyncResolver(); |
| 37 ~AsyncResolver() override; | 43 ~AsyncResolver() override; |
| 38 | 44 |
| 39 void Start(const SocketAddress& addr) override; | 45 void Start(const SocketAddress& addr) override; |
| 40 bool GetResolvedAddress(int family, SocketAddress* addr) const override; | 46 bool GetResolvedAddress(int family, SocketAddress* addr) const override; |
| 41 int GetError() const override; | 47 int GetError() const override; |
| 42 void Destroy(bool wait) override; | 48 void Destroy(bool wait) override; |
| 43 | 49 |
| 44 const std::vector<IPAddress>& addresses() const { return addresses_; } | 50 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; | |
| 50 | 51 |
| 51 private: | 52 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_; |
| 52 SocketAddress addr_; | 76 SocketAddress addr_; |
| 53 std::vector<IPAddress> addresses_; | 77 std::vector<IPAddress> addresses_; |
| 54 int error_; | 78 int error_ = -1; |
| 55 }; | 79 }; |
| 56 | 80 |
| 57 // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid | 81 // rtc namespaced wrappers for inet_ntop and inet_pton so we can avoid |
| 58 // the windows-native versions of these. | 82 // the windows-native versions of these. |
| 59 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size); | 83 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size); |
| 60 int inet_pton(int af, const char* src, void *dst); | 84 int inet_pton(int af, const char* src, void *dst); |
| 61 | 85 |
| 62 bool HasIPv4Enabled(); | 86 bool HasIPv4Enabled(); |
| 63 bool HasIPv6Enabled(); | 87 bool HasIPv6Enabled(); |
| 64 } // namespace rtc | 88 } // namespace rtc |
| 65 | 89 |
| 66 #endif // WEBRTC_RTC_BASE_NETHELPERS_H_ | 90 #endif // WEBRTC_RTC_BASE_NETHELPERS_H_ |
| OLD | NEW |