forked from boostorg/beast
fix #321 This synchronizes identifier names and some implementation details with the Networking-TS document and reference implementation. The following interfaces have changed: * async_completion * is_buffer_sequence (removed) * is_const_buffer_sequence (was is_ConstBufferSequence) * is_dynamic_buffer (was is_DynamicBuffer) * is_mutable_buffer_sequence (was is_MutableBufferSequence) * async_result (new) * BEAST_HANDLER_TYPE macro (new) * BEAST_INITFN_RESULT_TYPE macro (new) Filename changes: * async_result.hpp (was async_completion.hpp)
140 lines
3.5 KiB
C++
140 lines
3.5 KiB
C++
//
|
|
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
|
//
|
|
// 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)
|
|
//
|
|
|
|
#ifndef BEAST_TEST_STRING_OSTREAM_HPP
|
|
#define BEAST_TEST_STRING_OSTREAM_HPP
|
|
|
|
#include <beast/core/async_result.hpp>
|
|
#include <beast/core/bind_handler.hpp>
|
|
#include <beast/core/error.hpp>
|
|
#include <beast/websocket/teardown.hpp>
|
|
#include <boost/asio/buffer.hpp>
|
|
#include <boost/asio/io_service.hpp>
|
|
#include <string>
|
|
|
|
namespace beast {
|
|
namespace test {
|
|
|
|
class string_ostream
|
|
{
|
|
boost::asio::io_service& ios_;
|
|
|
|
public:
|
|
std::string str;
|
|
|
|
explicit
|
|
string_ostream(boost::asio::io_service& ios)
|
|
: ios_(ios)
|
|
{
|
|
}
|
|
|
|
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)
|
|
throw system_error{ec};
|
|
return n;
|
|
}
|
|
|
|
template<class MutableBufferSequence>
|
|
std::size_t
|
|
read_some(MutableBufferSequence const& buffers,
|
|
error_code& ec)
|
|
{
|
|
ec = boost::asio::error::eof;
|
|
return 0;
|
|
}
|
|
|
|
template<class MutableBufferSequence, class ReadHandler>
|
|
BEAST_INITFN_RESULT_TYPE(
|
|
ReadHandler, void(error_code, std::size_t))
|
|
async_read_some(MutableBufferSequence const& buffers,
|
|
ReadHandler&& handler)
|
|
{
|
|
async_completion<ReadHandler,
|
|
void(error_code, std::size_t)> init{handler};
|
|
ios_.post(bind_handler(init.completion_handler,
|
|
boost::asio::error::eof, 0));
|
|
return init.result.get();
|
|
}
|
|
|
|
template<class ConstBufferSequence>
|
|
std::size_t
|
|
write_some(ConstBufferSequence const& buffers)
|
|
{
|
|
error_code ec;
|
|
auto const n = write_some(buffers, ec);
|
|
if(ec)
|
|
throw system_error{ec};
|
|
return n;
|
|
}
|
|
|
|
template<class ConstBufferSequence>
|
|
std::size_t
|
|
write_some(
|
|
ConstBufferSequence const& buffers, error_code&)
|
|
{
|
|
auto const n = buffer_size(buffers);
|
|
using boost::asio::buffer_size;
|
|
using boost::asio::buffer_cast;
|
|
str.reserve(str.size() + n);
|
|
for(auto const& buffer : buffers)
|
|
str.append(buffer_cast<char const*>(buffer),
|
|
buffer_size(buffer));
|
|
return n;
|
|
}
|
|
|
|
template<class ConstBufferSequence, class WriteHandler>
|
|
BEAST_INITFN_RESULT_TYPE(
|
|
WriteHandler, void(error_code, std::size_t))
|
|
async_write_some(ConstBufferSequence const& buffers,
|
|
WriteHandler&& handler)
|
|
{
|
|
error_code ec;
|
|
auto const bytes_transferred = write_some(buffers, ec);
|
|
async_completion<WriteHandler,
|
|
void(error_code, std::size_t)> init{handler};
|
|
get_io_service().post(
|
|
bind_handler(init.completion_handler, ec, bytes_transferred));
|
|
return init.result.get();
|
|
}
|
|
|
|
friend
|
|
void
|
|
teardown(websocket::teardown_tag,
|
|
string_ostream& stream,
|
|
boost::system::error_code& ec)
|
|
{
|
|
}
|
|
|
|
template<class TeardownHandler>
|
|
friend
|
|
void
|
|
async_teardown(websocket::teardown_tag,
|
|
string_ostream& stream,
|
|
TeardownHandler&& handler)
|
|
{
|
|
stream.get_io_service().post(
|
|
bind_handler(std::move(handler),
|
|
error_code{}));
|
|
}
|
|
};
|
|
|
|
} // test
|
|
} // beast
|
|
|
|
#endif
|