asio.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2015, Peter Thorson. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the WebSocket++ Project nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. #ifndef WEBSOCKETPP_COMMON_ASIO_HPP
  28. #define WEBSOCKETPP_COMMON_ASIO_HPP
  29. // This file goes to some length to preserve compatibility with versions of
  30. // boost older than 1.49 (where the first modern steady_timer timer based on
  31. // boost/std chrono was introduced.
  32. //
  33. // For the versions older than 1.49, the deadline_timer is used instead. this
  34. // brings in dependencies on boost date_time and it has a different interface
  35. // that is normalized by the `lib::asio::is_neg` and `lib::asio::milliseconds`
  36. // wrappers provided by this file.
  37. //
  38. // The primary reason for this continued support is that boost 1.48 is the
  39. // default and not easily changeable version of boost supplied by the package
  40. // manager of popular Linux distributions like Ubuntu 12.04 LTS. Once the need
  41. // for this has passed this should be cleaned up and simplified.
  42. #ifdef ASIO_STANDALONE
  43. #include <asio/version.hpp>
  44. #if (ASIO_VERSION/100000) == 1 && ((ASIO_VERSION/100)%1000) < 8
  45. static_assert(false, "The minimum version of standalone Asio is 1.8.0");
  46. #endif
  47. #include <asio.hpp>
  48. #include <asio/steady_timer.hpp>
  49. #include <websocketpp/common/chrono.hpp>
  50. #else
  51. #include <boost/version.hpp>
  52. // See note above about boost <1.49 compatibility. If we are running on
  53. // boost > 1.48 pull in the steady timer and chrono library
  54. #if (BOOST_VERSION/100000) == 1 && ((BOOST_VERSION/100)%1000) > 48
  55. #include <boost/asio/steady_timer.hpp>
  56. #include <websocketpp/common/chrono.hpp>
  57. #endif
  58. #include <boost/asio.hpp>
  59. #include <boost/system/error_code.hpp>
  60. #endif
  61. namespace websocketpp {
  62. namespace lib {
  63. #ifdef ASIO_STANDALONE
  64. namespace asio {
  65. using namespace ::asio;
  66. // Here we assume that we will be using std::error_code with standalone
  67. // Asio. This is probably a good assumption, but it is possible in rare
  68. // cases that local Asio versions would be used.
  69. using std::errc;
  70. // See note above about boost <1.49 compatibility. Because we require
  71. // a standalone Asio version of 1.8+ we are guaranteed to have
  72. // steady_timer available. By convention we require the chrono library
  73. // (either boost or std) for use with standalone Asio.
  74. template <typename T>
  75. bool is_neg(T duration) {
  76. return duration.count() < 0;
  77. }
  78. inline lib::chrono::milliseconds milliseconds(long duration) {
  79. return lib::chrono::milliseconds(duration);
  80. }
  81. } // namespace asio
  82. #else
  83. namespace asio {
  84. using namespace boost::asio;
  85. // See note above about boost <1.49 compatibility
  86. #if (BOOST_VERSION/100000) == 1 && ((BOOST_VERSION/100)%1000) > 48
  87. // Using boost::asio >=1.49 so we use chrono and steady_timer
  88. template <typename T>
  89. bool is_neg(T duration) {
  90. return duration.count() < 0;
  91. }
  92. // If boost believes it has std::chrono available it will use it
  93. // so we should also use it for things that relate to boost, even
  94. // if the library would otherwise use boost::chrono.
  95. #if defined(BOOST_ASIO_HAS_STD_CHRONO)
  96. inline std::chrono::milliseconds milliseconds(long duration) {
  97. return std::chrono::milliseconds(duration);
  98. }
  99. #else
  100. inline lib::chrono::milliseconds milliseconds(long duration) {
  101. return lib::chrono::milliseconds(duration);
  102. }
  103. #endif
  104. #else
  105. // Using boost::asio <1.49 we pretend a deadline timer is a steady
  106. // timer and wrap the negative detection and duration conversion
  107. // appropriately.
  108. typedef boost::asio::deadline_timer steady_timer;
  109. template <typename T>
  110. bool is_neg(T duration) {
  111. return duration.is_negative();
  112. }
  113. inline boost::posix_time::time_duration milliseconds(long duration) {
  114. return boost::posix_time::milliseconds(duration);
  115. }
  116. #endif
  117. using boost::system::error_code;
  118. namespace errc = boost::system::errc;
  119. } // namespace asio
  120. #endif
  121. } // namespace lib
  122. } // namespace websocketpp
  123. #endif // WEBSOCKETPP_COMMON_ASIO_HPP