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.

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