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.

247 lines
8.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014, 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_BASE_CON_HPP
  28. #define WEBSOCKETPP_TRANSPORT_BASE_CON_HPP
  29. #include <string>
  30. #include <websocketpp/common/connection_hdl.hpp>
  31. #include <websocketpp/common/cpp11.hpp>
  32. #include <websocketpp/common/functional.hpp>
  33. #include <websocketpp/common/system_error.hpp>
  34. namespace websocketpp
  35. {
  36. /// Transport policies provide network connectivity and timers
  37. /**
  38. * ### Connection Interface
  39. *
  40. * Transport connection components needs to provide:
  41. *
  42. * **init**\n
  43. * `void init(init_handler handler)`\n
  44. * Called once shortly after construction to give the policy the chance to
  45. * perform one time initialization. When complete, the policy must call the
  46. * supplied `init_handler` to continue setup. The handler takes one argument
  47. * with the error code if any. If an error is returned here setup will fail and
  48. * the connection will be aborted or terminated.
  49. *
  50. * WebSocket++ will call init only once. The transport must call `handler`
  51. * exactly once.
  52. *
  53. * **async_read_at_least**\n
  54. * `void async_read_at_least(size_t num_bytes, char *buf, size_t len,
  55. * read_handler handler)`\n
  56. * start an async read for at least num_bytes and at most len
  57. * bytes into buf. Call handler when done with number of bytes read.
  58. *
  59. * WebSocket++ promises to have only one async_read_at_least in flight at a
  60. * time. The transport must promise to only call read_handler once per async
  61. * read.
  62. *
  63. * **async_write**\n
  64. * `void async_write(const char* buf, size_t len, write_handler handler)`\n
  65. * `void async_write(std::vector<buffer> & bufs, write_handler handler)`\n
  66. * Start a write of all of the data in buf or bufs. In second case data is
  67. * written sequentially and in place without copying anything to a temporary
  68. * location.
  69. *
  70. * Websocket++ promises to have only one async_write in flight at a time.
  71. * The transport must promise to only call the write_handler once per async
  72. * write
  73. *
  74. * **set_handle**\n
  75. * `void set_handle(connection_hdl hdl)`\n
  76. * Called by WebSocket++ to let this policy know the hdl to the connection. It
  77. * may be stored for later use or ignored/discarded. This handle should be used
  78. * if the policy adds any connection handlers. Connection handlers must be
  79. * called with the handle as the first argument so that the handler code knows
  80. * which connection generated the callback.
  81. *
  82. * **set_timer**\n
  83. * `timer_ptr set_timer(long duration, timer_handler handler)`\n
  84. * WebSocket++ uses the timers provided by the transport policy as the
  85. * implementation of timers is often highly coupled with the implementation of
  86. * the networking event loops.
  87. *
  88. * Transport timer support is an optional feature. A transport method may elect
  89. * to implement a dummy timer object and have this method return an empty
  90. * pointer. If so, all timer related features of WebSocket++ core will be
  91. * disabled. This includes many security features designed to prevent denial of
  92. * service attacks. Use timer-free transport policies with caution.
  93. *
  94. * **get_remote_endpoint**\n
  95. * `std::string get_remote_endpoint()`\n
  96. * retrieve address of remote endpoint
  97. *
  98. * **is_secure**\n
  99. * `void is_secure()`\n
  100. * whether or not the connection to the remote endpoint is secure
  101. *
  102. * **dispatch**\n
  103. * `lib::error_code dispatch(dispatch_handler handler)`: invoke handler within
  104. * the transport's event system if it uses one. Otherwise, this method should
  105. * simply call `handler` immediately.
  106. *
  107. * **async_shutdown**\n
  108. * `void async_shutdown(shutdown_handler handler)`\n
  109. * Perform any cleanup necessary (if any). Call `handler` when complete.
  110. */
  111. namespace transport
  112. {
  113. /// The type and signature of the callback passed to the init hook
  114. typedef lib::function<void(const lib::error_code &)> init_handler;
  115. /// The type and signature of the callback passed to the read method
  116. typedef lib::function<void(const lib::error_code &, size_t)> read_handler;
  117. /// The type and signature of the callback passed to the write method
  118. typedef lib::function<void(const lib::error_code &)> write_handler;
  119. /// The type and signature of the callback passed to the read method
  120. typedef lib::function<void(const lib::error_code &)> timer_handler;
  121. /// The type and signature of the callback passed to the shutdown method
  122. typedef lib::function<void(const lib::error_code &)> shutdown_handler;
  123. /// The type and signature of the callback passed to the interrupt method
  124. typedef lib::function<void()> interrupt_handler;
  125. /// The type and signature of the callback passed to the dispatch method
  126. typedef lib::function<void()> dispatch_handler;
  127. /// A simple utility buffer class
  128. struct buffer
  129. {
  130. buffer(const char * b, size_t l) :
  131. buf(b),
  132. len(l)
  133. {
  134. }
  135. const char * buf;
  136. size_t len;
  137. };
  138. /// Generic transport related 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. /// underlying transport pass through
  147. pass_through,
  148. /// async_read_at_least call requested more bytes than buffer can store
  149. invalid_num_bytes,
  150. /// async_read called while another async_read was in progress
  151. double_read,
  152. /// Operation aborted
  153. operation_aborted,
  154. /// Operation not supported
  155. operation_not_supported,
  156. /// End of file
  157. eof,
  158. /// TLS short read
  159. tls_short_read,
  160. /// Timer expired
  161. timeout,
  162. /// read or write after shutdown
  163. action_after_shutdown,
  164. /// Other TLS error
  165. tls_error
  166. };
  167. class category : public lib::error_category
  168. {
  169. public:
  170. category() {}
  171. const char * name() const _WEBSOCKETPP_NOEXCEPT_TOKEN_
  172. {
  173. return "websocketpp.transport";
  174. }
  175. std::string message(int value) const
  176. {
  177. switch (value)
  178. {
  179. case general : return "Generic transport policy error";
  180. case pass_through : return "Underlying Transport Error";
  181. case invalid_num_bytes : return "async_read_at_least call requested more bytes than buffer can store";
  182. case operation_aborted : return "The operation was aborted";
  183. case operation_not_supported: return "The operation is not supported by this transport";
  184. case eof : return "End of File";
  185. case tls_short_read : return "TLS Short Read";
  186. case timeout : return "Timer Expired";
  187. case action_after_shutdown : return "A transport action was requested after shutdown";
  188. case tls_error : return "Generic TLS related error";
  189. default : return "Unknown";
  190. }
  191. }
  192. };
  193. inline const lib::error_category & get_category()
  194. {
  195. static category instance;
  196. return instance;
  197. }
  198. inline lib::error_code make_error_code(error::value e)
  199. {
  200. return lib::error_code(static_cast<int>(e), get_category());
  201. }
  202. } // namespace error
  203. } // namespace transport
  204. } // namespace websocketpp
  205. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_START_
  206. template <> struct is_error_code_enum<websocketpp::transport::error::value>
  207. {
  208. static const bool value = true;
  209. };
  210. _WEBSOCKETPP_ERROR_CODE_ENUM_NS_END_
  211. #endif // WEBSOCKETPP_TRANSPORT_BASE_CON_HPP