28 #ifndef WEBSOCKETPP_CONNECTION_HPP
29 #define WEBSOCKETPP_CONNECTION_HPP
32 #include <websocketpp/common/connection_hdl.hpp>
33 #include <websocketpp/common/cpp11.hpp>
34 #include <websocketpp/common/functional.hpp>
35 #include <websocketpp/error.hpp>
36 #include <websocketpp/frame.hpp>
37 #include <websocketpp/http/constants.hpp>
38 #include <websocketpp/logger/levels.hpp>
39 #include <websocketpp/processors/processor.hpp>
40 #include <websocketpp/transport/base/connection.hpp>
154 typedef lib::function<void(lib::error_code const & ec, size_t bytes_transferred)> read_handler;
155 typedef lib::function<void(lib::error_code const & ec)> write_frame_handler;
158 #ifdef _WEBSOCKETPP_INITIALIZER_LISTS_ // simplified C++11 version
166 static int const helper[] = {0,7,8,13};
201 namespace internal_state {
209 READ_HTTP_REQUEST = 2,
210 WRITE_HTTP_REQUEST = 3,
211 READ_HTTP_RESPONSE = 4,
212 WRITE_HTTP_RESPONSE = 5,
213 PROCESS_HTTP_REQUEST = 6,
214 PROCESS_CONNECTION = 7
220 template <
typename config>
222 :
public config::transport_type::transport_con_type
223 ,
public config::connection_base
229 typedef lib::shared_ptr<type>
ptr;
241 typedef typename config::transport_type::transport_con_type
246 typedef lib::function<void(ptr)> termination_handler;
248 typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
249 typedef typename concurrency_type::mutex_type mutex_type;
251 typedef typename config::request_type request_type;
252 typedef typename config::response_type response_type;
254 typedef typename config::message_type message_type;
255 typedef typename message_type::ptr message_ptr;
257 typedef typename config::con_msg_manager_type con_msg_manager_type;
258 typedef typename con_msg_manager_type::ptr con_msg_manager_ptr;
264 typedef lib::shared_ptr<processor_type> processor_ptr;
267 typedef lib::function<void(connection_hdl,message_ptr)> message_handler;
270 typedef typename transport_con_type::timer_ptr
timer_ptr;
273 typedef session::internal_state::value istate_type;
276 enum terminate_status {
283 explicit connection(
bool p_is_server, std::string
const & ua, alog_type& alog,
284 elog_type& elog, rng_type & rng)
286 , m_handle_read_frame(lib::bind(
287 &type::handle_read_frame,
289 lib::placeholders::_1,
290 lib::placeholders::_2
292 , m_write_frame_handler(lib::bind(
295 lib::placeholders::_1
298 , m_open_handshake_timeout_dur(config::timeout_open_handshake)
299 , m_close_handshake_timeout_dur(config::timeout_close_handshake)
300 , m_pong_timeout_dur(config::timeout_pong)
301 , m_max_message_size(config::max_message_size)
302 , m_state(session::state::connecting)
303 , m_internal_state(session::internal_state::USER_INIT)
304 , m_msg_manager(new con_msg_manager_type())
305 , m_send_buffer_size(0)
306 , m_write_flag(false)
308 , m_is_server(p_is_server)
321 return lib::static_pointer_cast<type>(transport_con_type::get_shared());
406 m_pong_timeout_handler = h;
417 m_interrupt_handler = h;
448 m_validate_handler = h;
458 m_message_handler = h;
486 m_open_handshake_timeout_dur = dur;
510 m_close_handshake_timeout_dur = dur;
531 m_pong_timeout_dur = dur;
544 return m_max_message_size;
560 m_max_message_size = new_value;
562 m_processor->set_max_message_size(new_value);
604 lib::error_code
send(std::string
const & payload, frame::opcode::value op =
605 frame::opcode::text);
621 lib::error_code
send(
void const * payload,
size_t len, frame::opcode::value
622 op = frame::opcode::binary);
637 lib::error_code
send(message_ptr msg);
706 void ping(std::string
const & payload);
709 void ping(std::string
const & payload, lib::error_code & ec);
724 void pong(std::string
const & payload);
727 void pong(std::string
const & payload, lib::error_code & ec);
753 lib::error_code & ec);
775 std::string
const &
get_host()
const;
848 void add_subprotocol(std::string
const & request, lib::error_code & ec);
927 void set_status(http::status_code::value code);
942 void set_status(http::status_code::value code, std::string
const & msg);
973 void append_header(std::string
const & key, std::string
const & val);
988 void replace_header(std::string
const & key, std::string
const & val);
1033 return m_connection_hdl;
1059 session::state::value
get_state()
const;
1067 return m_local_close_code;
1075 return m_local_close_reason;
1083 return m_remote_close_code;
1091 return m_remote_close_reason;
1123 m_connection_hdl = hdl;
1124 transport_con_type::set_handle(hdl);
1146 message_ptr
get_message(websocketpp::frame::opcode::value op,
size_t size)
1149 return m_msg_manager->get_message(op, size);
1154 void read_handshake(
size_t num_bytes);
1156 void handle_read_handshake(lib::error_code
const & ec,
1157 size_t bytes_transferred);
1158 void handle_read_http_response(lib::error_code
const & ec,
1159 size_t bytes_transferred);
1161 void handle_send_http_response(lib::error_code
const & ec);
1162 void handle_send_http_request(lib::error_code
const & ec);
1164 void handle_open_handshake_timeout(lib::error_code
const & ec);
1165 void handle_close_handshake_timeout(lib::error_code
const & ec);
1167 void handle_read_frame(lib::error_code
const & ec,
size_t bytes_transferred);
1177 void terminate(lib::error_code
const & ec);
1178 void handle_terminate(terminate_status tstat, lib::error_code
const & ec);
1202 void handle_transport_init(lib::error_code
const & ec);
1239 session::state::value ereq, session::state::value edest,
1252 void send_http_response();
1255 void send_http_request();
1258 void send_http_response_error();
1264 void process_control_frame(message_ptr msg);
1309 processor_ptr get_processor(
int version)
const;
1321 void write_push(message_ptr msg);
1334 message_ptr write_pop();
1342 void log_open_result();
1348 void log_close_result();
1354 void log_fail_result();
1357 template <
typename error_type>
1358 void log_err(log::level l,
char const * msg, error_type
const & ec) {
1359 std::stringstream s;
1360 s << msg <<
" error: " << ec <<
" (" << ec.message() <<
")";
1361 m_elog.write(l, s.str());
1365 read_handler m_handle_read_frame;
1366 write_frame_handler m_write_frame_handler;
1369 std::string
const m_user_agent;
1375 open_handler m_open_handler;
1376 close_handler m_close_handler;
1377 fail_handler m_fail_handler;
1378 ping_handler m_ping_handler;
1379 pong_handler m_pong_handler;
1380 pong_timeout_handler m_pong_timeout_handler;
1381 interrupt_handler m_interrupt_handler;
1382 http_handler m_http_handler;
1383 validate_handler m_validate_handler;
1384 message_handler m_message_handler;
1387 long m_open_handshake_timeout_dur;
1388 long m_close_handshake_timeout_dur;
1389 long m_pong_timeout_dur;
1390 size_t m_max_message_size;
1402 istate_type m_internal_state;
1404 mutable mutex_type m_connection_state_lock;
1411 mutex_type m_write_lock;
1414 char m_buf[config::connection_read_buffer_size];
1415 size_t m_buf_cursor;
1416 termination_handler m_termination_handler;
1417 con_msg_manager_ptr m_msg_manager;
1418 timer_ptr m_handshake_timer;
1419 timer_ptr m_ping_timer;
1423 std::string m_handshake_buffer;
1434 processor_ptr m_processor;
1440 std::queue<message_ptr> m_send_queue;
1446 size_t m_send_buffer_size;
1452 std::vector<transport::buffer> m_send_buffer;
1456 std::vector<message_ptr> m_current_msgs;
1468 request_type m_request;
1469 response_type m_response;
1471 std::string m_subprotocol;
1475 std::vector<std::string> m_requested_subprotocols;
1477 bool const m_is_server;
1488 std::string m_local_close_reason;
1494 std::string m_remote_close_reason;
1497 lib::error_code m_ec;
1502 bool m_closed_by_me;
1505 bool m_failed_by_me;
1508 bool m_dropped_by_me;
1513 #include <websocketpp/impl/connection_impl.hpp>
1515 #endif // WEBSOCKETPP_CONNECTION_HPP
std::string const & get_remote_close_reason() const
Get the WebSocket close reason sent by the remote endpoint.
void add_subprotocol(std::string const &request, lib::error_code &ec)
Adds the given subprotocol string to the request list (exception free)
void set_open_handshake_timeout(long dur)
Set open handshake timeout.
void set_termination_handler(termination_handler new_handler)
connection_hdl get_handle() const
Get Connection Handle.
void set_pong_timeout(long dur)
Set pong timeout.
ptr get_shared()
Get a shared pointer to this component.
void read_frame()
Issue a new transport read unless reading is paused.
std::string const & get_subprotocol() const
Gets the negotated subprotocol.
uint16_t value
The type of a close code value.
message_ptr get_message(websocketpp::frame::opcode::value op, size_t size) const
Get a message buffer.
std::string const & get_origin() const
Return the same origin policy origin value from the opening request.
bool terminal(value code)
Determine if the code represents an unrecoverable error.
lib::function< void(connection_hdl)> close_handler
The type and function signature of a close handler.
lib::error_code pause_reading()
Pause reading of new data.
bool process_handshake_request()
lib::function< void(connection_hdl)> open_handler
The type and function signature of an open handler.
close::status::value get_local_close_code() const
Get the WebSocket close code sent by this endpoint.
lib::function< void(connection_hdl, std::string)> pong_timeout_handler
The type and function signature of a pong timeout handler.
std::string const & get_local_close_reason() const
Get the WebSocket close reason sent by this endpoint.
session::state::value get_state() const
Return the connection state.
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
lib::function< void(connection_hdl)> interrupt_handler
The type and function signature of an interrupt handler.
transport_con_type::ptr transport_con_ptr
Type of a shared pointer to the transport component of this connection.
void set_open_handler(open_handler h)
Set open handler.
WebSocket protocol processor abstract base class.
lib::function< bool(connection_hdl, std::string)> ping_handler
The type and function signature of a ping handler.
uri_ptr get_uri() const
Gets the connection URI.
void replace_header(std::string const &key, std::string const &val)
Replace a header.
void ping(std::string const &payload)
Send a ping.
lib::weak_ptr< type > weak_ptr
Type of a weak pointer to this connection.
Represents an individual WebSocket connection.
void set_fail_handler(fail_handler h)
Set fail handler.
size_t get_buffered_amount() const
Get the size of the outgoing write buffer (in payload bytes)
std::string const & get_request_header(std::string const &key)
Retrieve a request header.
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
void set_http_handler(http_handler h)
Set http handler.
void select_subprotocol(std::string const &value, lib::error_code &ec)
Select a subprotocol to use (exception free)
void set_close_handler(close_handler h)
Set close handler.
lib::error_code get_ec() const
Get the internal error code for a closed/failed connection.
void pong(std::string const &payload)
Send a pong.
void atomic_state_check(istate_type req, std::string msg)
Atomically read and compared the internal state.
void handle_resume_reading()
Resume reading callback.
config::alog_type alog_type
Type of the access logging policy.
std::string const & get_resource() const
Returns the resource component of the connection URI.
static level const devel
Development messages (warning: very chatty)
lib::function< bool(connection_hdl)> validate_handler
The type and function signature of a validate handler.
close::status::value get_remote_close_code() const
Get the WebSocket close code sent by the remote endpoint.
const std::vector< int > & get_supported_versions() const
Get array of WebSocket protocol versions that this connection supports.
void set_pong_handler(pong_handler h)
Set pong handler.
connection< config > type
Type of this connection.
std::string const & get_host() const
Returns the host component of the connection URI.
void set_close_handshake_timeout(long dur)
Set close handshake timeout.
lib::error_code resume_reading()
Resume reading of new data.
bool is_server() const
Get whether or not this connection is part of a server or client.
void close(close::status::value const code, std::string const &reason)
Close the connection.
void set_handle(connection_hdl hdl)
Set Connection Handle.
void atomic_state_change(istate_type req, istate_type dest, std::string msg)
Atomically change the internal connection state.
void set_status(http::status_code::value code)
Set response status code and message.
void handle_pong_timeout(std::string payload, lib::error_code const &ec)
Utility method that gets called back when the ping timer expires.
config::concurrency_type concurrency_type
Type of the concurrency component of this connection.
config::rng_type rng_type
Type of RNG.
void append_header(std::string const &key, std::string const &val)
Append a header.
lib::error_code send(std::string const &payload, frame::opcode::value op=frame::opcode::text)
Create a message and then add it to the outgoing send queue.
config::transport_type::transport_con_type transport_con_type
Type of the transport component of this connection.
lib::function< void(connection_hdl)> http_handler
The type and function signature of a http handler.
void set_ping_handler(ping_handler h)
Set ping handler.
void write_frame()
Checks if there are frames in the send queue and if there are sends one.
void handle_write_frame(lib::error_code const &ec)
Process the results of a frame write operation and start the next write.
Namespace for the WebSocket++ project.
void set_max_message_size(size_t new_value)
Set maximum message size.
void set_pong_timeout_handler(pong_timeout_handler h)
Set pong timeout handler.
uint16_t get_port() const
Returns the port component of the connection URI.
std::string const & get_response_header(std::string const &key)
Retrieve a response header.
void set_interrupt_handler(interrupt_handler h)
Set interrupt handler.
request_type const & get_request() const
Get request object.
static int const helper[]
Helper array to get around lack of initializer lists pre C++11.
bool initialize_processor()
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
void handle_interrupt()
Transport inturrupt callback.
void handle_pause_reading()
Pause reading callback.
void set_validate_handler(validate_handler h)
Set validate handler.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection.
size_t get_max_message_size() const
Get maximum message size.
void set_message_handler(message_handler h)
Set message handler.
std::vector< std::string > const & get_requested_subprotocols() const
Gets all of the subprotocols requested by the client.
bool get_secure() const
Returns the secure flag from the connection URI.
static value const abnormal_close
A dummy value to indicate that the connection was closed abnormally.
size_t buffered_amount() const
DEPRECATED: use get_buffered_amount instead.
lib::function< void(connection_hdl, std::string)> pong_handler
The type and function signature of a pong handler.
void set_uri(uri_ptr uri)
Sets the connection URI.
lib::error_code interrupt()
Asyncronously invoke handler::on_inturrupt.
transport_con_type::timer_ptr timer_ptr
Type of a pointer to a transport timer handle.
lib::function< void(connection_hdl)> fail_handler
The type and function signature of a fail handler.
config::elog_type elog_type
Type of the error logging policy.
static value const blank
A blank value for internal use.
void remove_header(std::string const &key)
Remove a header.
void set_body(std::string const &value)
Set response body content.