rippled
Loading...
Searching...
No Matches
SimpleWriter.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_SERVER_SIMPLEWRITER_H_INCLUDED
21#define RIPPLE_SERVER_SIMPLEWRITER_H_INCLUDED
22
23#include <xrpl/server/Writer.h>
24
25#include <boost/beast/core/multi_buffer.hpp>
26#include <boost/beast/core/ostream.hpp>
27#include <boost/beast/http/message.hpp>
28#include <boost/beast/http/write.hpp>
29
30#include <utility>
31
32namespace ripple {
33
35class SimpleWriter : public Writer
36{
37 boost::beast::multi_buffer sb_;
38
39public:
40 template <bool isRequest, class Body, class Fields>
41 explicit SimpleWriter(
42 boost::beast::http::message<isRequest, Body, Fields> const& msg)
43 {
44 boost::beast::ostream(sb_) << msg;
45 }
46
47 bool
48 complete() override
49 {
50 return sb_.size() == 0;
51 }
52
53 void
54 consume(std::size_t bytes) override
55 {
56 sb_.consume(bytes);
57 }
58
59 bool
60 prepare(std::size_t bytes, std::function<void(void)>) override
61 {
62 return true;
63 }
64
66 data() override
67 {
68 auto const& buf = sb_.data();
70 result.reserve(std::distance(buf.begin(), buf.end()));
71 for (auto const b : buf)
72 result.push_back(b);
73 return result;
74 }
75};
76
77} // namespace ripple
78
79#endif
Deprecated: Writer that serializes a HTTP/1 message.
Definition: SimpleWriter.h:36
std::vector< boost::asio::const_buffer > data() override
Returns a ConstBufferSequence representing the input sequence.
Definition: SimpleWriter.h:66
SimpleWriter(boost::beast::http::message< isRequest, Body, Fields > const &msg)
Definition: SimpleWriter.h:41
void consume(std::size_t bytes) override
Removes bytes from the input sequence.
Definition: SimpleWriter.h:54
bool prepare(std::size_t bytes, std::function< void(void)>) override
Add data to the input sequence.
Definition: SimpleWriter.h:60
boost::beast::multi_buffer sb_
Definition: SimpleWriter.h:37
bool complete() override
Returns true if there is no more data to pull.
Definition: SimpleWriter.h:48
T distance(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
T push_back(T... args)
T reserve(T... args)