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.

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