Skip to content

Commit a579568

Browse files
committed
Update buffered stream operations to adhere to current handler patterns.
1 parent d7228f4 commit a579568

File tree

8 files changed

+1109
-313
lines changed

8 files changed

+1109
-313
lines changed

asio/include/asio/buffered_read_stream.hpp

Lines changed: 29 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "asio/detail/config.hpp"
1919
#include <cstddef>
20+
#include "asio/async_result.hpp"
2021
#include "asio/buffered_read_stream_fwd.hpp"
2122
#include "asio/buffer.hpp"
2223
#include "asio/detail/bind_handler.hpp"
@@ -133,199 +134,65 @@ class buffered_read_stream
133134
/// Start an asynchronous write. The data being written must be valid for the
134135
/// lifetime of the asynchronous operation.
135136
template <typename ConstBufferSequence, typename WriteHandler>
136-
void async_write_some(const ConstBufferSequence& buffers,
137-
WriteHandler handler)
137+
ASIO_INITFN_RESULT_TYPE(WriteHandler,
138+
void (asio::error_code, std::size_t))
139+
async_write_some(const ConstBufferSequence& buffers,
140+
ASIO_MOVE_ARG(WriteHandler) handler)
138141
{
139-
next_layer_.async_write_some(buffers, handler);
142+
detail::async_result_init<
143+
WriteHandler, void (asio::error_code, std::size_t)> init(
144+
ASIO_MOVE_CAST(WriteHandler)(handler));
145+
146+
next_layer_.async_write_some(buffers,
147+
ASIO_MOVE_CAST(ASIO_HANDLER_TYPE(WriteHandler,
148+
void (asio::error_code, std::size_t)))(init.handler));
149+
150+
return init.result.get();
140151
}
141152

142153
/// Fill the buffer with some data. Returns the number of bytes placed in the
143154
/// buffer as a result of the operation. Throws an exception on failure.
144-
std::size_t fill()
145-
{
146-
detail::buffer_resize_guard<detail::buffered_stream_storage>
147-
resize_guard(storage_);
148-
std::size_t previous_size = storage_.size();
149-
storage_.resize(storage_.capacity());
150-
storage_.resize(previous_size + next_layer_.read_some(buffer(
151-
storage_.data() + previous_size,
152-
storage_.size() - previous_size)));
153-
resize_guard.commit();
154-
return storage_.size() - previous_size;
155-
}
155+
std::size_t fill();
156156

157157
/// Fill the buffer with some data. Returns the number of bytes placed in the
158158
/// buffer as a result of the operation, or 0 if an error occurred.
159-
std::size_t fill(asio::error_code& ec)
160-
{
161-
detail::buffer_resize_guard<detail::buffered_stream_storage>
162-
resize_guard(storage_);
163-
std::size_t previous_size = storage_.size();
164-
storage_.resize(storage_.capacity());
165-
storage_.resize(previous_size + next_layer_.read_some(buffer(
166-
storage_.data() + previous_size,
167-
storage_.size() - previous_size),
168-
ec));
169-
resize_guard.commit();
170-
return storage_.size() - previous_size;
171-
}
172-
173-
template <typename ReadHandler>
174-
class fill_handler
175-
{
176-
public:
177-
fill_handler(asio::io_service& io_service,
178-
detail::buffered_stream_storage& storage,
179-
std::size_t previous_size, ReadHandler handler)
180-
: io_service_(io_service),
181-
storage_(storage),
182-
previous_size_(previous_size),
183-
handler_(handler)
184-
{
185-
}
186-
187-
void operator()(const asio::error_code& ec,
188-
std::size_t bytes_transferred)
189-
{
190-
storage_.resize(previous_size_ + bytes_transferred);
191-
io_service_.dispatch(detail::bind_handler(
192-
handler_, ec, bytes_transferred));
193-
}
194-
195-
private:
196-
asio::io_service& io_service_;
197-
detail::buffered_stream_storage& storage_;
198-
std::size_t previous_size_;
199-
ReadHandler handler_;
200-
};
159+
std::size_t fill(asio::error_code& ec);
201160

202161
/// Start an asynchronous fill.
203162
template <typename ReadHandler>
204-
void async_fill(ReadHandler handler)
205-
{
206-
std::size_t previous_size = storage_.size();
207-
storage_.resize(storage_.capacity());
208-
next_layer_.async_read_some(
209-
buffer(
210-
storage_.data() + previous_size,
211-
storage_.size() - previous_size),
212-
fill_handler<ReadHandler>(get_io_service(),
213-
storage_, previous_size, handler));
214-
}
163+
ASIO_INITFN_RESULT_TYPE(ReadHandler,
164+
void (asio::error_code, std::size_t))
165+
async_fill(ASIO_MOVE_ARG(ReadHandler) handler);
215166

216167
/// Read some data from the stream. Returns the number of bytes read. Throws
217168
/// an exception on failure.
218169
template <typename MutableBufferSequence>
219-
std::size_t read_some(const MutableBufferSequence& buffers)
220-
{
221-
if (asio::buffer_size(buffers) == 0)
222-
return 0;
223-
224-
if (storage_.empty())
225-
fill();
226-
227-
return copy(buffers);
228-
}
170+
std::size_t read_some(const MutableBufferSequence& buffers);
229171

230172
/// Read some data from the stream. Returns the number of bytes read or 0 if
231173
/// an error occurred.
232174
template <typename MutableBufferSequence>
233175
std::size_t read_some(const MutableBufferSequence& buffers,
234-
asio::error_code& ec)
235-
{
236-
ec = asio::error_code();
237-
238-
if (asio::buffer_size(buffers) == 0)
239-
return 0;
240-
241-
if (storage_.empty() && !fill(ec))
242-
return 0;
243-
244-
return copy(buffers);
245-
}
246-
247-
template <typename MutableBufferSequence, typename ReadHandler>
248-
class read_some_handler
249-
{
250-
public:
251-
read_some_handler(asio::io_service& io_service,
252-
detail::buffered_stream_storage& storage,
253-
const MutableBufferSequence& buffers, ReadHandler handler)
254-
: io_service_(io_service),
255-
storage_(storage),
256-
buffers_(buffers),
257-
handler_(handler)
258-
{
259-
}
260-
261-
void operator()(const asio::error_code& ec, std::size_t)
262-
{
263-
if (ec || storage_.empty())
264-
{
265-
std::size_t length = 0;
266-
io_service_.dispatch(detail::bind_handler(handler_, ec, length));
267-
}
268-
else
269-
{
270-
std::size_t bytes_copied = asio::buffer_copy(
271-
buffers_, storage_.data(), storage_.size());
272-
storage_.consume(bytes_copied);
273-
io_service_.dispatch(detail::bind_handler(handler_, ec, bytes_copied));
274-
}
275-
}
276-
277-
private:
278-
asio::io_service& io_service_;
279-
detail::buffered_stream_storage& storage_;
280-
MutableBufferSequence buffers_;
281-
ReadHandler handler_;
282-
};
176+
asio::error_code& ec);
283177

284178
/// Start an asynchronous read. The buffer into which the data will be read
285179
/// must be valid for the lifetime of the asynchronous operation.
286180
template <typename MutableBufferSequence, typename ReadHandler>
287-
void async_read_some(const MutableBufferSequence& buffers,
288-
ReadHandler handler)
289-
{
290-
if (asio::buffer_size(buffers) == 0)
291-
{
292-
get_io_service().post(detail::bind_handler(
293-
handler, asio::error_code(), 0));
294-
}
295-
else if (storage_.empty())
296-
{
297-
async_fill(read_some_handler<MutableBufferSequence, ReadHandler>(
298-
get_io_service(), storage_, buffers, handler));
299-
}
300-
else
301-
{
302-
std::size_t length = copy(buffers);
303-
get_io_service().post(detail::bind_handler(
304-
handler, asio::error_code(), length));
305-
}
306-
}
181+
ASIO_INITFN_RESULT_TYPE(ReadHandler,
182+
void (asio::error_code, std::size_t))
183+
async_read_some(const MutableBufferSequence& buffers,
184+
ASIO_MOVE_ARG(ReadHandler) handler);
307185

308186
/// Peek at the incoming data on the stream. Returns the number of bytes read.
309187
/// Throws an exception on failure.
310188
template <typename MutableBufferSequence>
311-
std::size_t peek(const MutableBufferSequence& buffers)
312-
{
313-
if (storage_.empty())
314-
fill();
315-
return peek_copy(buffers);
316-
}
189+
std::size_t peek(const MutableBufferSequence& buffers);
317190

318191
/// Peek at the incoming data on the stream. Returns the number of bytes read,
319192
/// or 0 if an error occurred.
320193
template <typename MutableBufferSequence>
321194
std::size_t peek(const MutableBufferSequence& buffers,
322-
asio::error_code& ec)
323-
{
324-
ec = asio::error_code();
325-
if (storage_.empty() && !fill(ec))
326-
return 0;
327-
return peek_copy(buffers);
328-
}
195+
asio::error_code& ec);
329196

330197
/// Determine the amount of data that may be read without blocking.
331198
std::size_t in_avail()
@@ -372,4 +239,6 @@ class buffered_read_stream
372239

373240
#include "asio/detail/pop_options.hpp"
374241

242+
#include "asio/impl/buffered_read_stream.hpp"
243+
375244
#endif // ASIO_BUFFERED_READ_STREAM_HPP

asio/include/asio/buffered_stream.hpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "asio/detail/config.hpp"
1919
#include <cstddef>
20+
#include "asio/async_result.hpp"
2021
#include "asio/buffered_read_stream.hpp"
2122
#include "asio/buffered_write_stream.hpp"
2223
#include "asio/buffered_stream_fwd.hpp"
@@ -122,9 +123,12 @@ class buffered_stream
122123

123124
/// Start an asynchronous flush.
124125
template <typename WriteHandler>
125-
void async_flush(WriteHandler handler)
126+
ASIO_INITFN_RESULT_TYPE(WriteHandler,
127+
void (asio::error_code, std::size_t))
128+
async_flush(ASIO_MOVE_ARG(WriteHandler) handler)
126129
{
127-
return stream_impl_.next_layer().async_flush(handler);
130+
return stream_impl_.next_layer().async_flush(
131+
ASIO_MOVE_CAST(WriteHandler)(handler));
128132
}
129133

130134
/// Write the given data to the stream. Returns the number of bytes written.
@@ -147,10 +151,13 @@ class buffered_stream
147151
/// Start an asynchronous write. The data being written must be valid for the
148152
/// lifetime of the asynchronous operation.
149153
template <typename ConstBufferSequence, typename WriteHandler>
150-
void async_write_some(const ConstBufferSequence& buffers,
151-
WriteHandler handler)
154+
ASIO_INITFN_RESULT_TYPE(WriteHandler,
155+
void (asio::error_code, std::size_t))
156+
async_write_some(const ConstBufferSequence& buffers,
157+
ASIO_MOVE_ARG(WriteHandler) handler)
152158
{
153-
stream_impl_.async_write_some(buffers, handler);
159+
return stream_impl_.async_write_some(buffers,
160+
ASIO_MOVE_CAST(WriteHandler)(handler));
154161
}
155162

156163
/// Fill the buffer with some data. Returns the number of bytes placed in the
@@ -169,9 +176,11 @@ class buffered_stream
169176

170177
/// Start an asynchronous fill.
171178
template <typename ReadHandler>
172-
void async_fill(ReadHandler handler)
179+
ASIO_INITFN_RESULT_TYPE(ReadHandler,
180+
void (asio::error_code, std::size_t))
181+
async_fill(ASIO_MOVE_ARG(ReadHandler) handler)
173182
{
174-
stream_impl_.async_fill(handler);
183+
return stream_impl_.async_fill(ASIO_MOVE_CAST(ReadHandler)(handler));
175184
}
176185

177186
/// Read some data from the stream. Returns the number of bytes read. Throws
@@ -194,10 +203,13 @@ class buffered_stream
194203
/// Start an asynchronous read. The buffer into which the data will be read
195204
/// must be valid for the lifetime of the asynchronous operation.
196205
template <typename MutableBufferSequence, typename ReadHandler>
197-
void async_read_some(const MutableBufferSequence& buffers,
198-
ReadHandler handler)
206+
ASIO_INITFN_RESULT_TYPE(ReadHandler,
207+
void (asio::error_code, std::size_t))
208+
async_read_some(const MutableBufferSequence& buffers,
209+
ASIO_MOVE_ARG(ReadHandler) handler)
199210
{
200-
stream_impl_.async_read_some(buffers, handler);
211+
return stream_impl_.async_read_some(buffers,
212+
ASIO_MOVE_CAST(ReadHandler)(handler));
201213
}
202214

203215
/// Peek at the incoming data on the stream. Returns the number of bytes read.

0 commit comments

Comments
 (0)