Add write limit to test::string_ostream

This commit is contained in:
Vinnie Falco
2017-05-19 18:18:47 -07:00
parent b76d19c3cb
commit 384b5d53e7
2 changed files with 10 additions and 3 deletions

View File

@ -3,6 +3,7 @@ Version 43
* Require Boost 1.64.0
* Fix strict aliasing warnings in buffers_view
* Tidy up buffer_prefix overloads and test
* Add write limit to test::string_ostream
--------------------------------------------------------------------------------

View File

@ -10,6 +10,7 @@
#include <beast/core/async_result.hpp>
#include <beast/core/bind_handler.hpp>
#include <beast/core/buffer_prefix.hpp>
#include <beast/core/error.hpp>
#include <beast/websocket/teardown.hpp>
#include <boost/asio/buffer.hpp>
@ -22,13 +23,17 @@ namespace test {
class string_ostream
{
boost::asio::io_service& ios_;
std::size_t write_max_;
public:
std::string str;
explicit
string_ostream(boost::asio::io_service& ios)
string_ostream(boost::asio::io_service& ios,
std::size_t write_max =
(std::numeric_limits<std::size_t>::max)())
: ios_(ios)
, write_max_(write_max)
{
}
@ -87,11 +92,12 @@ public:
write_some(
ConstBufferSequence const& buffers, error_code&)
{
auto const n = buffer_size(buffers);
using boost::asio::buffer_size;
using boost::asio::buffer_cast;
auto const n =
(std::min)(buffer_size(buffers), write_max_);
str.reserve(str.size() + n);
for(auto const& buffer : buffers)
for(auto const& buffer : buffer_prefix(n, buffers))
str.append(buffer_cast<char const*>(buffer),
buffer_size(buffer));
return n;