Bumping manifests a=b2g-bump
[gecko.git] / media / mtransport / nriceresolverfake.cpp
blobead5787d468275f651f2e08c73834148c08c5fe0
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/. */
8 // Original author: ekr@rtfm.com
10 // Some of this code is cut-and-pasted from nICEr. Copyright is:
13 Copyright (c) 2007, Adobe Systems, Incorporated
14 All rights reserved.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are
18 met:
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
23 * Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
27 * Neither the name of Adobe Systems, Network Resonance nor the names of its
28 contributors may be used to endorse or promote products derived from
29 this software without specific prior written permission.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include "nspr.h"
45 #include "prnetdb.h"
47 #include "mozilla/Assertions.h"
49 extern "C" {
50 #include "nr_api.h"
51 #include "async_timer.h"
52 #include "nr_resolver.h"
53 #include "transport_addr.h"
56 #include "nriceresolverfake.h"
57 #include "nr_socket_prsock.h"
59 namespace mozilla {
61 NrIceResolverFake::NrIceResolverFake() :
62 vtbl_(new nr_resolver_vtbl), addrs_(), delay_ms_(100),
63 allocated_resolvers_(0) {
64 vtbl_->destroy = &NrIceResolverFake::destroy;
65 vtbl_->resolve = &NrIceResolverFake::resolve;
66 vtbl_->cancel = &NrIceResolverFake::cancel;
69 NrIceResolverFake::~NrIceResolverFake() {
70 MOZ_ASSERT(allocated_resolvers_ == 0);
71 delete vtbl_;
75 nr_resolver *NrIceResolverFake::AllocateResolver() {
76 nr_resolver *resolver;
78 int r = nr_resolver_create_int((void *)this,
79 vtbl_, &resolver);
80 MOZ_ASSERT(!r);
81 if(r)
82 return nullptr;
84 ++allocated_resolvers_;
86 return resolver;
89 void NrIceResolverFake::DestroyResolver() {
90 --allocated_resolvers_;
93 int NrIceResolverFake::destroy(void **objp) {
94 if (!objp || !*objp)
95 return 0;
97 NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(*objp);
98 *objp = 0;
100 fake->DestroyResolver();
102 return 0;
105 int NrIceResolverFake::resolve(void *obj,
106 nr_resolver_resource *resource,
107 int (*cb)(void *cb_arg,
108 nr_transport_addr *addr),
109 void *cb_arg,
110 void **handle) {
111 int r,_status;
113 MOZ_ASSERT(obj);
114 NrIceResolverFake *fake = static_cast<NrIceResolverFake *>(obj);
116 MOZ_ASSERT(fake->allocated_resolvers_ > 0);
118 PendingResolution *pending =
119 new PendingResolution(fake,
120 resource->domain_name,
121 resource->port ? resource->port : 3478,
122 resource->transport_protocol ?
123 resource->transport_protocol :
124 IPPROTO_UDP,
125 cb, cb_arg);
127 if ((r=NR_ASYNC_TIMER_SET(fake->delay_ms_,NrIceResolverFake::resolve_cb,
128 (void *)pending, &pending->timer_handle_))) {
129 delete pending;
130 ABORT(r);
132 *handle = pending;
134 _status=0;
135 abort:
136 return(_status);
139 void NrIceResolverFake::resolve_cb(NR_SOCKET s, int how, void *cb_arg) {
140 MOZ_ASSERT(cb_arg);
141 PendingResolution *pending = static_cast<PendingResolution *>(cb_arg);
143 const PRNetAddr *addr=pending->resolver_->Resolve(pending->hostname_);
145 if (addr) {
146 nr_transport_addr transport_addr;
148 int r = nr_praddr_to_transport_addr(addr, &transport_addr,
149 pending->transport_, 0);
150 MOZ_ASSERT(!r);
151 if (r)
152 goto abort;
154 r=nr_transport_addr_set_port(&transport_addr, pending->port_);
155 MOZ_ASSERT(!r);
156 if (r)
157 goto abort;
159 /* Fill in the address string */
160 r=nr_transport_addr_fmt_addr_string(&transport_addr);
161 MOZ_ASSERT(!r);
162 if (r)
163 goto abort;
165 pending->cb_(pending->cb_arg_, &transport_addr);
166 delete pending;
167 return;
170 abort:
171 // Resolution failed.
172 pending->cb_(pending->cb_arg_, nullptr);
174 delete pending;
177 int NrIceResolverFake::cancel(void *obj, void *handle) {
178 MOZ_ASSERT(obj);
179 MOZ_ASSERT(static_cast<NrIceResolverFake *>(obj)->allocated_resolvers_ > 0);
181 PendingResolution *pending = static_cast<PendingResolution *>(handle);
183 NR_async_timer_cancel(pending->timer_handle_);
184 delete pending;
186 return(0);
190 } // End of namespace mozilla