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.

300 lines
10 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_STUB_CON_HPP
  28. #define WEBSOCKETPP_TRANSPORT_STUB_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/stub/base.hpp>
  37. namespace websocketpp
  38. {
  39. namespace transport
  40. {
  41. namespace stub
  42. {
  43. /// Empty timer class to stub out for timer functionality that stub
  44. /// transport doesn't support
  45. struct timer
  46. {
  47. void cancel() {}
  48. };
  49. template <typename config> class connection : public lib::enable_shared_from_this<connection<config>>
  50. {
  51. public:
  52. /// Type of this connection transport component
  53. typedef connection<config> type;
  54. /// Type of a shared pointer to this connection transport component
  55. typedef lib::shared_ptr<type> ptr;
  56. /// transport concurrency policy
  57. typedef typename config::concurrency_type concurrency_type;
  58. /// Type of this transport's access logging policy
  59. typedef typename config::alog_type alog_type;
  60. /// Type of this transport's error logging policy
  61. typedef typename config::elog_type elog_type;
  62. // Concurrency policy types
  63. typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
  64. typedef typename concurrency_type::mutex_type mutex_type;
  65. typedef lib::shared_ptr<timer> timer_ptr;
  66. explicit connection(bool is_server, const lib::shared_ptr<alog_type> & alog, const lib::shared_ptr<elog_type> & elog) :
  67. m_alog(alog),
  68. m_elog(elog)
  69. {
  70. m_alog->write(log::alevel::devel, "stub con transport constructor");
  71. }
  72. /// Get a shared pointer to this component
  73. ptr get_shared()
  74. {
  75. return type::shared_from_this();
  76. }
  77. /// Set whether or not this connection is secure
  78. /**
  79. * Todo: docs
  80. *
  81. * @since 0.3.0-alpha4
  82. *
  83. * @param value Whether or not this connection is secure.
  84. */
  85. void set_secure(bool value) {}
  86. /// Tests whether or not the underlying transport is secure
  87. /**
  88. * TODO: docs
  89. *
  90. * @return Whether or not the underlying transport is secure
  91. */
  92. bool is_secure() const
  93. {
  94. return false;
  95. }
  96. /// Set uri hook
  97. /**
  98. * Called by the endpoint as a connection is being established to provide
  99. * the uri being connected to to the transport layer.
  100. *
  101. * Implementation is optional and can be ignored if the transport has no
  102. * need for this information.
  103. *
  104. * @since 0.6.0
  105. *
  106. * @param u The uri to set
  107. */
  108. void set_uri(uri_ptr) {}
  109. /// Set human readable remote endpoint address
  110. /**
  111. * Sets the remote endpoint address returned by `get_remote_endpoint`. This
  112. * value should be a human readable string that describes the remote
  113. * endpoint. Typically an IP address or hostname, perhaps with a port. But
  114. * may be something else depending on the nature of the underlying
  115. * transport.
  116. *
  117. * If none is set a default is returned.
  118. *
  119. * @since 0.3.0-alpha4
  120. *
  121. * @param value The remote endpoint address to set.
  122. */
  123. void set_remote_endpoint(std::string value) {}
  124. /// Get human readable remote endpoint address
  125. /**
  126. * TODO: docs
  127. *
  128. * This value is used in access and error logs and is available to the end
  129. * application for including in user facing interfaces and messages.
  130. *
  131. * @return A string identifying the address of the remote endpoint
  132. */
  133. std::string get_remote_endpoint() const
  134. {
  135. return "unknown (stub transport)";
  136. }
  137. /// Get the connection handle
  138. /**
  139. * @return The handle for this connection.
  140. */
  141. connection_hdl get_handle() const
  142. {
  143. return connection_hdl();
  144. }
  145. /// Call back a function after a period of time.
  146. /**
  147. * Timers are not implemented in this transport. The timer pointer will
  148. * always be empty. The handler will never be called.
  149. *
  150. * @param duration Length of time to wait in milliseconds
  151. * @param callback The function to call back when the timer has expired
  152. * @return A handle that can be used to cancel the timer if it is no longer
  153. * needed.
  154. */
  155. timer_ptr set_timer(long duration, timer_handler handler)
  156. {
  157. return timer_ptr();
  158. }
  159. protected:
  160. /// Initialize the connection transport
  161. /**
  162. * Initialize the connection's transport component.
  163. *
  164. * @param handler The `init_handler` to call when initialization is done
  165. */
  166. void init(init_handler handler)
  167. {
  168. m_alog->write(log::alevel::devel, "stub connection init");
  169. handler(make_error_code(error::not_implemented));
  170. }
  171. /// Initiate an async_read for at least num_bytes bytes into buf
  172. /**
  173. * Initiates an async_read request for at least num_bytes bytes. The input
  174. * will be read into buf. A maximum of len bytes will be input. When the
  175. * operation is complete, handler will be called with the status and number
  176. * of bytes read.
  177. *
  178. * This method may or may not call handler from within the initial call. The
  179. * application should be prepared to accept either.
  180. *
  181. * The application should never call this method a second time before it has
  182. * been called back for the first read. If this is done, the second read
  183. * will be called back immediately with a double_read error.
  184. *
  185. * If num_bytes or len are zero handler will be called back immediately
  186. * indicating success.
  187. *
  188. * @param num_bytes Don't call handler until at least this many bytes have
  189. * been read.
  190. * @param buf The buffer to read bytes into
  191. * @param len The size of buf. At maximum, this many bytes will be read.
  192. * @param handler The callback to invoke when the operation is complete or
  193. * ends in an error
  194. */
  195. void async_read_at_least(size_t num_bytes, char * buf, size_t len, read_handler handler)
  196. {
  197. m_alog->write(log::alevel::devel, "stub_con async_read_at_least");
  198. handler(make_error_code(error::not_implemented), 0);
  199. }
  200. /// Asyncronous Transport Write
  201. /**
  202. * Write len bytes in buf to the output stream. Call handler to report
  203. * success or failure. handler may or may not be called during async_write,
  204. * but it must be safe for this to happen.
  205. *
  206. * Will return 0 on success.
  207. *
  208. * @param buf buffer to read bytes from
  209. * @param len number of bytes to write
  210. * @param handler Callback to invoke with operation status.
  211. */
  212. void async_write(const char * buf, size_t len, write_handler handler)
  213. {
  214. m_alog->write(log::alevel::devel, "stub_con async_write");
  215. handler(make_error_code(error::not_implemented));
  216. }
  217. /// Asyncronous Transport Write (scatter-gather)
  218. /**
  219. * Write a sequence of buffers to the output stream. Call handler to report
  220. * success or failure. handler may or may not be called during async_write,
  221. * but it must be safe for this to happen.
  222. *
  223. * Will return 0 on success.
  224. *
  225. * @param bufs vector of buffers to write
  226. * @param handler Callback to invoke with operation status.
  227. */
  228. void async_write(const std::vector<buffer> & bufs, write_handler handler)
  229. {
  230. m_alog->write(log::alevel::devel, "stub_con async_write buffer list");
  231. handler(make_error_code(error::not_implemented));
  232. }
  233. /// Set Connection Handle
  234. /**
  235. * @param hdl The new handle
  236. */
  237. void set_handle(connection_hdl hdl) {}
  238. /// Call given handler back within the transport's event system (if present)
  239. /**
  240. * Invoke a callback within the transport's event system if it has one. If
  241. * it doesn't, the handler will be invoked immediately before this function
  242. * returns.
  243. *
  244. * @param handler The callback to invoke
  245. *
  246. * @return Whether or not the transport was able to register the handler for
  247. * callback.
  248. */
  249. lib::error_code dispatch(dispatch_handler handler)
  250. {
  251. handler();
  252. return lib::error_code();
  253. }
  254. /// Perform cleanup on socket shutdown_handler
  255. /**
  256. * @param h The `shutdown_handler` to call back when complete
  257. */
  258. void async_shutdown(shutdown_handler handler)
  259. {
  260. handler(lib::error_code());
  261. }
  262. private:
  263. // member variables!
  264. lib::shared_ptr<alog_type> m_alog;
  265. lib::shared_ptr<elog_type> m_elog;
  266. };
  267. } // namespace stub
  268. } // namespace transport
  269. } // namespace websocketpp
  270. #endif // WEBSOCKETPP_TRANSPORT_STUB_CON_HPP