WebSocket++ 0.8.2
C++ websocket client/server library
Loading...
Searching...
No Matches
client_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_CLIENT_ENDPOINT_HPP
29#define WEBSOCKETPP_CLIENT_ENDPOINT_HPP
30
31#include <websocketpp/endpoint.hpp>
32#include <websocketpp/uri.hpp>
33
34#include <websocketpp/logger/levels.hpp>
35
36#include <websocketpp/common/system_error.hpp>
37
38#include <string>
39
40namespace websocketpp {
41
42/// Client endpoint role based on the given config
43/**
44 *
45 */
46template <typename config>
47class client : public endpoint<connection<config>,config> {
48public:
49 /// Type of this endpoint
50 typedef client<config> type;
51
52 /// Type of the endpoint concurrency component
53 typedef typename config::concurrency_type concurrency_type;
54 /// Type of the endpoint transport component
55 typedef typename config::transport_type transport_type;
56
57 /// Type of the connections this server will create
58 typedef connection<config> connection_type;
59 /// Type of a shared pointer to the connections this server will create
60 typedef typename connection_type::ptr connection_ptr;
61
62 /// Type of the connection transport component
64 /// Type of a shared pointer to the connection transport component
66
67 /// Type of the endpoint component of this server
69
70 friend class connection<config>;
71
72 explicit client() : endpoint_type(false)
73 {
74 endpoint_type::m_alog->write(log::alevel::devel, "client constructor");
75 }
76
77 /// Get a new connection
78 /**
79 * Creates and returns a pointer to a new connection to the given URI
80 * suitable for passing to connect(connection_ptr). This method allows
81 * applying connection specific settings before performing the opening
82 * handshake.
83 *
84 * @param [in] location URI to open the connection to as a uri_ptr
85 * @param [out] ec An status code indicating failure reasons, if any
86 *
87 * @return A connection_ptr to the new connection
88 */
89 connection_ptr get_connection(uri_ptr location, lib::error_code & ec) {
90 if (location->get_secure() && !transport_type::is_secure()) {
91 ec = error::make_error_code(error::endpoint_not_secure);
92 return connection_ptr();
93 }
94
95 connection_ptr con = endpoint_type::create_connection();
96
97 if (!con) {
98 ec = error::make_error_code(error::con_creation_failed);
99 return con;
100 }
101
102 con->set_uri(location);
103
104 ec = lib::error_code();
105 return con;
106 }
107
108 /// Get a new connection (string version)
109 /**
110 * Creates and returns a pointer to a new connection to the given URI
111 * suitable for passing to connect(connection_ptr). This overload allows
112 * default construction of the uri_ptr from a standard string.
113 *
114 * @param [in] u URI to open the connection to as a string
115 * @param [out] ec An status code indicating failure reasons, if any
116 *
117 * @return A connection_ptr to the new connection
118 */
119 connection_ptr get_connection(std::string const & u, lib::error_code & ec) {
120 uri_ptr location = lib::make_shared<uri>(u);
121
122 if (!location->get_valid()) {
123 ec = error::make_error_code(error::invalid_uri);
124 return connection_ptr();
125 }
126
127 return get_connection(location, ec);
128 }
129
130 /// Begin the connection process for the given connection
131 /**
132 * Initiates the opening connection handshake for connection con. Exact
133 * behavior depends on the underlying transport policy.
134 *
135 * @param con The connection to connect
136 *
137 * @return The pointer to the connection originally passed in.
138 */
140 // Ask transport to perform a connection
141 transport_type::async_connect(
142 lib::static_pointer_cast<transport_con_type>(con),
143 con->get_uri(),
144 lib::bind(
145 &type::handle_connect,
146 this,
147 con,
148 lib::placeholders::_1
149 )
150 );
151
152 return con;
153 }
154private:
155 // handle_connect
156 void handle_connect(connection_ptr con, lib::error_code const & ec) {
157 if (ec) {
158 con->terminate(ec);
159
160 endpoint_type::m_elog->write(log::elevel::rerror,
161 "handle_connect error: "+ec.message());
162 } else {
163 endpoint_type::m_alog->write(log::alevel::connect,
164 "Successful connection");
165
166 con->start();
167 }
168 }
169};
170
171} // namespace websocketpp
172
173#endif //WEBSOCKETPP_CLIENT_ENDPOINT_HPP
#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_
Client endpoint role based on the given config.
config::transport_type transport_type
Type of the endpoint transport component.
connection_ptr get_connection(std::string const &u, lib::error_code &ec)
Get a new connection (string version)
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.
endpoint< connection_type, config > endpoint_type
Type of the endpoint component of this server.
config::concurrency_type concurrency_type
Type of the endpoint concurrency component.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the connection transport component.
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
client< config > type
Type of this endpoint.
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
transport_type::transport_con_type transport_con_type
Type of the connection transport component.
Concurrency policy that uses std::mutex / boost::mutex.
Definition basic.hpp:37
Stub for user supplied base class.
Represents an individual WebSocket connection.
Stub for user supplied base class.
Creates and manages connections associated with a WebSocket endpoint.
Definition endpoint.hpp:42
Stub class for use when disabling permessage_deflate extension.
Definition disabled.hpp:53
Stores, parses, and manipulates HTTP requests.
Definition request.hpp:50
Stores, parses, and manipulates HTTP responses.
Definition response.hpp:57
Basic logger that outputs to an ostream.
Definition basic.hpp:59
Thread safe non-deterministic random integer generator.
int_type operator()()
advances the engine's state and returns the generated value
Basic ASIO endpoint socket component.
Definition none.hpp:317
Asio based endpoint transport component.
Definition endpoint.hpp:54
Concurrency handling support.
Definition basic.hpp:34
Library level error codes.
Definition error.hpp:44
@ con_creation_failed
Connection creation attempted failed.
Definition error.hpp:99
@ endpoint_not_secure
Attempted to open a secure connection with an insecure endpoint.
Definition error.hpp:57
@ invalid_uri
An invalid uri was supplied.
Definition error.hpp:65
Implementation of RFC 7692, the permessage-deflate WebSocket extension.
Definition disabled.hpp:44
HTTP handling support.
Definition request.hpp:37
RNG policy based on std::random_device or boost::random_device.
Random number generation policies.
Transport policy that uses asio.
Definition endpoint.hpp:46
Transport policy that uses STL iostream for I/O and does not support timers.
Definition endpoint.hpp:43
Transport policies provide network connectivity and timers.
Definition endpoint.hpp:45
Namespace for the WebSocket++ project.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:352
Client config with asio transport and TLS disabled.
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
static const long timeout_socket_pre_init
Default timer values (in ms)
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
static const long timeout_connect
Length of time to wait for TCP connect.
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
Client config with iostream transport.
static bool const enable_multithreading
static const bool enable_extensions
Global flag for enabling/disabling extensions.
websocketpp::log::basic< concurrency_type, websocketpp::log::elevel > elog_type
Logging policies.
static const websocketpp::log::level alog_level
Default static access logging channels.
static const size_t max_http_body_size
Default maximum http body size.
static const websocketpp::log::level elog_level
Default static error logging channels.
static const size_t max_message_size
Default maximum message size.
websocketpp::transport::iostream::endpoint< transport_config > transport_type
Transport Endpoint Component.
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
static const long timeout_open_handshake
Default timer values (in ms)
websocketpp::random::random_device::int_generator< uint32_t, concurrency_type > rng_type
RNG policies.
static const long timeout_pong
Length of time to wait for a pong after a ping.
static const int client_version
WebSocket Protocol version to use as a client.
static const bool silent_close
Suppresses the return of detailed connection close information.
Package of log levels for logging access events.
Definition levels.hpp:112
static level const devel
Development messages (warning: very chatty)
Definition levels.hpp:141
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const connect
Information about new connections.
Definition levels.hpp:121
Package of log levels for logging errors.
Definition levels.hpp:59
static level const devel
Low level debugging information (warning: very chatty)
Definition levels.hpp:63
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:80
static level const rerror
Definition levels.hpp:75