Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / asio / impl / buffered_write_stream.hpp @ 2486

History | View | Annotate | Download (10.4 kB)

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