]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_TEST_STRING_OSTREAM_HPP | |
9 | #define BEAST_TEST_STRING_OSTREAM_HPP | |
10 | ||
11 | #include <beast/core/async_completion.hpp> | |
12 | #include <beast/core/bind_handler.hpp> | |
13 | #include <beast/core/error.hpp> | |
14 | #include <boost/asio/buffer.hpp> | |
15 | #include <boost/asio/io_service.hpp> | |
16 | #include <string> | |
17 | ||
18 | namespace beast { | |
19 | namespace test { | |
20 | ||
21 | class string_ostream | |
22 | { | |
23 | boost::asio::io_service& ios_; | |
24 | ||
25 | public: | |
26 | std::string str; | |
27 | ||
28 | explicit | |
29 | string_ostream(boost::asio::io_service& ios) | |
30 | : ios_(ios) | |
31 | { | |
32 | } | |
33 | ||
34 | boost::asio::io_service& | |
35 | get_io_service() | |
36 | { | |
37 | return ios_; | |
38 | } | |
39 | ||
40 | template<class MutableBufferSequence> | |
41 | std::size_t | |
42 | read_some(MutableBufferSequence const& buffers) | |
43 | { | |
44 | error_code ec; | |
45 | auto const n = read_some(buffers, ec); | |
46 | if(ec) | |
47 | throw system_error{ec}; | |
48 | return n; | |
49 | } | |
50 | ||
51 | template<class MutableBufferSequence> | |
52 | std::size_t | |
53 | read_some(MutableBufferSequence const& buffers, | |
54 | error_code& ec) | |
55 | { | |
56 | return 0; | |
57 | } | |
58 | ||
59 | template<class MutableBufferSequence, class ReadHandler> | |
60 | typename async_completion<ReadHandler, | |
61 | void(error_code, std::size_t)>::result_type | |
62 | async_read_some(MutableBufferSequence const& buffers, | |
63 | ReadHandler&& handler) | |
64 | { | |
65 | async_completion<ReadHandler, | |
66 | void(error_code, std::size_t)> completion{handler}; | |
67 | ios_.post(bind_handler(completion.handler, | |
68 | error_code{}, 0)); | |
69 | return completion.result.get(); | |
70 | } | |
71 | ||
72 | template<class ConstBufferSequence> | |
73 | std::size_t | |
74 | write_some(ConstBufferSequence const& buffers) | |
75 | { | |
76 | error_code ec; | |
77 | auto const n = write_some(buffers, ec); | |
78 | if(ec) | |
79 | throw system_error{ec}; | |
80 | return n; | |
81 | } | |
82 | ||
83 | template<class ConstBufferSequence> | |
84 | std::size_t | |
85 | write_some( | |
86 | ConstBufferSequence const& buffers, error_code&) | |
87 | { | |
88 | auto const n = buffer_size(buffers); | |
89 | using boost::asio::buffer_size; | |
90 | using boost::asio::buffer_cast; | |
91 | str.reserve(str.size() + n); | |
92 | for(auto const& buffer : buffers) | |
93 | str.append(buffer_cast<char const*>(buffer), | |
94 | buffer_size(buffer)); | |
95 | return n; | |
96 | } | |
97 | ||
98 | template<class ConstBufferSequence, class WriteHandler> | |
99 | typename async_completion< | |
100 | WriteHandler, void(error_code)>::result_type | |
101 | async_write_some(ConstBufferSequence const& buffers, | |
102 | WriteHandler&& handler) | |
103 | { | |
104 | error_code ec; | |
105 | auto const bytes_transferred = write_some(buffers, ec); | |
106 | async_completion< | |
107 | WriteHandler, void(error_code, std::size_t) | |
108 | > completion{handler}; | |
109 | get_io_service().post( | |
110 | bind_handler(completion.handler, ec, bytes_transferred)); | |
111 | return completion.result.get(); | |
112 | } | |
113 | }; | |
114 | ||
115 | } // test | |
116 | } // beast | |
117 | ||
118 | #endif |