websocketpp  0.3.0
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
client_endpoint.hpp
1 /*
2  * Copyright (c) 2013, 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_CLIENT_ENDPOINT_HPP
29 #define WEBSOCKETPP_CLIENT_ENDPOINT_HPP
30 
31 #include <websocketpp/endpoint.hpp>
32 #include <websocketpp/logger/levels.hpp>
33 
34 #include <iostream>
35 
36 namespace websocketpp {
37 
38 
40 
43 template <typename config>
44 class client : 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 client() : endpoint_type(false)
68  {
69  endpoint_type::m_alog.write(log::alevel::devel, "client constructor");
70  }
71 
73 
84  connection_ptr get_connection(uri_ptr location, lib::error_code & ec) {
85  if (location->get_secure() && !transport_type::is_secure()) {
86  ec = error::make_error_code(error::endpoint_not_secure);
87  return connection_ptr();
88  }
89 
90  connection_ptr con = endpoint_type::create_connection();
91 
92  if (!con) {
93  ec = error::make_error_code(error::con_creation_failed);
94  return con;
95  }
96 
97  con->set_uri(location);
98 
99  ec = lib::error_code();
100  return con;
101  }
102 
104 
114  connection_ptr get_connection(std::string const & u, lib::error_code & ec) {
115  uri_ptr location(new uri(u));
116 
117  if (!location->get_valid()) {
118  ec = error::make_error_code(error::invalid_uri);
119  return connection_ptr();
120  }
121 
122  return get_connection(location, ec);
123  }
124 
126 
134  connection_ptr connect(connection_ptr con) {
135  // Ask transport to perform a connection
136  transport_type::async_connect(
137  lib::static_pointer_cast<transport_con_type>(con),
138  con->get_uri(),
139  lib::bind(
140  &type::handle_connect,
141  this,
142  con,
143  lib::placeholders::_1
144  )
145  );
146 
147  return con;
148  }
149 private:
150  // handle_connect
151  void handle_connect(connection_ptr con, lib::error_code const & ec) {
152  if (ec) {
153  con->terminate(ec);
154 
155  endpoint_type::m_elog.write(log::elevel::rerror,
156  "handle_connect error: "+ec.message());
157  } else {
158  endpoint_type::m_alog.write(log::alevel::connect,
159  "Successful connection");
160 
161  con->start();
162  }
163  }
164 };
165 
166 } // namespace websocketpp
167 
168 #endif //WEBSOCKETPP_CLIENT_ENDPOINT_HPP
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
connection_ptr get_connection(std::string const &u, lib::error_code &ec)
Get a new connection (string version)
endpoint< connection_type, config > endpoint_type
Type of the endpoint component of this server.
Represents an individual WebSocket connection.
Definition: connection.hpp:221
Client endpoint role based on the given config.
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the connection transport component.
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:122
transport_type::transport_con_type transport_con_type
Type of the connection transport component.
client< config > type
Type of this endpoint.
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
config::concurrency_type concurrency_type
Type of the endpoint concurrency component.
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:42
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:350
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection.
Definition: connection.hpp:229
Attempted to open a secure connection with an insecure endpoint.
Definition: error.hpp:55
connection< config > connection_type
Type of the connections this server will create.
config::transport_type transport_type
Type of the endpoint transport component.
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
static level const rerror
Definition: levels.hpp:56
An invalid uri was supplied.
Definition: error.hpp:63
static level const connect
Information about new connections.
Definition: levels.hpp:102
Connection creation attempted failed.
Definition: error.hpp:97