nriceresolver.h (4108B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // Original authors: jib@mozilla.com, ekr@rtfm.com 8 9 // Some of this code is cut-and-pasted from nICEr. Copyright is: 10 11 /* 12 Copyright (c) 2007, Adobe Systems, Incorporated 13 All rights reserved. 14 15 Redistribution and use in source and binary forms, with or without 16 modification, are permitted provided that the following conditions are 17 met: 18 19 * Redistributions of source code must retain the above copyright 20 notice, this list of conditions and the following disclaimer. 21 22 * Redistributions in binary form must reproduce the above copyright 23 notice, this list of conditions and the following disclaimer in the 24 documentation and/or other materials provided with the distribution. 25 26 * Neither the name of Adobe Systems, Network Resonance nor the names of its 27 contributors may be used to endorse or promote products derived from 28 this software without specific prior written permission. 29 30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 */ 42 43 #ifndef nriceresolver_h__ 44 #define nriceresolver_h__ 45 46 #include "nricectx.h" 47 #include "nsICancelable.h" 48 #include "nsIDNSListener.h" 49 #include "nsIDNSService.h" 50 51 typedef struct nr_resolver_ nr_resolver; 52 typedef struct nr_resolver_vtbl_ nr_resolver_vtbl; 53 typedef struct nr_transport_addr_ nr_transport_addr; 54 typedef struct nr_resolver_resource_ nr_resolver_resource; 55 56 namespace mozilla { 57 58 class NrIceResolver { 59 private: 60 ~NrIceResolver(); 61 62 public: 63 NrIceResolver(); 64 65 nsresult Init(); 66 nr_resolver* AllocateResolver(); 67 void DestroyResolver(); 68 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrIceResolver) 69 70 int resolve(nr_resolver_resource* resource, 71 int (*cb)(void* cb_arg, nr_transport_addr* addr), void* cb_arg, 72 void** handle); 73 74 private: 75 // Implementations of vtbl functions 76 static int destroy(void** objp); 77 static int resolve(void* obj, nr_resolver_resource* resource, 78 int (*cb)(void* cb_arg, nr_transport_addr* addr), 79 void* cb_arg, void** handle); 80 static void resolve_cb(NR_SOCKET s, int how, void* cb_arg); 81 static int cancel(void* obj, void* handle); 82 83 class PendingResolution : public nsIDNSListener { 84 public: 85 PendingResolution(nsIEventTarget* thread, uint16_t port, int transport, 86 int (*cb)(void* cb_arg, nr_transport_addr* addr), 87 void* cb_arg) 88 : thread_(thread), 89 port_(port), 90 transport_(transport), 91 cb_(cb), 92 cb_arg_(cb_arg) {} 93 NS_IMETHOD OnLookupComplete(nsICancelable* request, nsIDNSRecord* record, 94 nsresult status) override; 95 96 int cancel(); 97 nsCOMPtr<nsICancelable> request_; 98 NS_DECL_THREADSAFE_ISUPPORTS 99 100 private: 101 virtual ~PendingResolution() = default; 102 103 nsCOMPtr<nsIEventTarget> thread_; 104 uint16_t port_; 105 int transport_; 106 int (*cb_)(void* cb_arg, nr_transport_addr* addr); 107 void* cb_arg_; 108 }; 109 110 nr_resolver_vtbl* vtbl_; 111 nsCOMPtr<nsIEventTarget> sts_thread_; 112 nsCOMPtr<nsIDNSService> dns_; 113 #ifdef DEBUG 114 int allocated_resolvers_; 115 #endif 116 }; 117 118 } // End of namespace mozilla 119 #endif