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.

238 lines
8.8 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_HPP
  28. #define WEBSOCKETPP_TRANSPORT_IOSTREAM_HPP
  29. #include <ostream>
  30. #include <websocketpp/common/memory.hpp>
  31. #include <websocketpp/logger/levels.hpp>
  32. #include <websocketpp/transport/base/endpoint.hpp>
  33. #include <websocketpp/transport/iostream/connection.hpp>
  34. #include <websocketpp/uri.hpp>
  35. namespace websocketpp
  36. {
  37. namespace transport
  38. {
  39. namespace iostream
  40. {
  41. template <typename config> class endpoint
  42. {
  43. public:
  44. /// Type of this endpoint transport component
  45. typedef endpoint type;
  46. /// Type of a pointer to this endpoint transport component
  47. typedef lib::shared_ptr<type> ptr;
  48. /// Type of this endpoint's concurrency policy
  49. typedef typename config::concurrency_type concurrency_type;
  50. /// Type of this endpoint's error logging policy
  51. typedef typename config::elog_type elog_type;
  52. /// Type of this endpoint's access logging policy
  53. typedef typename config::alog_type alog_type;
  54. /// Type of this endpoint transport component's associated connection
  55. /// transport component.
  56. typedef iostream::connection<config> transport_con_type;
  57. /// Type of a shared pointer to this endpoint transport component's
  58. /// associated connection transport component
  59. typedef typename transport_con_type::ptr transport_con_ptr;
  60. // generate and manage our own io_service
  61. explicit endpoint() :
  62. m_output_stream(NULL),
  63. m_is_secure(false)
  64. {
  65. // std::cout << "transport::iostream::endpoint constructor" << std::endl;
  66. }
  67. /// Register a default output stream
  68. /**
  69. * The specified output stream will be assigned to future connections as the
  70. * default output stream.
  71. *
  72. * @param o The ostream to use as the default output stream.
  73. */
  74. void register_ostream(std::ostream * o)
  75. {
  76. m_alog->write(log::alevel::devel, "register_ostream");
  77. m_output_stream = o;
  78. }
  79. /// Set whether or not endpoint can create secure connections
  80. /**
  81. * The iostream transport does not provide any security features. As such
  82. * it defaults to returning false when `is_secure` is called. However, the
  83. * iostream transport may be used to wrap an external socket API that may
  84. * provide secure transport. This method allows that external API to flag
  85. * whether or not it can create secure connections so that users of the
  86. * WebSocket++ API will get more accurate information.
  87. *
  88. * Setting this value only indicates whether or not the endpoint is capable
  89. * of producing and managing secure connections. Connections produced by
  90. * this endpoint must also be individually flagged as secure if they are.
  91. *
  92. * @since 0.3.0-alpha4
  93. *
  94. * @param value Whether or not the endpoint can create secure connections.
  95. */
  96. void set_secure(bool value)
  97. {
  98. m_is_secure = value;
  99. }
  100. /// Tests whether or not the underlying transport is secure
  101. /**
  102. * iostream transport will return false by default because it has no
  103. * information about the ultimate remote endpoint. This may or may not be
  104. * accurate depending on the real source of bytes being input. `set_secure`
  105. * may be used by a wrapper API to correct the return value in the case that
  106. * secure connections are in fact possible.
  107. *
  108. * @return Whether or not the underlying transport is secure
  109. */
  110. bool is_secure() const
  111. {
  112. return m_is_secure;
  113. }
  114. /// Sets the write handler
  115. /**
  116. * The write handler is called when the iostream transport receives data
  117. * that needs to be written to the appropriate output location. This handler
  118. * can be used in place of registering an ostream for output.
  119. *
  120. * The signature of the handler is
  121. * `lib::error_code (connection_hdl, char const *, size_t)` The
  122. * code returned will be reported and logged by the core library.
  123. *
  124. * @since 0.5.0
  125. *
  126. * @param h The handler to call on connection shutdown.
  127. */
  128. void set_write_handler(write_handler h)
  129. {
  130. m_write_handler = h;
  131. }
  132. /// Sets the shutdown handler
  133. /**
  134. * The shutdown handler is called when the iostream transport receives a
  135. * notification from the core library that it is finished with all read and
  136. * write operations and that the underlying transport can be cleaned up.
  137. *
  138. * If you are using iostream transport with another socket library, this is
  139. * a good time to close/shutdown the socket for this connection.
  140. *
  141. * The signature of the handler is lib::error_code (connection_hdl). The
  142. * code returned will be reported and logged by the core library.
  143. *
  144. * @since 0.5.0
  145. *
  146. * @param h The handler to call on connection shutdown.
  147. */
  148. void set_shutdown_handler(shutdown_handler h)
  149. {
  150. m_shutdown_handler = h;
  151. }
  152. protected:
  153. /// Initialize logging
  154. /**
  155. * The loggers are located in the main endpoint class. As such, the
  156. * transport doesn't have direct access to them. This method is called
  157. * by the endpoint constructor to allow shared logging from the transport
  158. * component. These are raw pointers to member variables of the endpoint.
  159. * In particular, they cannot be used in the transport constructor as they
  160. * haven't been constructed yet, and cannot be used in the transport
  161. * destructor as they will have been destroyed by then.
  162. *
  163. * @param a A pointer to the access logger to use.
  164. * @param e A pointer to the error logger to use.
  165. */
  166. void init_logging(lib::shared_ptr<alog_type> a, lib::shared_ptr<elog_type> e)
  167. {
  168. m_elog = e;
  169. m_alog = a;
  170. }
  171. /// Initiate a new connection
  172. /**
  173. * @param tcon A pointer to the transport connection component of the
  174. * connection to connect.
  175. * @param u A URI pointer to the URI to connect to.
  176. * @param cb The function to call back with the results when complete.
  177. */
  178. void async_connect(transport_con_ptr, uri_ptr, connect_handler cb)
  179. {
  180. cb(lib::error_code());
  181. }
  182. /// Initialize a connection
  183. /**
  184. * Init is called by an endpoint once for each newly created connection.
  185. * It's purpose is to give the transport policy the chance to perform any
  186. * transport specific initialization that couldn't be done via the default
  187. * constructor.
  188. *
  189. * @param tcon A pointer to the transport portion of the connection.
  190. * @return A status code indicating the success or failure of the operation
  191. */
  192. lib::error_code init(transport_con_ptr tcon)
  193. {
  194. tcon->register_ostream(m_output_stream);
  195. if (m_shutdown_handler)
  196. {
  197. tcon->set_shutdown_handler(m_shutdown_handler);
  198. }
  199. if (m_write_handler)
  200. {
  201. tcon->set_write_handler(m_write_handler);
  202. }
  203. return lib::error_code();
  204. }
  205. private:
  206. std::ostream * m_output_stream;
  207. shutdown_handler m_shutdown_handler;
  208. write_handler m_write_handler;
  209. lib::shared_ptr<elog_type> m_elog;
  210. lib::shared_ptr<alog_type> m_alog;
  211. bool m_is_secure;
  212. };
  213. } // namespace iostream
  214. } // namespace transport
  215. } // namespace websocketpp
  216. #endif // WEBSOCKETPP_TRANSPORT_IOSTREAM_HPP