]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/doc/overview/basics.qbk
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / asio / doc / overview / basics.qbk
CommitLineData
7c673cae
FG
1[/
2 / Copyright (c) 2003-2016 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
8[section:basics Basic Boost.Asio Anatomy]
9
10Boost.Asio may be used to perform both synchronous and asynchronous operations on I/O
11objects such as sockets. Before using Boost.Asio it may be useful to get a conceptual
12picture of the various parts of Boost.Asio, your program, and how they work together.
13
14As an introductory example, let's consider what happens when you perform a
15connect operation on a socket. We shall start by examining synchronous
16operations.
17
18[$boost_asio/sync_op.png]
19
20[*Your program] will have at least one [*io_service] object. The [*io_service]
21represents [*your program]'s link to the [*operating system]'s I/O services.
22
23 boost::asio::io_service io_service;
24
25To perform I/O operations [*your program] will need an [*I/O object] such as a
26TCP socket:
27
28 boost::asio::ip::tcp::socket socket(io_service);
29
30When a synchronous connect operation is performed, the following sequence of
31events occurs:
32
331. [*Your program] initiates the connect operation by calling the [*I/O
34object]:
35
36 socket.connect(server_endpoint);
37
382. The [*I/O object] forwards the request to the [*io_service].
39
403. The [*io_service] calls on the [*operating system] to perform the connect
41operation.
42
434. The [*operating system] returns the result of the operation to the
44[*io_service].
45
465. The [*io_service] translates any error resulting from the operation into an
47object of type `boost::system::error_code`. An `error_code` may be compared with
48specific values, or tested as a boolean (where a `false` result means that no
49error occurred). The result is then forwarded back up to the [*I/O object].
50
516. The [*I/O object] throws an exception of type `boost::system::system_error` if the
52operation failed. If the code to initiate the operation had instead been
53written as:
54
55 boost::system::error_code ec;
56 socket.connect(server_endpoint, ec);
57
58then the `error_code` variable `ec` would be set to the result of the
59operation, and no exception would be thrown.
60
61When an asynchronous operation is used, a different sequence of events occurs.
62
63[$boost_asio/async_op1.png]
64
651. [*Your program] initiates the connect operation by calling the [*I/O
66object]:
67
68 socket.async_connect(server_endpoint, your_completion_handler);
69
70where `your_completion_handler` is a function or function object with the
71signature:
72
73 void your_completion_handler(const boost::system::error_code& ec);
74
75The exact signature required depends on the asynchronous operation being
76performed. The reference documentation indicates the appropriate form for each
77operation.
78
792. The [*I/O object] forwards the request to the [*io_service].
80
813. The [*io_service] signals to the [*operating system] that it should start an
82asynchronous connect.
83
84Time passes. (In the synchronous case this wait would have been contained
85entirely within the duration of the connect operation.)
86
87[$boost_asio/async_op2.png]
88
894. The [*operating system] indicates that the connect operation has completed
90by placing the result on a queue, ready to be picked up by the [*io_service].
91
925. [*Your program] must make a call to `io_service::run()` (or to one of the
93similar [*io_service] member functions) in order for the result to be
94retrieved. A call to `io_service::run()` blocks while there are unfinished
95asynchronous operations, so you would typically call it as soon as you have
96started your first asynchronous operation.
97
986. While inside the call to `io_service::run()`, the [*io_service] dequeues the
99result of the operation, translates it into an `error_code`, and then passes it
100to [*your completion handler].
101
102This is a simplified picture of how Boost.Asio operates. You will want to delve
103further into the documentation if your needs are more advanced, such as
104extending Boost.Asio to perform other types of asynchronous operations.
105
106[endsect]