websocketpp  0.4.0
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 #include <websocketpp/logger/levels.hpp>
33 #include <websocketpp/version.hpp>
34 
35 #include <set>
36 
37 namespace websocketpp {
38 
40 template <typename connection, typename config>
41 class endpoint : public config::transport_type, public config::endpoint_base {
42 public:
43  // Import appropriate types from our helper class
44  // See endpoint_types for more details.
46 
48  typedef typename config::transport_type transport_type;
50  typedef typename config::concurrency_type concurrency_type;
51 
58 
61  typedef typename transport_type::transport_con_type transport_con_type;
64  typedef typename transport_con_type::ptr transport_con_ptr;
65 
67  typedef typename connection_type::message_handler message_handler;
69  typedef typename connection_type::message_ptr message_ptr;
70 
72  typedef typename config::elog_type elog_type;
74  typedef typename config::alog_type alog_type;
75 
77  typedef typename concurrency_type::scoped_lock_type scoped_lock_type;
79  typedef typename concurrency_type::mutex_type mutex_type;
80 
82  typedef typename config::rng_type rng_type;
83 
84  // TODO: organize these
85  typedef typename connection_type::termination_handler termination_handler;
86 
87  typedef lib::shared_ptr<connection_weak_ptr> hdl_type;
88 
89  explicit endpoint(bool p_is_server)
90  : m_alog(config::alog_level, log::channel_type_hint::access)
91  , m_elog(config::elog_level, log::channel_type_hint::error)
92  , m_user_agent(::websocketpp::user_agent)
93  , m_open_handshake_timeout_dur(config::timeout_open_handshake)
94  , m_close_handshake_timeout_dur(config::timeout_close_handshake)
95  , m_pong_timeout_dur(config::timeout_pong)
96  , m_max_message_size(config::max_message_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 
361  size_t get_max_message_size() const {
362  return m_max_message_size;
363  }
364 
366 
377  void set_max_message_size(size_t new_value) {
378  m_max_message_size = new_value;
379  }
380 
381  /*************************************/
382  /* Connection pass through functions */
383  /*************************************/
384 
393  void interrupt(connection_hdl hdl, lib::error_code & ec);
394  void interrupt(connection_hdl hdl);
395 
397 
416  void pause_reading(connection_hdl hdl, lib::error_code & ec);
417 
419  void pause_reading(connection_hdl hdl);
420 
422 
428  void resume_reading(connection_hdl hdl, lib::error_code & ec);
429 
431  void resume_reading(connection_hdl hdl);
432 
434 
442  void send(connection_hdl hdl, std::string const & payload,
443  frame::opcode::value op, lib::error_code & ec);
445 
453  void send(connection_hdl hdl, std::string const & payload,
454  frame::opcode::value op);
455 
456  void send(connection_hdl hdl, void const * payload, size_t len,
457  frame::opcode::value op, lib::error_code & ec);
458  void send(connection_hdl hdl, void const * payload, size_t len,
459  frame::opcode::value op);
460 
461  void send(connection_hdl hdl, message_ptr msg, lib::error_code & ec);
462  void send(connection_hdl hdl, message_ptr msg);
463 
464  void close(connection_hdl hdl, close::status::value const code,
465  std::string const & reason, lib::error_code & ec);
466  void close(connection_hdl hdl, close::status::value const code,
467  std::string const & reason);
468 
470 
477  void ping(connection_hdl hdl, std::string const & payload,
478  lib::error_code & ec);
480 
488  void ping(connection_hdl hdl, std::string const & payload);
489 
491 
498  void pong(connection_hdl hdl, std::string const & payload,
499  lib::error_code & ec);
501 
509  void pong(connection_hdl hdl, std::string const & payload);
510 
512 
525  connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code & ec) {
526  scoped_lock_type lock(m_mutex);
527  connection_ptr con = lib::static_pointer_cast<connection_type>(
528  hdl.lock());
529  if (!con) {
530  ec = error::make_error_code(error::bad_connection);
531  }
532  return con;
533  }
534 
536  connection_ptr get_con_from_hdl(connection_hdl hdl) {
537  lib::error_code ec;
538  connection_ptr con = this->get_con_from_hdl(hdl,ec);
539  if (ec) {
540  throw exception(ec);
541  }
542  return con;
543  }
544 protected:
545  connection_ptr create_connection();
546 
547  alog_type m_alog;
548  elog_type m_elog;
549 private:
550  // dynamic settings
551  std::string m_user_agent;
552 
553  open_handler m_open_handler;
554  close_handler m_close_handler;
555  fail_handler m_fail_handler;
556  ping_handler m_ping_handler;
557  pong_handler m_pong_handler;
558  pong_timeout_handler m_pong_timeout_handler;
559  interrupt_handler m_interrupt_handler;
560  http_handler m_http_handler;
561  validate_handler m_validate_handler;
562  message_handler m_message_handler;
563 
564  long m_open_handshake_timeout_dur;
565  long m_close_handshake_timeout_dur;
566  long m_pong_timeout_dur;
567  size_t m_max_message_size;
568 
569  rng_type m_rng;
570 
571  // static settings
572  bool const m_is_server;
573 
574  // endpoint state
575  mutable mutex_type m_mutex;
576 };
577 
578 } // namespace websocketpp
579 
580 #include <websocketpp/impl/endpoint_impl.hpp>
581 
582 #endif // WEBSOCKETPP_ENDPOINT_HPP
config::concurrency_type concurrency_type
Type of the concurrency component of this endpoint.
Definition: endpoint.hpp:50
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:361
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:82
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:69
void interrupt(connection_hdl hdl, lib::error_code &ec)
connection_type::message_handler message_handler
Type of message_handler.
Definition: endpoint.hpp:67
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:64
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:525
concurrency_type::scoped_lock_type scoped_lock_type
Type of our concurrency policy's scoped lock object.
Definition: endpoint.hpp:77
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:79
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_message_size(size_t new_value)
Set default maximum message size.
Definition: endpoint.hpp:377
transport_type::transport_con_type transport_con_type
Definition: endpoint.hpp:61
config::alog_type alog_type
Type of access logger.
Definition: endpoint.hpp:74
config::transport_type transport_type
Type of the transport component of this endpoint.
Definition: endpoint.hpp:48
connection_type::ptr connection_ptr
Shared pointer to connection_type.
Definition: endpoint.hpp:55
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:72
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:41
connection_type::weak_ptr connection_weak_ptr
Weak pointer to connection type.
Definition: endpoint.hpp:57
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:53
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:536
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