Files
boost_beast/extras/beast/test/string_istream.hpp
T

155 lines
4.0 KiB
C++
Raw Normal View History

2016-05-04 17:27:50 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-05-04 17:27:50 -04:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2017-01-09 11:13:19 -05:00
#ifndef BEAST_TEST_STRING_ISTREAM_HPP
#define BEAST_TEST_STRING_ISTREAM_HPP
2016-05-04 17:27:50 -04:00
#include <beast/core/async_result.hpp>
2016-05-07 14:57:15 -04:00
#include <beast/core/bind_handler.hpp>
#include <beast/core/error.hpp>
#include <beast/websocket/teardown.hpp>
2016-05-04 17:27:50 -04:00
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_service.hpp>
2017-05-22 15:30:12 -07:00
#include <boost/throw_exception.hpp>
2016-05-04 17:27:50 -04:00
#include <string>
namespace beast {
namespace test {
2016-05-07 07:08:21 -04:00
/** A SyncStream and AsyncStream that reads from a string.
This class behaves like a socket, except that written data is simply
discarded, and when data is read it comes from a string provided
at construction.
*/
2017-01-09 11:13:19 -05:00
class string_istream
2016-05-04 17:27:50 -04:00
{
std::string s_;
2017-01-09 11:13:19 -05:00
boost::asio::const_buffer cb_;
2016-05-04 17:27:50 -04:00
boost::asio::io_service& ios_;
2017-01-09 11:13:19 -05:00
std::size_t read_max_;
2016-05-04 17:27:50 -04:00
public:
2017-01-09 11:13:19 -05:00
string_istream(boost::asio::io_service& ios,
std::string s, std::size_t read_max =
(std::numeric_limits<std::size_t>::max)())
2016-05-04 17:27:50 -04:00
: s_(std::move(s))
2017-01-09 11:13:19 -05:00
, cb_(boost::asio::buffer(s_))
2016-05-04 17:27:50 -04:00
, ios_(ios)
2017-01-09 11:13:19 -05:00
, read_max_(read_max)
2016-05-04 17:27:50 -04:00
{
}
boost::asio::io_service&
get_io_service()
{
return ios_;
}
template<class MutableBufferSequence>
std::size_t
read_some(MutableBufferSequence const& buffers)
{
error_code ec;
auto const n = read_some(buffers, ec);
if(ec)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
2016-05-04 17:27:50 -04:00
return n;
}
template<class MutableBufferSequence>
std::size_t
read_some(MutableBufferSequence const& buffers,
error_code& ec)
{
auto const n = boost::asio::buffer_copy(
2017-05-24 19:42:24 -07:00
buffers, cb_, read_max_);
2016-05-07 17:06:46 -04:00
if(n > 0)
2017-01-09 11:13:19 -05:00
cb_ = cb_ + n;
2016-05-07 17:06:46 -04:00
else
ec = boost::asio::error::eof;
2016-05-04 17:27:50 -04:00
return n;
}
template<class MutableBufferSequence, class ReadHandler>
async_return_type<
ReadHandler, void(error_code, std::size_t)>
2016-05-04 17:27:50 -04:00
async_read_some(MutableBufferSequence const& buffers,
ReadHandler&& handler)
{
auto const n = boost::asio::buffer_copy(
buffers, boost::asio::buffer(s_));
2016-05-07 17:06:46 -04:00
error_code ec;
if(n > 0)
s_.erase(0, n);
else
ec = boost::asio::error::eof;
2016-05-04 17:27:50 -04:00
async_completion<ReadHandler,
void(error_code, std::size_t)> init{handler};
2016-05-04 17:27:50 -04:00
ios_.post(bind_handler(
init.completion_handler, ec, n));
return init.result.get();
2016-05-04 17:27:50 -04:00
}
template<class ConstBufferSequence>
std::size_t
write_some(ConstBufferSequence const& buffers)
{
error_code ec;
auto const n = write_some(buffers, ec);
if(ec)
2017-05-22 15:30:12 -07:00
BOOST_THROW_EXCEPTION(system_error{ec});
2016-05-04 17:27:50 -04:00
return n;
}
template<class ConstBufferSequence>
std::size_t
write_some(ConstBufferSequence const& buffers,
error_code&)
{
return boost::asio::buffer_size(buffers);
}
template<class ConstBuffeSequence, class WriteHandler>
async_return_type<
WriteHandler, void(error_code, std::size_t)>
2016-05-04 17:27:50 -04:00
async_write_some(ConstBuffeSequence const& buffers,
WriteHandler&& handler)
{
async_completion<WriteHandler,
void(error_code, std::size_t)> init{handler};
ios_.post(bind_handler(init.completion_handler,
2016-05-04 17:27:50 -04:00
error_code{}, boost::asio::buffer_size(buffers)));
return init.result.get();
2016-05-04 17:27:50 -04:00
}
friend
void
teardown(websocket::teardown_tag,
string_istream& stream,
boost::system::error_code& ec)
{
}
template<class TeardownHandler>
friend
void
async_teardown(websocket::teardown_tag,
string_istream& stream,
TeardownHandler&& handler)
{
stream.get_io_service().post(
bind_handler(std::move(handler),
error_code{}));
}
2016-05-04 17:27:50 -04:00
};
} // test
} // beast
#endif