2015-08-31 06:04:21 +02:00
|
|
|
/*
|
|
|
|
|
2015-09-03 13:29:24 +02:00
|
|
|
Copyright (c) 2015, Arvid Norberg
|
2015-08-31 06:04:21 +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_ALLOCATING_HANDLER_HPP_INCLUDED
|
|
|
|
#define TORRENT_ALLOCATING_HANDLER_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
2016-11-17 20:34:49 +01:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2017-01-21 00:27:20 +01:00
|
|
|
#include "libtorrent/aux_/aligned_storage.hpp"
|
2015-08-31 06:04:21 +02:00
|
|
|
|
2016-09-06 06:28:28 +02:00
|
|
|
#include <type_traits>
|
2015-08-31 06:04:21 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent { namespace aux {
|
|
|
|
|
2015-08-31 06:04:21 +02:00
|
|
|
// this is meant to provide the actual storage for the handler allocator.
|
|
|
|
// There's only a single slot, so the allocator is only supposed to be used
|
|
|
|
// for handlers where there's only a single outstanding operation at a time,
|
|
|
|
// per storage object. For instance, peers only ever have one outstanding
|
|
|
|
// read operation at a time, so it can reuse its storage for read handlers.
|
|
|
|
template <std::size_t Size>
|
|
|
|
struct handler_storage
|
|
|
|
{
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2015-08-31 06:04:21 +02:00
|
|
|
handler_storage()
|
|
|
|
: used(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool used;
|
2017-01-23 14:19:09 +01:00
|
|
|
#else
|
|
|
|
handler_storage() = default;
|
2015-08-31 06:04:21 +02:00
|
|
|
#endif
|
2017-01-21 00:27:20 +01:00
|
|
|
typename aux::aligned_storage<Size>::type bytes;
|
2015-08-31 06:04:21 +02:00
|
|
|
private:
|
|
|
|
handler_storage(handler_storage const&);
|
|
|
|
};
|
|
|
|
|
2016-11-18 14:21:42 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT error_handler_interface
|
2016-09-25 15:50:48 +02:00
|
|
|
{
|
|
|
|
virtual void on_exception(std::exception const&) = 0;
|
|
|
|
virtual void on_error(error_code const&) = 0;
|
2016-11-18 14:21:42 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
~error_handler_interface() {}
|
2016-09-25 15:50:48 +02:00
|
|
|
};
|
|
|
|
|
2015-08-31 06:04:21 +02:00
|
|
|
// this class is a wrapper for an asio handler object. Its main purpose
|
|
|
|
// is to pass along additional parameters to the asio handler allocator
|
|
|
|
// function, as well as providing a distinct type for the handler
|
|
|
|
// allocator function to overload on
|
|
|
|
template <class Handler, std::size_t Size>
|
|
|
|
struct allocating_handler
|
|
|
|
{
|
|
|
|
allocating_handler(
|
2016-09-25 15:50:48 +02:00
|
|
|
Handler h, handler_storage<Size>& s, error_handler_interface& eh)
|
2015-08-31 06:04:21 +02:00
|
|
|
: handler(std::move(h))
|
|
|
|
, storage(s)
|
2017-11-10 09:50:45 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2016-09-25 15:50:48 +02:00
|
|
|
, error_handler(eh)
|
2017-11-10 09:50:45 +01:00
|
|
|
#endif
|
2015-08-31 06:04:21 +02:00
|
|
|
{}
|
2015-08-31 06:54:16 +02:00
|
|
|
|
2015-08-31 06:04:21 +02:00
|
|
|
template <class... A>
|
|
|
|
void operator()(A&&... a) const
|
|
|
|
{
|
2016-09-25 15:50:48 +02:00
|
|
|
#ifdef BOOST_NO_EXCEPTIONS
|
2015-08-31 06:04:21 +02:00
|
|
|
handler(std::forward<A>(a)...);
|
2016-09-25 15:50:48 +02:00
|
|
|
#else
|
|
|
|
try
|
|
|
|
{
|
|
|
|
handler(std::forward<A>(a)...);
|
|
|
|
}
|
|
|
|
catch (system_error const& e)
|
|
|
|
{
|
|
|
|
error_handler.on_error(e.code());
|
|
|
|
}
|
|
|
|
catch (std::exception const& e)
|
|
|
|
{
|
|
|
|
error_handler.on_exception(e);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
// this is pretty bad
|
|
|
|
TORRENT_ASSERT(false);
|
|
|
|
std::runtime_error e("unknown exception");
|
|
|
|
error_handler.on_exception(e);
|
|
|
|
}
|
|
|
|
#endif
|
2015-08-31 06:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
friend void* asio_handler_allocate(
|
|
|
|
std::size_t size, allocating_handler<Handler, Size>* ctx)
|
|
|
|
{
|
|
|
|
TORRENT_UNUSED(size);
|
2017-12-25 09:02:18 +01:00
|
|
|
TORRENT_ASSERT_VAL(size <= Size, size);
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2015-08-31 06:04:21 +02:00
|
|
|
TORRENT_ASSERT(!ctx->storage.used);
|
|
|
|
ctx->storage.used = true;
|
|
|
|
#endif
|
|
|
|
return &ctx->storage.bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend void asio_handler_deallocate(
|
|
|
|
void* ptr, std::size_t size, allocating_handler<Handler, Size>* ctx)
|
|
|
|
{
|
|
|
|
TORRENT_UNUSED(ptr);
|
|
|
|
TORRENT_UNUSED(size);
|
|
|
|
TORRENT_UNUSED(ctx);
|
|
|
|
|
2017-12-25 09:02:18 +01:00
|
|
|
TORRENT_ASSERT_VAL(size <= Size, size);
|
2015-08-31 06:04:21 +02:00
|
|
|
TORRENT_ASSERT(ptr == &ctx->storage.bytes);
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2015-08-31 06:04:21 +02:00
|
|
|
ctx->storage.used = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Handler handler;
|
|
|
|
handler_storage<Size>& storage;
|
2017-11-10 09:50:45 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2016-09-25 15:50:48 +02:00
|
|
|
error_handler_interface& error_handler;
|
2017-11-10 09:50:45 +01:00
|
|
|
#endif
|
2015-08-31 06:04:21 +02:00
|
|
|
};
|
|
|
|
|
2017-11-10 09:50:45 +01:00
|
|
|
template <class Handler, size_t Size>
|
|
|
|
aux::allocating_handler<Handler, Size>
|
2017-12-18 00:09:27 +01:00
|
|
|
make_handler(Handler handler
|
2017-11-10 09:50:45 +01:00
|
|
|
, handler_storage<Size>& storage
|
|
|
|
, error_handler_interface& err_handler)
|
|
|
|
{
|
|
|
|
return aux::allocating_handler<Handler, Size>(
|
2017-12-18 00:09:27 +01:00
|
|
|
std::forward<Handler>(handler), storage, err_handler);
|
2017-11-10 09:50:45 +01:00
|
|
|
}
|
2015-08-31 06:04:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|