base.hpp 7.2 KB

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