From cf2dbdc0bed8557cc3fffc7a6a02b3d6c332b9b8 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 12 Jan 2019 15:32:12 -0800 Subject: [PATCH] Use new saved_handler in websocket --- CHANGELOG.md | 1 + .../boost/beast/core/impl/saved_handler.hpp | 7 +- .../beast/websocket/detail/saved_handler.hpp | 170 ------------------ include/boost/beast/websocket/stream.hpp | 14 +- 4 files changed, 13 insertions(+), 179 deletions(-) delete mode 100644 include/boost/beast/websocket/detail/saved_handler.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index d327fdf1..c3416dbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Version 203 * Update networking refresher doc * Include error code in call to set_option * saved_handler is a public interface +* Use new saved_handler in websocket -------------------------------------------------------------------------------- diff --git a/include/boost/beast/core/impl/saved_handler.hpp b/include/boost/beast/core/impl/saved_handler.hpp index 2492beb8..d1ef2bf7 100644 --- a/include/boost/beast/core/impl/saved_handler.hpp +++ b/include/boost/beast/core/impl/saved_handler.hpp @@ -125,9 +125,12 @@ emplace(Handler&& handler, Allocator const& alloc) { // Can't delete a handler before invoking BOOST_ASSERT(! has_value()); + using handler_type = + typename std::decay::type; using alloc_type = typename - beast::detail::allocator_traits:: - template rebind_alloc>; + detail::allocator_traits:: + template rebind_alloc>; using alloc_traits = beast::detail::allocator_traits; struct storage diff --git a/include/boost/beast/websocket/detail/saved_handler.hpp b/include/boost/beast/websocket/detail/saved_handler.hpp deleted file mode 100644 index 0de240f6..00000000 --- a/include/boost/beast/websocket/detail/saved_handler.hpp +++ /dev/null @@ -1,170 +0,0 @@ -// -// 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_WEBSOCKET_DETAIL_SAVED_HANDLER_HPP -#define BOOST_BEAST_WEBSOCKET_DETAIL_SAVED_HANDLER_HPP - -#include -#include -#include -#include -#include -#include -#include - -namespace boost { -namespace beast { -namespace websocket { -namespace detail { - -// A container that holds a suspended, asynchronous composed -// operation. The contained object may be invoked later to -// resume the operation, or the container may be destroyed. -// -class saved_handler -{ - struct base - { - base() = default; - base(base &&) = delete; - base(base const&) = delete; - virtual void destroy() = 0; - virtual void invoke() = 0; - - protected: - ~base() = default; - }; - - template - class impl final : public base - { - Handler h_; - net::executor_work_guard< - net::associated_executor_t> wg_; - - public: - template - impl(DeducedHandler&& h) - : h_(std::forward(h)) - , wg_(net::get_associated_executor(h_)) - { - } - - void - destroy() override - { - using A = typename beast::detail::allocator_traits< - net::associated_allocator_t< - Handler>>::template rebind_alloc; - using alloc_traits = - beast::detail::allocator_traits; - Handler h(std::move(h_)); - A alloc(net::get_associated_allocator(h)); - alloc_traits::destroy(alloc, this); - alloc_traits::deallocate(alloc, this, 1); - } - - void - invoke() override - { - using A = typename beast::detail::allocator_traits< - net::associated_allocator_t< - Handler>>::template rebind_alloc; - using alloc_traits = - beast::detail::allocator_traits; - Handler h(std::move(h_)); - A alloc(net::get_associated_allocator(h)); - alloc_traits::destroy(alloc, this); - alloc_traits::deallocate(alloc, this, 1); - h(); - } - }; - - base* p_ = nullptr; - -public: - saved_handler() = default; - saved_handler(saved_handler const&) = delete; - saved_handler& operator=(saved_handler const&) = delete; - - ~saved_handler() - { - if(p_) - p_->destroy(); - } - - saved_handler(saved_handler&& other) - { - boost::ignore_unused(other); - // Moving a saved handler that - // owns an object is illegal - BOOST_ASSERT(! other.p_); - } - - saved_handler& - operator=(saved_handler&& other) - { - boost::ignore_unused(other); - // Moving a saved handler that - // owns an object is illegal - BOOST_ASSERT(! p_); - BOOST_ASSERT(! other.p_); - return *this; - } - - template - void - emplace(Handler&& handler) - { - // Can't emplace twice without invoke - BOOST_ASSERT(! p_); - using A = typename beast::detail::allocator_traits< - net::associated_allocator_t>::template - rebind_alloc>; - using alloc_traits = - beast::detail::allocator_traits; - A alloc(net::get_associated_allocator(handler)); - auto const d = - [&alloc](impl* p) - { - alloc_traits::deallocate(alloc, p, 1); - }; - std::unique_ptr, decltype(d)> p( - alloc_traits::allocate(alloc, 1), d); - alloc_traits::construct(alloc, p.get(), - std::forward(handler)); - p_ = p.release(); - } - - explicit - operator bool() const noexcept - { - return p_ != nullptr; - } - - bool - maybe_invoke() - { - if(p_) - { - auto const h = p_; - p_ = nullptr; - h->invoke(); - return true; - } - return false; - } -}; - -} // detail -} // websocket -} // beast -} // boost - -#endif diff --git a/include/boost/beast/websocket/stream.hpp b/include/boost/beast/websocket/stream.hpp index b4b1a1b9..7b73a636 100644 --- a/include/boost/beast/websocket/stream.hpp +++ b/include/boost/beast/websocket/stream.hpp @@ -19,10 +19,10 @@ #include #include #include -#include #include #include #include +#include #include #include #include @@ -204,12 +204,12 @@ class stream = 4096; detail::fh_buffer wr_fb_; // header buffer used for writes - detail::saved_handler paused_rd_; // paused read op - detail::saved_handler paused_wr_; // paused write op - detail::saved_handler paused_ping_; // paused ping op - detail::saved_handler paused_close_; // paused close op - detail::saved_handler paused_r_rd_; // paused read op (async read) - detail::saved_handler paused_r_close_;// paused close op (async read) + saved_handler paused_rd_; // paused read op + saved_handler paused_wr_; // paused write op + saved_handler paused_ping_; // paused ping op + saved_handler paused_close_; // paused close op + saved_handler paused_r_rd_; // paused read op (async read) + saved_handler paused_r_close_;// paused close op (async read) public: /// Indicates if the permessage-deflate extension is supported