endpoint.hpp 8.6 KB

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