7_composed.qbk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. [/
  2. Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Official repository: https://github.com/boostorg/beast
  6. ]
  7. [section Writing Composed Operations]
  8. Asynchronous operations are started by calling a free function or member
  9. function known as an asynchronous ['__async_initfn__]. This function accepts
  10. parameters specific to the operation as well as a __CompletionToken__. The
  11. token is either a completion handler, or a type defining how the caller is
  12. informed of the asynchronous operation result. Networking provides the
  13. special tokens __use_future__ and __yield_context__ for using futures
  14. and coroutines respectively. This system of customizing the return value
  15. and method of completion notification is known as the
  16. ['Universal Asynchronous Model] described in __N3747__, and a built in
  17. to __NetTS__. Here is an example of an initiating function which reads a
  18. line from the stream and echoes it back. This function is developed
  19. further in the next section:
  20. [example_core_echo_op_1]
  21. [tip
  22. This initiating function receives the dynamic buffer by lvalue-reference,
  23. instead of by rvalue-reference as specified in networking. An explanation
  24. for this difference may be found in
  25. [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1100r0.html [P1100R0] Efficient composition with DynamicBuffer].
  26. ]
  27. Authors using Beast can reuse the library's primitives to create their
  28. own initiating functions for performing a series of other, intermediate
  29. asynchronous operations before invoking a final completion handler.
  30. The set of intermediate actions produced by an initiating function is
  31. known as a
  32. [@http://blog.think-async.com/2009/08/composed-operations-coroutines-and-code.html ['composed operation]].
  33. To ensure full interoperability and well-defined behavior, __Asio__ imposes
  34. requirements on the implementation of composed operations. These classes
  35. and functions make it easier to develop initiating functions and their
  36. composed operations:
  37. [table Asynchronous Helpers
  38. [[Name][Description]]
  39. [[
  40. [link beast.ref.boost__beast__async_base `async_op_base`]
  41. [link beast.ref.boost__beast__stable_async_base `stable_async_base`]
  42. ][
  43. This class is designed to be used as a base class when authoring
  44. composed asynchronous operations expressed as an intermediate
  45. completion handler. This eliminates the need for the extensive
  46. boilerplate to propagate the associated executor, associated
  47. allocator, and legacy completion handler hooks.
  48. ]]
  49. [[
  50. [link beast.ref.boost__beast__allocate_stable `allocate_stable`]
  51. ][
  52. For composed operation algorithms which need stable storage for
  53. temporary objects, this function may be used. Memory for the
  54. stable storage is allocated using the allocator associated with
  55. the final completion handler. The implementation automatically
  56. destroys the temporary object before the final completion handler
  57. is invoked, or when the intermediate completion handler is
  58. destroyed.
  59. ]]
  60. [[
  61. [link beast.ref.boost__beast__bind_handler `bind_handler`]
  62. ][
  63. This function creates a new handler which, when invoked, calls
  64. the original handler with the list of bound arguments. Any
  65. parameters passed in the invocation will be substituted for
  66. placeholders present in the list of bound arguments. Parameters
  67. which are not matched to placeholders are silently discarded.
  68. The passed handler and arguments are forwarded into the returned
  69. handler, whose associated allocator and associated executor will
  70. be the same as those of the original handler.
  71. ]]
  72. [[
  73. [link beast.ref.boost__beast__bind_front_handler `bind_front_handler`]
  74. ][
  75. This function creates a new handler which, when invoked, calls
  76. the original handler with the list of bound arguments, along with
  77. the list of invoked arguments at either the front or the back of
  78. the argument list. Placeholders are not supported.
  79. The passed handler and arguments are forwarded into the returned
  80. handler, whose associated allocator and associated executor will
  81. will be the same as those of the original handler.
  82. ]]
  83. [[
  84. [link beast.ref.boost__beast__saved_handler `saved_handler`]
  85. ][
  86. This wrapper safely stores a completion handler so it may be invoked
  87. later, allowing an implementation to "pause" an operation until some
  88. condition is met.
  89. ]]
  90. ]
  91. [include 7a_echo.qbk]
  92. [include 7b_detect_ssl.qbk]
  93. [endsect]