Loading...
Searching...
No Matches
client_endpoint.hpp
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef WEBSOCKETPP_CLIENT_ENDPOINT_HPP
#define WEBSOCKETPP_CLIENT_ENDPOINT_HPP
#include <websocketpp/endpoint.hpp>
#include <websocketpp/uri.hpp>
#include <websocketpp/logger/levels.hpp>
#include <websocketpp/common/system_error.hpp>
#include <string>
namespace websocketpp {
/// Client endpoint role based on the given config
/**
*
*/
template <typename config>
class client : public endpoint<connection<config>,config> {
public:
/// Type of this endpoint
typedef client<config> type;
/// Type of the endpoint concurrency component
typedef typename config::concurrency_type concurrency_type;
/// Type of the endpoint transport component
typedef typename config::transport_type transport_type;
/// Type of the connections this server will create
typedef connection<config> connection_type;
/// Type of a shared pointer to the connections this server will create
typedef typename connection_type::ptr connection_ptr;
/// Type of the connection transport component
typedef typename transport_type::transport_con_type transport_con_type;
/// Type of a shared pointer to the connection transport component
typedef typename transport_con_type::ptr transport_con_ptr;
/// Type of the endpoint component of this server
typedef endpoint<connection_type,config> endpoint_type;
friend class connection<config>;
explicit client() : endpoint_type(false)
{
endpoint_type::m_alog->write(log::alevel::devel, "client constructor");
}
/// Get a new connection
/**
* Creates and returns a pointer to a new connection to the given URI
* suitable for passing to connect(connection_ptr). This method allows
* applying connection specific settings before performing the opening
* handshake.
*
* @param [in] location URI to open the connection to as a uri_ptr
* @param [out] ec An status code indicating failure reasons, if any
*
* @return A connection_ptr to the new connection
*/
connection_ptr get_connection(uri_ptr location, lib::error_code & ec) {
if (location->get_secure() && !transport_type::is_secure()) {
ec = error::make_error_code(error::endpoint_not_secure);
return connection_ptr();
}
connection_ptr con = endpoint_type::create_connection(ec);
if (!con) {
// if the transport doesn't have a more specific error, set
// a generic one.
if (!ec) {
ec = error::make_error_code(error::con_creation_failed);
}
return con;
}
con->set_uri(location);
ec = lib::error_code();
return con;
}
/// Get a new connection (string version)
/**
* Creates and returns a pointer to a new connection to the given URI
* suitable for passing to connect(connection_ptr). This overload allows
* default construction of the uri_ptr from a standard string.
*
* @param [in] u URI to open the connection to as a string
* @param [out] ec An status code indicating failure reasons, if any
*
* @return A connection_ptr to the new connection
*/
connection_ptr get_connection(std::string const & u, lib::error_code & ec) {
uri_ptr location = lib::make_shared<uri>(u);
if (!location->get_valid()) {
ec = error::make_error_code(error::invalid_uri);
return connection_ptr();
}
return get_connection(location, ec);
}
/// Begin the connection process for the given connection
/**
* Initiates the opening connection handshake for connection con. Exact
* behavior depends on the underlying transport policy.
*
* @param con The connection to connect
*
* @return The pointer to the connection originally passed in.
*/
connection_ptr connect(connection_ptr con) {
// Ask transport to perform a connection
transport_type::async_connect(
lib::static_pointer_cast<transport_con_type>(con),
con->get_uri(),
lib::bind(
&type::handle_connect,
this,
con,
lib::placeholders::_1
)
);
return con;
}
private:
// handle_connect
void handle_connect(connection_ptr con, lib::error_code const & ec) {
if (ec) {
con->terminate(ec);
endpoint_type::m_elog->write(log::elevel::rerror,
"handle_connect error: "+ec.message());
} else {
endpoint_type::m_alog->write(log::alevel::connect,
"Successful connection");
con->start();
}
}
};
} // namespace websocketpp
#endif //WEBSOCKETPP_CLIENT_ENDPOINT_HPP
_WEBSOCKETPP_CPP11_RANDOM_DEVICE_
#define _WEBSOCKETPP_CPP11_RANDOM_DEVICE_
Definition boost_config.hpp:49
websocket_endpoint
Definition step3.cpp:42
websocketpp::client
Client endpoint role based on the given config.
Definition client_endpoint.hpp:47
websocketpp::client::transport_type
config::transport_type transport_type
Type of the endpoint transport component.
Definition client_endpoint.hpp:55
websocketpp::client::get_connection
connection_ptr get_connection(std::string const &u, lib::error_code &ec)
Get a new connection (string version).
Definition client_endpoint.hpp:123
websocketpp::client::connection_type
connection< config > connection_type
Type of the connections this server will create.
Definition client_endpoint.hpp:58
websocketpp::client::connection_ptr
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
Definition client_endpoint.hpp:60
websocketpp::client::endpoint_type
endpoint< connection_type, config > endpoint_type
Type of the endpoint component of this server.
Definition client_endpoint.hpp:68
websocketpp::client::concurrency_type
config::concurrency_type concurrency_type
Type of the endpoint concurrency component.
Definition client_endpoint.hpp:53
websocketpp::client::transport_con_ptr
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the connection transport component.
Definition client_endpoint.hpp:65
websocketpp::client::connect
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
Definition client_endpoint.hpp:143
websocketpp::client::type
client< config > type
Type of this endpoint.
Definition client_endpoint.hpp:50
websocketpp::client::get_connection
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
Definition client_endpoint.hpp:89
websocketpp::client::transport_con_type
transport_type::transport_con_type transport_con_type
Type of the connection transport component.
Definition client_endpoint.hpp:63
websocketpp::concurrency::basic
Concurrency policy that uses std::mutex / boost::mutex.
Definition basic.hpp:37
websocketpp::connection_base
Stub for user supplied base class.
Definition connection_base.hpp:34
websocketpp::connection
Represents an individual WebSocket connection.
Definition connection.hpp:238
websocketpp::endpoint_base
Stub for user supplied base class.
Definition endpoint_base.hpp:34
websocketpp::endpoint
Creates and manages connections associated with a WebSocket endpoint.
Definition endpoint.hpp:44
websocketpp::extensions::permessage_deflate::disabled
Stub class for use when disabling permessage_deflate extension.
Definition disabled.hpp:53
websocketpp::http::parser::request
Stores, parses, and manipulates HTTP requests.
Definition request.hpp:50
websocketpp::http::parser::response
Stores, parses, and manipulates HTTP responses.
Definition response.hpp:57
websocketpp::log::basic
Basic logger that outputs to an ostream.
Definition basic.hpp:59
websocketpp::message_buffer::alloc::con_msg_manager
Definition alloc.hpp:43
websocketpp::message_buffer::alloc::endpoint_msg_manager
Definition alloc.hpp:88
websocketpp::random::random_device::int_generator
Thread safe non-deterministic random integer generator.
Definition random_device.hpp:53
websocketpp::random::random_device::int_generator::operator()
int_type operator()()
advances the engine's state and returns the generated value
Definition random_device.hpp:63
websocketpp::random::random_device::int_generator::int_generator
int_generator()
constructor
Definition random_device.hpp:60
websocketpp::transport::asio::basic_socket::endpoint
Basic ASIO endpoint socket component.
Definition none.hpp:318
websocketpp::transport::asio::endpoint
Asio based endpoint transport component.
Definition endpoint.hpp:54
websocketpp::transport::iostream::endpoint
Definition endpoint.hpp:46
websocketpp::concurrency
Concurrency handling support.
Definition basic.hpp:34
websocketpp::error
Library level error codes.
Definition error.hpp:44
websocketpp::error::con_creation_failed
@ con_creation_failed
Connection creation attempted failed.
Definition error.hpp:99
websocketpp::error::endpoint_not_secure
@ endpoint_not_secure
Attempted to open a secure connection with an insecure endpoint.
Definition error.hpp:57
websocketpp::error::invalid_uri
@ invalid_uri
An invalid uri was supplied.
Definition error.hpp:65
websocketpp::extensions::permessage_deflate
Implementation of RFC 7692, the permessage-deflate WebSocket extension.
Definition disabled.hpp:44
websocketpp::extensions
Definition disabled.hpp:43
websocketpp::http
HTTP handling support.
Definition request.hpp:37
websocketpp::random::random_device
RNG policy based on std::random_device or boost::random_device.
Definition random_device.hpp:36
websocketpp::random
Random number generation policies.
Definition random_device.hpp:34
websocketpp::transport::asio::basic_socket
Definition none.hpp:47
websocketpp::transport::asio
Transport policy that uses asio.
Definition endpoint.hpp:46
websocketpp::transport::iostream
Transport policy that uses STL iostream for I/O and does not support timers.
Definition endpoint.hpp:43
websocketpp::transport
Transport policies provide network connectivity and timers.
Definition endpoint.hpp:45
websocketpp
Namespace for the WebSocket++ project.
Definition asio_no_tls_client.hpp:34
websocketpp::uri_ptr
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:791
websocketpp::config::asio_client::transport_config
Definition asio_no_tls_client.hpp:56
websocketpp::config::asio_client
Client config with asio transport and TLS disabled.
Definition asio_no_tls_client.hpp:38
websocketpp::config::core_client::permessage_deflate_config
Extension specific settings:
Definition core_client.hpp:257
websocketpp::config::core_client::permessage_deflate_config::minimum_outgoing_window_bits
static const uint8_t minimum_outgoing_window_bits
Definition core_client.hpp:269
websocketpp::config::core_client::permessage_deflate_config::allow_disabling_context_takeover
static const bool allow_disabling_context_takeover
Definition core_client.hpp:262
websocketpp::config::core_client::transport_config
Definition core_client.hpp:108
websocketpp::config::core_client::transport_config::timeout_dns_resolve
static const long timeout_dns_resolve
Length of time to wait for dns resolution.
Definition core_client.hpp:139
websocketpp::config::core_client::transport_config::timeout_socket_shutdown
static const long timeout_socket_shutdown
Length of time to wait for socket shutdown.
Definition core_client.hpp:145
websocketpp::config::core_client::transport_config::timeout_socket_pre_init
static const long timeout_socket_pre_init
Default timer values (in ms).
Definition core_client.hpp:126
websocketpp::config::core_client::transport_config::enable_multithreading
static bool const enable_multithreading
Definition core_client.hpp:118
websocketpp::config::core_client::transport_config::timeout_proxy
static const long timeout_proxy
Length of time to wait before a proxy handshake is aborted.
Definition core_client.hpp:129
websocketpp::config::core_client::transport_config::timeout_connect
static const long timeout_connect
Length of time to wait for TCP connect.
Definition core_client.hpp:142
websocketpp::config::core_client::transport_config::timeout_socket_post_init
static const long timeout_socket_post_init
Length of time to wait for socket post-initialization.
Definition core_client.hpp:136
websocketpp::config::core_client
Client config with iostream transport.
Definition core_client.hpp:71
websocketpp::config::core_client::enable_multithreading
static bool const enable_multithreading
Definition core_client.hpp:106
websocketpp::config::core_client::enable_extensions
static const bool enable_extensions
Global flag for enabling/disabling extensions.
Definition core_client.hpp:252
websocketpp::config::core_client::elog_type
websocketpp::log::basic< concurrency_type, websocketpp::log::elevel > elog_type
Logging policies.
Definition core_client.hpp:95
websocketpp::config::core_client::alog_level
static const websocketpp::log::level alog_level
Default static access logging channels.
Definition core_client.hpp:197
websocketpp::config::core_client::max_http_body_size
static const size_t max_http_body_size
Default maximum http body size.
Definition core_client.hpp:249
websocketpp::config::core_client::elog_level
static const websocketpp::log::level elog_level
Default static error logging channels.
Definition core_client.hpp:184
websocketpp::config::core_client::max_message_size
static const size_t max_message_size
Default maximum message size.
Definition core_client.hpp:237
websocketpp::config::core_client::transport_type
websocketpp::transport::iostream::endpoint< transport_config > transport_type
Transport Endpoint Component.
Definition core_client.hpp:150
websocketpp::config::core_client::drop_on_protocol_error
static const bool drop_on_protocol_error
Drop connections immediately on protocol error.
Definition core_client.hpp:210
websocketpp::config::core_client::timeout_close_handshake
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
Definition core_client.hpp:162
websocketpp::config::core_client::timeout_open_handshake
static const long timeout_open_handshake
Default timer values (in ms).
Definition core_client.hpp:160
websocketpp::config::core_client::rng_type
websocketpp::random::random_device::int_generator< uint32_t, concurrency_type > rng_type
RNG policies.
Definition core_client.hpp:101
websocketpp::config::core_client::timeout_pong
static const long timeout_pong
Length of time to wait for a pong after a ping.
Definition core_client.hpp:164
websocketpp::config::core_client::client_version
static const int client_version
WebSocket Protocol version to use as a client.
Definition core_client.hpp:172
websocketpp::config::core_client::silent_close
static const bool silent_close
Suppresses the return of detailed connection close information.
Definition core_client.hpp:225
websocketpp::log::alevel
Package of log levels for logging access events.
Definition levels.hpp:112
websocketpp::log::alevel::devel
static level const devel
Development messages (warning: very chatty).
Definition levels.hpp:141
websocketpp::log::alevel::all
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
websocketpp::log::alevel::connect
static level const connect
Information about new connections.
Definition levels.hpp:121
websocketpp::log::elevel
Package of log levels for logging errors.
Definition levels.hpp:59
websocketpp::log::elevel::devel
static level const devel
Low level debugging information (warning: very chatty).
Definition levels.hpp:63
websocketpp::log::elevel::all
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:80
websocketpp::log::elevel::rerror
static level const rerror
Definition levels.hpp:75