rippled
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 <ripple/server/Writer.h>
24 #include <boost/beast/core/multi_buffer.hpp>
25 #include <boost/beast/core/ostream.hpp>
26 #include <boost/beast/http/message.hpp>
27 #include <boost/beast/http/write.hpp>
28 #include <utility>
29 
30 namespace ripple {
31 
33 class SimpleWriter : public Writer
34 {
35  boost::beast::multi_buffer sb_;
36 
37 public:
38  template <bool isRequest, class Body, class Fields>
39  explicit SimpleWriter(
40  boost::beast::http::message<isRequest, Body, Fields> const& msg)
41  {
42  boost::beast::ostream(sb_) << msg;
43  }
44 
45  bool
46  complete() override
47  {
48  return sb_.size() == 0;
49  }
50 
51  void
52  consume(std::size_t bytes) override
53  {
54  sb_.consume(bytes);
55  }
56 
57  bool
58  prepare(std::size_t bytes, std::function<void(void)>) override
59  {
60  return true;
61  }
62 
64  data() override
65  {
66  auto const& buf = sb_.data();
68  result.reserve(std::distance(buf.begin(), buf.end()));
69  for (auto const b : buf)
70  result.push_back(b);
71  return result;
72  }
73 };
74 
75 } // namespace ripple
76 
77 #endif
ripple::SimpleWriter::data
std::vector< boost::asio::const_buffer > data() override
Returns a ConstBufferSequence representing the input sequence.
Definition: SimpleWriter.h:64
utility
std::vector::reserve
T reserve(T... args)
std::vector
STL class.
std::distance
T distance(T... args)
ripple::SimpleWriter::SimpleWriter
SimpleWriter(boost::beast::http::message< isRequest, Body, Fields > const &msg)
Definition: SimpleWriter.h:39
std::function
std::vector::push_back
T push_back(T... args)
ripple::Writer
Definition: server/Writer.h:29
ripple::SimpleWriter
Deprecated: Writer that serializes a HTTP/1 message.
Definition: SimpleWriter.h:33
ripple::SimpleWriter::consume
void consume(std::size_t bytes) override
Removes bytes from the input sequence.
Definition: SimpleWriter.h:52
ripple::SimpleWriter::complete
bool complete() override
Returns true if there is no more data to pull.
Definition: SimpleWriter.h:46
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::size_t
ripple::SimpleWriter::sb_
boost::beast::multi_buffer sb_
Definition: SimpleWriter.h:35
ripple::SimpleWriter::prepare
bool prepare(std::size_t bytes, std::function< void(void)>) override
Add data to the input sequence.
Definition: SimpleWriter.h:58