Realistic 3D camera system
3D camera system components
basic_socket_acceptor.hpp
Go to the documentation of this file.
1 //
2 // basic_socket_acceptor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_BASIC_SOCKET_ACCEPTOR_HPP
12 #define ASIO_BASIC_SOCKET_ACCEPTOR_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include "asio/basic_io_object.hpp"
20 #include "asio/basic_socket.hpp"
24 #include "asio/error.hpp"
26 #include "asio/socket_base.hpp"
27 
29 
30 namespace asio {
31 
33 
52 template <typename Protocol,
53  typename SocketAcceptorService = socket_acceptor_service<Protocol> >
55  : public basic_io_object<SocketAcceptorService>,
56  public socket_base
57 {
58 public:
61  typedef typename SocketAcceptorService::native_handle_type native_type;
62 
64  typedef typename SocketAcceptorService::native_handle_type native_handle_type;
65 
67  typedef Protocol protocol_type;
68 
70  typedef typename Protocol::endpoint endpoint_type;
71 
73 
83  : basic_io_object<SocketAcceptorService>(io_service)
84  {
85  }
86 
88 
100  const protocol_type& protocol)
101  : basic_io_object<SocketAcceptorService>(io_service)
102  {
103  asio::error_code ec;
104  this->get_service().open(this->get_implementation(), protocol, ec);
105  asio::detail::throw_error(ec, "open");
106  }
107 
109 
136  const endpoint_type& endpoint, bool reuse_addr = true)
137  : basic_io_object<SocketAcceptorService>(io_service)
138  {
139  asio::error_code ec;
140  const protocol_type protocol = endpoint.protocol();
141  this->get_service().open(this->get_implementation(), protocol, ec);
142  asio::detail::throw_error(ec, "open");
143  if (reuse_addr)
144  {
145  this->get_service().set_option(this->get_implementation(),
146  socket_base::reuse_address(true), ec);
147  asio::detail::throw_error(ec, "set_option");
148  }
149  this->get_service().bind(this->get_implementation(), endpoint, ec);
150  asio::detail::throw_error(ec, "bind");
151  this->get_service().listen(this->get_implementation(),
152  socket_base::max_connections, ec);
153  asio::detail::throw_error(ec, "listen");
154  }
155 
157 
172  const protocol_type& protocol, const native_handle_type& native_acceptor)
173  : basic_io_object<SocketAcceptorService>(io_service)
174  {
175  asio::error_code ec;
176  this->get_service().assign(this->get_implementation(),
177  protocol, native_acceptor, ec);
178  asio::detail::throw_error(ec, "assign");
179  }
180 
181 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
182 
195  {
196  }
197 
199 
208  basic_socket_acceptor& operator=(basic_socket_acceptor&& other)
209  {
212  return *this;
213  }
214 
215  // All socket acceptors have access to each other's implementations.
216  template <typename Protocol1, typename SocketAcceptorService1>
217  friend class basic_socket_acceptor;
218 
221 
230  template <typename Protocol1, typename SocketAcceptorService1>
233  typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
234  : basic_io_object<SocketAcceptorService>(other.get_io_service())
235  {
236  this->get_service().template converting_move_construct<Protocol1>(
237  this->get_implementation(), other.get_implementation());
238  }
239 
242 
251  template <typename Protocol1, typename SocketAcceptorService1>
253  basic_socket_acceptor>::type& operator=(
255  {
257  Protocol1, SocketAcceptorService1>)(other));
260  return *this;
261  }
262 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
263 
265 
279  void open(const protocol_type& protocol = protocol_type())
280  {
281  asio::error_code ec;
282  this->get_service().open(this->get_implementation(), protocol, ec);
283  asio::detail::throw_error(ec, "open");
284  }
285 
287 
306  asio::error_code open(const protocol_type& protocol,
307  asio::error_code& ec)
308  {
309  return this->get_service().open(this->get_implementation(), protocol, ec);
310  }
311 
313  /*
314  * This function opens the acceptor to hold an existing native acceptor.
315  *
316  * @param protocol An object specifying which protocol is to be used.
317  *
318  * @param native_acceptor A native acceptor.
319  *
320  * @throws asio::system_error Thrown on failure.
321  */
322  void assign(const protocol_type& protocol,
323  const native_handle_type& native_acceptor)
324  {
325  asio::error_code ec;
326  this->get_service().assign(this->get_implementation(),
327  protocol, native_acceptor, ec);
328  asio::detail::throw_error(ec, "assign");
329  }
330 
332  /*
333  * This function opens the acceptor to hold an existing native acceptor.
334  *
335  * @param protocol An object specifying which protocol is to be used.
336  *
337  * @param native_acceptor A native acceptor.
338  *
339  * @param ec Set to indicate what error occurred, if any.
340  */
341  asio::error_code assign(const protocol_type& protocol,
342  const native_handle_type& native_acceptor, asio::error_code& ec)
343  {
344  return this->get_service().assign(this->get_implementation(),
345  protocol, native_acceptor, ec);
346  }
347 
349  bool is_open() const
350  {
351  return this->get_service().is_open(this->get_implementation());
352  }
353 
355 
372  void bind(const endpoint_type& endpoint)
373  {
374  asio::error_code ec;
375  this->get_service().bind(this->get_implementation(), endpoint, ec);
376  asio::detail::throw_error(ec, "bind");
377  }
378 
380 
402  asio::error_code bind(const endpoint_type& endpoint,
403  asio::error_code& ec)
404  {
405  return this->get_service().bind(this->get_implementation(), endpoint, ec);
406  }
407 
410 
418  void listen(int backlog = socket_base::max_connections)
419  {
420  asio::error_code ec;
421  this->get_service().listen(this->get_implementation(), backlog, ec);
422  asio::detail::throw_error(ec, "listen");
423  }
424 
427 
448  {
449  return this->get_service().listen(this->get_implementation(), backlog, ec);
450  }
451 
453 
462  void close()
463  {
464  asio::error_code ec;
465  this->get_service().close(this->get_implementation(), ec);
466  asio::detail::throw_error(ec, "close");
467  }
468 
470 
492  {
493  return this->get_service().close(this->get_implementation(), ec);
494  }
495 
497 
502  native_type native()
503  {
504  return this->get_service().native_handle(this->get_implementation());
505  }
506 
508 
513  native_handle_type native_handle()
514  {
515  return this->get_service().native_handle(this->get_implementation());
516  }
517 
519 
526  void cancel()
527  {
528  asio::error_code ec;
529  this->get_service().cancel(this->get_implementation(), ec);
530  asio::detail::throw_error(ec, "cancel");
531  }
532 
534 
542  {
543  return this->get_service().cancel(this->get_implementation(), ec);
544  }
545 
547 
567  template <typename SettableSocketOption>
568  void set_option(const SettableSocketOption& option)
569  {
570  asio::error_code ec;
571  this->get_service().set_option(this->get_implementation(), option, ec);
572  asio::detail::throw_error(ec, "set_option");
573  }
574 
576 
601  template <typename SettableSocketOption>
602  asio::error_code set_option(const SettableSocketOption& option,
603  asio::error_code& ec)
604  {
605  return this->get_service().set_option(
606  this->get_implementation(), option, ec);
607  }
608 
610 
631  template <typename GettableSocketOption>
632  void get_option(GettableSocketOption& option)
633  {
634  asio::error_code ec;
635  this->get_service().get_option(this->get_implementation(), option, ec);
636  asio::detail::throw_error(ec, "get_option");
637  }
638 
640 
666  template <typename GettableSocketOption>
667  asio::error_code get_option(GettableSocketOption& option,
668  asio::error_code& ec)
669  {
670  return this->get_service().get_option(
671  this->get_implementation(), option, ec);
672  }
673 
675 
694  template <typename IoControlCommand>
695  void io_control(IoControlCommand& command)
696  {
697  asio::error_code ec;
698  this->get_service().io_control(this->get_implementation(), command, ec);
699  asio::detail::throw_error(ec, "io_control");
700  }
701 
703 
727  template <typename IoControlCommand>
728  asio::error_code io_control(IoControlCommand& command,
729  asio::error_code& ec)
730  {
731  return this->get_service().io_control(
732  this->get_implementation(), command, ec);
733  }
734 
736 
746  bool non_blocking() const
747  {
748  return this->get_service().non_blocking(this->get_implementation());
749  }
750 
752 
764  void non_blocking(bool mode)
765  {
766  asio::error_code ec;
767  this->get_service().non_blocking(this->get_implementation(), mode, ec);
768  asio::detail::throw_error(ec, "non_blocking");
769  }
770 
772 
785  bool mode, asio::error_code& ec)
786  {
787  return this->get_service().non_blocking(
788  this->get_implementation(), mode, ec);
789  }
790 
792 
805  bool native_non_blocking() const
806  {
807  return this->get_service().native_non_blocking(this->get_implementation());
808  }
809 
811 
825  void native_non_blocking(bool mode)
826  {
827  asio::error_code ec;
828  this->get_service().native_non_blocking(
829  this->get_implementation(), mode, ec);
830  asio::detail::throw_error(ec, "native_non_blocking");
831  }
832 
834 
849  bool mode, asio::error_code& ec)
850  {
851  return this->get_service().native_non_blocking(
852  this->get_implementation(), mode, ec);
853  }
854 
856 
870  endpoint_type local_endpoint() const
871  {
872  asio::error_code ec;
873  endpoint_type ep = this->get_service().local_endpoint(
874  this->get_implementation(), ec);
875  asio::detail::throw_error(ec, "local_endpoint");
876  return ep;
877  }
878 
880 
901  endpoint_type local_endpoint(asio::error_code& ec) const
902  {
903  return this->get_service().local_endpoint(this->get_implementation(), ec);
904  }
905 
907 
924  template <typename Protocol1, typename SocketService>
926  typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
927  {
928  asio::error_code ec;
929  this->get_service().accept(this->get_implementation(),
930  peer, static_cast<endpoint_type*>(0), ec);
931  asio::detail::throw_error(ec, "accept");
932  }
933 
935 
957  template <typename Protocol1, typename SocketService>
960  asio::error_code& ec,
961  typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
962  {
963  return this->get_service().accept(this->get_implementation(),
964  peer, static_cast<endpoint_type*>(0), ec);
965  }
966 
968 
1005  template <typename Protocol1, typename SocketService, typename AcceptHandler>
1006  ASIO_INITFN_RESULT_TYPE(AcceptHandler,
1007  void (asio::error_code))
1008  async_accept(basic_socket<Protocol1, SocketService>& peer,
1009  ASIO_MOVE_ARG(AcceptHandler) handler,
1010  typename enable_if<is_convertible<Protocol, Protocol1>::value>::type* = 0)
1011  {
1012  // If you get an error on the following line it means that your handler does
1013  // not meet the documented type requirements for a AcceptHandler.
1014  ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
1015 
1016  return this->get_service().async_accept(this->get_implementation(),
1017  peer, static_cast<endpoint_type*>(0),
1018  ASIO_MOVE_CAST(AcceptHandler)(handler));
1019  }
1020 
1022 
1044  template <typename SocketService>
1046  endpoint_type& peer_endpoint)
1047  {
1048  asio::error_code ec;
1049  this->get_service().accept(this->get_implementation(),
1050  peer, &peer_endpoint, ec);
1051  asio::detail::throw_error(ec, "accept");
1052  }
1053 
1055 
1082  template <typename SocketService>
1085  endpoint_type& peer_endpoint, asio::error_code& ec)
1086  {
1087  return this->get_service().accept(
1088  this->get_implementation(), peer, &peer_endpoint, ec);
1089  }
1090 
1092 
1117  template <typename SocketService, typename AcceptHandler>
1118  ASIO_INITFN_RESULT_TYPE(AcceptHandler,
1119  void (asio::error_code))
1120  async_accept(basic_socket<protocol_type, SocketService>& peer,
1121  endpoint_type& peer_endpoint, ASIO_MOVE_ARG(AcceptHandler) handler)
1122  {
1123  // If you get an error on the following line it means that your handler does
1124  // not meet the documented type requirements for a AcceptHandler.
1125  ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
1126 
1127  return this->get_service().async_accept(this->get_implementation(), peer,
1128  &peer_endpoint, ASIO_MOVE_CAST(AcceptHandler)(handler));
1129  }
1130 };
1131 
1132 } // namespace asio
1133 
1134 #include "asio/detail/pop_options.hpp"
1135 
1136 #endif // ASIO_BASIC_SOCKET_ACCEPTOR_HPP
void throw_error(const asio::error_code &err)
Definition: throw_error.hpp:31
basic_socket_acceptor(asio::io_service &io_service, const protocol_type &protocol, const native_handle_type &native_acceptor)
Construct a basic_socket_acceptor on an existing native acceptor.
SocketAcceptorService::native_handle_type native_handle_type
The native representation of an acceptor.
asio::error_code assign(const protocol_type &protocol, const native_handle_type &native_acceptor, asio::error_code &ec)
Assigns an existing native acceptor to the acceptor.
Provides core I/O functionality.
Definition: io_service.hpp:184
service_type & get_service()
Get the service associated with the I/O object.
#define ASIO_MOVE_CAST2(type1, type2)
Definition: config.hpp:139
SocketService enable_if< is_convertible< Protocol, Protocol1 >::value >::type AcceptHandler SocketService endpoint_type & peer_endpoint
void close()
Close the acceptor.
SocketService ASIO_MOVE_ARG(AcceptHandler) handler
basic_socket_acceptor(asio::io_service &io_service, const endpoint_type &endpoint, bool reuse_addr=true)
Construct an acceptor opened on the given endpoint.
endpoint_type local_endpoint() const
Get the local endpoint of the acceptor.
Base class for all I/O objects.
void native_non_blocking(bool mode)
Sets the non-blocking mode of the native acceptor implementation.
void cancel()
Cancel all asynchronous operations associated with the acceptor.
asio::error_code open(const protocol_type &protocol, asio::error_code &ec)
Open the acceptor using the specified protocol.
asio::error_code bind(const endpoint_type &endpoint, asio::error_code &ec)
Bind the acceptor to the given local endpoint.
asio::error_code non_blocking(bool mode, asio::error_code &ec)
Sets the non-blocking mode of the acceptor.
void bind(const endpoint_type &endpoint)
Bind the acceptor to the given local endpoint.
asio::error_code accept(basic_socket< Protocol1, SocketService > &peer, asio::error_code &ec, typename enable_if< is_convertible< Protocol, Protocol1 >::value >::type *=0)
Accept a new connection.
Provides the ability to accept new connections.
void open(const protocol_type &protocol=protocol_type())
Open the acceptor using the specified protocol.
void accept(basic_socket< Protocol1, SocketService > &peer, typename enable_if< is_convertible< Protocol, Protocol1 >::value >::type *=0)
Accept a new connection.
Protocol protocol_type
The protocol type.
#define ASIO_ACCEPT_HANDLER_CHECK(handler_type, handler)
asio::error_code cancel(asio::error_code &ec)
Cancel all asynchronous operations associated with the acceptor.
void get_option(GettableSocketOption &option)
Get an option from the acceptor.
native_type native()
(Deprecated: Use native_handle().) Get the native acceptor representation.
implementation_type & get_implementation()
Get the underlying implementation of the I/O object.
asio::error_code io_control(IoControlCommand &command, asio::error_code &ec)
Perform an IO control command on the acceptor.
Class to represent an error code value.
Definition: error_code.hpp:80
SocketAcceptorService::native_handle_type native_type
native_handle_type native_handle()
Get the native acceptor representation.
ASIO_INITFN_RESULT_TYPE(AcceptHandler, void(asio::error_code)) async_accept(basic_socket< Protocol1
Start an asynchronous accept.
asio::error_code set_option(const SettableSocketOption &option, asio::error_code &ec)
Set an option on the acceptor.
endpoint_type local_endpoint(asio::error_code &ec) const
Get the local endpoint of the acceptor.
asio::error_code close(asio::error_code &ec)
Close the acceptor.
asio::error_code get_option(GettableSocketOption &option, asio::error_code &ec)
Get an option from the acceptor.
basic_socket_acceptor(asio::io_service &io_service, const protocol_type &protocol)
Construct an open acceptor.
asio::error_code native_non_blocking(bool mode, asio::error_code &ec)
Sets the non-blocking mode of the native acceptor implementation.
void listen(int backlog=socket_base::max_connections)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
void set_option(const SettableSocketOption &option)
Set an option on the acceptor.
void io_control(IoControlCommand &command)
Perform an IO control command on the acceptor.
basic_socket_acceptor(asio::io_service &io_service)
Construct an acceptor without opening it.
void assign(const protocol_type &protocol, const native_handle_type &native_acceptor)
Assigns an existing native acceptor to the acceptor.
Protocol::endpoint endpoint_type
The endpoint type.
asio::error_code listen(int backlog, asio::error_code &ec)
void non_blocking(bool mode)
Sets the non-blocking mode of the acceptor.
bool native_non_blocking() const
Gets the non-blocking mode of the native acceptor implementation.
bool non_blocking() const
Gets the non-blocking mode of the acceptor.
bool is_open() const
Determine whether the acceptor is open.