summaryrefslogtreecommitdiff
path: root/boost/beast/experimental/core/detail/flat_stream.hpp
blob: 3a56897b5ab081c676fad290c0a7b507883c8253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Copyright (c) 2016-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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_CORE_DETAIL_FLAT_STREAM_HPP
#define BOOST_BEAST_CORE_DETAIL_FLAT_STREAM_HPP

#include <boost/asio/buffer.hpp>
#include <cstdlib>
#include <iterator>

namespace boost {
namespace beast {
namespace detail {

class flat_stream_base
{
public:
    // Largest buffer size we will flatten.
    // 16KB is the upper limit on reasonably sized HTTP messages.
    static std::size_t constexpr coalesce_limit = 16 * 1024;

    // calculates the coalesce settings for a buffer sequence
    template<class BufferSequence>
    static
    std::pair<std::size_t, bool>
    coalesce(BufferSequence const& buffers, std::size_t limit)
    {
        std::pair<std::size_t, bool> result{0, false};
        auto first = boost::asio::buffer_sequence_begin(buffers);
        auto last = boost::asio::buffer_sequence_end(buffers);
        if(first != last)
        {
            result.first = boost::asio::buffer_size(*first);
            if(result.first < limit)
            {
                auto it = first;
                auto prev = first;
                while(++it != last)
                {
                    auto const n =
                        boost::asio::buffer_size(*it);
                    if(result.first + n > limit)
                        break;
                    result.first += n;
                    prev = it;
                }
                result.second = prev != first;
            }
        }
        return result;
    }
};

} // detail
} // beast
} // boost

#endif