server_endpoint.hpp 7.2 KB

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