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.

243 lines
7.4 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_TRANSPORT_ASIO_BASE_HPP
  28. #define WEBSOCKETPP_TRANSPORT_ASIO_BASE_HPP
  29. #include <string>
  30. #include <websocketpp/common/asio.hpp>
  31. #include <websocketpp/common/cpp11.hpp>
  32. #include <websocketpp/common/functional.hpp>
  33. #include <websocketpp/common/system_error.hpp>
  34. #include <websocketpp/common/type_traits.hpp>
  35. namespace websocketpp
  36. {
  37. namespace transport
  38. {
  39. /// Transport policy that uses asio
  40. /**
  41. * This policy uses a single asio io_service to provide transport
  42. * services to a WebSocket++ endpoint.
  43. */
  44. namespace asio
  45. {
  46. // Class to manage the memory to be used for handler-based custom allocation.
  47. // It contains a single block of memory which may be returned for allocation
  48. // requests. If the memory is in use when an allocation request is made, the
  49. // allocator delegates allocation to the global heap.
  50. class handler_allocator
  51. {
  52. public:
  53. static const size_t size = 1024;
  54. handler_allocator() :
  55. m_in_use(false)
  56. {
  57. }
  58. #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  59. handler_allocator(handler_allocator const & cpy) = delete;
  60. handler_allocator & operator=(const handler_allocator &) = delete;
  61. #endif
  62. void * allocate(std::size_t memsize)
  63. {
  64. if (! m_in_use && memsize < size)
  65. {
  66. m_in_use = true;
  67. return static_cast<void *>(&m_storage);
  68. }
  69. else
  70. {
  71. return ::operator new(memsize);
  72. }
  73. }
  74. void deallocate(void * pointer)
  75. {
  76. if (pointer == &m_storage)
  77. {
  78. m_in_use = false;
  79. }
  80. else
  81. {
  82. ::operator delete(pointer);
  83. }
  84. }
  85. private:
  86. // Storage space used for handler-based custom memory allocation.
  87. lib::aligned_storage<size>::type m_storage;
  88. // Whether the handler-based custom allocation storage has been used.
  89. bool m_in_use;
  90. };
  91. // Wrapper class template for handler objects to allow handler memory
  92. // allocation to be customised. Calls to operator() are forwarded to the
  93. // encapsulated handler.
  94. template <typename Handler> class custom_alloc_handler
  95. {
  96. public:
  97. custom_alloc_handler(handler_allocator & a, Handler h) :
  98. allocator_(a),
  99. handler_(h)
  100. {
  101. }
  102. template <typename Arg1> void operator()(Arg1 arg1)
  103. {
  104. handler_(arg1);
  105. }
  106. template <typename Arg1, typename Arg2> void operator()(Arg1 arg1, Arg2 arg2)
  107. {
  108. handler_(arg1, arg2);
  109. }
  110. friend void * asio_handler_allocate(std::size_t size, custom_alloc_handler<Handler> * this_handler)
  111. {
  112. return this_handler->allocator_.allocate(size);
  113. }
  114. friend void asio_handler_deallocate(void * pointer, std::size_t /*size*/, custom_alloc_handler<Handler> * this_handler)
  115. {
  116. this_handler->allocator_.deallocate(pointer);
  117. }
  118. private:
  119. handler_allocator & allocator_;
  120. Handler handler_;
  121. };
  122. // Helper function to wrap a handler object to add custom allocation.
  123. template <typename Handler> inline custom_alloc_handler<Handler> make_custom_alloc_handler(handler_allocator & a, Handler h)
  124. {
  125. return custom_alloc_handler<Handler>(a, h);
  126. }
  127. // Forward declaration of class endpoint so that it can be friended/referenced
  128. // before being included.
  129. template <typename config> class endpoint;
  130. typedef lib::function<void(const lib::asio::error_code & ec, size_t bytes_transferred)> async_read_handler;
  131. typedef lib::function<void(const lib::asio::error_code & ec, size_t bytes_transferred)> async_write_handler;
  132. typedef lib::function<void(const lib::error_code & ec)> pre_init_handler;
  133. // handle_timer: dynamic parameters, multiple copies
  134. // handle_proxy_write
  135. // handle_proxy_read
  136. // handle_async_write
  137. // handle_pre_init
  138. /// Asio transport errors
  139. namespace error
  140. {
  141. enum value
  142. {
  143. /// Catch-all error for transport policy errors that don't fit in other
  144. /// categories
  145. general = 1,
  146. /// async_read_at_least call requested more bytes than buffer can store
  147. invalid_num_bytes,
  148. /// there was an error in the underlying transport library
  149. pass_through,
  150. /// The connection to the requested proxy server failed
  151. proxy_failed,
  152. /// Invalid Proxy URI
  153. proxy_invalid,
  154. /// Invalid host or service
  155. invalid_host_service
  156. };
  157. /// Asio transport error category
  158. class category : public lib::error_category
  159. {
  160. public:
  161. const char * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
  162. {
  163. return "websocketpp.transport.asio";
  164. }
  165. std::string message(int value) const
  166. {
  167. switch (value)
  168. {
  169. case error::general : return "Generic asio transport policy error";
  170. case error::invalid_num_bytes : return "async_read_at_least call requested more bytes than buffer can store";
  171. case error::pass_through : return "Underlying Transport Error";
  172. case error::proxy_failed : return "Proxy connection failed";
  173. case error::proxy_invalid : return "Invalid proxy URI";
  174. case error::invalid_host_service: return "Invalid host or service";
  175. default : return "Unknown";
  176. }
  177. }
  178. };
  179. /// Get a reference to a static copy of the asio transport error category
  180. inline const lib::error_category & get_category()
  181. {
  182. static category instance;
  183. return instance;
  184. }
  185. /// Create an error code with the given value and the asio transport category
  186. inline lib::error_code make_error_code(error::value e)
  187. {
  188. return lib::error_code(static_cast<int>(e), get_category());
  189. }
  190. } // namespace error
  191. } // namespace asio
  192. } // namespace transport
  193. } // namespace websocketpp
  194. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
  195. template <> struct is_error_code_enum<websocketpp::transport::asio::error::value>
  196. {
  197. static const bool value = true;
  198. };
  199. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
  200. #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP