media/mtransport/nriceresolverfake.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/nriceresolverfake.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +
    1.11 +// Original author: ekr@rtfm.com
    1.12 +
    1.13 +// Some of this code is cut-and-pasted from nICEr. Copyright is:
    1.14 +
    1.15 +/*
    1.16 +Copyright (c) 2007, Adobe Systems, Incorporated
    1.17 +All rights reserved.
    1.18 +
    1.19 +Redistribution and use in source and binary forms, with or without
    1.20 +modification, are permitted provided that the following conditions are
    1.21 +met:
    1.22 +
    1.23 +* Redistributions of source code must retain the above copyright
    1.24 +  notice, this list of conditions and the following disclaimer.
    1.25 +
    1.26 +* Redistributions in binary form must reproduce the above copyright
    1.27 +  notice, this list of conditions and the following disclaimer in the
    1.28 +  documentation and/or other materials provided with the distribution.
    1.29 +
    1.30 +* Neither the name of Adobe Systems, Network Resonance nor the names of its
    1.31 +  contributors may be used to endorse or promote products derived from
    1.32 +  this software without specific prior written permission.
    1.33 +
    1.34 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.35 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.36 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.37 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.38 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.39 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.40 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.41 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.42 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.43 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.44 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.45 +*/
    1.46 +
    1.47 +#ifndef nriceresolverfake_h__
    1.48 +#define nriceresolverfake_h__
    1.49 +
    1.50 +#include <map>
    1.51 +#include <string>
    1.52 +
    1.53 +#include "nspr.h"
    1.54 +#include "prnetdb.h"
    1.55 +#include "mozilla/NullPtr.h"
    1.56 +
    1.57 +typedef struct nr_resolver_ nr_resolver;
    1.58 +typedef struct nr_resolver_vtbl_ nr_resolver_vtbl;
    1.59 +typedef struct nr_transport_addr_ nr_transport_addr;
    1.60 +typedef struct nr_resolver_resource_ nr_resolver_resource;
    1.61 +
    1.62 +namespace mozilla {
    1.63 +
    1.64 +class NrIceResolverFake {
    1.65 + public:
    1.66 +  NrIceResolverFake();
    1.67 +  ~NrIceResolverFake();
    1.68 +
    1.69 +  void SetAddr(const std::string& hostname, const PRNetAddr& addr) {
    1.70 +    addrs_[hostname] = addr;
    1.71 +  }
    1.72 +
    1.73 +  nr_resolver *AllocateResolver();
    1.74 +
    1.75 +  void DestroyResolver();
    1.76 +
    1.77 +private:
    1.78 +  // Implementations of vtbl functions
    1.79 +  static int destroy(void **objp);
    1.80 +  static int resolve(void *obj,
    1.81 +                     nr_resolver_resource *resource,
    1.82 +                     int (*cb)(void *cb_arg,
    1.83 +                               nr_transport_addr *addr),
    1.84 +                     void *cb_arg,
    1.85 +                     void **handle);
    1.86 +  static void resolve_cb(NR_SOCKET s, int how, void *cb_arg);
    1.87 +  static int cancel(void *obj, void *handle);
    1.88 +
    1.89 +  // Get an address.
    1.90 +  const PRNetAddr *Resolve(const std::string& hostname) {
    1.91 +    if (!addrs_.count(hostname))
    1.92 +      return nullptr;
    1.93 +
    1.94 +    return &addrs_[hostname];
    1.95 +  }
    1.96 +
    1.97 +
    1.98 +  struct PendingResolution {
    1.99 +    PendingResolution(NrIceResolverFake *resolver,
   1.100 +                      const std::string& hostname,
   1.101 +                      uint16_t port,
   1.102 +                      int transport,
   1.103 +                      int (*cb)(void *cb_arg, nr_transport_addr *addr),
   1.104 +                      void *cb_arg) :
   1.105 +        resolver_(resolver),
   1.106 +        hostname_(hostname),
   1.107 +        port_(port),
   1.108 +        transport_(transport),
   1.109 +        cb_(cb), cb_arg_(cb_arg) {}
   1.110 +
   1.111 +    NrIceResolverFake *resolver_;
   1.112 +    std::string hostname_;
   1.113 +    uint16_t port_;
   1.114 +    int transport_;
   1.115 +    int (*cb_)(void *cb_arg, nr_transport_addr *addr);
   1.116 +    void *cb_arg_;
   1.117 +    void *timer_handle_;
   1.118 +  };
   1.119 +
   1.120 +  nr_resolver_vtbl* vtbl_;
   1.121 +  std::map<std::string, PRNetAddr> addrs_;
   1.122 +  uint32_t delay_ms_;
   1.123 +  int allocated_resolvers_;
   1.124 +};
   1.125 +
   1.126 +}  // End of namespace mozilla
   1.127 +
   1.128 +#endif

mercurial