endpoint.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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_HPP
  28. #define WEBSOCKETPP_ENDPOINT_HPP
  29. #include <websocketpp/connection.hpp>
  30. #include <websocketpp/logger/levels.hpp>
  31. #include <websocketpp/version.hpp>
  32. #include <string>
  33. namespace websocketpp {
  34. /// Creates and manages connections associated with a WebSocket endpoint
  35. template <typename connection, typename config>
  36. class endpoint : public config::transport_type, public config::endpoint_base {
  37. public:
  38. // Import appropriate types from our helper class
  39. // See endpoint_types for more details.
  40. typedef endpoint<connection,config> type;
  41. /// Type of the transport component of this endpoint
  42. typedef typename config::transport_type transport_type;
  43. /// Type of the concurrency component of this endpoint
  44. typedef typename config::concurrency_type concurrency_type;
  45. /// Type of the connections that this endpoint creates
  46. typedef connection connection_type;
  47. /// Shared pointer to connection_type
  48. typedef typename connection_type::ptr connection_ptr;
  49. /// Weak pointer to connection type
  50. typedef typename connection_type::weak_ptr connection_weak_ptr;
  51. /// Type of the transport component of the connections that this endpoint
  52. /// creates
  53. typedef typename transport_type::transport_con_type transport_con_type;
  54. /// Type of a shared pointer to the transport component of the connections
  55. /// that this endpoint creates.
  56. typedef typename transport_con_type::ptr transport_con_ptr;
  57. /// Type of message_handler
  58. typedef typename connection_type::message_handler message_handler;
  59. /// Type of message pointers that this endpoint uses
  60. typedef typename connection_type::message_ptr message_ptr;
  61. /// Type of error logger
  62. typedef typename config::elog_type elog_type;
  63. /// Type of access logger
  64. typedef typename config::alog_type alog_type;
  65. /// Type of our concurrency policy's scoped lock object
  66. typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
  67. /// Type of our concurrency policy's mutex object
  68. typedef typename concurrency_type::mutex_type mutex_type;
  69. /// Type of RNG
  70. typedef typename config::rng_type rng_type;
  71. // TODO: organize these
  72. typedef typename connection_type::termination_handler termination_handler;
  73. explicit endpoint(bool p_is_server)
  74. : m_alog(config::alog_level, log::channel_type_hint::access)
  75. , m_elog(config::elog_level, log::channel_type_hint::error)
  76. , m_user_agent(::websocketpp::user_agent)
  77. , m_open_handshake_timeout_dur(config::timeout_open_handshake)
  78. , m_close_handshake_timeout_dur(config::timeout_close_handshake)
  79. , m_pong_timeout_dur(config::timeout_pong)
  80. , m_max_message_size(config::max_message_size)
  81. , m_max_http_body_size(config::max_http_body_size)
  82. , m_is_server(p_is_server)
  83. {
  84. m_alog.set_channels(config::alog_level);
  85. m_elog.set_channels(config::elog_level);
  86. m_alog.write(log::alevel::devel, "endpoint constructor");
  87. transport_type::init_logging(&m_alog, &m_elog);
  88. }
  89. /// Destructor
  90. ~endpoint<connection,config>() {}
  91. #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  92. // no copy constructor because endpoints are not copyable
  93. endpoint(endpoint &) = delete;
  94. // no copy assignment operator because endpoints are not copyable
  95. endpoint & operator=(endpoint const &) = delete;
  96. #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  97. #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
  98. /// Move constructor
  99. endpoint(endpoint && o)
  100. : config::transport_type(std::move(o))
  101. , config::endpoint_base(std::move(o))
  102. , m_alog(std::move(o.m_alog))
  103. , m_elog(std::move(o.m_elog))
  104. , m_user_agent(std::move(o.m_user_agent))
  105. , m_open_handler(std::move(o.m_open_handler))
  106. , m_close_handler(std::move(o.m_close_handler))
  107. , m_fail_handler(std::move(o.m_fail_handler))
  108. , m_ping_handler(std::move(o.m_ping_handler))
  109. , m_pong_handler(std::move(o.m_pong_handler))
  110. , m_pong_timeout_handler(std::move(o.m_pong_timeout_handler))
  111. , m_interrupt_handler(std::move(o.m_interrupt_handler))
  112. , m_http_handler(std::move(o.m_http_handler))
  113. , m_validate_handler(std::move(o.m_validate_handler))
  114. , m_message_handler(std::move(o.m_message_handler))
  115. , m_open_handshake_timeout_dur(o.m_open_handshake_timeout_dur)
  116. , m_close_handshake_timeout_dur(o.m_close_handshake_timeout_dur)
  117. , m_pong_timeout_dur(o.m_pong_timeout_dur)
  118. , m_max_message_size(o.m_max_message_size)
  119. , m_max_http_body_size(o.m_max_http_body_size)
  120. , m_rng(std::move(o.m_rng))
  121. , m_is_server(o.m_is_server)
  122. {}
  123. #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  124. // no move assignment operator because of const member variables
  125. endpoint & operator=(endpoint &&) = delete;
  126. #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  127. #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
  128. /// Returns the user agent string that this endpoint will use
  129. /**
  130. * Returns the user agent string that this endpoint will use when creating
  131. * new connections.
  132. *
  133. * The default value for this version is stored in websocketpp::user_agent
  134. *
  135. * @return The user agent string.
  136. */
  137. std::string get_user_agent() const {
  138. scoped_lock_type guard(m_mutex);
  139. return m_user_agent;
  140. }
  141. /// Sets the user agent string that this endpoint will use
  142. /**
  143. * Sets the identifier that this endpoint will use when creating new
  144. * connections. Changing this value will only affect future connections.
  145. * For client endpoints this will be sent as the "User-Agent" header in
  146. * outgoing requests. For server endpoints this will be sent in the "Server"
  147. * response header.
  148. *
  149. * Setting this value to the empty string will suppress the use of the
  150. * Server and User-Agent headers. This is typically done to hide
  151. * implementation details for security purposes.
  152. *
  153. * For best results set this before accepting or opening connections.
  154. *
  155. * The default value for this version is stored in websocketpp::user_agent
  156. *
  157. * This can be overridden on an individual connection basis by setting a
  158. * custom "Server" header during the validate handler or "User-Agent"
  159. * header on a connection before calling connect().
  160. *
  161. * @param ua The string to set the user agent to.
  162. */
  163. void set_user_agent(std::string const & ua) {
  164. scoped_lock_type guard(m_mutex);
  165. m_user_agent = ua;
  166. }
  167. /// Returns whether or not this endpoint is a server.
  168. /**
  169. * @return Whether or not this endpoint is a server
  170. */
  171. bool is_server() const {
  172. return m_is_server;
  173. }
  174. /********************************/
  175. /* Pass-through logging adaptor */
  176. /********************************/
  177. /// Set Access logging channel
  178. /**
  179. * Set the access logger's channel value. The value is a number whose
  180. * interpretation depends on the logging policy in use.
  181. *
  182. * @param channels The channel value(s) to set
  183. */
  184. void set_access_channels(log::level channels) {
  185. m_alog.set_channels(channels);
  186. }
  187. /// Clear Access logging channels
  188. /**
  189. * Clear the access logger's channel value. The value is a number whose
  190. * interpretation depends on the logging policy in use.
  191. *
  192. * @param channels The channel value(s) to clear
  193. */
  194. void clear_access_channels(log::level channels) {
  195. m_alog.clear_channels(channels);
  196. }
  197. /// Set Error logging channel
  198. /**
  199. * Set the error logger's channel value. The value is a number whose
  200. * interpretation depends on the logging policy in use.
  201. *
  202. * @param channels The channel value(s) to set
  203. */
  204. void set_error_channels(log::level channels) {
  205. m_elog.set_channels(channels);
  206. }
  207. /// Clear Error logging channels
  208. /**
  209. * Clear the error logger's channel value. The value is a number whose
  210. * interpretation depends on the logging policy in use.
  211. *
  212. * @param channels The channel value(s) to clear
  213. */
  214. void clear_error_channels(log::level channels) {
  215. m_elog.clear_channels(channels);
  216. }
  217. /// Get reference to access logger
  218. /**
  219. * @return A reference to the access logger
  220. */
  221. alog_type & get_alog() {
  222. return m_alog;
  223. }
  224. /// Get reference to error logger
  225. /**
  226. * @return A reference to the error logger
  227. */
  228. elog_type & get_elog() {
  229. return m_elog;
  230. }
  231. /*************************/
  232. /* Set Handler functions */
  233. /*************************/
  234. void set_open_handler(open_handler h) {
  235. m_alog.write(log::alevel::devel,"set_open_handler");
  236. scoped_lock_type guard(m_mutex);
  237. m_open_handler = h;
  238. }
  239. void set_close_handler(close_handler h) {
  240. m_alog.write(log::alevel::devel,"set_close_handler");
  241. scoped_lock_type guard(m_mutex);
  242. m_close_handler = h;
  243. }
  244. void set_fail_handler(fail_handler h) {
  245. m_alog.write(log::alevel::devel,"set_fail_handler");
  246. scoped_lock_type guard(m_mutex);
  247. m_fail_handler = h;
  248. }
  249. void set_ping_handler(ping_handler h) {
  250. m_alog.write(log::alevel::devel,"set_ping_handler");
  251. scoped_lock_type guard(m_mutex);
  252. m_ping_handler = h;
  253. }
  254. void set_pong_handler(pong_handler h) {
  255. m_alog.write(log::alevel::devel,"set_pong_handler");
  256. scoped_lock_type guard(m_mutex);
  257. m_pong_handler = h;
  258. }
  259. void set_pong_timeout_handler(pong_timeout_handler h) {
  260. m_alog.write(log::alevel::devel,"set_pong_timeout_handler");
  261. scoped_lock_type guard(m_mutex);
  262. m_pong_timeout_handler = h;
  263. }
  264. void set_interrupt_handler(interrupt_handler h) {
  265. m_alog.write(log::alevel::devel,"set_interrupt_handler");
  266. scoped_lock_type guard(m_mutex);
  267. m_interrupt_handler = h;
  268. }
  269. void set_http_handler(http_handler h) {
  270. m_alog.write(log::alevel::devel,"set_http_handler");
  271. scoped_lock_type guard(m_mutex);
  272. m_http_handler = h;
  273. }
  274. void set_validate_handler(validate_handler h) {
  275. m_alog.write(log::alevel::devel,"set_validate_handler");
  276. scoped_lock_type guard(m_mutex);
  277. m_validate_handler = h;
  278. }
  279. void set_message_handler(message_handler h) {
  280. m_alog.write(log::alevel::devel,"set_message_handler");
  281. scoped_lock_type guard(m_mutex);
  282. m_message_handler = h;
  283. }
  284. //////////////////////////////////////////
  285. // Connection timeouts and other limits //
  286. //////////////////////////////////////////
  287. /// Set open handshake timeout
  288. /**
  289. * Sets the length of time the library will wait after an opening handshake
  290. * has been initiated before cancelling it. This can be used to prevent
  291. * excessive wait times for outgoing clients or excessive resource usage
  292. * from broken clients or DoS attacks on servers.
  293. *
  294. * Connections that time out will have their fail handlers called with the
  295. * open_handshake_timeout error code.
  296. *
  297. * The default value is specified via the compile time config value
  298. * 'timeout_open_handshake'. The default value in the core config
  299. * is 5000ms. A value of 0 will disable the timer entirely.
  300. *
  301. * To be effective, the transport you are using must support timers. See
  302. * the documentation for your transport policy for details about its
  303. * timer support.
  304. *
  305. * @param dur The length of the open handshake timeout in ms
  306. */
  307. void set_open_handshake_timeout(long dur) {
  308. scoped_lock_type guard(m_mutex);
  309. m_open_handshake_timeout_dur = dur;
  310. }
  311. /// Set close handshake timeout
  312. /**
  313. * Sets the length of time the library will wait after a closing handshake
  314. * has been initiated before cancelling it. This can be used to prevent
  315. * excessive wait times for outgoing clients or excessive resource usage
  316. * from broken clients or DoS attacks on servers.
  317. *
  318. * Connections that time out will have their close handlers called with the
  319. * close_handshake_timeout error code.
  320. *
  321. * The default value is specified via the compile time config value
  322. * 'timeout_close_handshake'. The default value in the core config
  323. * is 5000ms. A value of 0 will disable the timer entirely.
  324. *
  325. * To be effective, the transport you are using must support timers. See
  326. * the documentation for your transport policy for details about its
  327. * timer support.
  328. *
  329. * @param dur The length of the close handshake timeout in ms
  330. */
  331. void set_close_handshake_timeout(long dur) {
  332. scoped_lock_type guard(m_mutex);
  333. m_close_handshake_timeout_dur = dur;
  334. }
  335. /// Set pong timeout
  336. /**
  337. * Sets the length of time the library will wait for a pong response to a
  338. * ping. This can be used as a keepalive or to detect broken connections.
  339. *
  340. * Pong responses that time out will have the pong timeout handler called.
  341. *
  342. * The default value is specified via the compile time config value
  343. * 'timeout_pong'. The default value in the core config
  344. * is 5000ms. A value of 0 will disable the timer entirely.
  345. *
  346. * To be effective, the transport you are using must support timers. See
  347. * the documentation for your transport policy for details about its
  348. * timer support.
  349. *
  350. * @param dur The length of the pong timeout in ms
  351. */
  352. void set_pong_timeout(long dur) {
  353. scoped_lock_type guard(m_mutex);
  354. m_pong_timeout_dur = dur;
  355. }
  356. /// Get default maximum message size
  357. /**
  358. * Get the default maximum message size that will be used for new
  359. * connections created by this endpoint. The maximum message size determines
  360. * the point at which the connection will fail a connection with the
  361. * message_too_big protocol error.
  362. *
  363. * The default is set by the max_message_size value from the template config
  364. *
  365. * @since 0.3.0
  366. */
  367. size_t get_max_message_size() const {
  368. return m_max_message_size;
  369. }
  370. /// Set default maximum message size
  371. /**
  372. * Set the default maximum message size that will be used for new
  373. * connections created by this endpoint. Maximum message size determines the
  374. * point at which the connection will fail a connection with the
  375. * message_too_big protocol error.
  376. *
  377. * The default is set by the max_message_size value from the template config
  378. *
  379. * @since 0.3.0
  380. *
  381. * @param new_value The value to set as the maximum message size.
  382. */
  383. void set_max_message_size(size_t new_value) {
  384. m_max_message_size = new_value;
  385. }
  386. /// Get maximum HTTP message body size
  387. /**
  388. * Get maximum HTTP message body size. Maximum message body size determines
  389. * the point at which the connection will stop reading an HTTP request whose
  390. * body is too large.
  391. *
  392. * The default is set by the max_http_body_size value from the template
  393. * config
  394. *
  395. * @since 0.5.0
  396. *
  397. * @return The maximum HTTP message body size
  398. */
  399. size_t get_max_http_body_size() const {
  400. return m_max_http_body_size;
  401. }
  402. /// Set maximum HTTP message body size
  403. /**
  404. * Set maximum HTTP message body size. Maximum message body size determines
  405. * the point at which the connection will stop reading an HTTP request whose
  406. * body is too large.
  407. *
  408. * The default is set by the max_http_body_size value from the template
  409. * config
  410. *
  411. * @since 0.5.1
  412. *
  413. * @param new_value The value to set as the maximum message size.
  414. */
  415. void set_max_http_body_size(size_t new_value) {
  416. m_max_http_body_size = new_value;
  417. }
  418. /*************************************/
  419. /* Connection pass through functions */
  420. /*************************************/
  421. /**
  422. * These functions act as adaptors to their counterparts in connection. They
  423. * can produce one additional type of error, the bad_connection error, that
  424. * indicates that the conversion from connection_hdl to connection_ptr
  425. * failed due to the connection not existing anymore. Each method has a
  426. * default and an exception free varient.
  427. */
  428. void interrupt(connection_hdl hdl, lib::error_code & ec);
  429. void interrupt(connection_hdl hdl);
  430. /// Pause reading of new data (exception free)
  431. /**
  432. * Signals to the connection to halt reading of new data. While reading is
  433. * paused, the connection will stop reading from its associated socket. In
  434. * turn this will result in TCP based flow control kicking in and slowing
  435. * data flow from the remote endpoint.
  436. *
  437. * This is useful for applications that push new requests to a queue to be
  438. * processed by another thread and need a way to signal when their request
  439. * queue is full without blocking the network processing thread.
  440. *
  441. * Use `resume_reading()` to resume.
  442. *
  443. * If supported by the transport this is done asynchronously. As such
  444. * reading may not stop until the current read operation completes.
  445. * Typically you can expect to receive no more bytes after initiating a read
  446. * pause than the size of the read buffer.
  447. *
  448. * If reading is paused for this connection already nothing is changed.
  449. */
  450. void pause_reading(connection_hdl hdl, lib::error_code & ec);
  451. /// Pause reading of new data
  452. void pause_reading(connection_hdl hdl);
  453. /// Resume reading of new data (exception free)
  454. /**
  455. * Signals to the connection to resume reading of new data after it was
  456. * paused by `pause_reading()`.
  457. *
  458. * If reading is not paused for this connection already nothing is changed.
  459. */
  460. void resume_reading(connection_hdl hdl, lib::error_code & ec);
  461. /// Resume reading of new data
  462. void resume_reading(connection_hdl hdl);
  463. /// Send deferred HTTP Response
  464. /**
  465. * Sends an http response to an HTTP connection that was deferred. This will
  466. * send a complete response including all headers, status line, and body
  467. * text. The connection will be closed afterwards.
  468. *
  469. * Exception free variant
  470. *
  471. * @since 0.6.0
  472. *
  473. * @param hdl The connection to send the response on
  474. * @param ec A status code, zero on success, non-zero otherwise
  475. */
  476. void send_http_response(connection_hdl hdl, lib::error_code & ec);
  477. /// Send deferred HTTP Response (exception free)
  478. /**
  479. * Sends an http response to an HTTP connection that was deferred. This will
  480. * send a complete response including all headers, status line, and body
  481. * text. The connection will be closed afterwards.
  482. *
  483. * Exception variant
  484. *
  485. * @since 0.6.0
  486. *
  487. * @param hdl The connection to send the response on
  488. */
  489. void send_http_response(connection_hdl hdl);
  490. /// Create a message and add it to the outgoing send queue (exception free)
  491. /**
  492. * Convenience method to send a message given a payload string and an opcode
  493. *
  494. * @param [in] hdl The handle identifying the connection to send via.
  495. * @param [in] payload The payload string to generated the message with
  496. * @param [in] op The opcode to generated the message with.
  497. * @param [out] ec A code to fill in for errors
  498. */
  499. void send(connection_hdl hdl, std::string const & payload,
  500. frame::opcode::value op, lib::error_code & ec);
  501. /// Create a message and add it to the outgoing send queue
  502. /**
  503. * Convenience method to send a message given a payload string and an opcode
  504. *
  505. * @param [in] hdl The handle identifying the connection to send via.
  506. * @param [in] payload The payload string to generated the message with
  507. * @param [in] op The opcode to generated the message with.
  508. * @param [out] ec A code to fill in for errors
  509. */
  510. void send(connection_hdl hdl, std::string const & payload,
  511. frame::opcode::value op);
  512. void send(connection_hdl hdl, void const * payload, size_t len,
  513. frame::opcode::value op, lib::error_code & ec);
  514. void send(connection_hdl hdl, void const * payload, size_t len,
  515. frame::opcode::value op);
  516. void send(connection_hdl hdl, message_ptr msg, lib::error_code & ec);
  517. void send(connection_hdl hdl, message_ptr msg);
  518. void close(connection_hdl hdl, close::status::value const code,
  519. std::string const & reason, lib::error_code & ec);
  520. void close(connection_hdl hdl, close::status::value const code,
  521. std::string const & reason);
  522. /// Send a ping to a specific connection
  523. /**
  524. * @since 0.3.0-alpha3
  525. *
  526. * @param [in] hdl The connection_hdl of the connection to send to.
  527. * @param [in] payload The payload string to send.
  528. * @param [out] ec A reference to an error code to fill in
  529. */
  530. void ping(connection_hdl hdl, std::string const & payload,
  531. lib::error_code & ec);
  532. /// Send a ping to a specific connection
  533. /**
  534. * Exception variant of `ping`
  535. *
  536. * @since 0.3.0-alpha3
  537. *
  538. * @param [in] hdl The connection_hdl of the connection to send to.
  539. * @param [in] payload The payload string to send.
  540. */
  541. void ping(connection_hdl hdl, std::string const & payload);
  542. /// Send a pong to a specific connection
  543. /**
  544. * @since 0.3.0-alpha3
  545. *
  546. * @param [in] hdl The connection_hdl of the connection to send to.
  547. * @param [in] payload The payload string to send.
  548. * @param [out] ec A reference to an error code to fill in
  549. */
  550. void pong(connection_hdl hdl, std::string const & payload,
  551. lib::error_code & ec);
  552. /// Send a pong to a specific connection
  553. /**
  554. * Exception variant of `pong`
  555. *
  556. * @since 0.3.0-alpha3
  557. *
  558. * @param [in] hdl The connection_hdl of the connection to send to.
  559. * @param [in] payload The payload string to send.
  560. */
  561. void pong(connection_hdl hdl, std::string const & payload);
  562. /// Retrieves a connection_ptr from a connection_hdl (exception free)
  563. /**
  564. * Converting a weak pointer to shared_ptr is not thread safe because the
  565. * pointer could be deleted at any time.
  566. *
  567. * NOTE: This method may be called by handler to upgrade its handle to a
  568. * full connection_ptr. That full connection may then be used safely for the
  569. * remainder of the handler body. get_con_from_hdl and the resulting
  570. * connection_ptr are NOT safe to use outside the handler loop.
  571. *
  572. * @param hdl The connection handle to translate
  573. *
  574. * @return the connection_ptr. May be NULL if the handle was invalid.
  575. */
  576. connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code & ec) {
  577. connection_ptr con = lib::static_pointer_cast<connection_type>(
  578. hdl.lock());
  579. if (!con) {
  580. ec = error::make_error_code(error::bad_connection);
  581. }
  582. return con;
  583. }
  584. /// Retrieves a connection_ptr from a connection_hdl (exception version)
  585. connection_ptr get_con_from_hdl(connection_hdl hdl) {
  586. lib::error_code ec;
  587. connection_ptr con = this->get_con_from_hdl(hdl,ec);
  588. if (ec) {
  589. throw exception(ec);
  590. }
  591. return con;
  592. }
  593. protected:
  594. connection_ptr create_connection();
  595. alog_type m_alog;
  596. elog_type m_elog;
  597. private:
  598. // dynamic settings
  599. std::string m_user_agent;
  600. open_handler m_open_handler;
  601. close_handler m_close_handler;
  602. fail_handler m_fail_handler;
  603. ping_handler m_ping_handler;
  604. pong_handler m_pong_handler;
  605. pong_timeout_handler m_pong_timeout_handler;
  606. interrupt_handler m_interrupt_handler;
  607. http_handler m_http_handler;
  608. validate_handler m_validate_handler;
  609. message_handler m_message_handler;
  610. long m_open_handshake_timeout_dur;
  611. long m_close_handshake_timeout_dur;
  612. long m_pong_timeout_dur;
  613. size_t m_max_message_size;
  614. size_t m_max_http_body_size;
  615. rng_type m_rng;
  616. // static settings
  617. bool const m_is_server;
  618. // endpoint state
  619. mutable mutex_type m_mutex;
  620. };
  621. } // namespace websocketpp
  622. #include <websocketpp/impl/endpoint_impl.hpp>
  623. #endif // WEBSOCKETPP_ENDPOINT_HPP