123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- #ifndef BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SERVICE_HPP
- #define BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_SERVICE_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/config.hpp>
- #include <boost/asio/associated_cancellation_slot.hpp>
- #include <boost/asio/cancellation_type.hpp>
- #include <boost/asio/detail/completion_message.hpp>
- #include <boost/asio/detail/completion_payload.hpp>
- #include <boost/asio/detail/completion_payload_handler.hpp>
- #include <boost/asio/detail/mutex.hpp>
- #include <boost/asio/detail/op_queue.hpp>
- #include <boost/asio/execution_context.hpp>
- #include <boost/asio/experimental/detail/channel_receive_op.hpp>
- #include <boost/asio/experimental/detail/channel_send_op.hpp>
- #include <boost/asio/experimental/detail/has_signature.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace experimental {
- namespace detail {
- template <typename Mutex>
- class channel_service
- : public boost::asio::detail::execution_context_service_base<
- channel_service<Mutex>>
- {
- public:
-
- enum state
- {
- buffer = 0,
- waiter = 1,
- block = 2,
- closed = 3
- };
-
- struct base_implementation_type
- {
-
- base_implementation_type()
- : receive_state_(block),
- send_state_(block),
- max_buffer_size_(0),
- next_(0),
- prev_(0)
- {
- }
-
- state receive_state_ : 16;
- state send_state_ : 16;
-
- std::size_t max_buffer_size_;
-
- boost::asio::detail::op_queue<channel_operation> waiters_;
-
- base_implementation_type* next_;
- base_implementation_type* prev_;
-
- mutable Mutex mutex_;
- };
-
- template <typename Traits, typename... Signatures>
- struct implementation_type;
-
- channel_service(boost::asio::execution_context& ctx);
-
- void shutdown();
-
- void construct(base_implementation_type& impl, std::size_t max_buffer_size);
-
- template <typename Traits, typename... Signatures>
- void destroy(implementation_type<Traits, Signatures...>& impl);
-
- template <typename Traits, typename... Signatures>
- void move_construct(implementation_type<Traits, Signatures...>& impl,
- implementation_type<Traits, Signatures...>& other_impl);
-
- template <typename Traits, typename... Signatures>
- void move_assign(implementation_type<Traits, Signatures...>& impl,
- channel_service& other_service,
- implementation_type<Traits, Signatures...>& other_impl);
-
- std::size_t capacity(
- const base_implementation_type& impl) const noexcept;
-
- bool is_open(const base_implementation_type& impl) const noexcept;
-
- template <typename Traits, typename... Signatures>
- void reset(implementation_type<Traits, Signatures...>& impl);
-
- template <typename Traits, typename... Signatures>
- void close(implementation_type<Traits, Signatures...>& impl);
-
- template <typename Traits, typename... Signatures>
- void cancel(implementation_type<Traits, Signatures...>& impl);
-
- template <typename Traits, typename... Signatures>
- void cancel_by_key(implementation_type<Traits, Signatures...>& impl,
- void* cancellation_key);
-
- bool ready(const base_implementation_type& impl) const noexcept;
-
- template <typename Message, typename Traits,
- typename... Signatures, typename... Args>
- bool try_send(implementation_type<Traits, Signatures...>& impl,
- bool via_dispatch, Args&&... args);
-
- template <typename Message, typename Traits,
- typename... Signatures, typename... Args>
- std::size_t try_send_n(implementation_type<Traits, Signatures...>& impl,
- std::size_t count, bool via_dispatch, Args&&... args);
-
- template <typename Traits, typename... Signatures,
- typename Handler, typename IoExecutor>
- void async_send(implementation_type<Traits, Signatures...>& impl,
- typename implementation_type<Traits,
- Signatures...>::payload_type&& payload,
- Handler& handler, const IoExecutor& io_ex)
- {
- associated_cancellation_slot_t<Handler> slot
- = boost::asio::get_associated_cancellation_slot(handler);
-
- typedef channel_send_op<
- typename implementation_type<Traits, Signatures...>::payload_type,
- Handler, IoExecutor> op;
- typename op::ptr p = { boost::asio::detail::addressof(handler),
- op::ptr::allocate(handler), 0 };
- p.p = new (p.v) op(static_cast<typename implementation_type<
- Traits, Signatures...>::payload_type&&>(payload), handler, io_ex);
-
- if (slot.is_connected())
- {
- p.p->cancellation_key_ =
- &slot.template emplace<op_cancellation<Traits, Signatures...>>(
- this, &impl);
- }
- BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
- "channel", &impl, 0, "async_send"));
- start_send_op(impl, p.p);
- p.v = p.p = 0;
- }
-
- template <typename Traits, typename... Signatures, typename Handler>
- bool try_receive(implementation_type<Traits, Signatures...>& impl,
- Handler&& handler);
-
- template <typename Traits, typename... Signatures,
- typename Handler, typename IoExecutor>
- void async_receive(implementation_type<Traits, Signatures...>& impl,
- Handler& handler, const IoExecutor& io_ex)
- {
- associated_cancellation_slot_t<Handler> slot
- = boost::asio::get_associated_cancellation_slot(handler);
-
- typedef channel_receive_op<
- typename implementation_type<Traits, Signatures...>::payload_type,
- Handler, IoExecutor> op;
- typename op::ptr p = { boost::asio::detail::addressof(handler),
- op::ptr::allocate(handler), 0 };
- p.p = new (p.v) op(handler, io_ex);
-
- if (slot.is_connected())
- {
- p.p->cancellation_key_ =
- &slot.template emplace<op_cancellation<Traits, Signatures...>>(
- this, &impl);
- }
- BOOST_ASIO_HANDLER_CREATION((this->context(), *p.p,
- "channel", &impl, 0, "async_receive"));
- start_receive_op(impl, p.p);
- p.v = p.p = 0;
- }
- private:
-
- template <typename Payload, typename Signature>
- struct post_receive
- {
- explicit post_receive(channel_receive<Payload>* op)
- : op_(op)
- {
- }
- template <typename... Args>
- void operator()(Args&&... args)
- {
- op_->post(
- boost::asio::detail::completion_message<Signature>(0,
- static_cast<Args&&>(args)...));
- }
- channel_receive<Payload>* op_;
- };
-
- void base_destroy(base_implementation_type& impl);
-
- template <typename Traits, typename... Signatures>
- void start_send_op(implementation_type<Traits, Signatures...>& impl,
- channel_send<typename implementation_type<
- Traits, Signatures...>::payload_type>* send_op);
-
- template <typename Traits, typename... Signatures>
- void start_receive_op(implementation_type<Traits, Signatures...>& impl,
- channel_receive<typename implementation_type<
- Traits, Signatures...>::payload_type>* receive_op);
-
- template <typename Traits, typename... Signatures>
- class op_cancellation
- {
- public:
- op_cancellation(channel_service* s,
- implementation_type<Traits, Signatures...>* impl)
- : service_(s),
- impl_(impl)
- {
- }
- void operator()(cancellation_type_t type)
- {
- if (!!(type &
- (cancellation_type::terminal
- | cancellation_type::partial
- | cancellation_type::total)))
- {
- service_->cancel_by_key(*impl_, this);
- }
- }
- private:
- channel_service* service_;
- implementation_type<Traits, Signatures...>* impl_;
- };
-
- boost::asio::detail::mutex mutex_;
-
- base_implementation_type* impl_list_;
- };
- template <typename Mutex>
- template <typename Traits, typename... Signatures>
- struct channel_service<Mutex>::implementation_type : base_implementation_type
- {
-
- typedef typename Traits::template rebind<Signatures...>::other traits_type;
-
- typedef conditional_t<
- has_signature<
- typename traits_type::receive_cancelled_signature,
- Signatures...
- >::value,
- conditional_t<
- has_signature<
- typename traits_type::receive_closed_signature,
- Signatures...
- >::value,
- boost::asio::detail::completion_payload<Signatures...>,
- boost::asio::detail::completion_payload<
- Signatures...,
- typename traits_type::receive_closed_signature
- >
- >,
- conditional_t<
- has_signature<
- typename traits_type::receive_closed_signature,
- Signatures...,
- typename traits_type::receive_cancelled_signature
- >::value,
- boost::asio::detail::completion_payload<
- Signatures...,
- typename traits_type::receive_cancelled_signature
- >,
- boost::asio::detail::completion_payload<
- Signatures...,
- typename traits_type::receive_cancelled_signature,
- typename traits_type::receive_closed_signature
- >
- >
- > payload_type;
-
- void buffer_move_from(implementation_type& other)
- {
- buffer_ = static_cast<
- typename traits_type::template container<payload_type>::type&&>(
- other.buffer_);
- other.buffer_clear();
- }
-
- std::size_t buffer_size() const
- {
- return buffer_.size();
- }
-
- void buffer_push(payload_type payload)
- {
- buffer_.push_back(static_cast<payload_type&&>(payload));
- }
-
- std::size_t buffer_push_n(std::size_t count, payload_type payload)
- {
- std::size_t i = 0;
- for (; i < count && buffer_.size() < this->max_buffer_size_; ++i)
- buffer_.push_back(payload);
- return i;
- }
-
- payload_type buffer_front()
- {
- return static_cast<payload_type&&>(buffer_.front());
- }
-
- void buffer_pop()
- {
- buffer_.pop_front();
- }
-
- void buffer_clear()
- {
- buffer_.clear();
- }
- private:
-
- typename traits_type::template container<payload_type>::type buffer_;
- };
- template <typename Mutex>
- template <typename Traits, typename R>
- struct channel_service<Mutex>::implementation_type<Traits, R()>
- : channel_service::base_implementation_type
- {
-
- typedef typename Traits::template rebind<R()>::other traits_type;
-
- typedef conditional_t<
- has_signature<
- typename traits_type::receive_cancelled_signature,
- R()
- >::value,
- conditional_t<
- has_signature<
- typename traits_type::receive_closed_signature,
- R()
- >::value,
- boost::asio::detail::completion_payload<R()>,
- boost::asio::detail::completion_payload<
- R(),
- typename traits_type::receive_closed_signature
- >
- >,
- conditional_t<
- has_signature<
- typename traits_type::receive_closed_signature,
- R(),
- typename traits_type::receive_cancelled_signature
- >::value,
- boost::asio::detail::completion_payload<
- R(),
- typename traits_type::receive_cancelled_signature
- >,
- boost::asio::detail::completion_payload<
- R(),
- typename traits_type::receive_cancelled_signature,
- typename traits_type::receive_closed_signature
- >
- >
- > payload_type;
-
- implementation_type()
- : buffer_(0)
- {
- }
-
- void buffer_move_from(implementation_type& other)
- {
- buffer_ = other.buffer_;
- other.buffer_ = 0;
- }
-
- std::size_t buffer_size() const
- {
- return buffer_;
- }
-
- void buffer_push(payload_type)
- {
- ++buffer_;
- }
-
- std::size_t buffer_push_n(std::size_t count, payload_type)
- {
- std::size_t available = this->max_buffer_size_ - buffer_;
- count = (count < available) ? count : available;
- buffer_ += count;
- return count;
- }
-
- payload_type buffer_front()
- {
- return payload_type(boost::asio::detail::completion_message<R()>(0));
- }
-
- void buffer_pop()
- {
- --buffer_;
- }
-
- void buffer_clear()
- {
- buffer_ = 0;
- }
- private:
-
- std::size_t buffer_;
- };
- template <typename Mutex>
- template <typename Traits, typename R>
- struct channel_service<Mutex>::implementation_type<
- Traits, R(boost::system::error_code)>
- : channel_service::base_implementation_type
- {
-
- typedef typename Traits::template rebind<R(boost::system::error_code)>::other
- traits_type;
-
- typedef conditional_t<
- has_signature<
- typename traits_type::receive_cancelled_signature,
- R(boost::system::error_code)
- >::value,
- conditional_t<
- has_signature<
- typename traits_type::receive_closed_signature,
- R(boost::system::error_code)
- >::value,
- boost::asio::detail::completion_payload<R(boost::system::error_code)>,
- boost::asio::detail::completion_payload<
- R(boost::system::error_code),
- typename traits_type::receive_closed_signature
- >
- >,
- conditional_t<
- has_signature<
- typename traits_type::receive_closed_signature,
- R(boost::system::error_code),
- typename traits_type::receive_cancelled_signature
- >::value,
- boost::asio::detail::completion_payload<
- R(boost::system::error_code),
- typename traits_type::receive_cancelled_signature
- >,
- boost::asio::detail::completion_payload<
- R(boost::system::error_code),
- typename traits_type::receive_cancelled_signature,
- typename traits_type::receive_closed_signature
- >
- >
- > payload_type;
-
- implementation_type()
- : size_(0)
- {
- first_.count_ = 0;
- }
-
- void buffer_move_from(implementation_type& other)
- {
- size_ = other.buffer_;
- other.size_ = 0;
- first_ = other.first_;
- other.first.count_ = 0;
- rest_ = static_cast<
- typename traits_type::template container<buffered_value>::type&&>(
- other.rest_);
- other.buffer_clear();
- }
-
- std::size_t buffer_size() const
- {
- return size_;
- }
-
- void buffer_push(payload_type payload)
- {
- buffered_value& last = rest_.empty() ? first_ : rest_.back();
- if (last.count_ == 0)
- {
- value_handler handler{last.value_};
- payload.receive(handler);
- last.count_ = 1;
- }
- else
- {
- boost::system::error_code value{last.value_};
- value_handler handler{value};
- payload.receive(handler);
- if (last.value_ == value)
- ++last.count_;
- else
- rest_.push_back({value, 1});
- }
- ++size_;
- }
-
- std::size_t buffer_push_n(std::size_t count, payload_type payload)
- {
- std::size_t available = this->max_buffer_size_ - size_;
- count = (count < available) ? count : available;
- if (count > 0)
- {
- buffered_value& last = rest_.empty() ? first_ : rest_.back();
- if (last.count_ == 0)
- {
- payload.receive(value_handler{last.value_});
- last.count_ = count;
- }
- else
- {
- boost::system::error_code value{last.value_};
- payload.receive(value_handler{value});
- if (last.value_ == value)
- last.count_ += count;
- else
- rest_.push_back({value, count});
- }
- size_ += count;
- }
- return count;
- }
-
- payload_type buffer_front()
- {
- return payload_type({0, first_.value_});
- }
-
- void buffer_pop()
- {
- --size_;
- if (--first_.count_ == 0 && !rest_.empty())
- {
- first_ = rest_.front();
- rest_.pop_front();
- }
- }
-
- void buffer_clear()
- {
- size_ = 0;
- first_.count_ == 0;
- rest_.clear();
- }
- private:
- struct buffered_value
- {
- boost::system::error_code value_;
- std::size_t count_;
- };
- struct value_handler
- {
- boost::system::error_code& target_;
- template <typename... Args>
- void operator()(const boost::system::error_code& value, Args&&...)
- {
- target_ = value;
- }
- };
- buffered_value& last_value()
- {
- return rest_.empty() ? first_ : rest_.back();
- }
-
- std::size_t size_;
-
-
- buffered_value first_;
-
- typename traits_type::template container<buffered_value>::type rest_;
- };
- }
- }
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #include <boost/asio/experimental/detail/impl/channel_service.hpp>
- #endif
|