Home | History | Annotate | Download | only in ip
      1 //
      2 // ip/resolver_service.hpp
      3 // ~~~~~~~~~~~~~~~~~~~~~~~
      4 //
      5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
      6 //
      7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
      8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      9 //
     10 
     11 #ifndef ASIO_IP_RESOLVER_SERVICE_HPP
     12 #define ASIO_IP_RESOLVER_SERVICE_HPP
     13 
     14 
     15 #include "asio/detail/config.hpp"
     16 #include "asio/async_result.hpp"
     17 #include "asio/error_code.hpp"
     18 #include "asio/io_service.hpp"
     19 #include "asio/ip/basic_resolver_iterator.hpp"
     20 #include "asio/ip/basic_resolver_query.hpp"
     21 
     22 # include "asio/detail/resolver_service.hpp"
     23 
     24 #include "asio/detail/push_options.hpp"
     25 
     26 namespace asio {
     27 namespace ip {
     28 
     29 /// Default service implementation for a resolver.
     30 template <typename InternetProtocol>
     31 class resolver_service
     32   : public asio::detail::service_base<
     33       resolver_service<InternetProtocol> >
     34 {
     35 public:
     36 
     37   /// The protocol type.
     38   typedef InternetProtocol protocol_type;
     39 
     40   /// The endpoint type.
     41   typedef typename InternetProtocol::endpoint endpoint_type;
     42 
     43   /// The query type.
     44   typedef basic_resolver_query<InternetProtocol> query_type;
     45 
     46   /// The iterator type.
     47   typedef basic_resolver_iterator<InternetProtocol> iterator_type;
     48 
     49 private:
     50   // The type of the platform-specific implementation.
     51   typedef asio::detail::resolver_service<InternetProtocol>
     52     service_impl_type;
     53 
     54 public:
     55   /// The type of a resolver implementation.
     56   typedef typename service_impl_type::implementation_type implementation_type;
     57 
     58   /// Construct a new resolver service for the specified io_service.
     59   explicit resolver_service(asio::io_service& io_service)
     60     : asio::detail::service_base<
     61         resolver_service<InternetProtocol> >(io_service),
     62       service_impl_(io_service)
     63   {
     64   }
     65 
     66   /// Construct a new resolver implementation.
     67   void construct(implementation_type& impl)
     68   {
     69     service_impl_.construct(impl);
     70   }
     71 
     72   /// Destroy a resolver implementation.
     73   void destroy(implementation_type& impl)
     74   {
     75     service_impl_.destroy(impl);
     76   }
     77 
     78   /// Cancel pending asynchronous operations.
     79   void cancel(implementation_type& impl)
     80   {
     81     service_impl_.cancel(impl);
     82   }
     83 
     84   /// Resolve a query to a list of entries.
     85   iterator_type resolve(implementation_type& impl, const query_type& query,
     86       asio::error_code& ec)
     87   {
     88     return service_impl_.resolve(impl, query, ec);
     89   }
     90 
     91   /// Asynchronously resolve a query to a list of entries.
     92   template <typename ResolveHandler>
     93   ASIO_INITFN_RESULT_TYPE(ResolveHandler,
     94       void (asio::error_code, iterator_type))
     95   async_resolve(implementation_type& impl, const query_type& query,
     96       ASIO_MOVE_ARG(ResolveHandler) handler)
     97   {
     98     asio::detail::async_result_init<
     99       ResolveHandler, void (asio::error_code, iterator_type)> init(
    100         ASIO_MOVE_CAST(ResolveHandler)(handler));
    101 
    102     service_impl_.async_resolve(impl, query, init.handler);
    103 
    104     return init.result.get();
    105   }
    106 
    107   /// Resolve an endpoint to a list of entries.
    108   iterator_type resolve(implementation_type& impl,
    109       const endpoint_type& endpoint, asio::error_code& ec)
    110   {
    111     return service_impl_.resolve(impl, endpoint, ec);
    112   }
    113 
    114   /// Asynchronously resolve an endpoint to a list of entries.
    115   template <typename ResolveHandler>
    116   ASIO_INITFN_RESULT_TYPE(ResolveHandler,
    117       void (asio::error_code, iterator_type))
    118   async_resolve(implementation_type& impl, const endpoint_type& endpoint,
    119       ASIO_MOVE_ARG(ResolveHandler) handler)
    120   {
    121     asio::detail::async_result_init<
    122       ResolveHandler, void (asio::error_code, iterator_type)> init(
    123         ASIO_MOVE_CAST(ResolveHandler)(handler));
    124 
    125     service_impl_.async_resolve(impl, endpoint, init.handler);
    126 
    127     return init.result.get();
    128   }
    129 
    130 private:
    131   // Destroy all user-defined handler objects owned by the service.
    132   void shutdown_service()
    133   {
    134     service_impl_.shutdown_service();
    135   }
    136 
    137   // Perform any fork-related housekeeping.
    138   void fork_service(asio::io_service::fork_event event)
    139   {
    140     service_impl_.fork_service(event);
    141   }
    142 
    143   // The platform-specific implementation.
    144   service_impl_type service_impl_;
    145 };
    146 
    147 } // namespace ip
    148 } // namespace asio
    149 
    150 #include "asio/detail/pop_options.hpp"
    151 
    152 #endif // ASIO_IP_RESOLVER_SERVICE_HPP
    153