websocketpp  0.6.0
C++/Boost Asio based websocket client/server library
endpoint.hpp
1 /*
2  * Copyright (c) 2015, 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/transport/base/endpoint.hpp>
32 #include <websocketpp/transport/asio/connection.hpp>
33 #include <websocketpp/transport/asio/security/none.hpp>
34 
35 #include <websocketpp/uri.hpp>
36 #include <websocketpp/logger/levels.hpp>
37 
38 #include <websocketpp/common/functional.hpp>
39 
40 #include <sstream>
41 #include <string>
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 lib::asio::io_service * io_service_ptr;
82  typedef lib::shared_ptr<lib::asio::ip::tcp::acceptor> acceptor_ptr;
84  typedef lib::shared_ptr<lib::asio::ip::tcp::resolver> resolver_ptr;
86  typedef lib::shared_ptr<lib::asio::steady_timer> timer_ptr;
88  typedef lib::shared_ptr<lib::asio::io_service::work> work_ptr;
89 
90  // generate and manage our own io_service
91  explicit endpoint()
92  : m_io_service(NULL)
93  , m_external_io_service(false)
94  , m_listen_backlog(0)
95  , m_reuse_addr(false)
96  , m_state(UNINITIALIZED)
97  {
98  //std::cout << "transport::asio::endpoint constructor" << std::endl;
99  }
100 
101  ~endpoint() {
102  // clean up our io_service if we were initialized with an internal one.
103  m_acceptor.reset();
104  if (m_state != UNINITIALIZED && !m_external_io_service) {
105  delete m_io_service;
106  }
107  }
108 
112 #ifdef _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
113  endpoint(const endpoint & src) = delete;
114  endpoint& operator= (const endpoint & rhs) = delete;
115 #else
116 private:
117  endpoint(const endpoint & src);
118  endpoint & operator= (const endpoint & rhs);
119 public:
120 #endif // _WEBSOCKETPP_DEFAULT_DELETE_FUNCTIONS_
121 
122 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
123  endpoint (endpoint && src)
124  : config::socket_type(std::move(src))
125  , m_tcp_pre_init_handler(src.m_tcp_pre_init_handler)
126  , m_tcp_post_init_handler(src.m_tcp_post_init_handler)
127  , m_io_service(src.m_io_service)
128  , m_external_io_service(src.m_external_io_service)
129  , m_acceptor(src.m_acceptor)
130  , m_listen_backlog(lib::asio::socket_base::max_connections)
131  , m_reuse_addr(src.m_reuse_addr)
132  , m_elog(src.m_elog)
133  , m_alog(src.m_alog)
134  , m_state(src.m_state)
135  {
136  src.m_io_service = NULL;
137  src.m_external_io_service = false;
138  src.m_acceptor = NULL;
139  src.m_state = UNINITIALIZED;
140  }
141 
142  /*endpoint & operator= (const endpoint && rhs) {
143  if (this != &rhs) {
144  m_io_service = rhs.m_io_service;
145  m_external_io_service = rhs.m_external_io_service;
146  m_acceptor = rhs.m_acceptor;
147  m_listen_backlog = rhs.m_listen_backlog;
148  m_reuse_addr = rhs.m_reuse_addr;
149  m_state = rhs.m_state;
150 
151  rhs.m_io_service = NULL;
152  rhs.m_external_io_service = false;
153  rhs.m_acceptor = NULL;
154  rhs.m_listen_backlog = lib::asio::socket_base::max_connections;
155  rhs.m_state = UNINITIALIZED;
156 
157  // TODO: this needs to be updated
158  }
159  return *this;
160  }*/
161 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_
162 
164  bool is_secure() const {
165  return socket_type::is_secure();
166  }
167 
169 
177  void init_asio(io_service_ptr ptr, lib::error_code & ec) {
178  if (m_state != UNINITIALIZED) {
179  m_elog->write(log::elevel::library,
180  "asio::init_asio called from the wrong state");
181  using websocketpp::error::make_error_code;
182  ec = make_error_code(websocketpp::error::invalid_state);
183  return;
184  }
185 
186  m_alog->write(log::alevel::devel,"asio::init_asio");
187 
188  m_io_service = ptr;
189  m_external_io_service = true;
190  m_acceptor = lib::make_shared<lib::asio::ip::tcp::acceptor>(
191  lib::ref(*m_io_service));
192 
193  m_state = READY;
194  ec = lib::error_code();
195  }
196 
198 
205  void init_asio(io_service_ptr ptr) {
206  lib::error_code ec;
207  init_asio(ptr,ec);
208  if (ec) { throw exception(ec); }
209  }
210 
212 
220  void init_asio(lib::error_code & ec) {
221  init_asio(new lib::asio::io_service(), ec);
222  m_external_io_service = false;
223  }
224 
226 
232  void init_asio() {
233  init_asio(new lib::asio::io_service());
234  m_external_io_service = false;
235  }
236 
238 
247  void set_tcp_pre_init_handler(tcp_init_handler h) {
248  m_tcp_pre_init_handler = h;
249  }
250 
252 
261  void set_tcp_init_handler(tcp_init_handler h) {
263  }
264 
266 
276  void set_tcp_post_init_handler(tcp_init_handler h) {
277  m_tcp_post_init_handler = h;
278  }
279 
281 
299  void set_listen_backlog(int backlog) {
300  m_listen_backlog = backlog;
301  }
302 
304 
317  void set_reuse_addr(bool value) {
318  m_reuse_addr = value;
319  }
320 
322 
332  lib::asio::io_service & get_io_service() {
333  return *m_io_service;
334  }
335 
337 
344  void listen(lib::asio::ip::tcp::endpoint const & ep, lib::error_code & ec)
345  {
346  if (m_state != READY) {
347  m_elog->write(log::elevel::library,
348  "asio::listen called from the wrong state");
349  using websocketpp::error::make_error_code;
350  ec = make_error_code(websocketpp::error::invalid_state);
351  return;
352  }
353 
354  m_alog->write(log::alevel::devel,"asio::listen");
355 
356  lib::asio::error_code bec;
357 
358  m_acceptor->open(ep.protocol(),bec);
359  if (!bec) {
360  m_acceptor->set_option(lib::asio::socket_base::reuse_address(m_reuse_addr),bec);
361  }
362  if (!bec) {
363  m_acceptor->bind(ep,bec);
364  }
365  if (!bec) {
366  m_acceptor->listen(m_listen_backlog,bec);
367  }
368  if (bec) {
369  if (m_acceptor->is_open()) {
370  m_acceptor->close();
371  }
372  log_err(log::elevel::info,"asio listen",bec);
373  ec = make_error_code(error::pass_through);
374  } else {
375  m_state = LISTENING;
376  ec = lib::error_code();
377  }
378  }
379 
381 
386  void listen(lib::asio::ip::tcp::endpoint const & ep) {
387  lib::error_code ec;
388  listen(ep,ec);
389  if (ec) { throw exception(ec); }
390  }
391 
393 
406  template <typename InternetProtocol>
407  void listen(InternetProtocol const & internet_protocol, uint16_t port,
408  lib::error_code & ec)
409  {
410  lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
411  listen(ep,ec);
412  }
413 
415 
427  template <typename InternetProtocol>
428  void listen(InternetProtocol const & internet_protocol, uint16_t port)
429  {
430  lib::asio::ip::tcp::endpoint ep(internet_protocol, port);
431  listen(ep);
432  }
433 
435 
446  void listen(uint16_t port, lib::error_code & ec) {
447  listen(lib::asio::ip::tcp::v6(), port, ec);
448  }
449 
451 
462  void listen(uint16_t port) {
463  listen(lib::asio::ip::tcp::v6(), port);
464  }
465 
467 
482  void listen(std::string const & host, std::string const & service,
483  lib::error_code & ec)
484  {
485  using lib::asio::ip::tcp;
486  tcp::resolver r(*m_io_service);
487  tcp::resolver::query query(host, service);
488  tcp::resolver::iterator endpoint_iterator = r.resolve(query);
489  tcp::resolver::iterator end;
490  if (endpoint_iterator == end) {
491  m_elog->write(log::elevel::library,
492  "asio::listen could not resolve the supplied host or service");
493  ec = make_error_code(error::invalid_host_service);
494  return;
495  }
496  listen(*endpoint_iterator,ec);
497  }
498 
500 
515  void listen(std::string const & host, std::string const & service)
516  {
517  lib::error_code ec;
518  listen(host,service,ec);
519  if (ec) { throw exception(ec); }
520  }
521 
523 
530  void stop_listening(lib::error_code & ec) {
531  if (m_state != LISTENING) {
532  m_elog->write(log::elevel::library,
533  "asio::listen called from the wrong state");
534  using websocketpp::error::make_error_code;
535  ec = make_error_code(websocketpp::error::invalid_state);
536  return;
537  }
538 
539  m_acceptor->close();
540  m_state = READY;
541  ec = lib::error_code();
542  }
543 
545 
551  void stop_listening() {
552  lib::error_code ec;
553  stop_listening(ec);
554  if (ec) { throw exception(ec); }
555  }
556 
558 
561  bool is_listening() const {
562  return (m_state == LISTENING);
563  }
564 
566  std::size_t run() {
567  return m_io_service->run();
568  }
569 
571 
574  std::size_t run_one() {
575  return m_io_service->run_one();
576  }
577 
579  void stop() {
580  m_io_service->stop();
581  }
582 
584  std::size_t poll() {
585  return m_io_service->poll();
586  }
587 
589  std::size_t poll_one() {
590  return m_io_service->poll_one();
591  }
592 
594  void reset() {
595  m_io_service->reset();
596  }
597 
599  bool stopped() const {
600  return m_io_service->stopped();
601  }
602 
604 
616  m_work = lib::make_shared<lib::asio::io_service::work>(
617  lib::ref(*m_io_service)
618  );
619  }
620 
622 
629  void stop_perpetual() {
630  m_work.reset();
631  }
632 
634 
645  timer_ptr set_timer(long duration, timer_handler callback) {
646  timer_ptr new_timer = lib::make_shared<lib::asio::steady_timer>(
647  *m_io_service,
648  lib::asio::milliseconds(duration)
649  );
650 
651  new_timer->async_wait(
652  lib::bind(
654  this,
655  new_timer,
656  callback,
657  lib::placeholders::_1
658  )
659  );
660 
661  return new_timer;
662  }
663 
665 
673  void handle_timer(timer_ptr, timer_handler callback,
674  lib::asio::error_code const & ec)
675  {
676  if (ec) {
677  if (ec == lib::asio::error::operation_aborted) {
678  callback(make_error_code(transport::error::operation_aborted));
679  } else {
680  m_elog->write(log::elevel::info,
681  "asio handle_timer error: "+ec.message());
682  log_err(log::elevel::info,"asio handle_timer",ec);
683  callback(make_error_code(error::pass_through));
684  }
685  } else {
686  callback(lib::error_code());
687  }
688  }
689 
691 
696  void async_accept(transport_con_ptr tcon, accept_handler callback,
697  lib::error_code & ec)
698  {
699  if (m_state != LISTENING) {
700  using websocketpp::error::make_error_code;
702  return;
703  }
704 
705  m_alog->write(log::alevel::devel, "asio::async_accept");
706 
707  if (config::enable_multithreading) {
708  m_acceptor->async_accept(
709  tcon->get_raw_socket(),
710  tcon->get_strand()->wrap(lib::bind(
711  &type::handle_accept,
712  this,
713  callback,
714  lib::placeholders::_1
715  ))
716  );
717  } else {
718  m_acceptor->async_accept(
719  tcon->get_raw_socket(),
720  lib::bind(
721  &type::handle_accept,
722  this,
723  callback,
724  lib::placeholders::_1
725  )
726  );
727  }
728  }
729 
731 
735  void async_accept(transport_con_ptr tcon, accept_handler callback) {
736  lib::error_code ec;
737  async_accept(tcon,callback,ec);
738  if (ec) { throw exception(ec); }
739  }
740 protected:
742 
751  void init_logging(alog_type* a, elog_type* e) {
752  m_alog = a;
753  m_elog = e;
754  }
755 
756  void handle_accept(accept_handler callback, lib::asio::error_code const &
757  asio_ec)
758  {
759  lib::error_code ret_ec;
760 
761  m_alog->write(log::alevel::devel, "asio::handle_accept");
762 
763  if (asio_ec) {
764  if (asio_ec == lib::asio::errc::operation_canceled) {
765  ret_ec = make_error_code(websocketpp::error::operation_canceled);
766  } else {
767  log_err(log::elevel::info,"asio handle_accept",asio_ec);
768  ret_ec = make_error_code(error::pass_through);
769  }
770  }
771 
772  callback(ret_ec);
773  }
774 
776  // TODO: there have to be some more failure conditions here
777  void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb) {
778  using namespace lib::asio::ip;
779 
780  // Create a resolver
781  if (!m_resolver) {
782  m_resolver = lib::make_shared<lib::asio::ip::tcp::resolver>(
783  lib::ref(*m_io_service));
784  }
785 
786  tcon->set_uri(u);
787 
788  std::string proxy = tcon->get_proxy();
789  std::string host;
790  std::string port;
791 
792  if (proxy.empty()) {
793  host = u->get_host();
794  port = u->get_port_str();
795  } else {
796  lib::error_code ec;
797 
798  uri_ptr pu = lib::make_shared<uri>(proxy);
799 
800  if (!pu->get_valid()) {
801  cb(make_error_code(error::proxy_invalid));
802  return;
803  }
804 
805  ec = tcon->proxy_init(u->get_authority());
806  if (ec) {
807  cb(ec);
808  return;
809  }
810 
811  host = pu->get_host();
812  port = pu->get_port_str();
813  }
814 
815  tcp::resolver::query query(host,port);
816 
817  if (m_alog->static_test(log::alevel::devel)) {
818  m_alog->write(log::alevel::devel,
819  "starting async DNS resolve for "+host+":"+port);
820  }
821 
822  timer_ptr dns_timer;
823 
824  dns_timer = tcon->set_timer(
825  config::timeout_dns_resolve,
826  lib::bind(
828  this,
829  dns_timer,
830  cb,
831  lib::placeholders::_1
832  )
833  );
834 
835  if (config::enable_multithreading) {
836  m_resolver->async_resolve(
837  query,
838  tcon->get_strand()->wrap(lib::bind(
839  &type::handle_resolve,
840  this,
841  tcon,
842  dns_timer,
843  cb,
844  lib::placeholders::_1,
845  lib::placeholders::_2
846  ))
847  );
848  } else {
849  m_resolver->async_resolve(
850  query,
851  lib::bind(
852  &type::handle_resolve,
853  this,
854  tcon,
855  dns_timer,
856  cb,
857  lib::placeholders::_1,
858  lib::placeholders::_2
859  )
860  );
861  }
862  }
863 
865 
873  void handle_resolve_timeout(timer_ptr, connect_handler callback,
874  lib::error_code const & ec)
875  {
876  lib::error_code ret_ec;
877 
878  if (ec) {
880  m_alog->write(log::alevel::devel,
881  "asio handle_resolve_timeout timer cancelled");
882  return;
883  }
884 
885  log_err(log::elevel::devel,"asio handle_resolve_timeout",ec);
886  ret_ec = ec;
887  } else {
888  ret_ec = make_error_code(transport::error::timeout);
889  }
890 
891  m_alog->write(log::alevel::devel,"DNS resolution timed out");
892  m_resolver->cancel();
893  callback(ret_ec);
894  }
895 
896  void handle_resolve(transport_con_ptr tcon, timer_ptr dns_timer,
897  connect_handler callback, lib::asio::error_code const & ec,
898  lib::asio::ip::tcp::resolver::iterator iterator)
899  {
900  if (ec == lib::asio::error::operation_aborted ||
901  lib::asio::is_neg(dns_timer->expires_from_now()))
902  {
903  m_alog->write(log::alevel::devel,"async_resolve cancelled");
904  return;
905  }
906 
907  dns_timer->cancel();
908 
909  if (ec) {
910  log_err(log::elevel::info,"asio async_resolve",ec);
911  callback(make_error_code(error::pass_through));
912  return;
913  }
914 
915  if (m_alog->static_test(log::alevel::devel)) {
916  std::stringstream s;
917  s << "Async DNS resolve successful. Results: ";
918 
919  lib::asio::ip::tcp::resolver::iterator it, end;
920  for (it = iterator; it != end; ++it) {
921  s << (*it).endpoint() << " ";
922  }
923 
924  m_alog->write(log::alevel::devel,s.str());
925  }
926 
927  m_alog->write(log::alevel::devel,"Starting async connect");
928 
929  timer_ptr con_timer;
930 
931  con_timer = tcon->set_timer(
932  config::timeout_connect,
933  lib::bind(
935  this,
936  tcon,
937  con_timer,
938  callback,
939  lib::placeholders::_1
940  )
941  );
942 
943  if (config::enable_multithreading) {
944  lib::asio::async_connect(
945  tcon->get_raw_socket(),
946  iterator,
947  tcon->get_strand()->wrap(lib::bind(
948  &type::handle_connect,
949  this,
950  tcon,
951  con_timer,
952  callback,
953  lib::placeholders::_1
954  ))
955  );
956  } else {
957  lib::asio::async_connect(
958  tcon->get_raw_socket(),
959  iterator,
960  lib::bind(
961  &type::handle_connect,
962  this,
963  tcon,
964  con_timer,
965  callback,
966  lib::placeholders::_1
967  )
968  );
969  }
970  }
971 
973 
982  void handle_connect_timeout(transport_con_ptr tcon, timer_ptr,
983  connect_handler callback, lib::error_code const & ec)
984  {
985  lib::error_code ret_ec;
986 
987  if (ec) {
989  m_alog->write(log::alevel::devel,
990  "asio handle_connect_timeout timer cancelled");
991  return;
992  }
993 
994  log_err(log::elevel::devel,"asio handle_connect_timeout",ec);
995  ret_ec = ec;
996  } else {
997  ret_ec = make_error_code(transport::error::timeout);
998  }
999 
1000  m_alog->write(log::alevel::devel,"TCP connect timed out");
1001  tcon->cancel_socket();
1002  callback(ret_ec);
1003  }
1004 
1005  void handle_connect(transport_con_ptr tcon, timer_ptr con_timer,
1006  connect_handler callback, lib::asio::error_code const & ec)
1007  {
1008  if (ec == lib::asio::error::operation_aborted ||
1009  lib::asio::is_neg(con_timer->expires_from_now()))
1010  {
1011  m_alog->write(log::alevel::devel,"async_connect cancelled");
1012  return;
1013  }
1014 
1015  con_timer->cancel();
1016 
1017  if (ec) {
1018  log_err(log::elevel::info,"asio async_connect",ec);
1019  callback(make_error_code(error::pass_through));
1020  return;
1021  }
1022 
1023  if (m_alog->static_test(log::alevel::devel)) {
1024  m_alog->write(log::alevel::devel,
1025  "Async connect to "+tcon->get_remote_endpoint()+" successful.");
1026  }
1027 
1028  callback(lib::error_code());
1029  }
1030 
1032 
1042  lib::error_code init(transport_con_ptr tcon) {
1043  m_alog->write(log::alevel::devel, "transport::asio::init");
1044 
1045  // Initialize the connection socket component
1046  socket_type::init(lib::static_pointer_cast<socket_con_type,
1047  transport_con_type>(tcon));
1048 
1049  lib::error_code ec;
1050 
1051  ec = tcon->init_asio(m_io_service);
1052  if (ec) {return ec;}
1053 
1054  tcon->set_tcp_pre_init_handler(m_tcp_pre_init_handler);
1055  tcon->set_tcp_post_init_handler(m_tcp_post_init_handler);
1056 
1057  return lib::error_code();
1058  }
1059 private:
1061  template <typename error_type>
1062  void log_err(log::level l, char const * msg, error_type const & ec) {
1063  std::stringstream s;
1064  s << msg << " error: " << ec << " (" << ec.message() << ")";
1065  m_elog->write(l,s.str());
1066  }
1067 
1068  enum state {
1069  UNINITIALIZED = 0,
1070  READY = 1,
1071  LISTENING = 2
1072  };
1073 
1074  // Handlers
1075  tcp_init_handler m_tcp_pre_init_handler;
1076  tcp_init_handler m_tcp_post_init_handler;
1077 
1078  // Network Resources
1079  io_service_ptr m_io_service;
1080  bool m_external_io_service;
1081  acceptor_ptr m_acceptor;
1082  resolver_ptr m_resolver;
1083  work_ptr m_work;
1084 
1085  // Network constants
1086  int m_listen_backlog;
1087  bool m_reuse_addr;
1088 
1089  elog_type* m_elog;
1090  alog_type* m_alog;
1091 
1092  // Transport state
1093  state m_state;
1094 };
1095 
1096 } // namespace asio
1097 } // namespace transport
1098 } // namespace websocketpp
1099 
1100 #endif // WEBSOCKETPP_TRANSPORT_ASIO_HPP
lib::shared_ptr< lib::asio::ip::tcp::acceptor > acceptor_ptr
Type of a shared pointer to the acceptor being used.
Definition: endpoint.hpp:82
endpoint< config > type
Type of this endpoint transport component.
Definition: endpoint.hpp:56
Asio based endpoint transport component.
Definition: base.hpp:143
uint16_t value
The type of a close code value.
Definition: close.hpp:49
lib::shared_ptr< lib::asio::io_service::work > work_ptr
Type of a shared pointer to an io_service work object.
Definition: endpoint.hpp:88
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
Definition: endpoint.hpp:1042
void set_tcp_post_init_handler(tcp_init_handler h)
Sets the tcp post init handler.
Definition: endpoint.hpp:276
void init_asio()
Initialize asio transport with internal io_service.
Definition: endpoint.hpp:232
lib::function< void(lib::error_code const &)> accept_handler
The type and signature of the callback passed to the accept method.
Definition: endpoint.hpp:69
void listen(lib::asio::ip::tcp::endpoint const &ep)
Set up endpoint for listening manually.
Definition: endpoint.hpp:386
transport_con_type::ptr transport_con_ptr
Definition: endpoint.hpp:77
asio::connection< config > transport_con_type
Definition: endpoint.hpp:74
bool stopped() const
wraps the stopped method of the internal io_service object
Definition: endpoint.hpp:599
timer_ptr set_timer(long duration, timer_handler callback)
Call back a function after a period of time.
Definition: endpoint.hpp:645
void listen(uint16_t port, lib::error_code &ec)
Set up endpoint for listening on a port (exception free)
Definition: endpoint.hpp:446
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
Definition: endpoint.hpp:777
void set_reuse_addr(bool value)
Sets whether to use the SO_REUSEADDR flag when opening listening sockets.
Definition: endpoint.hpp:317
lib::shared_ptr< lib::asio::steady_timer > timer_ptr
Type of timer handle.
Definition: endpoint.hpp:86
static level const devel
Low level debugging information (warning: very chatty)
Definition: levels.hpp:63
STL namespace.
lib::shared_ptr< type > ptr
Type of a shared pointer to this connection transport component.
Definition: connection.hpp:72
void handle_resolve_timeout(timer_ptr, connect_handler callback, lib::error_code const &ec)
DNS resolution timeout handler.
Definition: endpoint.hpp:873
config::socket_type socket_type
Type of the socket policy.
Definition: endpoint.hpp:61
void listen(uint16_t port)
Set up endpoint for listening on a port.
Definition: endpoint.hpp:462
void set_tcp_init_handler(tcp_init_handler h)
Sets the tcp pre init handler (deprecated)
Definition: endpoint.hpp:261
lib::asio::io_service & get_io_service()
Retrieve a reference to the endpoint's io_service.
Definition: endpoint.hpp:332
static level const devel
Development messages (warning: very chatty)
Definition: levels.hpp:141
void listen(std::string const &host, std::string const &service)
Set up endpoint for listening on a host and service.
Definition: endpoint.hpp:515
bool is_secure() const
Return whether or not the endpoint produces secure connections.
Definition: endpoint.hpp:164
lib::shared_ptr< lib::asio::ip::tcp::resolver > resolver_ptr
Type of a shared pointer to the resolver being used.
Definition: endpoint.hpp:84
void init_asio(io_service_ptr ptr, lib::error_code &ec)
initialize asio transport with external io_service (exception free)
Definition: endpoint.hpp:177
std::size_t poll()
wraps the poll method of the internal io_service object
Definition: endpoint.hpp:584
void stop_perpetual()
Clears the endpoint's perpetual flag, allowing it to exit when empty.
Definition: endpoint.hpp:629
void stop_listening()
Stop listening.
Definition: endpoint.hpp:551
bool is_listening() const
Check if the endpoint is listening.
Definition: endpoint.hpp:561
config::alog_type alog_type
Type of the access logging policy.
Definition: endpoint.hpp:65
socket_con_type::ptr socket_con_ptr
Type of a shared pointer to the socket connection component.
Definition: endpoint.hpp:70
lib::asio::io_service * io_service_ptr
Type of a pointer to the ASIO io_service being used.
Definition: endpoint.hpp:80
The connection was in the wrong state for this operation.
Definition: error.hpp:74
static level const info
Definition: levels.hpp:69
lib::function< void(lib::error_code const &)> timer_handler
The type and signature of the callback passed to the read method.
Definition: connection.hpp:126
there was an error in the underlying transport library
Definition: base.hpp:174
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
std::size_t run_one()
wraps the run_one method of the internal io_service object
Definition: endpoint.hpp:574
The requested operation was canceled.
Definition: error.hpp:127
config::elog_type elog_type
Type of the error logging policy.
Definition: endpoint.hpp:63
void reset()
wraps the reset method of the internal io_service object
Definition: endpoint.hpp:594
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:482
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:696
Creates and manages connections associated with a WebSocket endpoint.
Definition: endpoint.hpp:42
Asio based connection transport component.
Definition: connection.hpp:67
void stop_listening(lib::error_code &ec)
Stop listening (exception free)
Definition: endpoint.hpp:530
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:350
void init_asio(io_service_ptr ptr)
initialize asio transport with external io_service
Definition: endpoint.hpp:205
void async_accept(transport_con_ptr tcon, accept_handler callback)
Accept the next connection attempt and assign it to con.
Definition: endpoint.hpp:735
std::size_t poll_one()
wraps the poll_one method of the internal io_service object
Definition: endpoint.hpp:589
void set_listen_backlog(int backlog)
Sets the maximum length of the queue of pending connections.
Definition: endpoint.hpp:299
void init_logging(alog_type *a, elog_type *e)
Initialize logging.
Definition: endpoint.hpp:751
void init_asio(lib::error_code &ec)
Initialize asio transport with internal io_service (exception free)
Definition: endpoint.hpp:220
void stop()
wraps the stop method of the internal io_service object
Definition: endpoint.hpp:579
void set_tcp_pre_init_handler(tcp_init_handler h)
Sets the tcp pre init handler.
Definition: endpoint.hpp:247
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:566
void listen(InternetProtocol const &internet_protocol, uint16_t port)
Set up endpoint for listening with protocol and port.
Definition: endpoint.hpp:428
socket_type::socket_con_type socket_con_type
Type of the socket connection component.
Definition: endpoint.hpp:68
void listen(lib::asio::ip::tcp::endpoint const &ep, lib::error_code &ec)
Set up endpoint for listening manually (exception free)
Definition: endpoint.hpp:344
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:407
void handle_connect_timeout(transport_con_ptr tcon, timer_ptr, connect_handler callback, lib::error_code const &ec)
Asio connect timeout handler.
Definition: endpoint.hpp:982
config::concurrency_type concurrency_type
Type of the concurrency policy.
Definition: endpoint.hpp:59
static level const library
Definition: levels.hpp:66
lib::function< void(lib::error_code const &)> connect_handler
The type and signature of the callback passed to the connect method.
Definition: endpoint.hpp:72
void handle_timer(timer_ptr, timer_handler callback, lib::asio::error_code const &ec)
Timer handler.
Definition: endpoint.hpp:673
void start_perpetual()
Marks the endpoint as perpetual, stopping it from exiting when empty.
Definition: endpoint.hpp:615