server_endpoint.hpp 7.1 KB

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