PageRenderTime 85ms CodeModel.GetById 43ms app.highlight 37ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/buffered_write_stream.hpp

http://hadesmem.googlecode.com/
C++ Header | 353 lines | 245 code | 47 blank | 61 comment | 16 complexity | 12d212c55f58b182124c375f6252043b MD5 | raw file
  1//
  2// buffered_write_stream.hpp
  3// ~~~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  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/type_traits/remove_reference.hpp>
 21#include <boost/asio/buffered_write_stream_fwd.hpp>
 22#include <boost/asio/buffer.hpp>
 23#include <boost/asio/completion_condition.hpp>
 24#include <boost/asio/detail/bind_handler.hpp>
 25#include <boost/asio/detail/buffered_stream_storage.hpp>
 26#include <boost/asio/detail/noncopyable.hpp>
 27#include <boost/asio/error.hpp>
 28#include <boost/asio/io_service.hpp>
 29#include <boost/asio/write.hpp>
 30
 31#include <boost/asio/detail/push_options.hpp>
 32
 33namespace boost {
 34namespace asio {
 35
 36/// Adds buffering to the write-related operations of a stream.
 37/**
 38 * The buffered_write_stream class template can be used to add buffering to the
 39 * synchronous and asynchronous write operations of a stream.
 40 *
 41 * @par Thread Safety
 42 * @e Distinct @e objects: Safe.@n
 43 * @e Shared @e objects: Unsafe.
 44 *
 45 * @par Concepts:
 46 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
 47 */
 48template <typename Stream>
 49class buffered_write_stream
 50  : private noncopyable
 51{
 52public:
 53  /// The type of the next layer.
 54  typedef typename boost::remove_reference<Stream>::type next_layer_type;
 55
 56  /// The type of the lowest layer.
 57  typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
 58
 59#if defined(GENERATING_DOCUMENTATION)
 60  /// The default buffer size.
 61  static const std::size_t default_buffer_size = implementation_defined;
 62#else
 63  BOOST_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
 64#endif
 65
 66  /// Construct, passing the specified argument to initialise the next layer.
 67  template <typename Arg>
 68  explicit buffered_write_stream(Arg& a)
 69    : next_layer_(a),
 70      storage_(default_buffer_size)
 71  {
 72  }
 73
 74  /// Construct, passing the specified argument to initialise the next layer.
 75  template <typename Arg>
 76  buffered_write_stream(Arg& a, std::size_t buffer_size)
 77    : next_layer_(a),
 78      storage_(buffer_size)
 79  {
 80  }
 81
 82  /// Get a reference to the next layer.
 83  next_layer_type& next_layer()
 84  {
 85    return next_layer_;
 86  }
 87
 88  /// Get a reference to the lowest layer.
 89  lowest_layer_type& lowest_layer()
 90  {
 91    return next_layer_.lowest_layer();
 92  }
 93
 94  /// Get a const reference to the lowest layer.
 95  const lowest_layer_type& lowest_layer() const
 96  {
 97    return next_layer_.lowest_layer();
 98  }
 99
100  /// Get the io_service associated with the object.
101  boost::asio::io_service& get_io_service()
102  {
103    return next_layer_.get_io_service();
104  }
105
106  /// Close the stream.
107  void close()
108  {
109    next_layer_.close();
110  }
111
112  /// Close the stream.
113  boost::system::error_code close(boost::system::error_code& ec)
114  {
115    return next_layer_.close(ec);
116  }
117
118  /// Flush all data from the buffer to the next layer. Returns the number of
119  /// bytes written to the next layer on the last write operation. Throws an
120  /// exception on failure.
121  std::size_t flush()
122  {
123    std::size_t bytes_written = write(next_layer_,
124        buffer(storage_.data(), storage_.size()));
125    storage_.consume(bytes_written);
126    return bytes_written;
127  }
128
129  /// Flush all data from the buffer to the next layer. Returns the number of
130  /// bytes written to the next layer on the last write operation, or 0 if an
131  /// error occurred.
132  std::size_t flush(boost::system::error_code& ec)
133  {
134    std::size_t bytes_written = write(next_layer_,
135        buffer(storage_.data(), storage_.size()),
136        transfer_all(), ec);
137    storage_.consume(bytes_written);
138    return bytes_written;
139  }
140
141  template <typename WriteHandler>
142  class flush_handler
143  {
144  public:
145    flush_handler(boost::asio::io_service& io_service,
146        detail::buffered_stream_storage& storage, WriteHandler handler)
147      : io_service_(io_service),
148        storage_(storage),
149        handler_(handler)
150    {
151    }
152
153    void operator()(const boost::system::error_code& ec,
154        std::size_t bytes_written)
155    {
156      storage_.consume(bytes_written);
157      io_service_.dispatch(detail::bind_handler(handler_, ec, bytes_written));
158    }
159
160  private:
161    boost::asio::io_service& io_service_;
162    detail::buffered_stream_storage& storage_;
163    WriteHandler handler_;
164  };
165
166  /// Start an asynchronous flush.
167  template <typename WriteHandler>
168  void async_flush(WriteHandler handler)
169  {
170    async_write(next_layer_, buffer(storage_.data(), storage_.size()),
171        flush_handler<WriteHandler>(get_io_service(), storage_, handler));
172  }
173
174  /// Write the given data to the stream. Returns the number of bytes written.
175  /// Throws an exception on failure.
176  template <typename ConstBufferSequence>
177  std::size_t write_some(const ConstBufferSequence& buffers)
178  {
179    if (boost::asio::buffer_size(buffers) == 0)
180      return 0;
181
182    if (storage_.size() == storage_.capacity())
183      flush();
184
185    return copy(buffers);
186  }
187
188  /// Write the given data to the stream. Returns the number of bytes written,
189  /// or 0 if an error occurred and the error handler did not throw.
190  template <typename ConstBufferSequence>
191  std::size_t write_some(const ConstBufferSequence& buffers,
192      boost::system::error_code& ec)
193  {
194    ec = boost::system::error_code();
195
196    if (boost::asio::buffer_size(buffers) == 0)
197      return 0;
198
199    if (storage_.size() == storage_.capacity() && !flush(ec))
200      return 0;
201
202    return copy(buffers);
203  }
204
205  template <typename ConstBufferSequence, typename WriteHandler>
206  class write_some_handler
207  {
208  public:
209    write_some_handler(boost::asio::io_service& io_service,
210        detail::buffered_stream_storage& storage,
211        const ConstBufferSequence& buffers, WriteHandler handler)
212      : io_service_(io_service),
213        storage_(storage),
214        buffers_(buffers),
215        handler_(handler)
216    {
217    }
218
219    void operator()(const boost::system::error_code& ec, std::size_t)
220    {
221      if (ec)
222      {
223        std::size_t length = 0;
224        io_service_.dispatch(detail::bind_handler(handler_, ec, length));
225      }
226      else
227      {
228        std::size_t orig_size = storage_.size();
229        std::size_t space_avail = storage_.capacity() - orig_size;
230        std::size_t bytes_avail = boost::asio::buffer_size(buffers_);
231        std::size_t length = bytes_avail < space_avail
232          ? bytes_avail : space_avail;
233        storage_.resize(orig_size + length);
234        std::size_t bytes_copied = boost::asio::buffer_copy(
235            storage_.data(), buffers_, length);
236
237        io_service_.dispatch(detail::bind_handler(handler_, ec, bytes_copied));
238      }
239    }
240
241  private:
242    boost::asio::io_service& io_service_;
243    detail::buffered_stream_storage& storage_;
244    ConstBufferSequence buffers_;
245    WriteHandler handler_;
246  };
247
248  /// Start an asynchronous write. The data being written must be valid for the
249  /// lifetime of the asynchronous operation.
250  template <typename ConstBufferSequence, typename WriteHandler>
251  void async_write_some(const ConstBufferSequence& buffers,
252      WriteHandler handler)
253  {
254    if (boost::asio::buffer_size(buffers) == 0)
255    {
256      get_io_service().post(detail::bind_handler(
257            handler, boost::system::error_code(), 0));
258    }
259    else if (storage_.size() == storage_.capacity())
260    {
261      async_flush(write_some_handler<ConstBufferSequence, WriteHandler>(
262            get_io_service(), storage_, buffers, handler));
263    }
264    else
265    {
266      std::size_t bytes_copied = copy(buffers);
267      get_io_service().post(detail::bind_handler(
268            handler, boost::system::error_code(), bytes_copied));
269    }
270  }
271
272  /// Read some data from the stream. Returns the number of bytes read. Throws
273  /// an exception on failure.
274  template <typename MutableBufferSequence>
275  std::size_t read_some(const MutableBufferSequence& buffers)
276  {
277    return next_layer_.read_some(buffers);
278  }
279
280  /// Read some data from the stream. Returns the number of bytes read or 0 if
281  /// an error occurred.
282  template <typename MutableBufferSequence>
283  std::size_t read_some(const MutableBufferSequence& buffers,
284      boost::system::error_code& ec)
285  {
286    return next_layer_.read_some(buffers, ec);
287  }
288
289  /// Start an asynchronous read. The buffer into which the data will be read
290  /// must be valid for the lifetime of the asynchronous operation.
291  template <typename MutableBufferSequence, typename ReadHandler>
292  void async_read_some(const MutableBufferSequence& buffers,
293      ReadHandler handler)
294  {
295    next_layer_.async_read_some(buffers, handler);
296  }
297
298  /// Peek at the incoming data on the stream. Returns the number of bytes read.
299  /// Throws an exception on failure.
300  template <typename MutableBufferSequence>
301  std::size_t peek(const MutableBufferSequence& buffers)
302  {
303    return next_layer_.peek(buffers);
304  }
305
306  /// Peek at the incoming data on the stream. Returns the number of bytes read,
307  /// or 0 if an error occurred.
308  template <typename MutableBufferSequence>
309  std::size_t peek(const MutableBufferSequence& buffers,
310      boost::system::error_code& ec)
311  {
312    return next_layer_.peek(buffers, ec);
313  }
314
315  /// Determine the amount of data that may be read without blocking.
316  std::size_t in_avail()
317  {
318    return next_layer_.in_avail();
319  }
320
321  /// Determine the amount of data that may be read without blocking.
322  std::size_t in_avail(boost::system::error_code& ec)
323  {
324    return next_layer_.in_avail(ec);
325  }
326
327private:
328  /// Copy data into the internal buffer from the specified source buffer.
329  /// Returns the number of bytes copied.
330  template <typename ConstBufferSequence>
331  std::size_t copy(const ConstBufferSequence& buffers)
332  {
333    std::size_t orig_size = storage_.size();
334    std::size_t space_avail = storage_.capacity() - orig_size;
335    std::size_t bytes_avail = boost::asio::buffer_size(buffers);
336    std::size_t length = bytes_avail < space_avail ? bytes_avail : space_avail;
337    storage_.resize(orig_size + length);
338    return boost::asio::buffer_copy(storage_.data(), buffers, length);
339  }
340
341  /// The next layer.
342  Stream next_layer_;
343
344  // The data in the buffer.
345  detail::buffered_stream_storage storage_;
346};
347
348} // namespace asio
349} // namespace boost
350
351#include <boost/asio/detail/pop_options.hpp>
352
353#endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP