MCPcopy Create free account
hub / github.com/boostorg/asio / async_write_messages

Function async_write_messages

example/cpp14/operations/composed_6.cpp:42–244  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40
41template <typename T, typename CompletionToken>
42auto async_write_messages(tcp::socket& socket,
43 const T& message, std::size_t repeat_count,
44 CompletionToken&& token)
45 // The return type of the initiating function is deduced from the combination
46 // of:
47 //
48 // - the CompletionToken type,
49 // - the completion handler signature, and
50 // - the asynchronous operation's initiation function object.
51 //
52 // When the completion token is a simple callback, the return type is always
53 // void. In this example, when the completion token is boost::asio::yield_context
54 // (used for stackful coroutines) the return type would also be void, as
55 // there is no non-error argument to the completion handler. When the
56 // completion token is boost::asio::use_future it would be std::future<void>. When
57 // the completion token is boost::asio::deferred, the return type differs for each
58 // asynchronous operation.
59 //
60 // In C++14 we can omit the return type as it is automatically deduced from
61 // the return type of boost::asio::async_initiate.
62{
63 // In addition to determining the mechanism by which an asynchronous
64 // operation delivers its result, a completion token also determines the time
65 // when the operation commences. For example, when the completion token is a
66 // simple callback the operation commences before the initiating function
67 // returns. However, if the completion token's delivery mechanism uses a
68 // future, we might instead want to defer initiation of the operation until
69 // the returned future object is waited upon.
70 //
71 // To enable this, when implementing an asynchronous operation we must
72 // package the initiation step as a function object. The initiation function
73 // object's call operator is passed the concrete completion handler produced
74 // by the completion token. This completion handler matches the asynchronous
75 // operation's completion handler signature, which in this example is:
76 //
77 // void(boost::system::error_code error)
78 //
79 // The initiation function object also receives any additional arguments
80 // required to start the operation. (Note: We could have instead passed these
81 // arguments in the lambda capture set. However, we should prefer to
82 // propagate them as function call arguments as this allows the completion
83 // token to optimise how they are passed. For example, a lazy future which
84 // defers initiation would need to make a decay-copy of the arguments, but
85 // when using a simple callback the arguments can be trivially forwarded
86 // straight through.)
87 auto initiation = [](auto&& completion_handler, tcp::socket& socket,
88 std::unique_ptr<std::string> encoded_message, std::size_t repeat_count,
89 std::unique_ptr<boost::asio::steady_timer> delay_timer)
90 {
91 // In this example, the composed operation's intermediate completion
92 // handler is implemented as a hand-crafted function object.
93 struct intermediate_completion_handler
94 {
95 // The intermediate completion handler holds a reference to the socket as
96 // it is used for multiple async_write operations, as well as for
97 // obtaining the I/O executor (see get_executor below).
98 tcp::socket& socket_;
99

Callers 3

test_callbackFunction · 0.70
test_deferredFunction · 0.70
test_futureFunction · 0.70

Calls 4

bufferFunction · 0.85
async_writeFunction · 0.85
refClass · 0.50
get_executorMethod · 0.45

Tested by 3

test_callbackFunction · 0.56
test_deferredFunction · 0.56
test_futureFunction · 0.56