connection.hpp 8.6 KB

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