websocketpp  0.6.0
C++/Boost Asio based websocket client/server library
server_endpoint.hpp
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 
28 #ifndef WEBSOCKETPP_SERVER_ENDPOINT_HPP
29 #define WEBSOCKETPP_SERVER_ENDPOINT_HPP
30 
31 #include <websocketpp/endpoint.hpp>
32 
33 #include <websocketpp/logger/levels.hpp>
34 
35 #include <websocketpp/common/system_error.hpp>
36 
37 namespace websocketpp {
38 
40 
43 template <typename config>
44 class server : public endpoint<connection<config>,config> {
45 public:
48 
50  typedef typename config::concurrency_type concurrency_type;
52  typedef typename config::transport_type transport_type;
53 
58 
60  typedef typename transport_type::transport_con_type transport_con_type;
62  typedef typename transport_con_type::ptr transport_con_ptr;
63 
66 
67  explicit server() : endpoint_type(true)
68  {
69  endpoint_type::m_alog.write(log::alevel::devel, "server constructor");
70  }
71 
74 
75 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
76  // no copy constructor because endpoints are not copyable
77  server<config>(server<config> &) = delete;
78 
79  // no copy assignment operator because endpoints are not copyable
80  server<config> & operator=(server<config> const &) = delete;
81 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
82 
83 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
84  server<config>(server<config> && o) : endpoint<connection<config>,config>(std::move(o)) {}
86 
87 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
88  // no move assignment operator because of const member variables
89  server<config> & operator=(server<config> &&) = delete;
90 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
91 
92 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
93 
95 
104  connection_ptr get_connection() {
105  return endpoint_type::create_connection();
106  }
107 
109 
119  void start_accept(lib::error_code & ec) {
120  if (!transport_type::is_listening()) {
121  ec = error::make_error_code(error::async_accept_not_listening);
122  return;
123  }
124 
125  ec = lib::error_code();
126  connection_ptr con = get_connection();
127 
128  transport_type::async_accept(
129  lib::static_pointer_cast<transport_con_type>(con),
130  lib::bind(&type::handle_accept,this,con,lib::placeholders::_1),
131  ec
132  );
133 
134  if (ec && con) {
135  // If the connection was constructed but the accept failed,
136  // terminate the connection to prevent memory leaks
137  con->terminate(lib::error_code());
138  }
139  }
140 
142 
150  void start_accept() {
151  lib::error_code ec;
152  start_accept(ec);
153  if (ec) {
154  throw exception(ec);
155  }
156  }
157 
159  void handle_accept(connection_ptr con, lib::error_code const & ec) {
160  if (ec) {
161  con->terminate(ec);
162 
163  if (ec == error::operation_canceled) {
164  endpoint_type::m_elog.write(log::elevel::info,
165  "handle_accept error: "+ec.message());
166  } else {
167  endpoint_type::m_elog.write(log::elevel::rerror,
168  "handle_accept error: "+ec.message());
169  }
170  } else {
171  con->start();
172  }
173 
174  lib::error_code start_ec;
175  start_accept(start_ec);
176  if (start_ec == error::async_accept_not_listening) {
177  endpoint_type::m_elog.write(log::elevel::info,
178  "Stopping acceptance of new connections because the underlying transport is no longer listening.");
179  } else if (start_ec) {
180  endpoint_type::m_elog.write(log::elevel::rerror,
181  "Restarting async_accept loop failed: "+ec.message());
182  }
183  }
184 };
185 
186 } // namespace websocketpp
187 
188 #endif //WEBSOCKETPP_SERVER_ENDPOINT_HPP
config::transport_type transport_type
Type of the endpoint transport component.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the connection transport component.
Represents an individual WebSocket connection.
Definition: connection.hpp:235
config::concurrency_type concurrency_type
Type of the endpoint concurrency component.
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
Server endpoint role based on the given config.
static level const info
Definition: levels.hpp:69
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
The requested operation was canceled.
Definition: error.hpp:127
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:42
void handle_accept(connection_ptr con, lib::error_code const &ec)
Handler callback for start_accept.
void start_accept()
Starts the server's async connection acceptance loop.
endpoint< connection_type, config > endpoint_type
Type of the endpoint component of this server.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection.
Definition: connection.hpp:243
connection_ptr get_connection()
Create and initialize a new connection.
connection< config > connection_type
Type of the connections this server will create.
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
transport_type::transport_con_type transport_con_type
Type of the connection transport component.
static level const rerror
Definition: levels.hpp:75
server< config > type
Type of this endpoint.