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.

208 lines
7.0 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_SERVER_ENDPOINT_HPP
  28. #define WEBSOCKETPP_SERVER_ENDPOINT_HPP
  29. #include <websocketpp/common/system_error.hpp>
  30. #include <websocketpp/endpoint.hpp>
  31. #include <websocketpp/logger/levels.hpp>
  32. namespace websocketpp
  33. {
  34. /// Server endpoint role based on the given config
  35. /**
  36. *
  37. */
  38. template <typename config> class server : public endpoint<connection<config>, config>
  39. {
  40. public:
  41. /// Type of this endpoint
  42. typedef server<config> type;
  43. /// Type of the endpoint concurrency component
  44. typedef typename config::concurrency_type concurrency_type;
  45. /// Type of the endpoint transport component
  46. typedef typename config::transport_type transport_type;
  47. /// Type of the connections this server will create
  48. typedef connection<config> connection_type;
  49. /// Type of a shared pointer to the connections this server will create
  50. typedef typename connection_type::ptr connection_ptr;
  51. /// Type of the connection transport component
  52. typedef typename transport_type::transport_con_type transport_con_type;
  53. /// Type of a shared pointer to the connection transport component
  54. typedef typename transport_con_type::ptr transport_con_ptr;
  55. /// Type of the endpoint component of this server
  56. typedef endpoint<connection_type, config> endpoint_type;
  57. friend class connection<config>;
  58. explicit server() :
  59. endpoint_type(true)
  60. {
  61. endpoint_type::m_alog->write(log::alevel::devel, "server constructor");
  62. }
  63. /// Destructor
  64. ~server<config>() {}
  65. #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  66. // no copy constructor because endpoints are not copyable
  67. server<config>(server<config> &) = delete;
  68. // no copy assignment operator because endpoints are not copyable
  69. server<config> & operator=(const server<config> &) = delete;
  70. #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  71. #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
  72. /// Move constructor
  73. server<config>(server<config> && o) :
  74. endpoint<connection<config>, config>(std::move(o))
  75. {
  76. }
  77. #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  78. // no move assignment operator because of const member variables
  79. server<config> & operator=(server<config> &&) = delete;
  80. #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
  81. #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
  82. /// Create and initialize a new connection
  83. /**
  84. * The connection will be initialized and ready to begin. Call its start()
  85. * method to begin the processing loop.
  86. *
  87. * Note: The connection must either be started or terminated using
  88. * connection::terminate in order to avoid memory leaks.
  89. *
  90. * @return A pointer to the new connection.
  91. */
  92. connection_ptr get_connection()
  93. {
  94. return endpoint_type::create_connection();
  95. }
  96. /// Starts the server's async connection acceptance loop (exception free)
  97. /**
  98. * Initiates the server connection acceptance loop. Must be called after
  99. * listen. This method will have no effect until the underlying io_service
  100. * starts running. It may be called after the io_service is already running.
  101. *
  102. * Refer to documentation for the transport policy you are using for
  103. * instructions on how to stop this acceptance loop.
  104. *
  105. * @param [out] ec A status code indicating an error, if any.
  106. */
  107. void start_accept(lib::error_code & ec)
  108. {
  109. if (! transport_type::is_listening())
  110. {
  111. ec = error::make_error_code(error::async_accept_not_listening);
  112. return;
  113. }
  114. ec = lib::error_code();
  115. connection_ptr con = get_connection();
  116. if (! con)
  117. {
  118. ec = error::make_error_code(error::con_creation_failed);
  119. return;
  120. }
  121. transport_type::async_accept(lib::static_pointer_cast<transport_con_type>(con), lib::bind(&type::handle_accept, this, con, lib::placeholders::_1), ec);
  122. if (ec && con)
  123. {
  124. // If the connection was constructed but the accept failed,
  125. // terminate the connection to prevent memory leaks
  126. con->terminate(lib::error_code());
  127. }
  128. }
  129. /// Starts the server's async connection acceptance loop
  130. /**
  131. * Initiates the server connection acceptance loop. Must be called after
  132. * listen. This method will have no effect until the underlying io_service
  133. * starts running. It may be called after the io_service is already running.
  134. *
  135. * Refer to documentation for the transport policy you are using for
  136. * instructions on how to stop this acceptance loop.
  137. */
  138. void start_accept()
  139. {
  140. lib::error_code ec;
  141. start_accept(ec);
  142. if (ec)
  143. {
  144. throw exception(ec);
  145. }
  146. }
  147. /// Handler callback for start_accept
  148. void handle_accept(connection_ptr con, const lib::error_code & ec)
  149. {
  150. if (ec)
  151. {
  152. con->terminate(ec);
  153. if (ec == error::operation_canceled)
  154. {
  155. endpoint_type::m_elog->write(log::elevel::info, "handle_accept error: " + ec.message());
  156. }
  157. else
  158. {
  159. endpoint_type::m_elog->write(log::elevel::rerror, "handle_accept error: " + ec.message());
  160. }
  161. }
  162. else
  163. {
  164. con->start();
  165. }
  166. lib::error_code start_ec;
  167. start_accept(start_ec);
  168. if (start_ec == error::async_accept_not_listening)
  169. {
  170. endpoint_type::m_elog->write(log::elevel::info, "Stopping acceptance of new connections because the underlying transport is no longer listening.");
  171. }
  172. else if (start_ec)
  173. {
  174. endpoint_type::m_elog->write(log::elevel::rerror, "Restarting async_accept loop failed: " + ec.message());
  175. }
  176. }
  177. };
  178. } // namespace websocketpp
  179. #endif // WEBSOCKETPP_SERVER_ENDPOINT_HPP