summaryrefslogtreecommitdiff
path: root/libs/asio/doc/overview/other_protocols.qbk
blob: 08b54d3a6c38a747a6e7371a34de5c2d05010390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
[/
 / Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
 /
 / Distributed under the Boost Software License, Version 1.0. (See accompanying
 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 /]

[section:other_protocols Support for Other Protocols]

Support for other socket protocols (such as Bluetooth or IRCOMM sockets) can be
added by implementing the [link boost_asio.reference.Protocol protocol type
requirements]. However, in many cases these protocols may also be used with
Boost.Asio's generic protocol support. For this, Boost.Asio provides the following four
classes:
    
* [link boost_asio.reference.generic__datagram_protocol `generic::datagram_protocol`]
* [link boost_asio.reference.generic__raw_protocol `generic::raw_protocol`]
* [link boost_asio.reference.generic__seq_packet_protocol `generic::seq_packet_protocol`]
* [link boost_asio.reference.generic__stream_protocol `generic::stream_protocol`]
    
These classes implement the [link boost_asio.reference.Protocol protocol type
requirements], but allow the user to specify the address family (e.g. `AF_INET`)
and protocol type (e.g. `IPPROTO_TCP`) at runtime. For example:

  boost::asio::generic::stream_protocol::socket my_socket(my_io_service);
  my_socket.open(boost::asio::generic::stream_protocol(AF_INET, IPPROTO_TCP));
  ...

An endpoint class template, [link boost_asio.reference.generic__basic_endpoint
`boost::asio::generic::basic_endpoint`], is included to support these protocol
classes. This endpoint can hold any other endpoint type, provided its native
representation fits into a `sockaddr_storage` object. This class will also
convert from other types that implement the [link boost_asio.reference.Endpoint
endpoint] type requirements:

  boost::asio::ip::tcp::endpoint my_endpoint1 = ...;
  boost::asio::generic::stream_protocol::endpoint my_endpoint2(my_endpoint1);

The conversion is implicit, so as to support the following use cases:

  boost::asio::generic::stream_protocol::socket my_socket(my_io_service);
  boost::asio::ip::tcp::endpoint my_endpoint = ...;
  my_socket.connect(my_endpoint);

[heading C++11 Move Construction]
    
When using C++11, it is possible to perform move construction from a socket (or
acceptor) object to convert to the more generic protocol's socket (or acceptor)
type. If the protocol conversion is valid:

  Protocol1 p1 = ...;
  Protocol2 p2(p1);

then the corresponding socket conversion is allowed:

  Protocol1::socket my_socket1(my_io_service);
  ...
  Protocol2::socket my_socket2(std::move(my_socket1));

For example, one possible conversion is from a TCP socket to a generic
stream-oriented socket:

  boost::asio::ip::tcp::socket my_socket1(my_io_service);
  ...
  boost::asio::generic::stream_protocol::socket my_socket2(std::move(my_socket1));

These conversions are also available for move-assignment.

These conversions are not limited to the above generic protocol classes.
User-defined protocols may take advantage of this feature by similarly ensuring
the conversion from `Protocol1` to `Protocol2` is valid, as above.

[heading Accepting Generic Sockets]

As a convenience, a socket acceptor's `accept()` and `async_accept()` functions
can directly accept into a different protocol's socket type, provided the
corresponding protocol conversion is valid. For example, the following is
supported because the protocol `boost::asio::ip::tcp` is convertible to
`boost::asio::generic::stream_protocol`:

  boost::asio::ip::tcp::acceptor my_acceptor(my_io_service);
  ...
  boost::asio::generic::stream_protocol::socket my_socket(my_io_service);
  my_acceptor.accept(my_socket);

[heading See Also]

[link boost_asio.reference.generic__datagram_protocol `generic::datagram_protocol`],
[link boost_asio.reference.generic__raw_protocol `generic::raw_protocol`],
[link boost_asio.reference.generic__seq_packet_protocol `generic::seq_packet_protocol`],
[link boost_asio.reference.generic__stream_protocol `generic::stream_protocol`],
[link boost_asio.reference.Protocol protocol type requirements].

[endsect]