endpoint.hpp 26 KB

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