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_TRANSPORT_ASIO_HPP
29 #define WEBSOCKETPP_TRANSPORT_ASIO_HPP
30 
31 #include <websocketpp/common/functional.hpp>
32 #include <websocketpp/logger/levels.hpp>
33 #include <websocketpp/transport/base/endpoint.hpp>
34 #include <websocketpp/transport/asio/connection.hpp>
35 #include <websocketpp/transport/asio/security/none.hpp>
36 
37 #include <boost/asio.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/system/error_code.hpp>
40 
41 #include <iostream>
42 
43 namespace websocketpp {
44 namespace transport {
45 namespace asio {
46 
48 
52 template <typename config>
53 class endpoint : public config::socket_type {
54 public:
57 
59  typedef typename config::concurrency_type concurrency_type;
61  typedef typename config::socket_type socket_type;
63  typedef typename config::elog_type elog_type;
65  typedef typename config::alog_type alog_type;
66 
68  typedef typename socket_type::socket_con_type socket_con_type;
70  typedef typename socket_con_type::ptr socket_con_ptr;
71 
78 
80  typedef boost::asio::io_service* io_service_ptr;
82  typedef lib::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor_ptr;
84  typedef lib::shared_ptr<boost::asio::ip::tcp::resolver> resolver_ptr;
86  typedef lib::shared_ptr<boost::asio::deadline_timer> timer_ptr;
88  typedef lib::shared_ptr<boost::asio::io_service::work> work_ptr;
89 
90  // generate and manage our own io_service
91  explicit endpoint()
92  : m_external_io_service(false)
93  , m_listen_backlog(0)
94  , m_reuse_addr(false)
95  , m_state(UNINITIALIZED)
96  {
97  //std::cout << "transport::asio::endpoint constructor" << std::endl;
98  }
99 
100  ~endpoint() {
101  // clean up our io_service if we were initialized with an internal one.
102  m_acceptor.reset();
103  if (m_state != UNINITIALIZED && !m_external_io_service) {
104  delete m_io_service;
105  }
106  }
107 
111 #ifdef _WEBSOCKETPP_DELETED_FUNCTIONS_
112  endpoint(const endpoint& src) = delete;
113  endpoint& operator= (const endpoint & rhs) = delete;
114 #else
115 private:
116  endpoint(const endpoint& src);
117  endpoint& operator= (const endpoint & rhs);
118 public:
119 #endif
120 
121 #ifdef _WEBSOCKETPP_RVALUE_REFERENCES_
122  endpoint (endpoint&& src)
123  : m_io_service(src.m_io_service)
124  , m_external_io_service(src.m_external_io_service)
125  , m_acceptor(src.m_acceptor)
126  , m_listen_backlog(boost::asio::socket_base::max_connections)
127  , m_reuse_addr(src.m_reuse_addr)
128  , m_state(src.m_state)
129  {
130  src.m_io_service = NULL;
131  src.m_external_io_service = false;
132  src.m_acceptor = NULL;
133  src.m_state = UNINITIALIZED;
134  }
135 
136  endpoint& operator= (const endpoint && rhs) {
137  if (this != &rhs) {
138  m_io_service = rhs.m_io_service;
139  m_external_io_service = rhs.m_external_io_service;
140  m_acceptor = rhs.m_acceptor;
141  m_listen_backlog = rhs.m_listen_backlog;
142  m_reuse_addr = rhs.m_reuse_addr;
143  m_state = rhs.m_state;
144 
145  rhs.m_io_service = NULL;
146  rhs.m_external_io_service = false;
147  rhs.m_acceptor = NULL;
148  rhs.m_listen_backlog = boost::asio::socket_base::max_connections;
149  rhs.m_state = UNINITIALIZED;
150  }
151  return *this;
152  }
153 #endif
154  bool is_secure() const {
156  return socket_type::is_secure();
157  }
158 
160 
168  void init_asio(io_service_ptr ptr, lib::error_code & ec) {
169  if (m_state != UNINITIALIZED) {
170  m_elog->write(log::elevel::library,
171  "asio::init_asio called from the wrong state");
172  using websocketpp::error::make_error_code;
173  ec = make_error_code(websocketpp::error::invalid_state);
174  return;
175  }
176 
177  m_alog->write(log::alevel::devel,"asio::init_asio");
178 
179  m_io_service = ptr;
180  m_external_io_service = true;
181  m_acceptor.reset(new boost::asio::ip::tcp::acceptor(*m_io_service));
182  m_state = READY;
183  ec = lib::error_code();
184  }
185 
187 
194  void init_asio(io_service_ptr ptr) {
195  lib::error_code ec;
196  init_asio(ptr,ec);
197  if (ec) {
198  throw ec;
199  }
200  }
201 
203 
211  void init_asio(lib::error_code & ec) {
212  init_asio(new boost::asio::io_service(),ec);
213  m_external_io_service = false;
214  }
215 
217 
223  void init_asio() {
224  init_asio(new boost::asio::io_service());
225  m_external_io_service = false;
226  }
227 
229 
238  void set_tcp_pre_init_handler(tcp_init_handler h) {
239  m_tcp_pre_init_handler = h;
240  }
241 
243 
252  void set_tcp_init_handler(tcp_init_handler h) {
254  }
255 
257 
267  void set_tcp_post_init_handler(tcp_init_handler h) {
268  m_tcp_post_init_handler = h;
269  }
270 
272 
290  void set_listen_backlog(int backlog) {
291  m_listen_backlog = backlog;
292  }
293 
295 
308  void set_reuse_addr(bool value) {
309  m_reuse_addr = value;
310  }
311 
313 
323  boost::asio::io_service & get_io_service() {
324  return *m_io_service;
325  }
326 
328 
335  void listen(boost::asio::ip::tcp::endpoint const & ep, lib::error_code & ec)
336  {
337  if (m_state != READY) {
338  m_elog->write(log::elevel::library,
339  "asio::listen called from the wrong state");
340  using websocketpp::error::make_error_code;
341  ec = make_error_code(websocketpp::error::invalid_state);
342  return;
343  }
344 
345  m_alog->write(log::alevel::devel,"asio::listen");
346 
347  boost::system::error_code bec;
348 
349  m_acceptor->open(ep.protocol(),bec);
350  if (!bec) {
351  m_acceptor->set_option(boost::asio::socket_base::reuse_address(m_reuse_addr),bec);
352  }
353  if (!bec) {
354  m_acceptor->bind(ep,bec);
355  }
356  if (!bec) {
357  m_acceptor->listen(m_listen_backlog,bec);
358  }
359  if (bec) {
360  log_err(log::elevel::info,"asio listen",bec);
361  ec = make_error_code(error::pass_through);
362  } else {
363  m_state = LISTENING;
364  ec = lib::error_code();
365  }
366  }
367 
369 
374  void listen(boost::asio::ip::tcp::endpoint const & ep) {
375  lib::error_code ec;
376  listen(ep,ec);
377  if (ec) {
378  throw ec;
379  }
380  }
381 
383 
396  template <typename InternetProtocol>
397  void listen(InternetProtocol const & internet_protocol, uint16_t port,
398  lib::error_code & ec)
399  {
400  boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
401  listen(ep,ec);
402  }
403 
405 
417  template <typename InternetProtocol>
418  void listen(InternetProtocol const & internet_protocol, uint16_t port)
419  {
420  boost::asio::ip::tcp::endpoint ep(internet_protocol, port);
421  listen(ep);
422  }
423 
425 
436  void listen(uint16_t port, lib::error_code & ec) {
437  listen(boost::asio::ip::tcp::v6(), port, ec);
438  }
439 
441 
452  void listen(uint16_t port) {
453  listen(boost::asio::ip::tcp::v6(), port);
454  }
455 
457 
472  void listen(std::string const & host, std::string const & service,
473  lib::error_code & ec)
474  {
475  using boost::asio::ip::tcp;
476  tcp::resolver r(*m_io_service);
477  tcp::resolver::query query(host, service);
478  tcp::resolver::iterator endpoint_iterator = r.resolve(query);
479  tcp::resolver::iterator end;
480  if (endpoint_iterator == end) {
481  m_elog->write(log::elevel::library,
482  "asio::listen could not resolve the supplied host or service");
483  ec = make_error_code(error::invalid_host_service);
484  return;
485  }
486  listen(*endpoint_iterator,ec);
487  }
488 
490 
505  void listen(std::string const & host, std::string const & service)
506  {
507  lib::error_code ec;
508  listen(host,service,ec);
509  if (ec) {
510  throw ec;
511  }
512  }
513 
515 
522  void stop_listening(lib::error_code & ec) {
523  if (m_state != LISTENING) {
524  m_elog->write(log::elevel::library,
525  "asio::listen called from the wrong state");
526  using websocketpp::error::make_error_code;
527  ec = make_error_code(websocketpp::error::invalid_state);
528  return;
529  }
530 
531  m_acceptor->close();
532  m_state = READY;
533  ec = lib::error_code();
534  }
535 
537 
543  void stop_listening() {
544  lib::error_code ec;
545  stop_listening(ec);
546  if (ec) {
547  throw ec;
548  }
549  }
550 
552 
555  bool is_listening() const {
556  return (m_state == LISTENING);
557  }
558 
560  std::size_t run() {
561  return m_io_service->run();
562  }
563 
565 
568  std::size_t run_one() {
569  return m_io_service->run_one();
570  }
571 
573  void stop() {
574  m_io_service->stop();
575  }
576 
578  std::size_t poll() {
579  return m_io_service->poll();
580  }
581 
583  std::size_t poll_one() {
584  return m_io_service->poll_one();
585  }
586 
588  void reset() {
589  m_io_service->reset();
590  }
591 
593  bool stopped() const {
594  return m_io_service->stopped();
595  }
596 
598 
610  m_work.reset(new boost::asio::io_service::work(*m_io_service));
611  }
612 
614 
621  void stop_perpetual() {
622  m_work.reset();
623  }
624 
626 
637  timer_ptr set_timer(long duration, timer_handler callback) {
638  timer_ptr new_timer(
639  new boost::asio::deadline_timer(
640  *m_io_service,
641  boost::posix_time::milliseconds(duration)
642  )
643  );
644 
645  new_timer->async_wait(
646  lib::bind(
648  this,
649  new_timer,
650  callback,
651  lib::placeholders::_1
652  )
653  );
654 
655  return new_timer;
656  }
657 
659 
667  void handle_timer(timer_ptr t, timer_handler callback,
668  boost::system::error_code const & ec)
669  {
670  if (ec) {
671  if (ec == boost::asio::error::operation_aborted) {
672  callback(make_error_code(transport::error::operation_aborted));
673  } else {
674  m_elog->write(log::elevel::info,
675  "asio handle_timer error: "+ec.message());
676  log_err(log::elevel::info,"asio handle_timer",ec);
677  callback(make_error_code(error::pass_through));
678  }
679  } else {
680  callback(lib::error_code());
681  }
682  }
683 
685 
690  void async_accept(transport_con_ptr tcon, accept_handler callback,
691  lib::error_code & ec)
692  {
693  if (m_state != LISTENING) {
694  using websocketpp::error::make_error_code;
696  return;
697  }
698 
699  m_alog->write(log::alevel::devel, "asio::async_accept");
700 
701  if (config::enable_multithreading) {
702  m_acceptor->async_accept(
703  tcon->get_raw_socket(),
704  tcon->get_strand()->wrap(lib::bind(
705  &type::handle_accept,
706  this,
707  callback,
708  lib::placeholders::_1
709  ))
710  );
711  } else {
712  m_acceptor->async_accept(
713  tcon->get_raw_socket(),
714  lib::bind(
715  &type::handle_accept,
716  this,
717  callback,
718  lib::placeholders::_1
719  )
720  );
721  }
722  }
723 
725 
729  void async_accept(transport_con_ptr tcon, accept_handler callback) {
730  lib::error_code ec;
731  async_accept(tcon,callback,ec);
732  if (ec) {
733  throw ec;
734  }
735  }
736 protected:
738 
747  void init_logging(alog_type* a, elog_type* e) {
748  m_alog = a;
749  m_elog = e;
750  }
751 
752  void handle_accept(accept_handler callback, boost::system::error_code const
753  & boost_ec)
754  {
755  lib::error_code ret_ec;
756 
757  m_alog->write(log::alevel::devel, "asio::handle_accept");
758 
759  if (boost_ec) {
760  if (boost_ec == boost::system::errc::operation_canceled) {
761  ret_ec = make_error_code(websocketpp::error::operation_canceled);
762  } else {
763  log_err(log::elevel::info,"asio handle_accept",boost_ec);
764  ret_ec = make_error_code(error::pass_through);
765  }
766  }
767 
768  callback(ret_ec);
769  }
770 
772  // TODO: there have to be some more failure conditions here
773  void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb) {
774  using namespace boost::asio::ip;
775 
776  // Create a resolver
777  if (!m_resolver) {
778  m_resolver.reset(new boost::asio::ip::tcp::resolver(*m_io_service));
779  }
780 
781  std::string proxy = tcon->get_proxy();
782  std::string host;
783  std::string port;
784 
785  if (proxy.empty()) {
786  host = u->get_host();
787  port = u->get_port_str();
788  } else {
789  lib::error_code ec;
790 
791  uri_ptr pu(new uri(proxy));
792 
793  if (!pu->get_valid()) {
794  cb(make_error_code(error::proxy_invalid));
795  return;
796  }
797 
798  ec = tcon->proxy_init(u->get_authority());
799  if (ec) {
800  cb(ec);
801  return;
802  }
803 
804  host = pu->get_host();
805  port = pu->get_port_str();
806  }
807 
808  tcp::resolver::query query(host,port);
809 
810  if (m_alog->static_test(log::alevel::devel)) {
811  m_alog->write(log::alevel::devel,
812  "starting async DNS resolve for "+host+":"+port);
813  }
814 
815  timer_ptr dns_timer;
816 
817  dns_timer = tcon->set_timer(
818  config::timeout_dns_resolve,
819  lib::bind(
820  &type::handle_resolve_timeout,
821  this,
822  dns_timer,
823  cb,
824  lib::placeholders::_1
825  )
826  );
827 
828  if (config::enable_multithreading) {
829  m_resolver->async_resolve(
830  query,
831  tcon->get_strand()->wrap(lib::bind(
832  &type::handle_resolve,
833  this,
834  tcon,
835  dns_timer,
836  cb,
837  lib::placeholders::_1,
838  lib::placeholders::_2
839  ))
840  );
841  } else {
842  m_resolver->async_resolve(
843  query,
844  lib::bind(
845  &type::handle_resolve,
846  this,
847  tcon,
848  dns_timer,
849  cb,
850  lib::placeholders::_1,
851  lib::placeholders::_2
852  )
853  );
854  }
855  }
856 
857  void handle_resolve_timeout(timer_ptr dns_timer, connect_handler callback,
858  lib::error_code const & ec)
859  {
860  lib::error_code ret_ec;
861 
862  if (ec) {
864  m_alog->write(log::alevel::devel,
865  "asio handle_resolve_timeout timer cancelled");
866  return;
867  }
868 
869  log_err(log::elevel::devel,"asio handle_resolve_timeout",ec);
870  ret_ec = ec;
871  } else {
872  ret_ec = make_error_code(transport::error::timeout);
873  }
874 
875  m_alog->write(log::alevel::devel,"DNS resolution timed out");
876  m_resolver->cancel();
877  callback(ret_ec);
878  }
879 
880  void handle_resolve(transport_con_ptr tcon, timer_ptr dns_timer,
881  connect_handler callback, boost::system::error_code const & ec,
882  boost::asio::ip::tcp::resolver::iterator iterator)
883  {
884  if (ec == boost::asio::error::operation_aborted ||
885  dns_timer->expires_from_now().is_negative())
886  {
887  m_alog->write(log::alevel::devel,"async_resolve cancelled");
888  return;
889  }
890 
891  dns_timer->cancel();
892 
893  if (ec) {
894  log_err(log::elevel::info,"asio async_resolve",ec);
895  callback(make_error_code(error::pass_through));
896  return;
897  }
898 
899  if (m_alog->static_test(log::alevel::devel)) {
900  std::stringstream s;
901  s << "Async DNS resolve successful. Results: ";
902 
903  boost::asio::ip::tcp::resolver::iterator it, end;
904  for (it = iterator; it != end; ++it) {
905  s << (*it).endpoint() << " ";
906  }
907 
908  m_alog->write(log::alevel::devel,s.str());
909  }
910 
911  m_alog->write(log::alevel::devel,"Starting async connect");
912 
913  timer_ptr con_timer;
914 
915  con_timer = tcon->set_timer(
916  config::timeout_connect,
917  lib::bind(
918  &type::handle_connect_timeout,
919  this,
920  tcon,
921  con_timer,
922  callback,
923  lib::placeholders::_1
924  )
925  );
926 
927  if (config::enable_multithreading) {
928  boost::asio::async_connect(
929  tcon->get_raw_socket(),
930  iterator,
931  tcon->get_strand()->wrap(lib::bind(
932  &type::handle_connect,
933  this,
934  tcon,
935  con_timer,
936  callback,
937  lib::placeholders::_1
938  ))
939  );
940  } else {
941  boost::asio::async_connect(
942  tcon->get_raw_socket(),
943  iterator,
944  lib::bind(
945  &type::handle_connect,
946  this,
947  tcon,
948  con_timer,
949  callback,
950  lib::placeholders::_1
951  )
952  );
953  }
954  }
955 
956  void handle_connect_timeout(transport_con_ptr tcon, timer_ptr con_timer,
957  connect_handler callback, lib::error_code const & ec)
958  {
959  lib::error_code ret_ec;
960 
961  if (ec) {
963  m_alog->write(log::alevel::devel,
964  "asio handle_connect_timeout timer cancelled");
965  return;
966  }
967 
968  log_err(log::elevel::devel,"asio handle_connect_timeout",ec);
969  ret_ec = ec;
970  } else {
971  ret_ec = make_error_code(transport::error::timeout);
972  }
973 
974  m_alog->write(log::alevel::devel,"TCP connect timed out");
975  tcon->cancel_socket();
976  callback(ret_ec);
977  }
978 
979  void handle_connect(transport_con_ptr tcon, timer_ptr con_timer,
980  connect_handler callback, boost::system::error_code const & ec)
981  {
982  if (ec == boost::asio::error::operation_aborted ||
983  con_timer->expires_from_now().is_negative())
984  {
985  m_alog->write(log::alevel::devel,"async_connect cancelled");
986  return;
987  }
988 
989  con_timer->cancel();
990 
991  if (ec) {
992  log_err(log::elevel::info,"asio async_connect",ec);
993  callback(make_error_code(error::pass_through));
994  return;
995  }
996 
997  if (m_alog->static_test(log::alevel::devel)) {
998  m_alog->write(log::alevel::devel,
999  "Async connect to "+tcon->get_remote_endpoint()+" successful.");
1000  }
1001 
1002  callback(lib::error_code());
1003  }
1004 
1006 
1016  lib::error_code init(transport_con_ptr tcon) {
1017  m_alog->write(log::alevel::devel, "transport::asio::init");
1018 
1019  // Initialize the connection socket component
1020  socket_type::init(lib::static_pointer_cast<socket_con_type,
1021  transport_con_type>(tcon));
1022 
1023  lib::error_code ec;
1024 
1025  ec = tcon->init_asio(m_io_service);
1026  if (ec) {return ec;}
1027 
1028  tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1029  tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1030 
1031  return lib::error_code();
1032  }
1033 private:
1035  template <typename error_type>
1036  void log_err(log::level l, char const * msg, error_type const & ec) {
1037  std::stringstream s;
1038  s << msg << " error: " << ec << " (" << ec.message() << ")";
1039  m_elog->write(l,s.str());
1040  }
1041 
1042  enum state {
1043  UNINITIALIZED = 0,
1044  READY = 1,
1045  LISTENING = 2
1046  };
1047 
1048  // Handlers
1049  tcp_init_handler m_tcp_pre_init_handler;
1050  tcp_init_handler m_tcp_post_init_handler;
1051 
1052  // Network Resources
1053  io_service_ptr m_io_service;
1054  bool m_external_io_service;
1055  acceptor_ptr m_acceptor;
1056  resolver_ptr m_resolver;
1057  work_ptr m_work;
1058 
1059  // Network constants
1060  int m_listen_backlog;
1061  bool m_reuse_addr;
1062 
1063  elog_type* m_elog;
1064  alog_type* m_alog;
1065 
1066  // Transport state
1067  state m_state;
1068 };
1069 
1070 } // namespace asio
1071 } // namespace transport
1072 } // namespace websocketpp
1073 
1074 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
Definition: endpoint.hpp:522
lib::shared_ptr< boost::asio::ip::tcp::acceptor > acceptor_ptr
Type of a shared pointer to the acceptor being used.
Definition: endpoint.hpp:82
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
Definition: endpoint.hpp:773
transport_con_type::ptr transport_con_ptr
Definition: endpoint.hpp:77
boost::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: endpoint.hpp:80
lib::shared_ptr< boost::asio::deadline_timer > timer_ptr
Type of timer handle.
Definition: endpoint.hpp:86
uint16_t value
The type of a close code value.
Definition: close.hpp:49
lib::shared_ptr< boost::asio::ip::tcp::resolver > resolver_ptr
Type of a shared pointer to the resolver being used.
Definition: endpoint.hpp:84
config::alog_type alog_type
Type of the access logging policy.
Definition: endpoint.hpp:65
void handle_timer(timer_ptr t, timer_handler callback, boost::system::error_code const &ec)
Timer callback.
Definition: endpoint.hpp:667
void listen(boost::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
Definition: endpoint.hpp:374
void listen(InternetProtocol const &internet_protocol, uint16_t port)
Set up endpoint for listening with protocol and port.
Definition: endpoint.hpp:418
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
Definition: endpoint.hpp:290
void listen(uint16_t port)
Set up endpoint for listening on a port.
Definition: endpoint.hpp:452
void stop_listening()
Stop listening.
Definition: endpoint.hpp:543
void init_asio(lib::error_code &ec)
Initialize asio transport with internal io_service (exception free)
Definition: endpoint.hpp:211
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated)
Definition: endpoint.hpp:252
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
Definition: endpoint.hpp:74
void start_perpetual()
Marks the endpoint as perpetual, stopping it from exiting when empty.
Definition: endpoint.hpp:609
bool is_secure() const
Return whether or not the endpoint produces secure connections.
Definition: endpoint.hpp:155
void init_asio()
Initialize asio transport with internal io_service.
Definition: endpoint.hpp:223
void listen(std::string const &host, std::string const &service, lib::error_code &ec)
Set up endpoint for listening on a host and service (exception free)
Definition: endpoint.hpp:472
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition: endpoint.hpp:168
void set_reuse_addr(bool value)
Sets whether or not to use the SO_REUSEADDR flag when opening a listening socket. ...
Definition: endpoint.hpp:308
std::size_t run_one()
wraps the run_one method of the internal io_service object
Definition: endpoint.hpp:568
static level const devel
Low level debugging information (warning: very chatty)
Definition: levels.hpp:44
void listen(uint16_t port, lib::error_code &ec)
Set up endpoint for listening on a port (exception free)
Definition: endpoint.hpp:436
bool is_listening() const
Check if the endpoint is listening.
Definition: endpoint.hpp:555
boost::asio::io_service & get_io_service()
Retrieve a reference to the endpoint's io_service.
Definition: endpoint.hpp:323
config::elog_type elog_type
Type of the error logging policy.
Definition: endpoint.hpp:63
asio::connection< config > transport_con_type
Definition: endpoint.hpp:74
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
Definition: connection.hpp:67
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
Definition: endpoint.hpp:1016
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:122
void reset()
wraps the reset method of the internal io_service object
Definition: endpoint.hpp:588
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
Definition: endpoint.hpp:70
void listen(InternetProtocol const &internet_protocol, uint16_t port, lib::error_code &ec)
Set up endpoint for listening with protocol and port (exception free)
Definition: endpoint.hpp:397
lib::shared_ptr< boost::asio::io_service::work > work_ptr
Type of a shared pointer to an io_service work object.
Definition: endpoint.hpp:88
void stop_perpetual()
Clears the endpoint's perpetual flag, allowing it to exit when empty.
Definition: endpoint.hpp:621
void listen(boost::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
Definition: endpoint.hpp:335
The connection was in the wrong state for this operation.
Definition: error.hpp:72
static level const info
Definition: levels.hpp:50
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
Definition: connection.hpp:125
there was an error in the underlying transport library
Definition: base.hpp:190
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
std::size_t poll()
wraps the poll method of the internal io_service object
Definition: endpoint.hpp:578
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
Definition: endpoint.hpp:267
The requested operation was canceled.
Definition: error.hpp:125
void stop()
wraps the stop method of the internal io_service object
Definition: endpoint.hpp:573
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:42
Boost Asio based connection transport component.
Definition: connection.hpp:62
Boost Asio based endpoint transport component.
Definition: base.hpp:159
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
Definition: endpoint.hpp:637
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:350
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:560
void async_accept(transport_con_ptr tcon, accept_handler callback, lib::error_code &ec)
Accept the next connection attempt and assign it to con (exception free)
Definition: endpoint.hpp:690
void listen(std::string const &host, std::string const &service)
Set up endpoint for listening on a host and service.
Definition: endpoint.hpp:505
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
Definition: endpoint.hpp:238
config::socket_type socket_type
Type of the socket policy.
Definition: endpoint.hpp:61
bool stopped() const
wraps the stopped method of the internal io_service object
Definition: endpoint.hpp:593
config::concurrency_type concurrency_type
Type of the concurrency policy.
Definition: endpoint.hpp:59
void async_accept(transport_con_ptr tcon, accept_handler callback)
Accept the next connection attempt and assign it to con.
Definition: endpoint.hpp:729
void init_asio(io_service_ptr ptr)
initialize asio transport with external io_service
Definition: endpoint.hpp:194
std::size_t poll_one()
wraps the poll_one method of the internal io_service object
Definition: endpoint.hpp:583
endpoint< config > type
Type of this endpoint transport component.
Definition: endpoint.hpp:56
static level const library
Definition: levels.hpp:47
socket_type::socket_con_type socket_con_type
Type of the socket connection component.
Definition: endpoint.hpp:68
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
Definition: endpoint.hpp:77
void init_logging(alog_type *a, elog_type *e)
Initialize logging.
Definition: endpoint.hpp:747