]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // buffered_write_stream.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #ifndef BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP | |
12 | #define BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | #include <cstddef> | |
20 | #include <boost/asio/buffered_write_stream_fwd.hpp> | |
21 | #include <boost/asio/buffer.hpp> | |
22 | #include <boost/asio/completion_condition.hpp> | |
23 | #include <boost/asio/detail/bind_handler.hpp> | |
24 | #include <boost/asio/detail/buffered_stream_storage.hpp> | |
25 | #include <boost/asio/detail/noncopyable.hpp> | |
26 | #include <boost/asio/detail/type_traits.hpp> | |
27 | #include <boost/asio/error.hpp> | |
7c673cae FG |
28 | #include <boost/asio/write.hpp> |
29 | ||
30 | #include <boost/asio/detail/push_options.hpp> | |
31 | ||
32 | namespace boost { | |
33 | namespace asio { | |
34 | ||
35 | /// Adds buffering to the write-related operations of a stream. | |
36 | /** | |
37 | * The buffered_write_stream class template can be used to add buffering to the | |
38 | * synchronous and asynchronous write operations of a stream. | |
39 | * | |
40 | * @par Thread Safety | |
41 | * @e Distinct @e objects: Safe.@n | |
42 | * @e Shared @e objects: Unsafe. | |
43 | * | |
44 | * @par Concepts: | |
45 | * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream. | |
46 | */ | |
47 | template <typename Stream> | |
48 | class buffered_write_stream | |
49 | : private noncopyable | |
50 | { | |
51 | public: | |
52 | /// The type of the next layer. | |
53 | typedef typename remove_reference<Stream>::type next_layer_type; | |
54 | ||
55 | /// The type of the lowest layer. | |
56 | typedef typename next_layer_type::lowest_layer_type lowest_layer_type; | |
57 | ||
b32b8144 FG |
58 | /// The type of the executor associated with the object. |
59 | typedef typename lowest_layer_type::executor_type executor_type; | |
60 | ||
7c673cae FG |
61 | #if defined(GENERATING_DOCUMENTATION) |
62 | /// The default buffer size. | |
63 | static const std::size_t default_buffer_size = implementation_defined; | |
64 | #else | |
65 | BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024); | |
66 | #endif | |
67 | ||
68 | /// Construct, passing the specified argument to initialise the next layer. | |
69 | template <typename Arg> | |
70 | explicit buffered_write_stream(Arg& a) | |
71 | : next_layer_(a), | |
72 | storage_(default_buffer_size) | |
73 | { | |
74 | } | |
75 | ||
76 | /// Construct, passing the specified argument to initialise the next layer. | |
77 | template <typename Arg> | |
78 | buffered_write_stream(Arg& a, std::size_t buffer_size) | |
79 | : next_layer_(a), | |
80 | storage_(buffer_size) | |
81 | { | |
82 | } | |
83 | ||
84 | /// Get a reference to the next layer. | |
85 | next_layer_type& next_layer() | |
86 | { | |
87 | return next_layer_; | |
88 | } | |
89 | ||
90 | /// Get a reference to the lowest layer. | |
91 | lowest_layer_type& lowest_layer() | |
92 | { | |
93 | return next_layer_.lowest_layer(); | |
94 | } | |
95 | ||
96 | /// Get a const reference to the lowest layer. | |
97 | const lowest_layer_type& lowest_layer() const | |
98 | { | |
99 | return next_layer_.lowest_layer(); | |
100 | } | |
101 | ||
b32b8144 FG |
102 | /// Get the executor associated with the object. |
103 | executor_type get_executor() BOOST_ASIO_NOEXCEPT | |
104 | { | |
105 | return next_layer_.lowest_layer().get_executor(); | |
106 | } | |
107 | ||
7c673cae FG |
108 | /// Close the stream. |
109 | void close() | |
110 | { | |
111 | next_layer_.close(); | |
112 | } | |
113 | ||
114 | /// Close the stream. | |
b32b8144 | 115 | BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec) |
7c673cae | 116 | { |
b32b8144 FG |
117 | next_layer_.close(ec); |
118 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
119 | } |
120 | ||
121 | /// Flush all data from the buffer to the next layer. Returns the number of | |
122 | /// bytes written to the next layer on the last write operation. Throws an | |
123 | /// exception on failure. | |
124 | std::size_t flush(); | |
125 | ||
126 | /// Flush all data from the buffer to the next layer. Returns the number of | |
127 | /// bytes written to the next layer on the last write operation, or 0 if an | |
128 | /// error occurred. | |
129 | std::size_t flush(boost::system::error_code& ec); | |
130 | ||
131 | /// Start an asynchronous flush. | |
92f5a8d4 TL |
132 | template < |
133 | BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code, | |
134 | std::size_t)) WriteHandler | |
135 | BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)> | |
136 | BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler, | |
7c673cae | 137 | void (boost::system::error_code, std::size_t)) |
92f5a8d4 TL |
138 | async_flush( |
139 | BOOST_ASIO_MOVE_ARG(WriteHandler) handler | |
140 | BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)); | |
7c673cae FG |
141 | |
142 | /// Write the given data to the stream. Returns the number of bytes written. | |
143 | /// Throws an exception on failure. | |
144 | template <typename ConstBufferSequence> | |
145 | std::size_t write_some(const ConstBufferSequence& buffers); | |
146 | ||
147 | /// Write the given data to the stream. Returns the number of bytes written, | |
148 | /// or 0 if an error occurred and the error handler did not throw. | |
149 | template <typename ConstBufferSequence> | |
150 | std::size_t write_some(const ConstBufferSequence& buffers, | |
151 | boost::system::error_code& ec); | |
152 | ||
153 | /// Start an asynchronous write. The data being written must be valid for the | |
154 | /// lifetime of the asynchronous operation. | |
92f5a8d4 TL |
155 | template <typename ConstBufferSequence, |
156 | BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code, | |
157 | std::size_t)) WriteHandler | |
158 | BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)> | |
159 | BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler, | |
7c673cae FG |
160 | void (boost::system::error_code, std::size_t)) |
161 | async_write_some(const ConstBufferSequence& buffers, | |
92f5a8d4 TL |
162 | BOOST_ASIO_MOVE_ARG(WriteHandler) handler |
163 | BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)); | |
7c673cae FG |
164 | |
165 | /// Read some data from the stream. Returns the number of bytes read. Throws | |
166 | /// an exception on failure. | |
167 | template <typename MutableBufferSequence> | |
168 | std::size_t read_some(const MutableBufferSequence& buffers) | |
169 | { | |
170 | return next_layer_.read_some(buffers); | |
171 | } | |
172 | ||
173 | /// Read some data from the stream. Returns the number of bytes read or 0 if | |
174 | /// an error occurred. | |
175 | template <typename MutableBufferSequence> | |
176 | std::size_t read_some(const MutableBufferSequence& buffers, | |
177 | boost::system::error_code& ec) | |
178 | { | |
179 | return next_layer_.read_some(buffers, ec); | |
180 | } | |
181 | ||
182 | /// Start an asynchronous read. The buffer into which the data will be read | |
183 | /// must be valid for the lifetime of the asynchronous operation. | |
92f5a8d4 TL |
184 | template <typename MutableBufferSequence, |
185 | BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code, | |
186 | std::size_t)) ReadHandler | |
187 | BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)> | |
188 | BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler, | |
7c673cae FG |
189 | void (boost::system::error_code, std::size_t)) |
190 | async_read_some(const MutableBufferSequence& buffers, | |
92f5a8d4 TL |
191 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler |
192 | BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)) | |
7c673cae | 193 | { |
b32b8144 | 194 | return next_layer_.async_read_some(buffers, |
7c673cae | 195 | BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); |
7c673cae FG |
196 | } |
197 | ||
198 | /// Peek at the incoming data on the stream. Returns the number of bytes read. | |
199 | /// Throws an exception on failure. | |
200 | template <typename MutableBufferSequence> | |
201 | std::size_t peek(const MutableBufferSequence& buffers) | |
202 | { | |
203 | return next_layer_.peek(buffers); | |
204 | } | |
205 | ||
206 | /// Peek at the incoming data on the stream. Returns the number of bytes read, | |
207 | /// or 0 if an error occurred. | |
208 | template <typename MutableBufferSequence> | |
209 | std::size_t peek(const MutableBufferSequence& buffers, | |
210 | boost::system::error_code& ec) | |
211 | { | |
212 | return next_layer_.peek(buffers, ec); | |
213 | } | |
214 | ||
215 | /// Determine the amount of data that may be read without blocking. | |
216 | std::size_t in_avail() | |
217 | { | |
218 | return next_layer_.in_avail(); | |
219 | } | |
220 | ||
221 | /// Determine the amount of data that may be read without blocking. | |
222 | std::size_t in_avail(boost::system::error_code& ec) | |
223 | { | |
224 | return next_layer_.in_avail(ec); | |
225 | } | |
226 | ||
227 | private: | |
228 | /// Copy data into the internal buffer from the specified source buffer. | |
229 | /// Returns the number of bytes copied. | |
230 | template <typename ConstBufferSequence> | |
231 | std::size_t copy(const ConstBufferSequence& buffers); | |
232 | ||
233 | /// The next layer. | |
234 | Stream next_layer_; | |
235 | ||
236 | // The data in the buffer. | |
237 | detail::buffered_stream_storage storage_; | |
238 | }; | |
239 | ||
240 | } // namespace asio | |
241 | } // namespace boost | |
242 | ||
243 | #include <boost/asio/detail/pop_options.hpp> | |
244 | ||
245 | #include <boost/asio/impl/buffered_write_stream.hpp> | |
246 | ||
247 | #endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP |