2 * Copyright (c) 2015, Peter Thorson. All rights reserved.
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.
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.
28 #ifndef WEBSOCKETPP_COMMON_ASIO_HPP
29 #define WEBSOCKETPP_COMMON_ASIO_HPP
31 // This file goes to some length to preserve compatibility with versions of
32 // boost older than 1.49 (where the first modern steady_timer timer based on
33 // boost/std chrono was introduced.
35 // For the versions older than 1.49, the deadline_timer is used instead. this
36 // brings in dependencies on boost date_time and it has a different interface
37 // that is normalized by the `lib::asio::is_neg` and `lib::asio::milliseconds`
38 // wrappers provided by this file.
40 // The primary reason for this continued support is that boost 1.48 is the
41 // default and not easily changeable version of boost supplied by the package
42 // manager of popular Linux distributions like Ubuntu 12.04 LTS. Once the need
43 // for this has passed this should be cleaned up and simplified.
45 #ifdef ASIO_STANDALONE
46 #include <asio/version.hpp>
48 #if (ASIO_VERSION/100000) == 1 && ((ASIO_VERSION/100)%1000) < 8
49 static_assert(false, "The minimum version of standalone Asio is 1.8.0");
53 #include <asio/steady_timer.hpp>
54 #include <websocketpp/common/chrono.hpp>
56 #include <boost/version.hpp>
58 // See note above about boost <1.49 compatibility. If we are running on
59 // boost > 1.48 pull in the steady timer and chrono library
60 #if (BOOST_VERSION/100000) == 1 && ((BOOST_VERSION/100)%1000) > 48
61 #include <boost/asio/steady_timer.hpp>
62 #include <websocketpp/common/chrono.hpp>
65 #include <boost/asio.hpp>
66 #include <boost/system/error_code.hpp>
69 namespace websocketpp {
72 #ifdef ASIO_STANDALONE
74 using namespace ::asio;
75 // Here we assume that we will be using std::error_code with standalone
76 // Asio. This is probably a good assumption, but it is possible in rare
77 // cases that local Asio versions would be used.
80 // See note above about boost <1.49 compatibility. Because we require
81 // a standalone Asio version of 1.8+ we are guaranteed to have
82 // steady_timer available. By convention we require the chrono library
83 // (either boost or std) for use with standalone Asio.
85 bool is_neg(T duration) {
86 return duration.count() < 0;
88 inline lib::chrono::milliseconds milliseconds(long duration) {
89 return lib::chrono::milliseconds(duration);
95 using namespace boost::asio;
97 // See note above about boost <1.49 compatibility
98 #if (BOOST_VERSION/100000) == 1 && ((BOOST_VERSION/100)%1000) > 48
99 // Using boost::asio >=1.49 so we use chrono and steady_timer
100 template <typename T>
101 bool is_neg(T duration) {
102 return duration.count() < 0;
104 inline lib::chrono::milliseconds milliseconds(long duration) {
105 return lib::chrono::milliseconds(duration);
108 // Using boost::asio <1.49 we pretend a deadline timer is a steady
109 // timer and wrap the negative detection and duration conversion
111 typedef boost::asio::deadline_timer steady_timer;
113 template <typename T>
114 bool is_neg(T duration) {
115 return duration.is_negative();
117 inline boost::posix_time::time_duration milliseconds(long duration) {
118 return boost::posix_time::milliseconds(duration);
122 using boost::system::error_code;
123 namespace errc = boost::system::errc;
129 } // namespace websocketpp
131 #endif // WEBSOCKETPP_COMMON_ASIO_HPP