5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11
#ifndef ASIO_WRITE_IPP
12
#define ASIO_WRITE_IPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/buffer.hpp"
21
#include "asio/completion_condition.hpp"
22
#include "asio/detail/bind_handler.hpp"
23
#include "asio/detail/consuming_buffers.hpp"
24
#include "asio/detail/handler_alloc_helpers.hpp"
25
#include "asio/detail/handler_invoke_helpers.hpp"
26
#include "asio/detail/throw_error.hpp"
30
template <typename SyncWriteStream, typename ConstBufferSequence,
31
typename CompletionCondition>
32
std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
33
CompletionCondition completion_condition, asio::error_code& ec)
35
asio::detail::consuming_buffers<
36
const_buffer, ConstBufferSequence> tmp(buffers);
37
std::size_t total_transferred = 0;
38
while (tmp.begin() != tmp.end())
40
std::size_t bytes_transferred = s.write_some(tmp, ec);
41
tmp.consume(bytes_transferred);
42
total_transferred += bytes_transferred;
43
if (completion_condition(ec, total_transferred))
44
return total_transferred;
46
ec = asio::error_code();
47
return total_transferred;
50
template <typename SyncWriteStream, typename ConstBufferSequence>
51
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers)
54
std::size_t bytes_transferred = write(s, buffers, transfer_all(), ec);
55
asio::detail::throw_error(ec);
56
return bytes_transferred;
59
template <typename SyncWriteStream, typename ConstBufferSequence,
60
typename CompletionCondition>
61
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
62
CompletionCondition completion_condition)
65
std::size_t bytes_transferred = write(s, buffers, completion_condition, ec);
66
asio::detail::throw_error(ec);
67
return bytes_transferred;
70
template <typename SyncWriteStream, typename Allocator,
71
typename CompletionCondition>
72
std::size_t write(SyncWriteStream& s,
73
asio::basic_streambuf<Allocator>& b,
74
CompletionCondition completion_condition, asio::error_code& ec)
76
std::size_t bytes_transferred = write(s, b.data(), completion_condition, ec);
77
b.consume(bytes_transferred);
78
return bytes_transferred;
81
template <typename SyncWriteStream, typename Allocator>
82
inline std::size_t write(SyncWriteStream& s,
83
asio::basic_streambuf<Allocator>& b)
86
std::size_t bytes_transferred = write(s, b, transfer_all(), ec);
87
asio::detail::throw_error(ec);
88
return bytes_transferred;
91
template <typename SyncWriteStream, typename Allocator,
92
typename CompletionCondition>
93
inline std::size_t write(SyncWriteStream& s,
94
asio::basic_streambuf<Allocator>& b,
95
CompletionCondition completion_condition)
98
std::size_t bytes_transferred = write(s, b, completion_condition, ec);
99
asio::detail::throw_error(ec);
100
return bytes_transferred;
105
template <typename AsyncWriteStream, typename ConstBufferSequence,
106
typename CompletionCondition, typename WriteHandler>
110
typedef asio::detail::consuming_buffers<
111
const_buffer, ConstBufferSequence> buffers_type;
113
write_handler(AsyncWriteStream& stream, const buffers_type& buffers,
114
CompletionCondition completion_condition, WriteHandler handler)
117
total_transferred_(0),
118
completion_condition_(completion_condition),
123
void operator()(const asio::error_code& ec,
124
std::size_t bytes_transferred)
126
total_transferred_ += bytes_transferred;
127
buffers_.consume(bytes_transferred);
128
if (completion_condition_(ec, total_transferred_)
129
|| buffers_.begin() == buffers_.end())
131
handler_(ec, total_transferred_);
135
stream_.async_write_some(buffers_, *this);
140
AsyncWriteStream& stream_;
141
buffers_type buffers_;
142
std::size_t total_transferred_;
143
CompletionCondition completion_condition_;
144
WriteHandler handler_;
147
template <typename AsyncWriteStream, typename ConstBufferSequence,
148
typename CompletionCondition, typename WriteHandler>
149
inline void* asio_handler_allocate(std::size_t size,
150
write_handler<AsyncWriteStream, ConstBufferSequence,
151
CompletionCondition, WriteHandler>* this_handler)
153
return asio_handler_alloc_helpers::allocate(
154
size, &this_handler->handler_);
157
template <typename AsyncWriteStream, typename ConstBufferSequence,
158
typename CompletionCondition, typename WriteHandler>
159
inline void asio_handler_deallocate(void* pointer, std::size_t size,
160
write_handler<AsyncWriteStream, ConstBufferSequence,
161
CompletionCondition, WriteHandler>* this_handler)
163
asio_handler_alloc_helpers::deallocate(
164
pointer, size, &this_handler->handler_);
167
template <typename Function, typename AsyncWriteStream,
168
typename ConstBufferSequence, typename CompletionCondition,
169
typename WriteHandler>
170
inline void asio_handler_invoke(const Function& function,
171
write_handler<AsyncWriteStream, ConstBufferSequence,
172
CompletionCondition, WriteHandler>* this_handler)
174
asio_handler_invoke_helpers::invoke(
175
function, &this_handler->handler_);
177
} // namespace detail
179
template <typename AsyncWriteStream, typename ConstBufferSequence,
180
typename CompletionCondition, typename WriteHandler>
181
inline void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
182
CompletionCondition completion_condition, WriteHandler handler)
184
asio::detail::consuming_buffers<
185
const_buffer, ConstBufferSequence> tmp(buffers);
186
s.async_write_some(tmp,
187
detail::write_handler<AsyncWriteStream, ConstBufferSequence,
188
CompletionCondition, WriteHandler>(
189
s, tmp, completion_condition, handler));
192
template <typename AsyncWriteStream, typename ConstBufferSequence,
193
typename WriteHandler>
194
inline void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
195
WriteHandler handler)
197
async_write(s, buffers, transfer_all(), handler);
202
template <typename AsyncWriteStream, typename Allocator,
203
typename WriteHandler>
204
class write_streambuf_handler
207
write_streambuf_handler(asio::basic_streambuf<Allocator>& streambuf,
208
WriteHandler handler)
209
: streambuf_(streambuf),
214
void operator()(const asio::error_code& ec,
215
std::size_t bytes_transferred)
217
streambuf_.consume(bytes_transferred);
218
handler_(ec, bytes_transferred);
222
asio::basic_streambuf<Allocator>& streambuf_;
223
WriteHandler handler_;
226
template <typename AsyncWriteStream, typename Allocator,
227
typename WriteHandler>
228
inline void* asio_handler_allocate(std::size_t size,
229
write_streambuf_handler<AsyncWriteStream,
230
Allocator, WriteHandler>* this_handler)
232
return asio_handler_alloc_helpers::allocate(
233
size, &this_handler->handler_);
236
template <typename AsyncWriteStream, typename Allocator,
237
typename WriteHandler>
238
inline void asio_handler_deallocate(void* pointer, std::size_t size,
239
write_streambuf_handler<AsyncWriteStream,
240
Allocator, WriteHandler>* this_handler)
242
asio_handler_alloc_helpers::deallocate(
243
pointer, size, &this_handler->handler_);
246
template <typename Function, typename AsyncWriteStream, typename Allocator,
247
typename WriteHandler>
248
inline void asio_handler_invoke(const Function& function,
249
write_streambuf_handler<AsyncWriteStream,
250
Allocator, WriteHandler>* this_handler)
252
asio_handler_invoke_helpers::invoke(
253
function, &this_handler->handler_);
255
} // namespace detail
257
template <typename AsyncWriteStream, typename Allocator,
258
typename CompletionCondition, typename WriteHandler>
259
inline void async_write(AsyncWriteStream& s,
260
asio::basic_streambuf<Allocator>& b,
261
CompletionCondition completion_condition, WriteHandler handler)
263
async_write(s, b.data(), completion_condition,
264
detail::write_streambuf_handler<
265
AsyncWriteStream, Allocator, WriteHandler>(b, handler));
268
template <typename AsyncWriteStream, typename Allocator, typename WriteHandler>
269
inline void async_write(AsyncWriteStream& s,
270
asio::basic_streambuf<Allocator>& b, WriteHandler handler)
272
async_write(s, b, transfer_all(), handler);
277
#include "asio/detail/pop_options.hpp"
279
#endif // ASIO_WRITE_IPP