endpoint_impl.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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_ENDPOINT_IMPL_HPP
  28. #define WEBSOCKETPP_ENDPOINT_IMPL_HPP
  29. #include <string>
  30. namespace websocketpp {
  31. template <typename connection, typename config>
  32. typename endpoint<connection,config>::connection_ptr
  33. endpoint<connection,config>::create_connection() {
  34. m_alog->write(log::alevel::devel,"create_connection");
  35. //scoped_lock_type lock(m_state_lock);
  36. /*if (m_state == STOPPING || m_state == STOPPED) {
  37. return connection_ptr();
  38. }*/
  39. //scoped_lock_type guard(m_mutex);
  40. // Create a connection on the heap and manage it using a shared pointer
  41. connection_ptr con = lib::make_shared<connection_type>(m_is_server,
  42. m_user_agent, m_alog, m_elog, lib::ref(m_rng));
  43. connection_weak_ptr w(con);
  44. // Create a weak pointer on the heap using that shared_ptr.
  45. // Cast that weak pointer to void* and manage it using another shared_ptr
  46. // connection_hdl hdl(reinterpret_cast<void*>(new connection_weak_ptr(con)));
  47. con->set_handle(w);
  48. // Copy default handlers from the endpoint
  49. con->set_open_handler(m_open_handler);
  50. con->set_close_handler(m_close_handler);
  51. con->set_fail_handler(m_fail_handler);
  52. con->set_ping_handler(m_ping_handler);
  53. con->set_pong_handler(m_pong_handler);
  54. con->set_pong_timeout_handler(m_pong_timeout_handler);
  55. con->set_interrupt_handler(m_interrupt_handler);
  56. con->set_http_handler(m_http_handler);
  57. con->set_validate_handler(m_validate_handler);
  58. con->set_message_handler(m_message_handler);
  59. if (m_open_handshake_timeout_dur != config::timeout_open_handshake) {
  60. con->set_open_handshake_timeout(m_open_handshake_timeout_dur);
  61. }
  62. if (m_close_handshake_timeout_dur != config::timeout_close_handshake) {
  63. con->set_close_handshake_timeout(m_close_handshake_timeout_dur);
  64. }
  65. if (m_pong_timeout_dur != config::timeout_pong) {
  66. con->set_pong_timeout(m_pong_timeout_dur);
  67. }
  68. if (m_max_message_size != config::max_message_size) {
  69. con->set_max_message_size(m_max_message_size);
  70. }
  71. con->set_max_http_body_size(m_max_http_body_size);
  72. lib::error_code ec;
  73. ec = transport_type::init(con);
  74. if (ec) {
  75. m_elog->write(log::elevel::fatal,ec.message());
  76. return connection_ptr();
  77. }
  78. return con;
  79. }
  80. template <typename connection, typename config>
  81. void endpoint<connection,config>::interrupt(connection_hdl hdl, lib::error_code & ec)
  82. {
  83. connection_ptr con = get_con_from_hdl(hdl,ec);
  84. if (ec) {return;}
  85. m_alog->write(log::alevel::devel,"Interrupting connection");
  86. ec = con->interrupt();
  87. }
  88. template <typename connection, typename config>
  89. void endpoint<connection,config>::interrupt(connection_hdl hdl) {
  90. lib::error_code ec;
  91. interrupt(hdl,ec);
  92. if (ec) { throw exception(ec); }
  93. }
  94. template <typename connection, typename config>
  95. void endpoint<connection,config>::pause_reading(connection_hdl hdl, lib::error_code & ec)
  96. {
  97. connection_ptr con = get_con_from_hdl(hdl,ec);
  98. if (ec) {return;}
  99. ec = con->pause_reading();
  100. }
  101. template <typename connection, typename config>
  102. void endpoint<connection,config>::pause_reading(connection_hdl hdl) {
  103. lib::error_code ec;
  104. pause_reading(hdl,ec);
  105. if (ec) { throw exception(ec); }
  106. }
  107. template <typename connection, typename config>
  108. void endpoint<connection,config>::resume_reading(connection_hdl hdl, lib::error_code & ec)
  109. {
  110. connection_ptr con = get_con_from_hdl(hdl,ec);
  111. if (ec) {return;}
  112. ec = con->resume_reading();
  113. }
  114. template <typename connection, typename config>
  115. void endpoint<connection,config>::resume_reading(connection_hdl hdl) {
  116. lib::error_code ec;
  117. resume_reading(hdl,ec);
  118. if (ec) { throw exception(ec); }
  119. }
  120. template <typename connection, typename config>
  121. void endpoint<connection,config>::send_http_response(connection_hdl hdl,
  122. lib::error_code & ec)
  123. {
  124. connection_ptr con = get_con_from_hdl(hdl,ec);
  125. if (ec) {return;}
  126. con->send_http_response(ec);
  127. }
  128. template <typename connection, typename config>
  129. void endpoint<connection,config>::send_http_response(connection_hdl hdl) {
  130. lib::error_code ec;
  131. send_http_response(hdl,ec);
  132. if (ec) { throw exception(ec); }
  133. }
  134. template <typename connection, typename config>
  135. void endpoint<connection,config>::send(connection_hdl hdl, std::string const & payload,
  136. frame::opcode::value op, lib::error_code & ec)
  137. {
  138. connection_ptr con = get_con_from_hdl(hdl,ec);
  139. if (ec) {return;}
  140. ec = con->send(payload,op);
  141. }
  142. template <typename connection, typename config>
  143. void endpoint<connection,config>::send(connection_hdl hdl, std::string const & payload,
  144. frame::opcode::value op)
  145. {
  146. lib::error_code ec;
  147. send(hdl,payload,op,ec);
  148. if (ec) { throw exception(ec); }
  149. }
  150. template <typename connection, typename config>
  151. void endpoint<connection,config>::send(connection_hdl hdl, void const * payload,
  152. size_t len, frame::opcode::value op, lib::error_code & ec)
  153. {
  154. connection_ptr con = get_con_from_hdl(hdl,ec);
  155. if (ec) {return;}
  156. ec = con->send(payload,len,op);
  157. }
  158. template <typename connection, typename config>
  159. void endpoint<connection,config>::send(connection_hdl hdl, void const * payload,
  160. size_t len, frame::opcode::value op)
  161. {
  162. lib::error_code ec;
  163. send(hdl,payload,len,op,ec);
  164. if (ec) { throw exception(ec); }
  165. }
  166. template <typename connection, typename config>
  167. void endpoint<connection,config>::send(connection_hdl hdl, message_ptr msg,
  168. lib::error_code & ec)
  169. {
  170. connection_ptr con = get_con_from_hdl(hdl,ec);
  171. if (ec) {return;}
  172. ec = con->send(msg);
  173. }
  174. template <typename connection, typename config>
  175. void endpoint<connection,config>::send(connection_hdl hdl, message_ptr msg) {
  176. lib::error_code ec;
  177. send(hdl,msg,ec);
  178. if (ec) { throw exception(ec); }
  179. }
  180. template <typename connection, typename config>
  181. void endpoint<connection,config>::close(connection_hdl hdl, close::status::value
  182. const code, std::string const & reason,
  183. lib::error_code & ec)
  184. {
  185. connection_ptr con = get_con_from_hdl(hdl,ec);
  186. if (ec) {return;}
  187. con->close(code,reason,ec);
  188. }
  189. template <typename connection, typename config>
  190. void endpoint<connection,config>::close(connection_hdl hdl, close::status::value
  191. const code, std::string const & reason)
  192. {
  193. lib::error_code ec;
  194. close(hdl,code,reason,ec);
  195. if (ec) { throw exception(ec); }
  196. }
  197. template <typename connection, typename config>
  198. void endpoint<connection,config>::ping(connection_hdl hdl, std::string const &
  199. payload, lib::error_code & ec)
  200. {
  201. connection_ptr con = get_con_from_hdl(hdl,ec);
  202. if (ec) {return;}
  203. con->ping(payload,ec);
  204. }
  205. template <typename connection, typename config>
  206. void endpoint<connection,config>::ping(connection_hdl hdl, std::string const & payload)
  207. {
  208. lib::error_code ec;
  209. ping(hdl,payload,ec);
  210. if (ec) { throw exception(ec); }
  211. }
  212. template <typename connection, typename config>
  213. void endpoint<connection,config>::pong(connection_hdl hdl, std::string const & payload,
  214. lib::error_code & ec)
  215. {
  216. connection_ptr con = get_con_from_hdl(hdl,ec);
  217. if (ec) {return;}
  218. con->pong(payload,ec);
  219. }
  220. template <typename connection, typename config>
  221. void endpoint<connection,config>::pong(connection_hdl hdl, std::string const & payload)
  222. {
  223. lib::error_code ec;
  224. pong(hdl,payload,ec);
  225. if (ec) { throw exception(ec); }
  226. }
  227. } // namespace websocketpp
  228. #endif // WEBSOCKETPP_ENDPOINT_IMPL_HPP