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

Function async_write_message

example/cpp20/operations/composed_2.cpp:39–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

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
async_write_someMethod · 0.45

Tested by 3

test_callbackFunction · 0.56
test_deferredFunction · 0.56
test_futureFunction · 0.56