WebSocket++  0.8.0-dev
C++ websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
asio.hpp
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 
28 #ifndef WEBSOCKETPP_COMMON_ASIO_HPP
29 #define WEBSOCKETPP_COMMON_ASIO_HPP
30 
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.
34 //
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.
39 //
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.
44 
45 #ifdef ASIO_STANDALONE
46  #include <asio/version.hpp>
47 
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");
50  #endif
51 
52  #include <asio.hpp>
53  #include <asio/steady_timer.hpp>
54  #include <websocketpp/common/chrono.hpp>
55 #else
56  #include <boost/version.hpp>
57 
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>
63  #endif
64 
65  #include <boost/asio.hpp>
66  #include <boost/system/error_code.hpp>
67 #endif
68 
69 namespace websocketpp {
70 namespace lib {
71 
72 #ifdef ASIO_STANDALONE
73  namespace asio {
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.
78  using std::errc;
79 
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.
84  template <typename T>
85  bool is_neg(T duration) {
86  return duration.count() < 0;
87  }
88  inline lib::chrono::milliseconds milliseconds(long duration) {
89  return lib::chrono::milliseconds(duration);
90  }
91  } // namespace asio
92 
93 #else
94  namespace asio {
95  using namespace boost::asio;
96 
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;
103  }
104 
105  // If boost believes it has std::chrono available it will use it
106  // so we should also use it for things that relate to boost, even
107  // if the library would otherwise use boost::chrono.
108  #if defined(BOOST_ASIO_HAS_STD_CHRONO)
109  inline std::chrono::milliseconds milliseconds(long duration) {
110  return std::chrono::milliseconds(duration);
111  }
112  #else
113  inline lib::chrono::milliseconds milliseconds(long duration) {
114  return lib::chrono::milliseconds(duration);
115  }
116  #endif
117  #else
118  // Using boost::asio <1.49 we pretend a deadline timer is a steady
119  // timer and wrap the negative detection and duration conversion
120  // appropriately.
121  typedef boost::asio::deadline_timer steady_timer;
122 
123  template <typename T>
124  bool is_neg(T duration) {
125  return duration.is_negative();
126  }
127  inline boost::posix_time::time_duration milliseconds(long duration) {
128  return boost::posix_time::milliseconds(duration);
129  }
130  #endif
131 
132  using boost::system::error_code;
133  namespace errc = boost::system::errc;
134  } // namespace asio
135 #endif
136 
137 
138 } // namespace lib
139 } // namespace websocketpp
140 
141 #endif // WEBSOCKETPP_COMMON_ASIO_HPP
void handle_accept(connection_ptr con, lib::error_code const &ec)
Handler callback for start_accept.