2007-09-29 18:14:03 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2007-2016, Arvid Norberg
|
2007-09-29 18:14:03 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_CHAINED_BUFFER_HPP_INCLUDED
|
|
|
|
#define TORRENT_CHAINED_BUFFER_HPP_INCLUDED
|
|
|
|
|
2011-11-02 10:31:32 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2016-11-18 06:25:27 +01:00
|
|
|
#include "libtorrent/aux_/block_cache_reference.hpp"
|
2017-01-21 00:27:20 +01:00
|
|
|
#include "libtorrent/aux_/aligned_storage.hpp"
|
2015-04-24 07:37:17 +02:00
|
|
|
#include "libtorrent/debug.hpp"
|
2016-07-24 00:54:39 +02:00
|
|
|
#include "libtorrent/buffer.hpp"
|
2015-04-24 07:37:17 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <deque>
|
|
|
|
#include <vector>
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-08-26 06:14:11 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
|
|
|
#include <boost/asio/buffer.hpp>
|
2015-04-24 07:37:17 +02:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
#ifdef _MSC_VER
|
2016-12-29 02:57:46 +01:00
|
|
|
// visual studio requires the value in a deque to be copyable. C++11
|
|
|
|
// has looser requirements depending on which functions are actually used.
|
2016-12-29 02:47:18 +01:00
|
|
|
#define TORRENT_CPP98_DEQUE 1
|
|
|
|
#else
|
|
|
|
#define TORRENT_CPP98_DEQUE 0
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-06-20 05:05:59 +02:00
|
|
|
// TODO: 2 this type should probably be renamed to send_buffer
|
2014-07-06 21:18:00 +02:00
|
|
|
struct TORRENT_EXTRA_EXPORT chained_buffer : private single_threaded
|
2007-09-29 18:14:03 +02:00
|
|
|
{
|
2011-08-14 01:01:38 +02:00
|
|
|
chained_buffer(): m_bytes(0), m_capacity(0)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
thread_started();
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2011-08-14 01:01:38 +02:00
|
|
|
m_destructed = false;
|
|
|
|
#endif
|
|
|
|
}
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
// destructs/frees the holder object
|
|
|
|
using destruct_holder_fun = void (*)(void*);
|
|
|
|
using move_construct_holder_fun = void (*)(void*, void*);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
struct buffer_t
|
|
|
|
{
|
2016-12-29 02:47:18 +01:00
|
|
|
buffer_t() {}
|
|
|
|
#if TORRENT_CPP98_DEQUE
|
|
|
|
buffer_t(buffer_t&& rhs) noexcept
|
|
|
|
{
|
|
|
|
destruct_holder = rhs.destruct_holder;
|
|
|
|
move_holder = rhs.move_holder;
|
|
|
|
buf = rhs.buf;
|
|
|
|
size = rhs.size;
|
|
|
|
used_size = rhs.used_size;
|
|
|
|
move_holder(&holder, &rhs.holder);
|
|
|
|
}
|
|
|
|
buffer_t& operator=(buffer_t&& rhs) noexcept
|
|
|
|
{
|
|
|
|
destruct_holder(&holder);
|
|
|
|
destruct_holder = rhs.destruct_holder;
|
|
|
|
move_holder = rhs.move_holder;
|
|
|
|
buf = rhs.buf;
|
|
|
|
size = rhs.size;
|
|
|
|
used_size = rhs.used_size;
|
|
|
|
move_holder(&holder, &rhs.holder);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
buffer_t(buffer_t const& rhs) noexcept
|
|
|
|
: buffer_t(std::move(const_cast<buffer_t&>(rhs))) {}
|
|
|
|
buffer_t& operator=(buffer_t const& rhs) noexcept
|
|
|
|
{ return this->operator=(std::move(const_cast<buffer_t&>(rhs))); }
|
|
|
|
#else
|
|
|
|
buffer_t(buffer_t&&) = delete;
|
|
|
|
buffer_t& operator=(buffer_t&&) = delete;
|
|
|
|
buffer_t(buffer_t const&) = delete;
|
|
|
|
buffer_t& operator=(buffer_t const&) = delete;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
destruct_holder_fun destruct_holder;
|
|
|
|
#if TORRENT_CPP98_DEQUE
|
|
|
|
move_construct_holder_fun move_holder;
|
|
|
|
#endif
|
2017-10-13 01:34:24 +02:00
|
|
|
aux::aligned_storage<32>::type holder;
|
|
|
|
char* buf = nullptr; // the first byte of the buffer
|
|
|
|
int size = 0; // the total size of the buffer
|
|
|
|
int used_size = 0; // this is the number of bytes to send/receive
|
2007-09-29 18:14:03 +02:00
|
|
|
};
|
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
public:
|
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
bool empty() const { return m_bytes == 0; }
|
|
|
|
int size() const { return m_bytes; }
|
|
|
|
int capacity() const { return m_capacity; }
|
|
|
|
|
2011-08-14 01:01:38 +02:00
|
|
|
void pop_front(int bytes_to_pop);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
template <typename Holder>
|
2017-08-28 11:37:53 +02:00
|
|
|
void append_buffer(Holder buffer, int used_size)
|
2016-12-29 02:47:18 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2017-08-28 11:37:53 +02:00
|
|
|
TORRENT_ASSERT(int(buffer.size()) >= used_size);
|
2016-12-29 02:47:18 +01:00
|
|
|
m_vec.emplace_back();
|
|
|
|
buffer_t& b = m_vec.back();
|
2017-08-28 11:37:53 +02:00
|
|
|
init_buffer_entry<Holder>(b, std::move(buffer), used_size);
|
2016-12-29 02:47:18 +01:00
|
|
|
}
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
template <typename Holder>
|
2017-08-28 11:37:53 +02:00
|
|
|
void prepend_buffer(Holder buffer, int used_size)
|
2016-12-29 02:47:18 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_single_thread());
|
2017-08-28 11:37:53 +02:00
|
|
|
TORRENT_ASSERT(int(buffer.size()) >= used_size);
|
2016-12-29 02:47:18 +01:00
|
|
|
m_vec.emplace_front();
|
|
|
|
buffer_t& b = m_vec.front();
|
2017-08-28 11:37:53 +02:00
|
|
|
init_buffer_entry<Holder>(b, std::move(buffer), used_size);
|
2016-12-29 02:47:18 +01:00
|
|
|
}
|
2014-11-23 07:14:47 +01:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
// returns the number of bytes available at the
|
|
|
|
// end of the last chained buffer.
|
2011-08-14 01:01:38 +02:00
|
|
|
int space_in_last_buffer();
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
// tries to copy the given buffer to the end of the
|
|
|
|
// last chained buffer. If there's not enough room
|
2016-12-29 02:47:18 +01:00
|
|
|
// it returns nullptr
|
2017-04-23 06:37:29 +02:00
|
|
|
char* append(span<char const> buf);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
// tries to allocate memory from the end
|
|
|
|
// of the last buffer. If there isn't
|
|
|
|
// enough room, returns 0
|
2011-08-14 01:01:38 +02:00
|
|
|
char* allocate_appendix(int s);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2015-06-06 08:10:53 +02:00
|
|
|
std::vector<boost::asio::const_buffer> const& build_iovec(int to_send);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
void clear();
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-07-28 04:31:08 +02:00
|
|
|
void build_mutable_iovec(int bytes, std::vector<span<char>>& vec);
|
2014-11-23 07:14:47 +01:00
|
|
|
|
2011-08-14 01:01:38 +02:00
|
|
|
~chained_buffer();
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
private:
|
2016-12-29 02:47:18 +01:00
|
|
|
|
|
|
|
template <typename Holder>
|
2017-08-28 11:37:53 +02:00
|
|
|
void init_buffer_entry(buffer_t& b, Holder buf, int used_size)
|
2016-12-29 02:47:18 +01:00
|
|
|
{
|
|
|
|
static_assert(sizeof(Holder) <= sizeof(b.holder), "buffer holder too large");
|
|
|
|
|
2017-08-28 11:37:53 +02:00
|
|
|
b.buf = buf.data();
|
|
|
|
b.size = static_cast<int>(buf.size());
|
2016-12-29 02:47:18 +01:00
|
|
|
b.used_size = used_size;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// this appears to be a false positive msvc warning
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#pragma warning(disable : 4100)
|
|
|
|
#endif
|
|
|
|
b.destruct_holder = [](void* holder)
|
|
|
|
{ reinterpret_cast<Holder*>(holder)->~Holder(); };
|
|
|
|
|
|
|
|
#if TORRENT_CPP98_DEQUE
|
|
|
|
b.move_holder = [](void* dst, void* src)
|
|
|
|
{ new (dst) Holder(std::move(*reinterpret_cast<Holder*>(src))); };
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2017-08-28 11:37:53 +02:00
|
|
|
new (&b.holder) Holder(std::move(buf));
|
2016-12-29 02:47:18 +01:00
|
|
|
|
|
|
|
m_bytes += used_size;
|
2017-08-28 11:37:53 +02:00
|
|
|
TORRENT_ASSERT(m_capacity < std::numeric_limits<int>::max() - b.size);
|
|
|
|
m_capacity += b.size;
|
2016-12-29 02:47:18 +01:00
|
|
|
TORRENT_ASSERT(m_bytes <= m_capacity);
|
|
|
|
}
|
|
|
|
|
2014-11-23 07:14:47 +01:00
|
|
|
template <typename Buffer>
|
|
|
|
void build_vec(int bytes, std::vector<Buffer>& vec);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
// this is the list of all the buffers we want to
|
|
|
|
// send
|
2014-07-06 21:18:00 +02:00
|
|
|
std::deque<buffer_t> m_vec;
|
2013-11-26 05:39:33 +01:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
// this is the number of bytes in the send buf.
|
|
|
|
// this will always be equal to the sum of the
|
|
|
|
// size of all buffers in vec
|
|
|
|
int m_bytes;
|
|
|
|
|
|
|
|
// the total size of all buffers in the chain
|
|
|
|
// including unused space
|
|
|
|
int m_capacity;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is the vector of buffers used when
|
|
|
|
// invoking the async write call
|
2015-06-06 08:10:53 +02:00
|
|
|
std::vector<boost::asio::const_buffer> m_tmp_vec;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2011-08-14 01:01:38 +02:00
|
|
|
bool m_destructed;
|
|
|
|
#endif
|
2015-06-06 08:10:53 +02:00
|
|
|
};
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|