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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
//
// composed_1.cpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 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)
//
#include <asio/deferred.hpp>
#include <asio/io_context.hpp>
#include <asio/ip/tcp.hpp>
#include <asio/use_future.hpp>
#include <asio/write.hpp>
#include <cstring>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
using asio::ip::tcp;
//------------------------------------------------------------------------------
// This is the simplest example of a composed asynchronous operation, where we
// simply repackage an existing operation. The asynchronous operation
// requirements are met by delegating responsibility to the underlying
// operation.
template <typename CompletionToken>
auto async_write_message(tcp::socket& socket,
const char* message, CompletionToken&& token)
// The return type of the initiating function is deduced from the combination
// of:
//
// - the CompletionToken type,
// - the completion handler signature, and
// - the asynchronous operation's initiation function object.
//
// When the completion token is a simple callback, the return type is void.
// However, when the completion token is asio::yield_context (used for
// stackful coroutines) the return type would be std::size_t, and when the
// completion token is asio::use_future it would be std::future<std::size_t>.
// When the completion token is asio::deferred, the return type differs for
// each asynchronous operation.
//
// In this example we are trivially delegating to an underlying asynchronous
// operation, so we can deduce the return type from that.
-> decltype(
asio::async_write(socket,
asio::buffer(message, std::strlen(message)),
std::forward<CompletionToken>(token)))
{
// When delegating to the underlying operation we must take care to perfectly
// forward the completion token. This ensures that our operation works
// correctly with move-only function objects as callbacks, as well as other
// completion token types.
return asio::async_write(socket,
asio::buffer(message, std::strlen(message)),
std::forward<CompletionToken>(token));
}
//------------------------------------------------------------------------------
void test_callback()
{
asio::io_context io_context;
tcp::acceptor acceptor(io_context, {tcp::v4(), 55555});
tcp::socket socket = acceptor.accept();
// Test our asynchronous operation using a lambda as a callback.
async_write_message(socket, "Testing callback\r\n",
[](const std::error_code& error, std::size_t n)
{
if (!error)
{
std::cout << n << " bytes transferred\n";
}
else
{
std::cout << "Error: " << error.message() << "\n";
}
});
io_context.run();
}
//------------------------------------------------------------------------------
void test_deferred()
{
asio::io_context io_context;
tcp::acceptor acceptor(io_context, {tcp::v4(), 55555});
tcp::socket socket = acceptor.accept();
// Test our asynchronous operation using the deferred completion token. This
// token causes the operation's initiating function to package up the
// operation and its arguments to return a function object, which may then be
// used to launch the asynchronous operation.
auto op = async_write_message(socket,
"Testing deferred\r\n", asio::deferred);
// Launch the operation using a lambda as a callback.
std::move(op)(
[](const std::error_code& error, std::size_t n)
{
if (!error)
{
std::cout << n << " bytes transferred\n";
}
else
{
std::cout << "Error: " << error.message() << "\n";
}
});
io_context.run();
}
//------------------------------------------------------------------------------
void test_future()
{
asio::io_context io_context;
tcp::acceptor acceptor(io_context, {tcp::v4(), 55555});
tcp::socket socket = acceptor.accept();
// Test our asynchronous operation using the use_future completion token.
// This token causes the operation's initiating function to return a future,
// which may be used to synchronously wait for the result of the operation.
std::future<std::size_t> f = async_write_message(
socket, "Testing future\r\n", asio::use_future);
io_context.run();
try
{
// Get the result of the operation.
std::size_t n = f.get();
std::cout << n << " bytes transferred\n";
}
catch (const std::exception& e)
{
std::cout << "Error: " << e.what() << "\n";
}
}
//------------------------------------------------------------------------------
int main()
{
test_callback();
test_deferred();
test_future();
}
|