4__layers.qbk 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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:layered_streams Layered Streams]
  8. Networking's __ssl_stream__ is a class template meeting the requirements
  9. of both synchronous and asynchronous read and write streams, implemented
  10. in terms of a "next layer" object whose type is determined by a class
  11. template parameter. The SSL stream constructs an instance of the next
  12. layer object internally, while allowing external access through the
  13. observer `net::ssl::stream::next_layer()`. This declares an SSL stream
  14. which uses a regular TCP/IP socket as the next layer:
  15. [code_core_4_layers_1]
  16. Objects using this design pattern are referred to in networking as "a
  17. stack of stream layers". In Beast we use the term ['layered stream],
  18. although the property of having a next layer is not exclusive to streams.
  19. As with the SSL stream, __websocket_stream__ is a class template
  20. parameterized on a next layer object. This declares a websocket
  21. stream which uses a regular TCP/IP socket as the next layer:
  22. [code_core_4_layers_2]
  23. If a Secure WebSockets stream is desired, this is accomplished simply
  24. by changing the type of the next layer and adjusting the constructor
  25. arguments to match:
  26. [code_core_4_layers_3]
  27. Higher level abstractions can be developed in this fashion by nesting
  28. stream layers to arbitrary degree. The stack of stream layers effectively
  29. forms a compile-time singly linked list. The object at the end of
  30. this list is called the ['lowest layer], and is special from the
  31. others because it typically represents the underlying socket.
  32. Beast comes with several layered stream wrappers, as well as
  33. facilities for authoring and working with layered streams:
  34. [table Layered Stream Algorithms and Types
  35. [[Name][Description]]
  36. [[
  37. [link beast.ref.boost__beast__basic_stream `basic_stream`]
  38. [link beast.ref.boost__beast__tcp_stream `tcp_stream`]
  39. ][
  40. This stream can be used for synchronous and asynchronous reading
  41. and writing. It allows timeouts to be set on logical operations,
  42. and can have an executor associated with the stream which is
  43. used to invoke completion handlers. This lets you set a strand
  44. on the stream once, which is then used for all asynchronous
  45. operations automatically.
  46. ]]
  47. [[
  48. [link beast.ref.boost__beast__buffered_read_stream `buffered_read_stream`]
  49. ][
  50. A buffered read stream meets the requirements for synchronous and
  51. asynchronous read and write streams, and additionally implements
  52. configurable buffering for reads.
  53. ]]
  54. [[
  55. [link beast.ref.boost__beast__close_socket `close_socket`]
  56. ][
  57. This function closes a socket by performing an unqualified call
  58. to the
  59. [link beast.ref.boost__beast__beast_close_socket `beast_close_socket`]
  60. customization point, allowing sockets to be closed in generic
  61. contexts in an extensible fashion.
  62. ]]
  63. [[
  64. [link beast.ref.boost__beast__flat_stream `flat_stream`]
  65. ][
  66. A flat stream operates as a transparent stream which helps to work around
  67. a limitation of `net::ssl::stream`. It is used in the implementation
  68. of [link beast.ref.boost__beast__ssl_stream `ssl_stream`].
  69. ]]
  70. [[
  71. [link beast.ref.boost__beast__get_lowest_layer `get_lowest_layer`]
  72. ][
  73. Returns the lowest layer in a stack of stream layers by recursively
  74. calling the `next_layer` member function on each object until reaching
  75. an object which lacks the member. This example
  76. puts a layered stream into non-blocking mode by retrieving the
  77. TCP/IP socket in the lowest layer and changing the socket option:
  78. [code_core_4_layers_4]
  79. ]]
  80. [[
  81. [link beast.ref.boost__beast__http__icy_stream `http::icy_stream`]
  82. ][
  83. An ICY stream transparently converts the non-standard "ICY 200 OK"
  84. HTTP response from Shoutcast servers into a conforming 200 level
  85. HTTP response.
  86. ]]
  87. [[
  88. [link beast.ref.boost__beast__lowest_layer_type `lowest_layer_type`]
  89. ][
  90. A metafunction to return the type of the lowest layer used in
  91. a type representing a stack of stream layers. This is the type
  92. of reference returned by
  93. [link beast.ref.boost__beast__get_lowest_layer `get_lowest_layer`]
  94. ]]
  95. [[
  96. [link beast.ref.boost__beast__ssl_stream `ssl_stream`]
  97. ][
  98. The SSL stream is a drop-in replacement for `net::ssl::stream` which
  99. allows for move-construction and move-assignment, and also implements
  100. a work-around for a performance limitation in the original SSL stream.
  101. ]]
  102. ]
  103. [/-----------------------------------------------------------------------------]
  104. [section Counted Stream __example__]
  105. This example shows the definition of a layered stream which keeps individual
  106. counts of the total number of bytes read from and written to the next layer.
  107. It meets the requirements for synchronous and asynchronous read and write
  108. streams:
  109. [code_core_4_layers_5]
  110. [endsect]
  111. [/-----------------------------------------------------------------------------]
  112. [endsect]