Realistic 3D camera system
3D camera system components
basic_socket.hpp
Go to the documentation of this file.
1 //
2 // basic_socket.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_HPP
12 #define ASIO_BASIC_SOCKET_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/async_result.hpp"
20 #include "asio/basic_io_object.hpp"
24 #include "asio/error.hpp"
25 #include "asio/socket_base.hpp"
26 
28 
29 namespace asio {
30 
32 
40 template <typename Protocol, typename SocketService>
42  : public basic_io_object<SocketService>,
43  public socket_base
44 {
45 public:
48  typedef typename SocketService::native_handle_type native_type;
49 
51  typedef typename SocketService::native_handle_type native_handle_type;
52 
54  typedef Protocol protocol_type;
55 
57  typedef typename Protocol::endpoint endpoint_type;
58 
61 
63 
70  : basic_io_object<SocketService>(io_service)
71  {
72  }
73 
75 
86  const protocol_type& protocol)
87  : basic_io_object<SocketService>(io_service)
88  {
90  this->get_service().open(this->get_implementation(), protocol, ec);
91  asio::detail::throw_error(ec, "open");
92  }
93 
96 
110  const endpoint_type& endpoint)
111  : basic_io_object<SocketService>(io_service)
112  {
113  asio::error_code ec;
114  const protocol_type protocol = endpoint.protocol();
115  this->get_service().open(this->get_implementation(), protocol, ec);
116  asio::detail::throw_error(ec, "open");
117  this->get_service().bind(this->get_implementation(), endpoint, ec);
118  asio::detail::throw_error(ec, "bind");
119  }
120 
122 
135  const protocol_type& protocol, const native_handle_type& native_socket)
136  : basic_io_object<SocketService>(io_service)
137  {
138  asio::error_code ec;
139  this->get_service().assign(this->get_implementation(),
140  protocol, native_socket, ec);
141  asio::detail::throw_error(ec, "assign");
142  }
143 
144 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
145 
155  basic_socket(basic_socket&& other)
158  {
159  }
160 
162 
171  basic_socket& operator=(basic_socket&& other)
172  {
174  ASIO_MOVE_CAST(basic_socket)(other));
175  return *this;
176  }
177 
178  // All sockets have access to each other's implementations.
179  template <typename Protocol1, typename SocketService1>
180  friend class basic_socket;
181 
183 
192  template <typename Protocol1, typename SocketService1>
194  typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0)
195  : basic_io_object<SocketService>(other.get_io_service())
196  {
197  this->get_service().template converting_move_construct<Protocol1>(
198  this->get_implementation(), other.get_implementation());
199  }
200 
202 
211  template <typename Protocol1, typename SocketService1>
213  basic_socket>::type& operator=(
215  {
217  Protocol1, SocketService1>)(other));
220  return *this;
221  }
222 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
223 
225 
233  lowest_layer_type& lowest_layer()
234  {
235  return *this;
236  }
237 
239 
247  const lowest_layer_type& lowest_layer() const
248  {
249  return *this;
250  }
251 
253 
266  void open(const protocol_type& protocol = protocol_type())
267  {
268  asio::error_code ec;
269  this->get_service().open(this->get_implementation(), protocol, ec);
270  asio::detail::throw_error(ec, "open");
271  }
272 
274 
292  asio::error_code open(const protocol_type& protocol,
293  asio::error_code& ec)
294  {
295  return this->get_service().open(this->get_implementation(), protocol, ec);
296  }
297 
299  /*
300  * This function opens the socket to hold an existing native socket.
301  *
302  * @param protocol An object specifying which protocol is to be used.
303  *
304  * @param native_socket A native socket.
305  *
306  * @throws asio::system_error Thrown on failure.
307  */
308  void assign(const protocol_type& protocol,
309  const native_handle_type& native_socket)
310  {
311  asio::error_code ec;
312  this->get_service().assign(this->get_implementation(),
313  protocol, native_socket, ec);
314  asio::detail::throw_error(ec, "assign");
315  }
316 
318  /*
319  * This function opens the socket to hold an existing native socket.
320  *
321  * @param protocol An object specifying which protocol is to be used.
322  *
323  * @param native_socket A native socket.
324  *
325  * @param ec Set to indicate what error occurred, if any.
326  */
327  asio::error_code assign(const protocol_type& protocol,
328  const native_handle_type& native_socket, asio::error_code& ec)
329  {
330  return this->get_service().assign(this->get_implementation(),
331  protocol, native_socket, ec);
332  }
333 
335  bool is_open() const
336  {
337  return this->get_service().is_open(this->get_implementation());
338  }
339 
341 
352  void close()
353  {
354  asio::error_code ec;
355  this->get_service().close(this->get_implementation(), ec);
356  asio::detail::throw_error(ec, "close");
357  }
358 
360 
384  {
385  return this->get_service().close(this->get_implementation(), ec);
386  }
387 
389 
394  native_type native()
395  {
396  return this->get_service().native_handle(this->get_implementation());
397  }
398 
400 
405  native_handle_type native_handle()
406  {
407  return this->get_service().native_handle(this->get_implementation());
408  }
409 
411 
444 #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
445  && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
446  && !defined(ASIO_ENABLE_CANCELIO)
447  __declspec(deprecated("By default, this function always fails with "
448  "operation_not_supported when used on Windows XP, Windows Server 2003, "
449  "or earlier. Consult documentation for details."))
450 #endif
451  void cancel()
452  {
453  asio::error_code ec;
454  this->get_service().cancel(this->get_implementation(), ec);
455  asio::detail::throw_error(ec, "cancel");
456  }
457 
459 
492 #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \
493  && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \
494  && !defined(ASIO_ENABLE_CANCELIO)
495  __declspec(deprecated("By default, this function always fails with "
496  "operation_not_supported when used on Windows XP, Windows Server 2003, "
497  "or earlier. Consult documentation for details."))
498 #endif
500  {
501  return this->get_service().cancel(this->get_implementation(), ec);
502  }
503 
505 
514  bool at_mark() const
515  {
516  asio::error_code ec;
517  bool b = this->get_service().at_mark(this->get_implementation(), ec);
518  asio::detail::throw_error(ec, "at_mark");
519  return b;
520  }
521 
523 
532  bool at_mark(asio::error_code& ec) const
533  {
534  return this->get_service().at_mark(this->get_implementation(), ec);
535  }
536 
538 
547  std::size_t available() const
548  {
549  asio::error_code ec;
550  std::size_t s = this->get_service().available(
551  this->get_implementation(), ec);
552  asio::detail::throw_error(ec, "available");
553  return s;
554  }
555 
557 
566  std::size_t available(asio::error_code& ec) const
567  {
568  return this->get_service().available(this->get_implementation(), ec);
569  }
570 
572 
589  void bind(const endpoint_type& endpoint)
590  {
591  asio::error_code ec;
592  this->get_service().bind(this->get_implementation(), endpoint, ec);
593  asio::detail::throw_error(ec, "bind");
594  }
595 
597 
619  asio::error_code bind(const endpoint_type& endpoint,
620  asio::error_code& ec)
621  {
622  return this->get_service().bind(this->get_implementation(), endpoint, ec);
623  }
624 
626 
648  void connect(const endpoint_type& peer_endpoint)
649  {
650  asio::error_code ec;
651  if (!is_open())
652  {
653  this->get_service().open(this->get_implementation(),
654  peer_endpoint.protocol(), ec);
655  asio::detail::throw_error(ec, "connect");
656  }
657  this->get_service().connect(this->get_implementation(), peer_endpoint, ec);
658  asio::detail::throw_error(ec, "connect");
659  }
660 
662 
689  asio::error_code connect(const endpoint_type& peer_endpoint,
690  asio::error_code& ec)
691  {
692  if (!is_open())
693  {
694  if (this->get_service().open(this->get_implementation(),
695  peer_endpoint.protocol(), ec))
696  {
697  return ec;
698  }
699  }
700 
701  return this->get_service().connect(
702  this->get_implementation(), peer_endpoint, ec);
703  }
704 
706 
746  template <typename ConnectHandler>
747  ASIO_INITFN_RESULT_TYPE(ConnectHandler,
748  void (asio::error_code))
749  async_connect(const endpoint_type& peer_endpoint,
750  ASIO_MOVE_ARG(ConnectHandler) handler)
751  {
752  // If you get an error on the following line it means that your handler does
753  // not meet the documented type requirements for a ConnectHandler.
754  ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
755 
756  if (!is_open())
757  {
758  asio::error_code ec;
759  const protocol_type protocol = peer_endpoint.protocol();
760  if (this->get_service().open(this->get_implementation(), protocol, ec))
761  {
763  ConnectHandler, void (asio::error_code)> init(
764  ASIO_MOVE_CAST(ConnectHandler)(handler));
765 
766  this->get_io_service().post(
769  ConnectHandler, void (asio::error_code)))(
770  init.handler), ec));
771 
772  return init.result.get();
773  }
774  }
775 
776  return this->get_service().async_connect(this->get_implementation(),
777  peer_endpoint, ASIO_MOVE_CAST(ConnectHandler)(handler));
778  }
779 
781 
814  template <typename SettableSocketOption>
815  void set_option(const SettableSocketOption& option)
816  {
817  asio::error_code ec;
818  this->get_service().set_option(this->get_implementation(), option, ec);
819  asio::detail::throw_error(ec, "set_option");
820  }
821 
823 
861  template <typename SettableSocketOption>
862  asio::error_code set_option(const SettableSocketOption& option,
863  asio::error_code& ec)
864  {
865  return this->get_service().set_option(
866  this->get_implementation(), option, ec);
867  }
868 
870 
904  template <typename GettableSocketOption>
905  void get_option(GettableSocketOption& option) const
906  {
907  asio::error_code ec;
908  this->get_service().get_option(this->get_implementation(), option, ec);
909  asio::detail::throw_error(ec, "get_option");
910  }
911 
913 
952  template <typename GettableSocketOption>
953  asio::error_code get_option(GettableSocketOption& option,
954  asio::error_code& ec) const
955  {
956  return this->get_service().get_option(
957  this->get_implementation(), option, ec);
958  }
959 
961 
982  template <typename IoControlCommand>
983  void io_control(IoControlCommand& command)
984  {
985  asio::error_code ec;
986  this->get_service().io_control(this->get_implementation(), command, ec);
987  asio::detail::throw_error(ec, "io_control");
988  }
989 
991 
1017  template <typename IoControlCommand>
1018  asio::error_code io_control(IoControlCommand& command,
1019  asio::error_code& ec)
1020  {
1021  return this->get_service().io_control(
1022  this->get_implementation(), command, ec);
1023  }
1024 
1026 
1036  bool non_blocking() const
1037  {
1038  return this->get_service().non_blocking(this->get_implementation());
1039  }
1040 
1042 
1054  void non_blocking(bool mode)
1055  {
1056  asio::error_code ec;
1057  this->get_service().non_blocking(this->get_implementation(), mode, ec);
1058  asio::detail::throw_error(ec, "non_blocking");
1059  }
1060 
1062 
1075  bool mode, asio::error_code& ec)
1076  {
1077  return this->get_service().non_blocking(
1078  this->get_implementation(), mode, ec);
1079  }
1080 
1082 
1165  bool native_non_blocking() const
1166  {
1167  return this->get_service().native_non_blocking(this->get_implementation());
1168  }
1169 
1171 
1255  void native_non_blocking(bool mode)
1256  {
1257  asio::error_code ec;
1258  this->get_service().native_non_blocking(
1259  this->get_implementation(), mode, ec);
1260  asio::detail::throw_error(ec, "native_non_blocking");
1261  }
1262 
1264 
1349  bool mode, asio::error_code& ec)
1350  {
1351  return this->get_service().native_non_blocking(
1352  this->get_implementation(), mode, ec);
1353  }
1354 
1356 
1370  endpoint_type local_endpoint() const
1371  {
1372  asio::error_code ec;
1373  endpoint_type ep = this->get_service().local_endpoint(
1374  this->get_implementation(), ec);
1375  asio::detail::throw_error(ec, "local_endpoint");
1376  return ep;
1377  }
1378 
1380 
1400  endpoint_type local_endpoint(asio::error_code& ec) const
1401  {
1402  return this->get_service().local_endpoint(this->get_implementation(), ec);
1403  }
1404 
1406 
1420  endpoint_type remote_endpoint() const
1421  {
1422  asio::error_code ec;
1423  endpoint_type ep = this->get_service().remote_endpoint(
1424  this->get_implementation(), ec);
1425  asio::detail::throw_error(ec, "remote_endpoint");
1426  return ep;
1427  }
1428 
1430 
1450  endpoint_type remote_endpoint(asio::error_code& ec) const
1451  {
1452  return this->get_service().remote_endpoint(this->get_implementation(), ec);
1453  }
1454 
1456 
1473  {
1474  asio::error_code ec;
1475  this->get_service().shutdown(this->get_implementation(), what, ec);
1476  asio::detail::throw_error(ec, "shutdown");
1477  }
1478 
1480 
1502  asio::error_code& ec)
1503  {
1504  return this->get_service().shutdown(this->get_implementation(), what, ec);
1505  }
1506 
1507 protected:
1510  {
1511  }
1512 };
1513 
1514 } // namespace asio
1515 
1516 #include "asio/detail/pop_options.hpp"
1517 
1518 #endif // ASIO_BASIC_SOCKET_HPP
bool at_mark(asio::error_code &ec) const
Determine whether the socket is at the out-of-band data mark.
bool is_open() const
Determine whether the socket is open.
void open(const protocol_type &protocol=protocol_type())
Open the socket using the specified protocol.
void set_option(const SettableSocketOption &option)
Set an option on the socket.
endpoint_type local_endpoint(asio::error_code &ec) const
Get the local endpoint of the socket.
asio::error_code shutdown(shutdown_type what, asio::error_code &ec)
Disable sends or receives on the socket.
void throw_error(const asio::error_code &err)
Definition: throw_error.hpp:31
asio::error_code assign(const protocol_type &protocol, const native_handle_type &native_socket, asio::error_code &ec)
Assign an existing native socket to the socket.
asio::error_code open(const protocol_type &protocol, asio::error_code &ec)
Open the socket using the specified protocol.
#define ASIO_CONNECT_HANDLER_CHECK(handler_type, handler)
Provides core I/O functionality.
Definition: io_service.hpp:184
service_type & get_service()
Get the service associated with the I/O object.
post(ASIO_MOVE_ARG(CompletionHandler) handler)
Request the io_service to invoke the given handler and return immediately.
#define ASIO_MOVE_CAST2(type1, type2)
Definition: config.hpp:139
SocketService & s
Definition: connect.hpp:521
endpoint_type local_endpoint() const
Get the local endpoint of the socket.
ASIO_MOVE_ARG(ConnectHandler) handler)
Base class for all I/O objects.
basic_socket< Protocol, SocketService > lowest_layer_type
A basic_socket is always the lowest layer.
asio::error_code close(asio::error_code &ec)
Close the socket.
basic_socket(asio::io_service &io_service, const endpoint_type &endpoint)
asio::basic_streambuf< Allocator > MatchCondition enable_if< is_match_condition< MatchCondition >::value >::type *detail::async_result_init< ReadHandler, void(asio::error_code, std::size_t)> init(ASIO_MOVE_CAST(ReadHandler)(handler))
#define ASIO_HANDLER_TYPE(h, sig)
Protocol protocol_type
The protocol type.
bool at_mark() const
Determine whether the socket is at the out-of-band data mark.
const lowest_layer_type & lowest_layer() const
Get a const reference to the lowest layer.
endpoint_type remote_endpoint(asio::error_code &ec) const
Get the remote endpoint of the socket.
basic_socket(asio::io_service &io_service)
Construct a basic_socket without opening it.
asio::basic_streambuf< Allocator > & b
Definition: read.hpp:702
void assign(const protocol_type &protocol, const native_handle_type &native_socket)
Assign an existing native socket to the socket.
void non_blocking(bool mode)
Sets the non-blocking mode of the socket.
basic_socket(asio::io_service &io_service, const protocol_type &protocol, const native_handle_type &native_socket)
Construct a basic_socket on an existing native socket.
asio::error_code bind(const endpoint_type &endpoint, asio::error_code &ec)
Bind the socket to the given local endpoint.
~basic_socket()
Protected destructor to prevent deletion through this type.
SocketService::native_handle_type native_type
void shutdown(shutdown_type what)
Disable sends or receives on the socket.
bool non_blocking() const
Gets the non-blocking mode of the socket.
void native_non_blocking(bool mode)
Sets the non-blocking mode of the native socket implementation.
void cancel()
Cancel all asynchronous operations associated with the socket.
asio::error_code non_blocking(bool mode, asio::error_code &ec)
Sets the non-blocking mode of the socket.
ASIO_INITFN_RESULT_TYPE(ConnectHandler, void(asio::error_code)) async_connect(const endpoint_type &peer_endpoint
Start an asynchronous connect.
implementation_type & get_implementation()
Get the underlying implementation of the I/O object.
void bind(const endpoint_type &endpoint)
Bind the socket to the given local endpoint.
std::size_t available() const
Determine the number of bytes available for reading.
shutdown_type
Different ways a socket may be shutdown.
Definition: socket_base.hpp:34
void io_control(IoControlCommand &command)
Perform an IO control command on the socket.
asio::error_code get_option(GettableSocketOption &option, asio::error_code &ec) const
Get an option from the socket.
basic_socket(asio::io_service &io_service, const protocol_type &protocol)
Construct and open a basic_socket.
Protocol::endpoint endpoint_type
The endpoint type.
Class to represent an error code value.
Definition: error_code.hpp:80
native_type native()
(Deprecated: Use native_handle().) Get the native socket representation.
asio::error_code native_non_blocking(bool mode, asio::error_code &ec)
Sets the non-blocking mode of the native socket implementation.
bool native_non_blocking() const
Gets the non-blocking mode of the native socket implementation.
asio::io_service & get_io_service()
Get the io_service associated with the object.
asio::error_code cancel(asio::error_code &ec)
Cancel all asynchronous operations associated with the socket.
void close()
Close the socket.
asio::error_code set_option(const SettableSocketOption &option, asio::error_code &ec)
Set an option on the socket.
void get_option(GettableSocketOption &option) const
Get an option from the socket.
asio::error_code connect(const endpoint_type &peer_endpoint, asio::error_code &ec)
Connect the socket to the specified endpoint.
handler_type< Handler, Signature >::type handler
Provides socket functionality.
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
binder1< Handler, Arg1 > bind_handler(Handler handler, const Arg1 &arg1)
async_result< typename handler_type< Handler, Signature >::type > result
SocketService::native_handle_type native_handle_type
The native representation of a socket.
asio::error_code io_control(IoControlCommand &command, asio::error_code &ec)
Perform an IO control command on the socket.
native_handle_type native_handle()
Get the native socket representation.
std::size_t available(asio::error_code &ec) const
Determine the number of bytes available for reading.
endpoint_type remote_endpoint() const
Get the remote endpoint of the socket.
void connect(const endpoint_type &peer_endpoint)
Connect the socket to the specified endpoint.
lowest_layer_type & lowest_layer()
Get a reference to the lowest layer.