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

Function async_write_message

example/cpp20/operations/composed_4.cpp:40–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 3

test_callbackFunction · 0.70
test_deferredFunction · 0.70
test_futureFunction · 0.70

Calls 7

get_associated_executorFunction · 0.85
async_writeFunction · 0.85
bufferFunction · 0.85
postFunction · 0.50
bindFunction · 0.50
refClass · 0.50
get_executorMethod · 0.45

Tested by 3

test_callbackFunction · 0.56
test_deferredFunction · 0.56
test_futureFunction · 0.56