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.

440 lines
14 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_DEBUG_CON_HPP
  28. #define WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP
  29. #include <string>
  30. #include <vector>
  31. #include <websocketpp/common/connection_hdl.hpp>
  32. #include <websocketpp/common/memory.hpp>
  33. #include <websocketpp/common/platforms.hpp>
  34. #include <websocketpp/logger/levels.hpp>
  35. #include <websocketpp/transport/base/connection.hpp>
  36. #include <websocketpp/transport/debug/base.hpp>
  37. #include <websocketpp/uri.hpp>
  38. namespace websocketpp
  39. {
  40. namespace transport
  41. {
  42. namespace debug
  43. {
  44. /// Empty timer class to stub out for timer functionality that stub
  45. /// transport doesn't support
  46. struct timer
  47. {
  48. void cancel() {}
  49. };
  50. template <typename config> class connection : public lib::enable_shared_from_this<connection<config>>
  51. {
  52. public:
  53. /// Type of this connection transport component
  54. typedef connection<config> type;
  55. /// Type of a shared pointer to this connection transport component
  56. typedef lib::shared_ptr<type> ptr;
  57. /// transport concurrency policy
  58. typedef typename config::concurrency_type concurrency_type;
  59. /// Type of this transport's access logging policy
  60. typedef typename config::alog_type alog_type;
  61. /// Type of this transport's error logging policy
  62. typedef typename config::elog_type elog_type;
  63. // Concurrency policy types
  64. typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
  65. typedef typename concurrency_type::mutex_type mutex_type;
  66. typedef lib::shared_ptr<timer> timer_ptr;
  67. explicit connection(bool is_server, const lib::shared_ptr<alog_type> & alog, const lib::shared_ptr<elog_type> & elog) :
  68. m_reading(false),
  69. m_is_server(is_server),
  70. m_alog(alog),
  71. m_elog(elog)
  72. {
  73. m_alog->write(log::alevel::devel, "debug con transport constructor");
  74. }
  75. /// Get a shared pointer to this component
  76. ptr get_shared()
  77. {
  78. return type::shared_from_this();
  79. }
  80. /// Set whether or not this connection is secure
  81. /**
  82. * Todo: docs
  83. *
  84. * @since 0.3.0-alpha4
  85. *
  86. * @param value Whether or not this connection is secure.
  87. */
  88. void set_secure(bool) {}
  89. /// Tests whether or not the underlying transport is secure
  90. /**
  91. * TODO: docs
  92. *
  93. * @return Whether or not the underlying transport is secure
  94. */
  95. bool is_secure() const
  96. {
  97. return false;
  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. * Implementation is optional and can be ignored if the transport has no
  105. * need for this information.
  106. *
  107. * @since 0.6.0
  108. *
  109. * @param u The uri to set
  110. */
  111. void set_uri(uri_ptr) {}
  112. /// Set human readable remote endpoint address
  113. /**
  114. * Sets the remote endpoint address returned by `get_remote_endpoint`. This
  115. * value should be a human readable string that describes the remote
  116. * endpoint. Typically an IP address or hostname, perhaps with a port. But
  117. * may be something else depending on the nature of the underlying
  118. * transport.
  119. *
  120. * If none is set a default is returned.
  121. *
  122. * @since 0.3.0-alpha4
  123. *
  124. * @param value The remote endpoint address to set.
  125. */
  126. void set_remote_endpoint(std::string) {}
  127. /// Get human readable remote endpoint address
  128. /**
  129. * TODO: docs
  130. *
  131. * This value is used in access and error logs and is available to the end
  132. * application for including in user facing interfaces and messages.
  133. *
  134. * @return A string identifying the address of the remote endpoint
  135. */
  136. std::string get_remote_endpoint() const
  137. {
  138. return "unknown (debug transport)";
  139. }
  140. /// Get the connection handle
  141. /**
  142. * @return The handle for this connection.
  143. */
  144. connection_hdl get_handle() const
  145. {
  146. return connection_hdl();
  147. }
  148. /// Call back a function after a period of time.
  149. /**
  150. * Timers are not implemented in this transport. The timer pointer will
  151. * always be empty. The handler will never be called.
  152. *
  153. * @param duration Length of time to wait in milliseconds
  154. * @param callback The function to call back when the timer has expired
  155. * @return A handle that can be used to cancel the timer if it is no longer
  156. * needed.
  157. */
  158. timer_ptr set_timer(long, timer_handler handler)
  159. {
  160. m_alog->write(log::alevel::devel, "debug connection set timer");
  161. m_timer_handler = handler;
  162. return timer_ptr();
  163. }
  164. /// Manual input supply (read all)
  165. /**
  166. * Similar to read_some, but continues to read until all bytes in the
  167. * supplied buffer have been read or the connection runs out of read
  168. * requests.
  169. *
  170. * This method still may not read all of the bytes in the input buffer. if
  171. * it doesn't it indicates that the connection was most likely closed or
  172. * is in an error state where it is no longer accepting new input.
  173. *
  174. * @since 0.3.0
  175. *
  176. * @param buf Char buffer to read into the websocket
  177. * @param len Length of buf
  178. * @return The number of characters from buf actually read.
  179. */
  180. size_t read_all(const char * buf, size_t len)
  181. {
  182. size_t total_read = 0;
  183. size_t temp_read = 0;
  184. do
  185. {
  186. temp_read = this->read_some_impl(buf + total_read, len - total_read);
  187. total_read += temp_read;
  188. }
  189. while (temp_read != 0 && total_read < len);
  190. return total_read;
  191. }
  192. // debug stuff to invoke the async handlers
  193. void expire_timer(const lib::error_code & ec)
  194. {
  195. m_timer_handler(ec);
  196. }
  197. void fullfil_write()
  198. {
  199. m_write_handler(lib::error_code());
  200. }
  201. protected:
  202. /// Initialize the connection transport
  203. /**
  204. * Initialize the connection's transport component.
  205. *
  206. * @param handler The `init_handler` to call when initialization is done
  207. */
  208. void init(init_handler handler)
  209. {
  210. m_alog->write(log::alevel::devel, "debug connection init");
  211. handler(lib::error_code());
  212. }
  213. /// Initiate an async_read for at least num_bytes bytes into buf
  214. /**
  215. * Initiates an async_read request for at least num_bytes bytes. The input
  216. * will be read into buf. A maximum of len bytes will be input. When the
  217. * operation is complete, handler will be called with the status and number
  218. * of bytes read.
  219. *
  220. * This method may or may not call handler from within the initial call. The
  221. * application should be prepared to accept either.
  222. *
  223. * The application should never call this method a second time before it has
  224. * been called back for the first read. If this is done, the second read
  225. * will be called back immediately with a double_read error.
  226. *
  227. * If num_bytes or len are zero handler will be called back immediately
  228. * indicating success.
  229. *
  230. * @param num_bytes Don't call handler until at least this many bytes have
  231. * been read.
  232. * @param buf The buffer to read bytes into
  233. * @param len The size of buf. At maximum, this many bytes will be read.
  234. * @param handler The callback to invoke when the operation is complete or
  235. * ends in an error
  236. */
  237. void async_read_at_least(size_t num_bytes, char * buf, size_t len, read_handler handler)
  238. {
  239. std::stringstream s;
  240. s << "debug_con async_read_at_least: " << num_bytes;
  241. m_alog->write(log::alevel::devel, s.str());
  242. if (num_bytes > len)
  243. {
  244. handler(make_error_code(error::invalid_num_bytes), size_t(0));
  245. return;
  246. }
  247. if (m_reading == true)
  248. {
  249. handler(make_error_code(error::double_read), size_t(0));
  250. return;
  251. }
  252. if (num_bytes == 0 || len == 0)
  253. {
  254. handler(lib::error_code(), size_t(0));
  255. return;
  256. }
  257. m_buf = buf;
  258. m_len = len;
  259. m_bytes_needed = num_bytes;
  260. m_read_handler = handler;
  261. m_cursor = 0;
  262. m_reading = true;
  263. }
  264. /// Asyncronous Transport Write
  265. /**
  266. * Write len bytes in buf to the output stream. Call handler to report
  267. * success or failure. handler may or may not be called during async_write,
  268. * but it must be safe for this to happen.
  269. *
  270. * Will return 0 on success.
  271. *
  272. * @param buf buffer to read bytes from
  273. * @param len number of bytes to write
  274. * @param handler Callback to invoke with operation status.
  275. */
  276. void async_write(const char *, size_t, write_handler handler)
  277. {
  278. m_alog->write(log::alevel::devel, "debug_con async_write");
  279. m_write_handler = handler;
  280. }
  281. /// Asyncronous Transport Write (scatter-gather)
  282. /**
  283. * Write a sequence of buffers to the output stream. Call handler to report
  284. * success or failure. handler may or may not be called during async_write,
  285. * but it must be safe for this to happen.
  286. *
  287. * Will return 0 on success.
  288. *
  289. * @param bufs vector of buffers to write
  290. * @param handler Callback to invoke with operation status.
  291. */
  292. void async_write(const std::vector<buffer> &, write_handler handler)
  293. {
  294. m_alog->write(log::alevel::devel, "debug_con async_write buffer list");
  295. m_write_handler = handler;
  296. }
  297. /// Set Connection Handle
  298. /**
  299. * @param hdl The new handle
  300. */
  301. void set_handle(connection_hdl) {}
  302. /// Call given handler back within the transport's event system (if present)
  303. /**
  304. * Invoke a callback within the transport's event system if it has one. If
  305. * it doesn't, the handler will be invoked immediately before this function
  306. * returns.
  307. *
  308. * @param handler The callback to invoke
  309. *
  310. * @return Whether or not the transport was able to register the handler for
  311. * callback.
  312. */
  313. lib::error_code dispatch(dispatch_handler handler)
  314. {
  315. handler();
  316. return lib::error_code();
  317. }
  318. /// Perform cleanup on socket shutdown_handler
  319. /**
  320. * @param h The `shutdown_handler` to call back when complete
  321. */
  322. void async_shutdown(shutdown_handler handler)
  323. {
  324. handler(lib::error_code());
  325. }
  326. size_t read_some_impl(const char * buf, size_t len)
  327. {
  328. m_alog->write(log::alevel::devel, "debug_con read_some");
  329. if (! m_reading)
  330. {
  331. m_elog->write(log::elevel::devel, "write while not reading");
  332. return 0;
  333. }
  334. size_t bytes_to_copy = (std::min) (len, m_len - m_cursor);
  335. std::copy(buf, buf + bytes_to_copy, m_buf + m_cursor);
  336. m_cursor += bytes_to_copy;
  337. if (m_cursor >= m_bytes_needed)
  338. {
  339. complete_read(lib::error_code());
  340. }
  341. return bytes_to_copy;
  342. }
  343. /// Signal that a requested read is complete
  344. /**
  345. * Sets the reading flag to false and returns the handler that should be
  346. * called back with the result of the read. The cursor position that is sent
  347. * is whatever the value of m_cursor is.
  348. *
  349. * It MUST NOT be called when m_reading is false.
  350. * it MUST be called while holding the read lock
  351. *
  352. * It is important to use this method rather than directly setting/calling
  353. * m_read_handler back because this function makes sure to delete the
  354. * locally stored handler which contains shared pointers that will otherwise
  355. * cause circular reference based memory leaks.
  356. *
  357. * @param ec The error code to forward to the read handler
  358. */
  359. void complete_read(const lib::error_code & ec)
  360. {
  361. m_reading = false;
  362. read_handler handler = m_read_handler;
  363. m_read_handler = read_handler();
  364. handler(ec, m_cursor);
  365. }
  366. private:
  367. timer_handler m_timer_handler;
  368. // Read space (Protected by m_read_mutex)
  369. char * m_buf;
  370. size_t m_len;
  371. size_t m_bytes_needed;
  372. read_handler m_read_handler;
  373. size_t m_cursor;
  374. // transport resources
  375. connection_hdl m_connection_hdl;
  376. write_handler m_write_handler;
  377. shutdown_handler m_shutdown_handler;
  378. bool m_reading;
  379. const bool m_is_server;
  380. bool m_is_secure;
  381. lib::shared_ptr<alog_type> m_alog;
  382. lib::shared_ptr<elog_type> m_elog;
  383. std::string m_remote_endpoint;
  384. };
  385. } // namespace debug
  386. } // namespace transport
  387. } // namespace websocketpp
  388. #endif // WEBSOCKETPP_TRANSPORT_DEBUG_CON_HPP