Chris@16
|
1 //
|
Chris@16
|
2 // impl/buffered_write_stream.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
|
Chris@16
|
12 #define BOOST_ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
Chris@16
|
19 #include <boost/asio/detail/handler_cont_helpers.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/handler_invoke_helpers.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost {
|
Chris@16
|
26 namespace asio {
|
Chris@16
|
27
|
Chris@16
|
28 template <typename Stream>
|
Chris@16
|
29 std::size_t buffered_write_stream<Stream>::flush()
|
Chris@16
|
30 {
|
Chris@16
|
31 std::size_t bytes_written = write(next_layer_,
|
Chris@16
|
32 buffer(storage_.data(), storage_.size()));
|
Chris@16
|
33 storage_.consume(bytes_written);
|
Chris@16
|
34 return bytes_written;
|
Chris@16
|
35 }
|
Chris@16
|
36
|
Chris@16
|
37 template <typename Stream>
|
Chris@16
|
38 std::size_t buffered_write_stream<Stream>::flush(boost::system::error_code& ec)
|
Chris@16
|
39 {
|
Chris@16
|
40 std::size_t bytes_written = write(next_layer_,
|
Chris@16
|
41 buffer(storage_.data(), storage_.size()),
|
Chris@16
|
42 transfer_all(), ec);
|
Chris@16
|
43 storage_.consume(bytes_written);
|
Chris@16
|
44 return bytes_written;
|
Chris@16
|
45 }
|
Chris@16
|
46
|
Chris@16
|
47 namespace detail
|
Chris@16
|
48 {
|
Chris@16
|
49 template <typename WriteHandler>
|
Chris@16
|
50 class buffered_flush_handler
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 buffered_flush_handler(detail::buffered_stream_storage& storage,
|
Chris@16
|
54 WriteHandler& handler)
|
Chris@16
|
55 : storage_(storage),
|
Chris@16
|
56 handler_(handler)
|
Chris@16
|
57 {
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@16
|
60 #if defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
61 buffered_flush_handler(const buffered_flush_handler& other)
|
Chris@16
|
62 : storage_(other.storage_),
|
Chris@16
|
63 handler_(other.handler_)
|
Chris@16
|
64 {
|
Chris@16
|
65 }
|
Chris@16
|
66
|
Chris@16
|
67 buffered_flush_handler(buffered_flush_handler&& other)
|
Chris@16
|
68 : storage_(other.storage_),
|
Chris@16
|
69 handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
Chris@16
|
70 {
|
Chris@16
|
71 }
|
Chris@16
|
72 #endif // defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
73
|
Chris@16
|
74 void operator()(const boost::system::error_code& ec,
|
Chris@16
|
75 const std::size_t bytes_written)
|
Chris@16
|
76 {
|
Chris@16
|
77 storage_.consume(bytes_written);
|
Chris@16
|
78 handler_(ec, bytes_written);
|
Chris@16
|
79 }
|
Chris@16
|
80
|
Chris@16
|
81 //private:
|
Chris@16
|
82 detail::buffered_stream_storage& storage_;
|
Chris@16
|
83 WriteHandler handler_;
|
Chris@16
|
84 };
|
Chris@16
|
85
|
Chris@16
|
86 template <typename WriteHandler>
|
Chris@16
|
87 inline void* asio_handler_allocate(std::size_t size,
|
Chris@16
|
88 buffered_flush_handler<WriteHandler>* this_handler)
|
Chris@16
|
89 {
|
Chris@16
|
90 return boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
91 size, this_handler->handler_);
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 template <typename WriteHandler>
|
Chris@16
|
95 inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
Chris@16
|
96 buffered_flush_handler<WriteHandler>* this_handler)
|
Chris@16
|
97 {
|
Chris@16
|
98 boost_asio_handler_alloc_helpers::deallocate(
|
Chris@16
|
99 pointer, size, this_handler->handler_);
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 template <typename WriteHandler>
|
Chris@16
|
103 inline bool asio_handler_is_continuation(
|
Chris@16
|
104 buffered_flush_handler<WriteHandler>* this_handler)
|
Chris@16
|
105 {
|
Chris@16
|
106 return boost_asio_handler_cont_helpers::is_continuation(
|
Chris@16
|
107 this_handler->handler_);
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 template <typename Function, typename WriteHandler>
|
Chris@16
|
111 inline void asio_handler_invoke(Function& function,
|
Chris@16
|
112 buffered_flush_handler<WriteHandler>* this_handler)
|
Chris@16
|
113 {
|
Chris@16
|
114 boost_asio_handler_invoke_helpers::invoke(
|
Chris@16
|
115 function, this_handler->handler_);
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 template <typename Function, typename WriteHandler>
|
Chris@16
|
119 inline void asio_handler_invoke(const Function& function,
|
Chris@16
|
120 buffered_flush_handler<WriteHandler>* this_handler)
|
Chris@16
|
121 {
|
Chris@16
|
122 boost_asio_handler_invoke_helpers::invoke(
|
Chris@16
|
123 function, this_handler->handler_);
|
Chris@16
|
124 }
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 template <typename Stream>
|
Chris@16
|
128 template <typename WriteHandler>
|
Chris@16
|
129 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
130 void (boost::system::error_code, std::size_t))
|
Chris@16
|
131 buffered_write_stream<Stream>::async_flush(
|
Chris@16
|
132 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
133 {
|
Chris@16
|
134 // If you get an error on the following line it means that your handler does
|
Chris@16
|
135 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
136 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
137
|
Chris@16
|
138 detail::async_result_init<
|
Chris@16
|
139 WriteHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
140 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
141
|
Chris@16
|
142 async_write(next_layer_, buffer(storage_.data(), storage_.size()),
|
Chris@16
|
143 detail::buffered_flush_handler<BOOST_ASIO_HANDLER_TYPE(
|
Chris@16
|
144 WriteHandler, void (boost::system::error_code, std::size_t))>(
|
Chris@16
|
145 storage_, init.handler));
|
Chris@16
|
146
|
Chris@16
|
147 return init.result.get();
|
Chris@16
|
148 }
|
Chris@16
|
149
|
Chris@16
|
150 template <typename Stream>
|
Chris@16
|
151 template <typename ConstBufferSequence>
|
Chris@16
|
152 std::size_t buffered_write_stream<Stream>::write_some(
|
Chris@16
|
153 const ConstBufferSequence& buffers)
|
Chris@16
|
154 {
|
Chris@16
|
155 if (boost::asio::buffer_size(buffers) == 0)
|
Chris@16
|
156 return 0;
|
Chris@16
|
157
|
Chris@16
|
158 if (storage_.size() == storage_.capacity())
|
Chris@16
|
159 this->flush();
|
Chris@16
|
160
|
Chris@16
|
161 return this->copy(buffers);
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 template <typename Stream>
|
Chris@16
|
165 template <typename ConstBufferSequence>
|
Chris@16
|
166 std::size_t buffered_write_stream<Stream>::write_some(
|
Chris@16
|
167 const ConstBufferSequence& buffers, boost::system::error_code& ec)
|
Chris@16
|
168 {
|
Chris@16
|
169 ec = boost::system::error_code();
|
Chris@16
|
170
|
Chris@16
|
171 if (boost::asio::buffer_size(buffers) == 0)
|
Chris@16
|
172 return 0;
|
Chris@16
|
173
|
Chris@16
|
174 if (storage_.size() == storage_.capacity() && !flush(ec))
|
Chris@16
|
175 return 0;
|
Chris@16
|
176
|
Chris@16
|
177 return this->copy(buffers);
|
Chris@16
|
178 }
|
Chris@16
|
179
|
Chris@16
|
180 namespace detail
|
Chris@16
|
181 {
|
Chris@16
|
182 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
183 class buffered_write_some_handler
|
Chris@16
|
184 {
|
Chris@16
|
185 public:
|
Chris@16
|
186 buffered_write_some_handler(detail::buffered_stream_storage& storage,
|
Chris@16
|
187 const ConstBufferSequence& buffers, WriteHandler& handler)
|
Chris@16
|
188 : storage_(storage),
|
Chris@16
|
189 buffers_(buffers),
|
Chris@16
|
190 handler_(handler)
|
Chris@16
|
191 {
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 #if defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
195 buffered_write_some_handler(const buffered_write_some_handler& other)
|
Chris@16
|
196 : storage_(other.storage_),
|
Chris@16
|
197 buffers_(other.buffers_),
|
Chris@16
|
198 handler_(other.handler_)
|
Chris@16
|
199 {
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202 buffered_write_some_handler(buffered_write_some_handler&& other)
|
Chris@16
|
203 : storage_(other.storage_),
|
Chris@16
|
204 buffers_(other.buffers_),
|
Chris@16
|
205 handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
Chris@16
|
206 {
|
Chris@16
|
207 }
|
Chris@16
|
208 #endif // defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
209
|
Chris@16
|
210 void operator()(const boost::system::error_code& ec, std::size_t)
|
Chris@16
|
211 {
|
Chris@16
|
212 if (ec)
|
Chris@16
|
213 {
|
Chris@16
|
214 const std::size_t length = 0;
|
Chris@16
|
215 handler_(ec, length);
|
Chris@16
|
216 }
|
Chris@16
|
217 else
|
Chris@16
|
218 {
|
Chris@16
|
219 std::size_t orig_size = storage_.size();
|
Chris@16
|
220 std::size_t space_avail = storage_.capacity() - orig_size;
|
Chris@16
|
221 std::size_t bytes_avail = boost::asio::buffer_size(buffers_);
|
Chris@16
|
222 std::size_t length = bytes_avail < space_avail
|
Chris@16
|
223 ? bytes_avail : space_avail;
|
Chris@16
|
224 storage_.resize(orig_size + length);
|
Chris@16
|
225 const std::size_t bytes_copied = boost::asio::buffer_copy(
|
Chris@16
|
226 storage_.data() + orig_size, buffers_, length);
|
Chris@16
|
227 handler_(ec, bytes_copied);
|
Chris@16
|
228 }
|
Chris@16
|
229 }
|
Chris@16
|
230
|
Chris@16
|
231 //private:
|
Chris@16
|
232 detail::buffered_stream_storage& storage_;
|
Chris@16
|
233 ConstBufferSequence buffers_;
|
Chris@16
|
234 WriteHandler handler_;
|
Chris@16
|
235 };
|
Chris@16
|
236
|
Chris@16
|
237 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
238 inline void* asio_handler_allocate(std::size_t size,
|
Chris@16
|
239 buffered_write_some_handler<
|
Chris@16
|
240 ConstBufferSequence, WriteHandler>* this_handler)
|
Chris@16
|
241 {
|
Chris@16
|
242 return boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
243 size, this_handler->handler_);
|
Chris@16
|
244 }
|
Chris@16
|
245
|
Chris@16
|
246 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
247 inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
Chris@16
|
248 buffered_write_some_handler<
|
Chris@16
|
249 ConstBufferSequence, WriteHandler>* this_handler)
|
Chris@16
|
250 {
|
Chris@16
|
251 boost_asio_handler_alloc_helpers::deallocate(
|
Chris@16
|
252 pointer, size, this_handler->handler_);
|
Chris@16
|
253 }
|
Chris@16
|
254
|
Chris@16
|
255 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
256 inline bool asio_handler_is_continuation(
|
Chris@16
|
257 buffered_write_some_handler<
|
Chris@16
|
258 ConstBufferSequence, WriteHandler>* this_handler)
|
Chris@16
|
259 {
|
Chris@16
|
260 return boost_asio_handler_cont_helpers::is_continuation(
|
Chris@16
|
261 this_handler->handler_);
|
Chris@16
|
262 }
|
Chris@16
|
263
|
Chris@16
|
264 template <typename Function, typename ConstBufferSequence,
|
Chris@16
|
265 typename WriteHandler>
|
Chris@16
|
266 inline void asio_handler_invoke(Function& function,
|
Chris@16
|
267 buffered_write_some_handler<
|
Chris@16
|
268 ConstBufferSequence, WriteHandler>* this_handler)
|
Chris@16
|
269 {
|
Chris@16
|
270 boost_asio_handler_invoke_helpers::invoke(
|
Chris@16
|
271 function, this_handler->handler_);
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 template <typename Function, typename ConstBufferSequence,
|
Chris@16
|
275 typename WriteHandler>
|
Chris@16
|
276 inline void asio_handler_invoke(const Function& function,
|
Chris@16
|
277 buffered_write_some_handler<
|
Chris@16
|
278 ConstBufferSequence, WriteHandler>* this_handler)
|
Chris@16
|
279 {
|
Chris@16
|
280 boost_asio_handler_invoke_helpers::invoke(
|
Chris@16
|
281 function, this_handler->handler_);
|
Chris@16
|
282 }
|
Chris@16
|
283 } // namespace detail
|
Chris@16
|
284
|
Chris@16
|
285 template <typename Stream>
|
Chris@16
|
286 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
287 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
288 void (boost::system::error_code, std::size_t))
|
Chris@16
|
289 buffered_write_stream<Stream>::async_write_some(
|
Chris@16
|
290 const ConstBufferSequence& buffers,
|
Chris@16
|
291 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
292 {
|
Chris@16
|
293 // If you get an error on the following line it means that your handler does
|
Chris@16
|
294 // not meet the documented type requirements for a WriteHandler.
|
Chris@16
|
295 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
Chris@16
|
296
|
Chris@16
|
297 detail::async_result_init<
|
Chris@16
|
298 WriteHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
299 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
300
|
Chris@16
|
301 if (boost::asio::buffer_size(buffers) == 0
|
Chris@16
|
302 || storage_.size() < storage_.capacity())
|
Chris@16
|
303 {
|
Chris@16
|
304 next_layer_.async_write_some(boost::asio::const_buffers_1(0, 0),
|
Chris@16
|
305 detail::buffered_write_some_handler<
|
Chris@16
|
306 ConstBufferSequence, BOOST_ASIO_HANDLER_TYPE(
|
Chris@16
|
307 WriteHandler, void (boost::system::error_code, std::size_t))>(
|
Chris@16
|
308 storage_, buffers, init.handler));
|
Chris@16
|
309 }
|
Chris@16
|
310 else
|
Chris@16
|
311 {
|
Chris@16
|
312 this->async_flush(detail::buffered_write_some_handler<
|
Chris@16
|
313 ConstBufferSequence, BOOST_ASIO_HANDLER_TYPE(
|
Chris@16
|
314 WriteHandler, void (boost::system::error_code, std::size_t))>(
|
Chris@16
|
315 storage_, buffers, init.handler));
|
Chris@16
|
316 }
|
Chris@16
|
317
|
Chris@16
|
318 return init.result.get();
|
Chris@16
|
319 }
|
Chris@16
|
320
|
Chris@16
|
321 template <typename Stream>
|
Chris@16
|
322 template <typename ConstBufferSequence>
|
Chris@16
|
323 std::size_t buffered_write_stream<Stream>::copy(
|
Chris@16
|
324 const ConstBufferSequence& buffers)
|
Chris@16
|
325 {
|
Chris@16
|
326 std::size_t orig_size = storage_.size();
|
Chris@16
|
327 std::size_t space_avail = storage_.capacity() - orig_size;
|
Chris@16
|
328 std::size_t bytes_avail = boost::asio::buffer_size(buffers);
|
Chris@16
|
329 std::size_t length = bytes_avail < space_avail ? bytes_avail : space_avail;
|
Chris@16
|
330 storage_.resize(orig_size + length);
|
Chris@16
|
331 return boost::asio::buffer_copy(
|
Chris@16
|
332 storage_.data() + orig_size, buffers, length);
|
Chris@16
|
333 }
|
Chris@16
|
334
|
Chris@16
|
335 } // namespace asio
|
Chris@16
|
336 } // namespace boost
|
Chris@16
|
337
|
Chris@16
|
338 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
339
|
Chris@16
|
340 #endif // BOOST_ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
|