You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. {
  63. namespace lib
  64. {
  65. #ifdef ASIO_STANDALONE
  66. namespace asio
  67. {
  68. using namespace ::asio;
  69. // Here we assume that we will be using std::error_code with standalone
  70. // Asio. This is probably a good assumption, but it is possible in rare
  71. // cases that local Asio versions would be used.
  72. using std::errc;
  73. // See note above about boost <1.49 compatibility. Because we require
  74. // a standalone Asio version of 1.8+ we are guaranteed to have
  75. // steady_timer available. By convention we require the chrono library
  76. // (either boost or std) for use with standalone Asio.
  77. template <typename T> bool is_neg(T duration)
  78. {
  79. return duration.count() < 0;
  80. }
  81. inline lib::chrono::milliseconds milliseconds(long duration)
  82. {
  83. return lib::chrono::milliseconds(duration);
  84. }
  85. } // namespace asio
  86. #else
  87. namespace asio
  88. {
  89. using namespace boost::asio;
  90. // See note above about boost <1.49 compatibility
  91. #if (BOOST_VERSION / 100000) == 1 && ((BOOST_VERSION / 100) % 1000) > 48
  92. // Using boost::asio >=1.49 so we use chrono and steady_timer
  93. template <typename T> bool is_neg(T duration)
  94. {
  95. return duration.count() < 0;
  96. }
  97. // If boost believes it has std::chrono available it will use it
  98. // so we should also use it for things that relate to boost, even
  99. // if the library would otherwise use boost::chrono.
  100. #if defined(BOOST_ASIO_HAS_STD_CHRONO)
  101. inline std::chrono::milliseconds milliseconds(long duration)
  102. {
  103. return std::chrono::milliseconds(duration);
  104. }
  105. #else
  106. inline lib::chrono::milliseconds milliseconds(long duration)
  107. {
  108. return lib::chrono::milliseconds(duration);
  109. }
  110. #endif
  111. #else
  112. // Using boost::asio <1.49 we pretend a deadline timer is a steady
  113. // timer and wrap the negative detection and duration conversion
  114. // appropriately.
  115. typedef boost::asio::deadline_timer steady_timer;
  116. template <typename T> bool is_neg(T duration)
  117. {
  118. return duration.is_negative();
  119. }
  120. inline boost::posix_time::time_duration milliseconds(long duration)
  121. {
  122. return boost::posix_time::milliseconds(duration);
  123. }
  124. #endif
  125. using boost::system::error_code;
  126. namespace errc = boost::system::errc;
  127. } // namespace asio
  128. #endif
  129. } // namespace lib
  130. } // namespace websocketpp
  131. #endif // WEBSOCKETPP_COMMON_ASIO_HPP