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.

771 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_TRANSPORT_IOSTREAM_CON_HPP
  28. #define WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP
  29. #include <algorithm>
  30. #include <iostream>
  31. #include <sstream>
  32. #include <string>
  33. #include <vector>
  34. #include <websocketpp/common/connection_hdl.hpp>
  35. #include <websocketpp/common/memory.hpp>
  36. #include <websocketpp/common/platforms.hpp>
  37. #include <websocketpp/logger/levels.hpp>
  38. #include <websocketpp/transport/base/connection.hpp>
  39. #include <websocketpp/transport/iostream/base.hpp>
  40. #include <websocketpp/uri.hpp>
  41. namespace websocketpp
  42. {
  43. namespace transport
  44. {
  45. namespace iostream
  46. {
  47. /// Empty timer class to stub out for timer functionality that iostream
  48. /// transport doesn't support
  49. struct timer
  50. {
  51. void cancel() {}
  52. };
  53. template <typename config> class connection : public lib::enable_shared_from_this<connection<config>>
  54. {
  55. public:
  56. /// Type of this connection transport component
  57. typedef connection<config> type;
  58. /// Type of a shared pointer to this connection transport component
  59. typedef lib::shared_ptr<type> ptr;
  60. /// transport concurrency policy
  61. typedef typename config::concurrency_type concurrency_type;
  62. /// Type of this transport's access logging policy
  63. typedef typename config::alog_type alog_type;
  64. /// Type of this transport's error logging policy
  65. typedef typename config::elog_type elog_type;
  66. // Concurrency policy types
  67. typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
  68. typedef typename concurrency_type::mutex_type mutex_type;
  69. typedef lib::shared_ptr<timer> timer_ptr;
  70. explicit connection(bool is_server, const lib::shared_ptr<alog_type> & alog, const lib::shared_ptr<elog_type> & elog) :
  71. m_output_stream(NULL),
  72. m_reading(false),
  73. m_is_server(is_server),
  74. m_is_secure(false),
  75. m_alog(alog),
  76. m_elog(elog),
  77. m_remote_endpoint("iostream transport")
  78. {
  79. m_alog->write(log::alevel::devel, "iostream con transport constructor");
  80. }
  81. /// Get a shared pointer to this component
  82. ptr get_shared()
  83. {
  84. return type::shared_from_this();
  85. }
  86. /// Register a std::ostream with the transport for writing output
  87. /**
  88. * Register a std::ostream with the transport. All future writes will be
  89. * done to this output stream.
  90. *
  91. * @param o A pointer to the ostream to use for output.
  92. */
  93. void register_ostream(std::ostream * o)
  94. {
  95. // TODO: lock transport state?
  96. scoped_lock_type lock(m_read_mutex);
  97. m_output_stream = o;
  98. }
  99. /// Set uri hook
  100. /**
  101. * Called by the endpoint as a connection is being established to provide
  102. * the uri being connected to to the transport layer.
  103. *
  104. * This transport policy doesn't use the uri so it is ignored.
  105. *
  106. * @since 0.6.0
  107. *
  108. * @param u The uri to set
  109. */
  110. void set_uri(uri_ptr) {}
  111. /// Overloaded stream input operator
  112. /**
  113. * Attempts to read input from the given stream into the transport. Bytes
  114. * will be extracted from the input stream to fulfill any pending reads.
  115. * Input in this manner will only read until the current read buffer has
  116. * been filled. Then it will signal the library to process the input. If the
  117. * library's input handler adds a new async_read, additional bytes will be
  118. * read, otherwise the input operation will end.
  119. *
  120. * When this function returns one of the following conditions is true:
  121. * - There is no outstanding read operation
  122. * - There are no more bytes available in the input stream
  123. *
  124. * You can use tellg() on the input stream to determine if all of the input
  125. * bytes were read or not.
  126. *
  127. * If there is no pending read operation when the input method is called, it
  128. * will return immediately and tellg() will not have changed.
  129. */
  130. friend std::istream & operator>>(std::istream & in, type & t)
  131. {
  132. // this serializes calls to external read.
  133. scoped_lock_type lock(t.m_read_mutex);
  134. t.read(in);
  135. return in;
  136. }
  137. /// Manual input supply (read some)
  138. /**
  139. * Copies bytes from buf into WebSocket++'s input buffers. Bytes will be
  140. * copied from the supplied buffer to fulfill any pending library reads. It
  141. * will return the number of bytes successfully processed. If there are no
  142. * pending reads read_some will return immediately. Not all of the bytes may
  143. * be able to be read in one call.
  144. *
  145. * @since 0.3.0-alpha4
  146. *
  147. * @param buf Char buffer to read into the websocket
  148. * @param len Length of buf
  149. * @return The number of characters from buf actually read.
  150. */
  151. size_t read_some(const char * buf, size_t len)
  152. {
  153. // this serializes calls to external read.
  154. scoped_lock_type lock(m_read_mutex);
  155. return this->read_some_impl(buf, len);
  156. }
  157. /// Manual input supply (read all)
  158. /**
  159. * Similar to read_some, but continues to read until all bytes in the
  160. * supplied buffer have been read or the connection runs out of read
  161. * requests.
  162. *
  163. * This method still may not read all of the bytes in the input buffer. if
  164. * it doesn't it indicates that the connection was most likely closed or
  165. * is in an error state where it is no longer accepting new input.
  166. *
  167. * @since 0.3.0
  168. *
  169. * @param buf Char buffer to read into the websocket
  170. * @param len Length of buf
  171. * @return The number of characters from buf actually read.
  172. */
  173. size_t read_all(const char * buf, size_t len)
  174. {
  175. // this serializes calls to external read.
  176. scoped_lock_type lock(m_read_mutex);
  177. size_t total_read = 0;
  178. size_t temp_read = 0;
  179. do
  180. {
  181. temp_read = this->read_some_impl(buf + total_read, len - total_read);
  182. total_read += temp_read;
  183. }
  184. while (temp_read != 0 && total_read < len);
  185. return total_read;
  186. }
  187. /// Manual input supply (DEPRECATED)
  188. /**
  189. * @deprecated DEPRECATED in favor of read_some()
  190. * @see read_some()
  191. */
  192. size_t readsome(const char * buf, size_t len)
  193. {
  194. return this->read_some(buf, len);
  195. }
  196. /// Signal EOF
  197. /**
  198. * Signals to the transport that data stream being read has reached EOF and
  199. * that no more bytes may be read or written to/from the transport.
  200. *
  201. * @since 0.3.0-alpha4
  202. */
  203. void eof()
  204. {
  205. // this serializes calls to external read.
  206. scoped_lock_type lock(m_read_mutex);
  207. if (m_reading)
  208. {
  209. complete_read(make_error_code(transport::error::eof));
  210. }
  211. }
  212. /// Signal transport error
  213. /**
  214. * Signals to the transport that a fatal data stream error has occurred and
  215. * that no more bytes may be read or written to/from the transport.
  216. *
  217. * @since 0.3.0-alpha4
  218. */
  219. void fatal_error()
  220. {
  221. // this serializes calls to external read.
  222. scoped_lock_type lock(m_read_mutex);
  223. if (m_reading)
  224. {
  225. complete_read(make_error_code(transport::error::pass_through));
  226. }
  227. }
  228. /// Set whether or not this connection is secure
  229. /**
  230. * The iostream transport does not provide any security features. As such
  231. * it defaults to returning false when `is_secure` is called. However, the
  232. * iostream transport may be used to wrap an external socket API that may
  233. * provide secure transport. This method allows that external API to flag
  234. * whether or not this connection is secure so that users of the WebSocket++
  235. * API will get more accurate information.
  236. *
  237. * @since 0.3.0-alpha4
  238. *
  239. * @param value Whether or not this connection is secure.
  240. */
  241. void set_secure(bool value)
  242. {
  243. m_is_secure = value;
  244. }
  245. /// Tests whether or not the underlying transport is secure
  246. /**
  247. * iostream transport will return false always because it has no information
  248. * about the ultimate remote endpoint. This may or may not be accurate
  249. * depending on the real source of bytes being input. The `set_secure`
  250. * method may be used to flag connections that are secured by an external
  251. * API
  252. *
  253. * @return Whether or not the underlying transport is secure
  254. */
  255. bool is_secure() const
  256. {
  257. return m_is_secure;
  258. }
  259. /// Set human readable remote endpoint address
  260. /**
  261. * Sets the remote endpoint address returned by `get_remote_endpoint`. This
  262. * value should be a human readable string that describes the remote
  263. * endpoint. Typically an IP address or hostname, perhaps with a port. But
  264. * may be something else depending on the nature of the underlying
  265. * transport.
  266. *
  267. * If none is set the default is "iostream transport".
  268. *
  269. * @since 0.3.0-alpha4
  270. *
  271. * @param value The remote endpoint address to set.
  272. */
  273. void set_remote_endpoint(std::string value)
  274. {
  275. m_remote_endpoint = value;
  276. }
  277. /// Get human readable remote endpoint address
  278. /**
  279. * The iostream transport has no information about the ultimate remote
  280. * endpoint. It will return the string "iostream transport". The
  281. * `set_remote_endpoint` method may be used by external network code to set
  282. * a more accurate value.
  283. *
  284. * This value is used in access and error logs and is available to the end
  285. * application for including in user facing interfaces and messages.
  286. *
  287. * @return A string identifying the address of the remote endpoint
  288. */
  289. std::string get_remote_endpoint() const
  290. {
  291. return m_remote_endpoint;
  292. }
  293. /// Get the connection handle
  294. /**
  295. * @return The handle for this connection.
  296. */
  297. connection_hdl get_handle() const
  298. {
  299. return m_connection_hdl;
  300. }
  301. /// Call back a function after a period of time.
  302. /**
  303. * Timers are not implemented in this transport. The timer pointer will
  304. * always be empty. The handler will never be called.
  305. *
  306. * @param duration Length of time to wait in milliseconds
  307. * @param callback The function to call back when the timer has expired
  308. * @return A handle that can be used to cancel the timer if it is no longer
  309. * needed.
  310. */
  311. timer_ptr set_timer(long, timer_handler)
  312. {
  313. return timer_ptr();
  314. }
  315. /// Sets the write handler
  316. /**
  317. * The write handler is called when the iostream transport receives data
  318. * that needs to be written to the appropriate output location. This handler
  319. * can be used in place of registering an ostream for output.
  320. *
  321. * The signature of the handler is
  322. * `lib::error_code (connection_hdl, char const *, size_t)` The
  323. * code returned will be reported and logged by the core library.
  324. *
  325. * See also, set_vector_write_handler, for an optional write handler that
  326. * allows more efficient handling of multiple writes at once.
  327. *
  328. * @see set_vector_write_handler
  329. *
  330. * @since 0.5.0
  331. *
  332. * @param h The handler to call when data is to be written.
  333. */
  334. void set_write_handler(write_handler h)
  335. {
  336. m_write_handler = h;
  337. }
  338. /// Sets the vectored write handler
  339. /**
  340. * The vectored write handler is called when the iostream transport receives
  341. * multiple chunks of data that need to be written to the appropriate output
  342. * location. This handler can be used in conjunction with the write_handler
  343. * in place of registering an ostream for output.
  344. *
  345. * The sequence of buffers represents bytes that should be written
  346. * consecutively and it is suggested to group the buffers into as few next
  347. * layer packets as possible. Vector write is used to allow implementations
  348. * that support it to coalesce writes into a single TCP packet or TLS
  349. * segment for improved efficiency.
  350. *
  351. * This is an optional handler. If it is not defined then multiple calls
  352. * will be made to the standard write handler.
  353. *
  354. * The signature of the handler is
  355. * `lib::error_code (connection_hdl, std::vector<websocketpp::transport::buffer>
  356. * const & bufs)`. The code returned will be reported and logged by the core
  357. * library. The `websocketpp::transport::buffer` type is a struct with two
  358. * data members. buf (char const *) and len (size_t).
  359. *
  360. * @since 0.6.0
  361. *
  362. * @param h The handler to call when vectored data is to be written.
  363. */
  364. void set_vector_write_handler(vector_write_handler h)
  365. {
  366. m_vector_write_handler = h;
  367. }
  368. /// Sets the shutdown handler
  369. /**
  370. * The shutdown handler is called when the iostream transport receives a
  371. * notification from the core library that it is finished with all read and
  372. * write operations and that the underlying transport can be cleaned up.
  373. *
  374. * If you are using iostream transport with another socket library, this is
  375. * a good time to close/shutdown the socket for this connection.
  376. *
  377. * The signature of the handler is `lib::error_code (connection_hdl)`. The
  378. * code returned will be reported and logged by the core library.
  379. *
  380. * @since 0.5.0
  381. *
  382. * @param h The handler to call on connection shutdown.
  383. */
  384. void set_shutdown_handler(shutdown_handler h)
  385. {
  386. m_shutdown_handler = h;
  387. }
  388. protected:
  389. /// Initialize the connection transport
  390. /**
  391. * Initialize the connection's transport component.
  392. *
  393. * @param handler The `init_handler` to call when initialization is done
  394. */
  395. void init(init_handler handler)
  396. {
  397. m_alog->write(log::alevel::devel, "iostream connection init");
  398. handler(lib::error_code());
  399. }
  400. /// Initiate an async_read for at least num_bytes bytes into buf
  401. /**
  402. * Initiates an async_read request for at least num_bytes bytes. The input
  403. * will be read into buf. A maximum of len bytes will be input. When the
  404. * operation is complete, handler will be called with the status and number
  405. * of bytes read.
  406. *
  407. * This method may or may not call handler from within the initial call. The
  408. * application should be prepared to accept either.
  409. *
  410. * The application should never call this method a second time before it has
  411. * been called back for the first read. If this is done, the second read
  412. * will be called back immediately with a double_read error.
  413. *
  414. * If num_bytes or len are zero handler will be called back immediately
  415. * indicating success.
  416. *
  417. * @param num_bytes Don't call handler until at least this many bytes have
  418. * been read.
  419. * @param buf The buffer to read bytes into
  420. * @param len The size of buf. At maximum, this many bytes will be read.
  421. * @param handler The callback to invoke when the operation is complete or
  422. * ends in an error
  423. */
  424. void async_read_at_least(size_t num_bytes, char * buf, size_t len, read_handler handler)
  425. {
  426. std::stringstream s;
  427. s << "iostream_con async_read_at_least: " << num_bytes;
  428. m_alog->write(log::alevel::devel, s.str());
  429. if (num_bytes > len)
  430. {
  431. handler(make_error_code(error::invalid_num_bytes), size_t(0));
  432. return;
  433. }
  434. if (m_reading == true)
  435. {
  436. handler(make_error_code(error::double_read), size_t(0));
  437. return;
  438. }
  439. if (num_bytes == 0 || len == 0)
  440. {
  441. handler(lib::error_code(), size_t(0));
  442. return;
  443. }
  444. m_buf = buf;
  445. m_len = len;
  446. m_bytes_needed = num_bytes;
  447. m_read_handler = handler;
  448. m_cursor = 0;
  449. m_reading = true;
  450. }
  451. /// Asyncronous Transport Write
  452. /**
  453. * Write len bytes in buf to the output method. Call handler to report
  454. * success or failure. handler may or may not be called during async_write,
  455. * but it must be safe for this to happen.
  456. *
  457. * Will return 0 on success. Other possible errors (not exhaustive)
  458. * output_stream_required: No output stream was registered to write to
  459. * bad_stream: a ostream pass through error
  460. *
  461. * This method will attempt to write to the registered ostream first. If an
  462. * ostream is not registered it will use the write handler. If neither are
  463. * registered then an error is passed up to the connection.
  464. *
  465. * @param buf buffer to read bytes from
  466. * @param len number of bytes to write
  467. * @param handler Callback to invoke with operation status.
  468. */
  469. void async_write(const char * buf, size_t len, transport::write_handler handler)
  470. {
  471. m_alog->write(log::alevel::devel, "iostream_con async_write");
  472. // TODO: lock transport state?
  473. lib::error_code ec;
  474. if (m_output_stream)
  475. {
  476. m_output_stream->write(buf, len);
  477. if (m_output_stream->bad())
  478. {
  479. ec = make_error_code(error::bad_stream);
  480. }
  481. }
  482. else if (m_write_handler)
  483. {
  484. ec = m_write_handler(m_connection_hdl, buf, len);
  485. }
  486. else
  487. {
  488. ec = make_error_code(error::output_stream_required);
  489. }
  490. handler(ec);
  491. }
  492. /// Asyncronous Transport Write (scatter-gather)
  493. /**
  494. * Write a sequence of buffers to the output method. Call handler to report
  495. * success or failure. handler may or may not be called during async_write,
  496. * but it must be safe for this to happen.
  497. *
  498. * Will return 0 on success. Other possible errors (not exhaustive)
  499. * output_stream_required: No output stream was registered to write to
  500. * bad_stream: a ostream pass through error
  501. *
  502. * This method will attempt to write to the registered ostream first. If an
  503. * ostream is not registered it will use the write handler. If neither are
  504. * registered then an error is passed up to the connection.
  505. *
  506. * @param bufs vector of buffers to write
  507. * @param handler Callback to invoke with operation status.
  508. */
  509. void async_write(const std::vector<buffer> & bufs, transport::write_handler handler)
  510. {
  511. m_alog->write(log::alevel::devel, "iostream_con async_write buffer list");
  512. // TODO: lock transport state?
  513. lib::error_code ec;
  514. if (m_output_stream)
  515. {
  516. std::vector<buffer>::const_iterator it;
  517. for (it = bufs.begin(); it != bufs.end(); it++)
  518. {
  519. m_output_stream->write((*it).buf, (*it).len);
  520. if (m_output_stream->bad())
  521. {
  522. ec = make_error_code(error::bad_stream);
  523. break;
  524. }
  525. }
  526. }
  527. else if (m_vector_write_handler)
  528. {
  529. ec = m_vector_write_handler(m_connection_hdl, bufs);
  530. }
  531. else if (m_write_handler)
  532. {
  533. std::vector<buffer>::const_iterator it;
  534. for (it = bufs.begin(); it != bufs.end(); it++)
  535. {
  536. ec = m_write_handler(m_connection_hdl, (*it).buf, (*it).len);
  537. if (ec)
  538. {
  539. break;
  540. }
  541. }
  542. }
  543. else
  544. {
  545. ec = make_error_code(error::output_stream_required);
  546. }
  547. handler(ec);
  548. }
  549. /// Set Connection Handle
  550. /**
  551. * @param hdl The new handle
  552. */
  553. void set_handle(connection_hdl hdl)
  554. {
  555. m_connection_hdl = hdl;
  556. }
  557. /// Call given handler back within the transport's event system (if present)
  558. /**
  559. * Invoke a callback within the transport's event system if it has one. If
  560. * it doesn't, the handler will be invoked immediately before this function
  561. * returns.
  562. *
  563. * @param handler The callback to invoke
  564. *
  565. * @return Whether or not the transport was able to register the handler for
  566. * callback.
  567. */
  568. lib::error_code dispatch(dispatch_handler handler)
  569. {
  570. handler();
  571. return lib::error_code();
  572. }
  573. /// Perform cleanup on socket shutdown_handler
  574. /**
  575. * If a shutdown handler is set, call it and pass through its return error
  576. * code. Otherwise assume there is nothing to do and pass through a success
  577. * code.
  578. *
  579. * @param handler The `shutdown_handler` to call back when complete
  580. */
  581. void async_shutdown(transport::shutdown_handler handler)
  582. {
  583. lib::error_code ec;
  584. if (m_shutdown_handler)
  585. {
  586. ec = m_shutdown_handler(m_connection_hdl);
  587. }
  588. handler(ec);
  589. }
  590. private:
  591. void read(std::istream & in)
  592. {
  593. m_alog->write(log::alevel::devel, "iostream_con read");
  594. while (in.good())
  595. {
  596. if (! m_reading)
  597. {
  598. m_elog->write(log::elevel::devel, "write while not reading");
  599. break;
  600. }
  601. in.read(m_buf + m_cursor, static_cast<std::streamsize>(m_len - m_cursor));
  602. if (in.gcount() == 0)
  603. {
  604. m_elog->write(log::elevel::devel, "read zero bytes");
  605. break;
  606. }
  607. m_cursor += static_cast<size_t>(in.gcount());
  608. // TODO: error handling
  609. if (in.bad())
  610. {
  611. m_reading = false;
  612. complete_read(make_error_code(error::bad_stream));
  613. }
  614. if (m_cursor >= m_bytes_needed)
  615. {
  616. m_reading = false;
  617. complete_read(lib::error_code());
  618. }
  619. }
  620. }
  621. size_t read_some_impl(const char * buf, size_t len)
  622. {
  623. m_alog->write(log::alevel::devel, "iostream_con read_some");
  624. if (! m_reading)
  625. {
  626. m_elog->write(log::elevel::devel, "write while not reading");
  627. return 0;
  628. }
  629. size_t bytes_to_copy = (std::min) (len, m_len - m_cursor);
  630. std::copy(buf, buf + bytes_to_copy, m_buf + m_cursor);
  631. m_cursor += bytes_to_copy;
  632. if (m_cursor >= m_bytes_needed)
  633. {
  634. complete_read(lib::error_code());
  635. }
  636. return bytes_to_copy;
  637. }
  638. /// Signal that a requested read is complete
  639. /**
  640. * Sets the reading flag to false and returns the handler that should be
  641. * called back with the result of the read. The cursor position that is sent
  642. * is whatever the value of m_cursor is.
  643. *
  644. * It MUST NOT be called when m_reading is false.
  645. * it MUST be called while holding the read lock
  646. *
  647. * It is important to use this method rather than directly setting/calling
  648. * m_read_handler back because this function makes sure to delete the
  649. * locally stored handler which contains shared pointers that will otherwise
  650. * cause circular reference based memory leaks.
  651. *
  652. * @param ec The error code to forward to the read handler
  653. */
  654. void complete_read(const lib::error_code & ec)
  655. {
  656. m_reading = false;
  657. read_handler handler = m_read_handler;
  658. m_read_handler = read_handler();
  659. handler(ec, m_cursor);
  660. }
  661. // Read space (Protected by m_read_mutex)
  662. char * m_buf;
  663. size_t m_len;
  664. size_t m_bytes_needed;
  665. read_handler m_read_handler;
  666. size_t m_cursor;
  667. // transport resources
  668. std::ostream * m_output_stream;
  669. connection_hdl m_connection_hdl;
  670. write_handler m_write_handler;
  671. vector_write_handler m_vector_write_handler;
  672. shutdown_handler m_shutdown_handler;
  673. bool m_reading;
  674. const bool m_is_server;
  675. bool m_is_secure;
  676. lib::shared_ptr<alog_type> m_alog;
  677. lib::shared_ptr<elog_type> m_elog;
  678. std::string m_remote_endpoint;
  679. // This lock ensures that only one thread can edit read data for this
  680. // connection. This is a very coarse lock that is basically locked all the
  681. // time. The nature of the connection is such that it cannot be
  682. // parallelized, the locking is here to prevent intra-connection concurrency
  683. // in order to allow inter-connection concurrency.
  684. mutex_type m_read_mutex;
  685. };
  686. } // namespace iostream
  687. } // namespace transport
  688. } // namespace websocketpp
  689. #endif // WEBSOCKETPP_TRANSPORT_IOSTREAM_CON_HPP