2017-07-20 08:01:46 -07:00
|
|
|
//
|
2017-07-24 09:42:36 -07:00
|
|
|
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
|
// 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)
|
|
|
|
|
//
|
2017-07-20 13:40:34 -07:00
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
|
//
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#ifndef BOOST_BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP
|
|
|
|
|
#define BOOST_BEAST_WEBSOCKET_DETAIL_PAUSATION_HPP
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
#include <boost/beast/core/handler_ptr.hpp>
|
2016-09-25 12:17:32 -04:00
|
|
|
#include <boost/assert.hpp>
|
2017-07-20 08:01:46 -07:00
|
|
|
#include <array>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <new>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2017-07-20 13:40:34 -07:00
|
|
|
namespace boost {
|
2017-07-20 08:01:46 -07:00
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
2017-04-27 17:29:25 -07:00
|
|
|
// A container that holds a suspended, asynchronous composed
|
2017-05-31 08:01:55 -07:00
|
|
|
// operation. The contained object may be invoked later to
|
2017-04-27 17:29:25 -07:00
|
|
|
// resume the operation, or the container may be destroyed.
|
2017-07-20 08:01:46 -07:00
|
|
|
//
|
2017-04-27 17:29:25 -07:00
|
|
|
class pausation
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
|
struct base
|
|
|
|
|
{
|
|
|
|
|
base() = default;
|
|
|
|
|
base(base &&) = default;
|
|
|
|
|
virtual ~base() = default;
|
2017-06-02 20:50:11 -07:00
|
|
|
virtual base* move(void* p) = 0;
|
2017-07-20 08:01:46 -07:00
|
|
|
virtual void operator()() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
|
struct holder : base
|
|
|
|
|
{
|
|
|
|
|
F f;
|
|
|
|
|
|
|
|
|
|
holder(holder&&) = default;
|
|
|
|
|
|
|
|
|
|
template<class U>
|
|
|
|
|
explicit
|
|
|
|
|
holder(U&& u)
|
|
|
|
|
: f(std::forward<U>(u))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-02 20:50:11 -07:00
|
|
|
base*
|
2017-07-20 08:01:46 -07:00
|
|
|
move(void* p) override
|
|
|
|
|
{
|
2017-06-02 20:50:11 -07:00
|
|
|
return ::new(p) holder(std::move(*this));
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
operator()() override
|
|
|
|
|
{
|
|
|
|
|
F f_(std::move(f));
|
|
|
|
|
this->~holder();
|
|
|
|
|
// invocation of f_() can
|
2017-04-27 17:29:25 -07:00
|
|
|
// assign a new object to *this.
|
2017-07-20 08:01:46 -07:00
|
|
|
f_();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct exemplar
|
|
|
|
|
{
|
2017-01-02 13:29:48 -05:00
|
|
|
struct H
|
|
|
|
|
{
|
|
|
|
|
void operator()();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct T
|
|
|
|
|
{
|
|
|
|
|
using handler_type = H;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handler_ptr<T, H> hp;
|
|
|
|
|
|
|
|
|
|
void operator()();
|
2017-07-20 08:01:46 -07:00
|
|
|
};
|
|
|
|
|
|
2017-07-15 17:05:24 -07:00
|
|
|
template<class Op>
|
|
|
|
|
class saved_op
|
|
|
|
|
{
|
|
|
|
|
Op* op_ = nullptr;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
~saved_op()
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::asio_handler_deallocate;
|
|
|
|
|
if(op_)
|
|
|
|
|
{
|
|
|
|
|
auto h = std::move(op_->handler());
|
|
|
|
|
op_->~Op();
|
|
|
|
|
asio_handler_deallocate(op_,
|
|
|
|
|
sizeof(*op_), std::addressof(h));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saved_op(saved_op&& other)
|
|
|
|
|
: op_(other.op_)
|
|
|
|
|
{
|
|
|
|
|
other.op_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saved_op& operator=(saved_op&& other)
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(! op_);
|
|
|
|
|
op_ = other.op_;
|
|
|
|
|
other.op_ = 0;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
explicit
|
|
|
|
|
saved_op(Op&& op)
|
|
|
|
|
{
|
|
|
|
|
using boost::asio::asio_handler_allocate;
|
|
|
|
|
new(asio_handler_allocate(sizeof(Op),
|
|
|
|
|
std::addressof(op.handler()))) Op{
|
|
|
|
|
std::move(op)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
operator()()
|
|
|
|
|
{
|
|
|
|
|
BOOST_ASSERT(op_);
|
|
|
|
|
Op op{std::move(*op_)};
|
|
|
|
|
using boost::asio::asio_handler_deallocate;
|
|
|
|
|
asio_handler_deallocate(op_,
|
|
|
|
|
sizeof(*op_), std::addressof(op_->handler()));
|
|
|
|
|
op_ = nullptr;
|
|
|
|
|
op();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
using buf_type = char[sizeof(holder<exemplar>)];
|
|
|
|
|
|
|
|
|
|
base* base_ = nullptr;
|
|
|
|
|
alignas(holder<exemplar>) buf_type buf_;
|
|
|
|
|
|
|
|
|
|
public:
|
2017-04-27 17:29:25 -07:00
|
|
|
~pausation()
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2016-10-18 07:22:01 -04:00
|
|
|
if(base_)
|
|
|
|
|
base_->~base();
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:29:25 -07:00
|
|
|
pausation() = default;
|
2017-07-20 08:01:46 -07:00
|
|
|
|
2017-04-27 17:29:25 -07:00
|
|
|
pausation(pausation&& other)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
|
|
|
|
if(other.base_)
|
|
|
|
|
{
|
2017-06-02 20:50:11 -07:00
|
|
|
base_ = other.base_->move(buf_);
|
2017-07-20 08:01:46 -07:00
|
|
|
other.base_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-27 17:29:25 -07:00
|
|
|
pausation&
|
|
|
|
|
operator=(pausation&& other)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-04-27 17:29:25 -07:00
|
|
|
// Engaged pausations must be invoked before
|
2017-07-20 08:01:46 -07:00
|
|
|
// assignment otherwise the io_service
|
2017-02-24 10:03:33 -05:00
|
|
|
// completion invariants are broken.
|
2016-09-25 12:17:32 -04:00
|
|
|
BOOST_ASSERT(! base_);
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
|
if(other.base_)
|
|
|
|
|
{
|
2017-06-02 20:50:11 -07:00
|
|
|
base_ = other.base_->move(buf_);
|
2017-07-20 08:01:46 -07:00
|
|
|
other.base_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
|
void
|
|
|
|
|
emplace(F&& f);
|
|
|
|
|
|
2017-07-15 17:05:24 -07:00
|
|
|
template<class F>
|
|
|
|
|
void
|
|
|
|
|
save(F&& f);
|
|
|
|
|
|
2016-10-24 18:41:25 -04:00
|
|
|
bool
|
2017-07-20 08:01:46 -07:00
|
|
|
maybe_invoke()
|
|
|
|
|
{
|
|
|
|
|
if(base_)
|
|
|
|
|
{
|
|
|
|
|
auto const basep = base_;
|
|
|
|
|
base_ = nullptr;
|
|
|
|
|
(*basep)();
|
2016-10-24 18:41:25 -04:00
|
|
|
return true;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
2016-10-24 18:41:25 -04:00
|
|
|
return false;
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class F>
|
|
|
|
|
void
|
2017-04-27 17:29:25 -07:00
|
|
|
pausation::emplace(F&& f)
|
2017-07-20 08:01:46 -07:00
|
|
|
{
|
2017-06-02 20:50:11 -07:00
|
|
|
using type = holder<typename std::decay<F>::type>;
|
|
|
|
|
static_assert(sizeof(buf_type) >= sizeof(type),
|
2017-07-20 08:01:46 -07:00
|
|
|
"buffer too small");
|
2016-09-25 12:17:32 -04:00
|
|
|
BOOST_ASSERT(! base_);
|
2017-06-02 20:50:11 -07:00
|
|
|
base_ = ::new(buf_) type{std::forward<F>(f)};
|
2017-07-20 08:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-15 17:05:24 -07:00
|
|
|
template<class F>
|
|
|
|
|
void
|
|
|
|
|
pausation::save(F&& f)
|
|
|
|
|
{
|
|
|
|
|
emplace(saved_op<F>{std::move(f)});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 08:01:46 -07:00
|
|
|
} // detail
|
|
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
2017-07-20 13:40:34 -07:00
|
|
|
} // boost
|
2017-07-20 08:01:46 -07:00
|
|
|
|
|
|
|
|
#endif
|