other_protocols.qbk 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:other_protocols Support for Other Protocols]
  8. Support for other socket protocols (such as Bluetooth or IRCOMM sockets) can be
  9. added by implementing the [link boost_asio.reference.Protocol protocol type
  10. requirements]. However, in many cases these protocols may also be used with
  11. Boost.Asio's generic protocol support. For this, Boost.Asio provides the following four
  12. classes:
  13. * [link boost_asio.reference.generic__datagram_protocol `generic::datagram_protocol`]
  14. * [link boost_asio.reference.generic__raw_protocol `generic::raw_protocol`]
  15. * [link boost_asio.reference.generic__seq_packet_protocol `generic::seq_packet_protocol`]
  16. * [link boost_asio.reference.generic__stream_protocol `generic::stream_protocol`]
  17. These classes implement the [link boost_asio.reference.Protocol protocol type
  18. requirements], but allow the user to specify the address family (e.g. `AF_INET`)
  19. and protocol type (e.g. `IPPROTO_TCP`) at runtime. For example:
  20. boost::asio::generic::stream_protocol::socket my_socket(my_io_context);
  21. my_socket.open(boost::asio::generic::stream_protocol(AF_INET, IPPROTO_TCP));
  22. ...
  23. An endpoint class template, [link boost_asio.reference.generic__basic_endpoint
  24. `boost::asio::generic::basic_endpoint`], is included to support these protocol
  25. classes. This endpoint can hold any other endpoint type, provided its native
  26. representation fits into a `sockaddr_storage` object. This class will also
  27. convert from other types that implement the [link boost_asio.reference.Endpoint
  28. endpoint] type requirements:
  29. boost::asio::ip::tcp::endpoint my_endpoint1 = ...;
  30. boost::asio::generic::stream_protocol::endpoint my_endpoint2(my_endpoint1);
  31. The conversion is implicit, so as to support the following use cases:
  32. boost::asio::generic::stream_protocol::socket my_socket(my_io_context);
  33. boost::asio::ip::tcp::endpoint my_endpoint = ...;
  34. my_socket.connect(my_endpoint);
  35. [heading C++11 Move Construction]
  36. When using C++11, it is possible to perform move construction from a socket (or
  37. acceptor) object to convert to the more generic protocol's socket (or acceptor)
  38. type. If the protocol conversion is valid:
  39. Protocol1 p1 = ...;
  40. Protocol2 p2(p1);
  41. then the corresponding socket conversion is allowed:
  42. Protocol1::socket my_socket1(my_io_context);
  43. ...
  44. Protocol2::socket my_socket2(std::move(my_socket1));
  45. For example, one possible conversion is from a TCP socket to a generic
  46. stream-oriented socket:
  47. boost::asio::ip::tcp::socket my_socket1(my_io_context);
  48. ...
  49. boost::asio::generic::stream_protocol::socket my_socket2(std::move(my_socket1));
  50. These conversions are also available for move-assignment.
  51. These conversions are not limited to the above generic protocol classes.
  52. User-defined protocols may take advantage of this feature by similarly ensuring
  53. the conversion from `Protocol1` to `Protocol2` is valid, as above.
  54. [heading Accepting Generic Sockets]
  55. As a convenience, a socket acceptor's `accept()` and `async_accept()` functions
  56. can directly accept into a different protocol's socket type, provided the
  57. corresponding protocol conversion is valid. For example, the following is
  58. supported because the protocol `boost::asio::ip::tcp` is convertible to
  59. `boost::asio::generic::stream_protocol`:
  60. boost::asio::ip::tcp::acceptor my_acceptor(my_io_context);
  61. ...
  62. boost::asio::generic::stream_protocol::socket my_socket(my_io_context);
  63. my_acceptor.accept(my_socket);
  64. [heading See Also]
  65. [link boost_asio.reference.generic__datagram_protocol `generic::datagram_protocol`],
  66. [link boost_asio.reference.generic__raw_protocol `generic::raw_protocol`],
  67. [link boost_asio.reference.generic__seq_packet_protocol `generic::seq_packet_protocol`],
  68. [link boost_asio.reference.generic__stream_protocol `generic::stream_protocol`],
  69. [link boost_asio.reference.Protocol protocol type requirements].
  70. [endsect]