websocketpp  0.3.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) 2013, 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 <iostream>
36 #include <set>
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  typedef lib::shared_ptr<connection_weak_ptr> hdl_type;
89 
90  explicit endpoint(bool p_is_server)
91  : m_alog(config::alog_level, &std::cout)
92  , m_elog(config::elog_level, &std::cerr)
93  , m_user_agent(::websocketpp::user_agent)
94  , m_open_handshake_timeout_dur(config::timeout_open_handshake)
95  , m_close_handshake_timeout_dur(config::timeout_close_handshake)
96  , m_pong_timeout_dur(config::timeout_pong)
97  , m_max_message_size(config::max_message_size)
98  , m_is_server(p_is_server)
99  {
100  m_alog.set_channels(config::alog_level);
101  m_elog.set_channels(config::elog_level);
102 
103  m_alog.write(log::alevel::devel, "endpoint constructor");
104 
105  transport_type::init_logging(&m_alog, &m_elog);
106  }
107 
109 
117  std::string get_user_agent() const {
118  scoped_lock_type guard(m_mutex);
119  return m_user_agent;
120  }
121 
123 
144  void set_user_agent(std::string const & ua) {
145  scoped_lock_type guard(m_mutex);
146  m_user_agent = ua;
147  }
148 
150 
153  bool is_server() const {
154  return m_is_server;
155  }
156 
157  /********************************/
158  /* Pass-through logging adaptor */
159  /********************************/
160 
162 
168  void set_access_channels(log::level channels) {
169  m_alog.set_channels(channels);
170  }
171 
173 
179  void clear_access_channels(log::level channels) {
180  m_alog.clear_channels(channels);
181  }
182 
184 
190  void set_error_channels(log::level channels) {
191  m_elog.set_channels(channels);
192  }
193 
195 
201  void clear_error_channels(log::level channels) {
202  m_elog.clear_channels(channels);
203  }
204 
206 
209  alog_type & get_alog() {
210  return m_alog;
211  }
212 
214 
217  elog_type & get_elog() {
218  return m_elog;
219  }
220 
221  /*************************/
222  /* Set Handler functions */
223  /*************************/
224 
225  void set_open_handler(open_handler h) {
226  m_alog.write(log::alevel::devel,"set_open_handler");
227  scoped_lock_type guard(m_mutex);
228  m_open_handler = h;
229  }
230  void set_close_handler(close_handler h) {
231  m_alog.write(log::alevel::devel,"set_close_handler");
232  scoped_lock_type guard(m_mutex);
233  m_close_handler = h;
234  }
235  void set_fail_handler(fail_handler h) {
236  m_alog.write(log::alevel::devel,"set_fail_handler");
237  scoped_lock_type guard(m_mutex);
238  m_fail_handler = h;
239  }
240  void set_ping_handler(ping_handler h) {
241  m_alog.write(log::alevel::devel,"set_ping_handler");
242  scoped_lock_type guard(m_mutex);
243  m_ping_handler = h;
244  }
245  void set_pong_handler(pong_handler h) {
246  m_alog.write(log::alevel::devel,"set_pong_handler");
247  scoped_lock_type guard(m_mutex);
248  m_pong_handler = h;
249  }
250  void set_pong_timeout_handler(pong_timeout_handler h) {
251  m_alog.write(log::alevel::devel,"set_pong_timeout_handler");
252  scoped_lock_type guard(m_mutex);
253  m_pong_timeout_handler = h;
254  }
255  void set_interrupt_handler(interrupt_handler h) {
256  m_alog.write(log::alevel::devel,"set_interrupt_handler");
257  scoped_lock_type guard(m_mutex);
258  m_interrupt_handler = h;
259  }
260  void set_http_handler(http_handler h) {
261  m_alog.write(log::alevel::devel,"set_http_handler");
262  scoped_lock_type guard(m_mutex);
263  m_http_handler = h;
264  }
265  void set_validate_handler(validate_handler h) {
266  m_alog.write(log::alevel::devel,"set_validate_handler");
267  scoped_lock_type guard(m_mutex);
268  m_validate_handler = h;
269  }
270  void set_message_handler(message_handler h) {
271  m_alog.write(log::alevel::devel,"set_message_handler");
272  scoped_lock_type guard(m_mutex);
273  m_message_handler = h;
274  }
275 
277  // Connection timeouts and other limits //
279 
281 
300  void set_open_handshake_timeout(long dur) {
301  scoped_lock_type guard(m_mutex);
302  m_open_handshake_timeout_dur = dur;
303  }
304 
306 
326  scoped_lock_type guard(m_mutex);
327  m_close_handshake_timeout_dur = dur;
328  }
329 
331 
347  void set_pong_timeout(long dur) {
348  scoped_lock_type guard(m_mutex);
349  m_pong_timeout_dur = dur;
350  }
351 
353 
362  size_t get_max_message_size() const {
363  return m_max_message_size;
364  }
365 
367 
378  void set_max_message_size(size_t new_value) {
379  m_max_message_size = new_value;
380  }
381 
382  /*************************************/
383  /* Connection pass through functions */
384  /*************************************/
385 
394  void interrupt(connection_hdl hdl, lib::error_code & ec);
395  void interrupt(connection_hdl hdl);
396 
398 
417  void pause_reading(connection_hdl hdl, lib::error_code & ec);
418 
420  void pause_reading(connection_hdl hdl);
421 
423 
429  void resume_reading(connection_hdl hdl, lib::error_code & ec);
430 
432  void resume_reading(connection_hdl hdl);
433 
435 
443  void send(connection_hdl hdl, std::string const & payload,
444  frame::opcode::value op, lib::error_code & ec);
446 
454  void send(connection_hdl hdl, std::string const & payload,
455  frame::opcode::value op);
456 
457  void send(connection_hdl hdl, void const * payload, size_t len,
458  frame::opcode::value op, lib::error_code & ec);
459  void send(connection_hdl hdl, void const * payload, size_t len,
460  frame::opcode::value op);
461 
462  void send(connection_hdl hdl, message_ptr msg, lib::error_code & ec);
463  void send(connection_hdl hdl, message_ptr msg);
464 
465  void close(connection_hdl hdl, close::status::value const code,
466  std::string const & reason, lib::error_code & ec);
467  void close(connection_hdl hdl, close::status::value const code,
468  std::string const & reason);
469 
471 
478  void ping(connection_hdl hdl, std::string const & payload,
479  lib::error_code & ec);
481 
489  void ping(connection_hdl hdl, std::string const & payload);
490 
492 
499  void pong(connection_hdl hdl, std::string const & payload,
500  lib::error_code & ec);
502 
510  void pong(connection_hdl hdl, std::string const & payload);
511 
513 
526  connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code & ec) {
527  scoped_lock_type lock(m_mutex);
528  connection_ptr con = lib::static_pointer_cast<connection_type>(
529  hdl.lock());
530  if (!con) {
531  ec = error::make_error_code(error::bad_connection);
532  }
533  return con;
534  }
535 
537  connection_ptr get_con_from_hdl(connection_hdl hdl) {
538  lib::error_code ec;
539  connection_ptr con = this->get_con_from_hdl(hdl,ec);
540  if (ec) {
541  throw ec;
542  }
543  return con;
544  }
545 protected:
546  connection_ptr create_connection();
547 
548  alog_type m_alog;
549  elog_type m_elog;
550 private:
551  // dynamic settings
552  std::string m_user_agent;
553 
554  open_handler m_open_handler;
555  close_handler m_close_handler;
556  fail_handler m_fail_handler;
557  ping_handler m_ping_handler;
558  pong_handler m_pong_handler;
559  pong_timeout_handler m_pong_timeout_handler;
560  interrupt_handler m_interrupt_handler;
561  http_handler m_http_handler;
562  validate_handler m_validate_handler;
563  message_handler m_message_handler;
564 
565  long m_open_handshake_timeout_dur;
566  long m_close_handshake_timeout_dur;
567  long m_pong_timeout_dur;
568  size_t m_max_message_size;
569 
570  rng_type m_rng;
571 
572  // static settings
573  bool const m_is_server;
574 
575  // endpoint state
576  mutable mutex_type m_mutex;
577 };
578 
579 } // namespace websocketpp
580 
581 #include <websocketpp/impl/endpoint_impl.hpp>
582 
583 #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:153
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:325
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
STL namespace.
void set_open_handshake_timeout(long dur)
Set open handshake timeout.
Definition: endpoint.hpp:300
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:209
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:526
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:117
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:122
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_message_size(size_t new_value)
Set default maximum message size.
Definition: endpoint.hpp:378
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
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:201
void set_access_channels(log::level channels)
Set Access logging channel.
Definition: endpoint.hpp:168
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
elog_type & get_elog()
Get reference to error logger.
Definition: endpoint.hpp:217
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:179
void set_error_channels(log::level channels)
Set Error logging channel.
Definition: endpoint.hpp:190
void set_pong_timeout(long dur)
Set pong timeout.
Definition: endpoint.hpp:347
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:537
void set_user_agent(std::string const &ua)
Sets the user agent string that this endpoint will use.
Definition: endpoint.hpp:144
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