websocketpp  0.5.1
C++/Boost Asio based websocket client/server library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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_ENDPOINT_HPP
29 #define WEBSOCKETPP_ENDPOINT_HPP
30 
31 #include <websocketpp/connection.hpp>
32 
33 #include <websocketpp/logger/levels.hpp>
34 #include <websocketpp/version.hpp>
35 
36 #include <string>
37 
38 namespace websocketpp {
39 
41 template <typename connection, typename config>
42 class endpoint : public config::transport_type, public config::endpoint_base {
43 public:
44  // Import appropriate types from our helper class
45  // See endpoint_types for more details.
47 
49  typedef typename config::transport_type transport_type;
51  typedef typename config::concurrency_type concurrency_type;
52 
59 
62  typedef typename transport_type::transport_con_type transport_con_type;
65  typedef typename transport_con_type::ptr transport_con_ptr;
66 
68  typedef typename connection_type::message_handler message_handler;
70  typedef typename connection_type::message_ptr message_ptr;
71 
73  typedef typename config::elog_type elog_type;
75  typedef typename config::alog_type alog_type;
76 
78  typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
80  typedef typename concurrency_type::mutex_type mutex_type;
81 
83  typedef typename config::rng_type rng_type;
84 
85  // TODO: organize these
86  typedef typename connection_type::termination_handler termination_handler;
87 
88  explicit endpoint(bool p_is_server)
89  : m_alog(config::alog_level, log::channel_type_hint::access)
90  , m_elog(config::elog_level, log::channel_type_hint::error)
91  , m_user_agent(::websocketpp::user_agent)
92  , m_open_handshake_timeout_dur(config::timeout_open_handshake)
93  , m_close_handshake_timeout_dur(config::timeout_close_handshake)
94  , m_pong_timeout_dur(config::timeout_pong)
95  , m_max_message_size(config::max_message_size)
96  , m_max_http_body_size(config::max_http_body_size)
97  , m_is_server(p_is_server)
98  {
99  m_alog.set_channels(config::alog_level);
100  m_elog.set_channels(config::elog_level);
101 
102  m_alog.write(log::alevel::devel, "endpoint constructor");
103 
104  transport_type::init_logging(&m_alog, &m_elog);
105  }
106 
108 
116  std::string get_user_agent() const {
117  scoped_lock_type guard(m_mutex);
118  return m_user_agent;
119  }
120 
122 
143  void set_user_agent(std::string const & ua) {
144  scoped_lock_type guard(m_mutex);
145  m_user_agent = ua;
146  }
147 
149 
152  bool is_server() const {
153  return m_is_server;
154  }
155 
156  /********************************/
157  /* Pass-through logging adaptor */
158  /********************************/
159 
161 
167  void set_access_channels(log::level channels) {
168  m_alog.set_channels(channels);
169  }
170 
172 
178  void clear_access_channels(log::level channels) {
179  m_alog.clear_channels(channels);
180  }
181 
183 
189  void set_error_channels(log::level channels) {
190  m_elog.set_channels(channels);
191  }
192 
194 
200  void clear_error_channels(log::level channels) {
201  m_elog.clear_channels(channels);
202  }
203 
205 
208  alog_type & get_alog() {
209  return m_alog;
210  }
211 
213 
216  elog_type & get_elog() {
217  return m_elog;
218  }
219 
220  /*************************/
221  /* Set Handler functions */
222  /*************************/
223 
224  void set_open_handler(open_handler h) {
225  m_alog.write(log::alevel::devel,"set_open_handler");
226  scoped_lock_type guard(m_mutex);
227  m_open_handler = h;
228  }
229  void set_close_handler(close_handler h) {
230  m_alog.write(log::alevel::devel,"set_close_handler");
231  scoped_lock_type guard(m_mutex);
232  m_close_handler = h;
233  }
234  void set_fail_handler(fail_handler h) {
235  m_alog.write(log::alevel::devel,"set_fail_handler");
236  scoped_lock_type guard(m_mutex);
237  m_fail_handler = h;
238  }
239  void set_ping_handler(ping_handler h) {
240  m_alog.write(log::alevel::devel,"set_ping_handler");
241  scoped_lock_type guard(m_mutex);
242  m_ping_handler = h;
243  }
244  void set_pong_handler(pong_handler h) {
245  m_alog.write(log::alevel::devel,"set_pong_handler");
246  scoped_lock_type guard(m_mutex);
247  m_pong_handler = h;
248  }
249  void set_pong_timeout_handler(pong_timeout_handler h) {
250  m_alog.write(log::alevel::devel,"set_pong_timeout_handler");
251  scoped_lock_type guard(m_mutex);
252  m_pong_timeout_handler = h;
253  }
254  void set_interrupt_handler(interrupt_handler h) {
255  m_alog.write(log::alevel::devel,"set_interrupt_handler");
256  scoped_lock_type guard(m_mutex);
257  m_interrupt_handler = h;
258  }
259  void set_http_handler(http_handler h) {
260  m_alog.write(log::alevel::devel,"set_http_handler");
261  scoped_lock_type guard(m_mutex);
262  m_http_handler = h;
263  }
264  void set_validate_handler(validate_handler h) {
265  m_alog.write(log::alevel::devel,"set_validate_handler");
266  scoped_lock_type guard(m_mutex);
267  m_validate_handler = h;
268  }
269  void set_message_handler(message_handler h) {
270  m_alog.write(log::alevel::devel,"set_message_handler");
271  scoped_lock_type guard(m_mutex);
272  m_message_handler = h;
273  }
274 
276  // Connection timeouts and other limits //
278 
280 
299  void set_open_handshake_timeout(long dur) {
300  scoped_lock_type guard(m_mutex);
301  m_open_handshake_timeout_dur = dur;
302  }
303 
305 
325  scoped_lock_type guard(m_mutex);
326  m_close_handshake_timeout_dur = dur;
327  }
328 
330 
346  void set_pong_timeout(long dur) {
347  scoped_lock_type guard(m_mutex);
348  m_pong_timeout_dur = dur;
349  }
350 
352 
362  size_t get_max_message_size() const {
363  return m_max_message_size;
364  }
365 
367 
379  void set_max_message_size(size_t new_value) {
380  m_max_message_size = new_value;
381  }
382 
384 
396  size_t get_max_http_body_size() const {
397  return m_max_http_body_size;
398  }
399 
401 
413  void set_max_http_body_size(size_t new_value) {
414  m_max_http_body_size = new_value;
415  }
416 
417  /*************************************/
418  /* Connection pass through functions */
419  /*************************************/
420 
429  void interrupt(connection_hdl hdl, lib::error_code & ec);
430  void interrupt(connection_hdl hdl);
431 
433 
452  void pause_reading(connection_hdl hdl, lib::error_code & ec);
453 
455  void pause_reading(connection_hdl hdl);
456 
458 
464  void resume_reading(connection_hdl hdl, lib::error_code & ec);
465 
467  void resume_reading(connection_hdl hdl);
468 
470 
478  void send(connection_hdl hdl, std::string const & payload,
479  frame::opcode::value op, lib::error_code & ec);
481 
489  void send(connection_hdl hdl, std::string const & payload,
490  frame::opcode::value op);
491 
492  void send(connection_hdl hdl, void const * payload, size_t len,
493  frame::opcode::value op, lib::error_code & ec);
494  void send(connection_hdl hdl, void const * payload, size_t len,
495  frame::opcode::value op);
496 
497  void send(connection_hdl hdl, message_ptr msg, lib::error_code & ec);
498  void send(connection_hdl hdl, message_ptr msg);
499 
500  void close(connection_hdl hdl, close::status::value const code,
501  std::string const & reason, lib::error_code & ec);
502  void close(connection_hdl hdl, close::status::value const code,
503  std::string const & reason);
504 
506 
513  void ping(connection_hdl hdl, std::string const & payload,
514  lib::error_code & ec);
516 
524  void ping(connection_hdl hdl, std::string const & payload);
525 
527 
534  void pong(connection_hdl hdl, std::string const & payload,
535  lib::error_code & ec);
537 
545  void pong(connection_hdl hdl, std::string const & payload);
546 
548 
561  connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code & ec) {
562  scoped_lock_type lock(m_mutex);
563  connection_ptr con = lib::static_pointer_cast<connection_type>(
564  hdl.lock());
565  if (!con) {
566  ec = error::make_error_code(error::bad_connection);
567  }
568  return con;
569  }
570 
572  connection_ptr get_con_from_hdl(connection_hdl hdl) {
573  lib::error_code ec;
574  connection_ptr con = this->get_con_from_hdl(hdl,ec);
575  if (ec) {
576  throw exception(ec);
577  }
578  return con;
579  }
580 protected:
581  connection_ptr create_connection();
582 
583  alog_type m_alog;
584  elog_type m_elog;
585 private:
586  // dynamic settings
587  std::string m_user_agent;
588 
589  open_handler m_open_handler;
590  close_handler m_close_handler;
591  fail_handler m_fail_handler;
592  ping_handler m_ping_handler;
593  pong_handler m_pong_handler;
594  pong_timeout_handler m_pong_timeout_handler;
595  interrupt_handler m_interrupt_handler;
596  http_handler m_http_handler;
597  validate_handler m_validate_handler;
598  message_handler m_message_handler;
599 
600  long m_open_handshake_timeout_dur;
601  long m_close_handshake_timeout_dur;
602  long m_pong_timeout_dur;
603  size_t m_max_message_size;
604  size_t m_max_http_body_size;
605 
606  rng_type m_rng;
607 
608  // static settings
609  bool const m_is_server;
610 
611  // endpoint state
612  mutable mutex_type m_mutex;
613 };
614 
615 } // namespace websocketpp
616 
617 #include <websocketpp/impl/endpoint_impl.hpp>
618 
619 #endif // WEBSOCKETPP_ENDPOINT_HPP
config::concurrency_type concurrency_type
Type of the concurrency component of this endpoint.
Definition: endpoint.hpp:51
uint16_t value
The type of a close code value.
Definition: close.hpp:49
size_t get_max_message_size() const
Get default maximum message size.
Definition: endpoint.hpp:362
bool is_server() const
Returns whether or not this endpoint is a server.
Definition: endpoint.hpp:152
lib::function< void(connection_hdl)> close_handler
The type and function signature of a close handler.
Definition: connection.hpp:69
lib::function< void(connection_hdl)> open_handler
The type and function signature of an open handler.
Definition: connection.hpp:59
void ping(connection_hdl hdl, std::string const &payload, lib::error_code &ec)
Send a ping to a specific connection.
lib::function< void(connection_hdl, std::string)> pong_timeout_handler
The type and function signature of a pong timeout handler.
Definition: connection.hpp:116
lib::function< void(connection_hdl)> interrupt_handler
The type and function signature of an interrupt handler.
Definition: connection.hpp:91
config::rng_type rng_type
Type of RNG.
Definition: endpoint.hpp:83
lib::function< bool(connection_hdl, std::string)> ping_handler
The type and function signature of a ping handler.
Definition: connection.hpp:101
void set_close_handshake_timeout(long dur)
Set close handshake timeout.
Definition: endpoint.hpp:324
lib::weak_ptr< type > weak_ptr
Type of a weak pointer to this connection.
Definition: connection.hpp:231
Represents an individual WebSocket connection.
Definition: connection.hpp:221
connection_type::message_ptr message_ptr
Type of message pointers that this endpoint uses.
Definition: endpoint.hpp:70
void interrupt(connection_hdl hdl, lib::error_code &ec)
connection_type::message_handler message_handler
Type of message_handler.
Definition: endpoint.hpp:68
void set_open_handshake_timeout(long dur)
Set open handshake timeout.
Definition: endpoint.hpp:299
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
transport_con_type::ptr transport_con_ptr
Definition: endpoint.hpp:65
alog_type & get_alog()
Get reference to access logger.
Definition: endpoint.hpp:208
static char const user_agent[]
Default user agent string.
Definition: version.hpp:56
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
Definition: endpoint.hpp:561
concurrency_type::scoped_lock_type scoped_lock_type
Type of our concurrency policy's scoped lock object.
Definition: endpoint.hpp:78
void pause_reading(connection_hdl hdl, lib::error_code &ec)
Pause reading of new data (exception free)
std::string get_user_agent() const
Returns the user agent string that this endpoint will use.
Definition: endpoint.hpp:116
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
concurrency_type::mutex_type mutex_type
Type of our concurrency policy's mutex object.
Definition: endpoint.hpp:80
void resume_reading(connection_hdl hdl, lib::error_code &ec)
Resume reading of new data (exception free)
lib::function< bool(connection_hdl)> validate_handler
The type and function signature of a validate handler.
Definition: connection.hpp:129
void set_max_http_body_size(size_t new_value)
Set maximum HTTP message body size.
Definition: endpoint.hpp:413
void set_max_message_size(size_t new_value)
Set default maximum message size.
Definition: endpoint.hpp:379
transport_type::transport_con_type transport_con_type
Definition: endpoint.hpp:62
config::alog_type alog_type
Type of access logger.
Definition: endpoint.hpp:75
size_t get_max_http_body_size() const
Get maximum HTTP message body size.
Definition: endpoint.hpp:396
config::transport_type transport_type
Type of the transport component of this endpoint.
Definition: endpoint.hpp:49
connection_type::ptr connection_ptr
Shared pointer to connection_type.
Definition: endpoint.hpp:56
lib::function< void(connection_hdl)> http_handler
The type and function signature of a http handler.
Definition: connection.hpp:151
void pong(connection_hdl hdl, std::string const &payload, lib::error_code &ec)
Send a pong to a specific connection.
void clear_error_channels(log::level channels)
Clear Error logging channels.
Definition: endpoint.hpp:200
void set_access_channels(log::level channels)
Set Access logging channel.
Definition: endpoint.hpp:167
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
elog_type & get_elog()
Get reference to error logger.
Definition: endpoint.hpp:216
config::elog_type elog_type
Type of error logger.
Definition: endpoint.hpp:73
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:42
connection_type::weak_ptr connection_weak_ptr
Weak pointer to connection type.
Definition: endpoint.hpp:58
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection.
Definition: connection.hpp:229
connection connection_type
Type of the connections that this endpoint creates.
Definition: endpoint.hpp:54
void clear_access_channels(log::level channels)
Clear Access logging channels.
Definition: endpoint.hpp:178
void set_error_channels(log::level channels)
Set Error logging channel.
Definition: endpoint.hpp:189
void set_pong_timeout(long dur)
Set pong timeout.
Definition: endpoint.hpp:346
lib::function< void(connection_hdl, std::string)> pong_handler
The type and function signature of a pong handler.
Definition: connection.hpp:109
connection_ptr get_con_from_hdl(connection_hdl hdl)
Retrieves a connection_ptr from a connection_hdl (exception version)
Definition: endpoint.hpp:572
void set_user_agent(std::string const &ua)
Sets the user agent string that this endpoint will use.
Definition: endpoint.hpp:143
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)
lib::function< void(connection_hdl)> fail_handler
The type and function signature of a fail handler.
Definition: connection.hpp:79