View on GitHub

Asio Service Discovery

Components for service discovery via udp multicasting. It's using boost::asio for async networking. It's non-blocking and non-locking.

Download this project as a .zip file Download this project as a tar.gz file
1 //
2 // service_discoverer.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2015 Benjamin Schulz (beschulz at betabugs dot de)
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 // Created by Benjamin Schulz on 14/03/15.
11 //
12 
13 #ifndef _ASIO_SERVICE_DISCOVERY_STD_CHRONO_TIME_TRAITS_HPP_
14 #define _ASIO_SERVICE_DISCOVERY_STD_CHRONO_TIME_TRAITS_HPP_
15 
16 #pragma once
17 
18 #include <chrono>
19 
20 namespace betabugs {
21 namespace networking {
22 namespace detail {
23 
35 template<typename Clock>
37 {
38  typedef typename Clock::time_point time_type;
39  typedef typename Clock::duration duration_type;
40 
41  static time_type now()
42  {
43  return Clock::now();
44  }
45 
46  static time_type add(time_type t, duration_type d)
47  {
48  return t + d;
49  }
50 
51  static duration_type subtract(time_type t1, time_type t2)
52  {
53  return t1 - t2;
54  }
55 
56  static bool less_than(time_type t1, time_type t2)
57  {
58  return t1 < t2;
59  }
60 
61  static boost::posix_time::time_duration
62  to_posix_duration(duration_type d1)
63  {
64  using std::chrono::duration_cast;
65  auto in_sec = duration_cast<std::chrono::seconds>(d1);
66  auto in_usec = duration_cast<std::chrono::microseconds>(d1 - in_sec);
67  boost::posix_time::time_duration result =
68  boost::posix_time::seconds(in_sec.count()) +
69  boost::posix_time::microseconds(in_usec.count());
70  return result;
71  }
72 };
73 
74 }
75 }
76 }
77 
78 #endif //_ASIO_SERVICE_DISCOVERY_STD_CHRONO_TIME_TRAITS_HPP_
Definition: std_chrono_time_traits.hpp:36