Chris@16
|
1 //
|
Chris@16
|
2 // 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_BUFFERED_WRITE_STREAM_HPP
|
Chris@16
|
12 #define BOOST_ASIO_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/config.hpp>
|
Chris@16
|
19 #include <cstddef>
|
Chris@16
|
20 #include <boost/asio/buffered_write_stream_fwd.hpp>
|
Chris@16
|
21 #include <boost/asio/buffer.hpp>
|
Chris@16
|
22 #include <boost/asio/completion_condition.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/bind_handler.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/buffered_stream_storage.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/noncopyable.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/type_traits.hpp>
|
Chris@16
|
27 #include <boost/asio/error.hpp>
|
Chris@16
|
28 #include <boost/asio/io_service.hpp>
|
Chris@16
|
29 #include <boost/asio/write.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost {
|
Chris@16
|
34 namespace asio {
|
Chris@16
|
35
|
Chris@16
|
36 /// Adds buffering to the write-related operations of a stream.
|
Chris@16
|
37 /**
|
Chris@16
|
38 * The buffered_write_stream class template can be used to add buffering to the
|
Chris@16
|
39 * synchronous and asynchronous write operations of a stream.
|
Chris@16
|
40 *
|
Chris@16
|
41 * @par Thread Safety
|
Chris@16
|
42 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
43 * @e Shared @e objects: Unsafe.
|
Chris@16
|
44 *
|
Chris@16
|
45 * @par Concepts:
|
Chris@16
|
46 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
|
Chris@16
|
47 */
|
Chris@16
|
48 template <typename Stream>
|
Chris@16
|
49 class buffered_write_stream
|
Chris@16
|
50 : private noncopyable
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 /// The type of the next layer.
|
Chris@16
|
54 typedef typename remove_reference<Stream>::type next_layer_type;
|
Chris@16
|
55
|
Chris@16
|
56 /// The type of the lowest layer.
|
Chris@16
|
57 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
|
Chris@16
|
58
|
Chris@16
|
59 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
60 /// The default buffer size.
|
Chris@16
|
61 static const std::size_t default_buffer_size = implementation_defined;
|
Chris@16
|
62 #else
|
Chris@16
|
63 BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
|
Chris@16
|
64 #endif
|
Chris@16
|
65
|
Chris@16
|
66 /// Construct, passing the specified argument to initialise the next layer.
|
Chris@16
|
67 template <typename Arg>
|
Chris@16
|
68 explicit buffered_write_stream(Arg& a)
|
Chris@16
|
69 : next_layer_(a),
|
Chris@16
|
70 storage_(default_buffer_size)
|
Chris@16
|
71 {
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 /// Construct, passing the specified argument to initialise the next layer.
|
Chris@16
|
75 template <typename Arg>
|
Chris@16
|
76 buffered_write_stream(Arg& a, std::size_t buffer_size)
|
Chris@16
|
77 : next_layer_(a),
|
Chris@16
|
78 storage_(buffer_size)
|
Chris@16
|
79 {
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 /// Get a reference to the next layer.
|
Chris@16
|
83 next_layer_type& next_layer()
|
Chris@16
|
84 {
|
Chris@16
|
85 return next_layer_;
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 /// Get a reference to the lowest layer.
|
Chris@16
|
89 lowest_layer_type& lowest_layer()
|
Chris@16
|
90 {
|
Chris@16
|
91 return next_layer_.lowest_layer();
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 /// Get a const reference to the lowest layer.
|
Chris@16
|
95 const lowest_layer_type& lowest_layer() const
|
Chris@16
|
96 {
|
Chris@16
|
97 return next_layer_.lowest_layer();
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 /// Get the io_service associated with the object.
|
Chris@16
|
101 boost::asio::io_service& get_io_service()
|
Chris@16
|
102 {
|
Chris@16
|
103 return next_layer_.get_io_service();
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 /// Close the stream.
|
Chris@16
|
107 void close()
|
Chris@16
|
108 {
|
Chris@16
|
109 next_layer_.close();
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 /// Close the stream.
|
Chris@16
|
113 boost::system::error_code close(boost::system::error_code& ec)
|
Chris@16
|
114 {
|
Chris@16
|
115 return next_layer_.close(ec);
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 /// Flush all data from the buffer to the next layer. Returns the number of
|
Chris@16
|
119 /// bytes written to the next layer on the last write operation. Throws an
|
Chris@16
|
120 /// exception on failure.
|
Chris@16
|
121 std::size_t flush();
|
Chris@16
|
122
|
Chris@16
|
123 /// Flush all data from the buffer to the next layer. Returns the number of
|
Chris@16
|
124 /// bytes written to the next layer on the last write operation, or 0 if an
|
Chris@16
|
125 /// error occurred.
|
Chris@16
|
126 std::size_t flush(boost::system::error_code& ec);
|
Chris@16
|
127
|
Chris@16
|
128 /// Start an asynchronous flush.
|
Chris@16
|
129 template <typename WriteHandler>
|
Chris@16
|
130 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
131 void (boost::system::error_code, std::size_t))
|
Chris@16
|
132 async_flush(BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
|
Chris@16
|
133
|
Chris@16
|
134 /// Write the given data to the stream. Returns the number of bytes written.
|
Chris@16
|
135 /// Throws an exception on failure.
|
Chris@16
|
136 template <typename ConstBufferSequence>
|
Chris@16
|
137 std::size_t write_some(const ConstBufferSequence& buffers);
|
Chris@16
|
138
|
Chris@16
|
139 /// Write the given data to the stream. Returns the number of bytes written,
|
Chris@16
|
140 /// or 0 if an error occurred and the error handler did not throw.
|
Chris@16
|
141 template <typename ConstBufferSequence>
|
Chris@16
|
142 std::size_t write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
143 boost::system::error_code& ec);
|
Chris@16
|
144
|
Chris@16
|
145 /// Start an asynchronous write. The data being written must be valid for the
|
Chris@16
|
146 /// lifetime of the asynchronous operation.
|
Chris@16
|
147 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
148 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
149 void (boost::system::error_code, std::size_t))
|
Chris@16
|
150 async_write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
151 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
|
Chris@16
|
152
|
Chris@16
|
153 /// Read some data from the stream. Returns the number of bytes read. Throws
|
Chris@16
|
154 /// an exception on failure.
|
Chris@16
|
155 template <typename MutableBufferSequence>
|
Chris@16
|
156 std::size_t read_some(const MutableBufferSequence& buffers)
|
Chris@16
|
157 {
|
Chris@16
|
158 return next_layer_.read_some(buffers);
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 /// Read some data from the stream. Returns the number of bytes read or 0 if
|
Chris@16
|
162 /// an error occurred.
|
Chris@16
|
163 template <typename MutableBufferSequence>
|
Chris@16
|
164 std::size_t read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
165 boost::system::error_code& ec)
|
Chris@16
|
166 {
|
Chris@16
|
167 return next_layer_.read_some(buffers, ec);
|
Chris@16
|
168 }
|
Chris@16
|
169
|
Chris@16
|
170 /// Start an asynchronous read. The buffer into which the data will be read
|
Chris@16
|
171 /// must be valid for the lifetime of the asynchronous operation.
|
Chris@16
|
172 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
173 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
174 void (boost::system::error_code, std::size_t))
|
Chris@16
|
175 async_read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
176 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
177 {
|
Chris@16
|
178 detail::async_result_init<
|
Chris@16
|
179 ReadHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
180 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
181
|
Chris@16
|
182 next_layer_.async_read_some(buffers,
|
Chris@16
|
183 BOOST_ASIO_MOVE_CAST(BOOST_ASIO_HANDLER_TYPE(ReadHandler,
|
Chris@16
|
184 void (boost::system::error_code, std::size_t)))(init.handler));
|
Chris@16
|
185
|
Chris@16
|
186 return init.result.get();
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 /// Peek at the incoming data on the stream. Returns the number of bytes read.
|
Chris@16
|
190 /// Throws an exception on failure.
|
Chris@16
|
191 template <typename MutableBufferSequence>
|
Chris@16
|
192 std::size_t peek(const MutableBufferSequence& buffers)
|
Chris@16
|
193 {
|
Chris@16
|
194 return next_layer_.peek(buffers);
|
Chris@16
|
195 }
|
Chris@16
|
196
|
Chris@16
|
197 /// Peek at the incoming data on the stream. Returns the number of bytes read,
|
Chris@16
|
198 /// or 0 if an error occurred.
|
Chris@16
|
199 template <typename MutableBufferSequence>
|
Chris@16
|
200 std::size_t peek(const MutableBufferSequence& buffers,
|
Chris@16
|
201 boost::system::error_code& ec)
|
Chris@16
|
202 {
|
Chris@16
|
203 return next_layer_.peek(buffers, ec);
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 /// Determine the amount of data that may be read without blocking.
|
Chris@16
|
207 std::size_t in_avail()
|
Chris@16
|
208 {
|
Chris@16
|
209 return next_layer_.in_avail();
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 /// Determine the amount of data that may be read without blocking.
|
Chris@16
|
213 std::size_t in_avail(boost::system::error_code& ec)
|
Chris@16
|
214 {
|
Chris@16
|
215 return next_layer_.in_avail(ec);
|
Chris@16
|
216 }
|
Chris@16
|
217
|
Chris@16
|
218 private:
|
Chris@16
|
219 /// Copy data into the internal buffer from the specified source buffer.
|
Chris@16
|
220 /// Returns the number of bytes copied.
|
Chris@16
|
221 template <typename ConstBufferSequence>
|
Chris@16
|
222 std::size_t copy(const ConstBufferSequence& buffers);
|
Chris@16
|
223
|
Chris@16
|
224 /// The next layer.
|
Chris@16
|
225 Stream next_layer_;
|
Chris@16
|
226
|
Chris@16
|
227 // The data in the buffer.
|
Chris@16
|
228 detail::buffered_stream_storage storage_;
|
Chris@16
|
229 };
|
Chris@16
|
230
|
Chris@16
|
231 } // namespace asio
|
Chris@16
|
232 } // namespace boost
|
Chris@16
|
233
|
Chris@16
|
234 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
235
|
Chris@16
|
236 #include <boost/asio/impl/buffered_write_stream.hpp>
|
Chris@16
|
237
|
Chris@16
|
238 #endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP
|