123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- //
- // reactive_descriptor_service.hpp
- // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff 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)
- //
- #ifndef BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
- #define BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/push_options.hpp>
- #include <boost/asio/buffer.hpp>
- #include <boost/asio/error.hpp>
- #include <boost/asio/io_service.hpp>
- #include <boost/asio/detail/bind_handler.hpp>
- #include <boost/asio/detail/handler_base_from_member.hpp>
- #include <boost/asio/detail/noncopyable.hpp>
- #include <boost/asio/detail/service_base.hpp>
- #include <boost/asio/detail/descriptor_ops.hpp>
- #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
- namespace boost {
- namespace asio {
- namespace detail {
- template <typename Reactor>
- class reactive_descriptor_service
- : public boost::asio::detail::service_base<
- reactive_descriptor_service<Reactor> >
- {
- public:
- // The native type of a descriptor.
- typedef int native_type;
- // The implementation type of the descriptor.
- class implementation_type
- : private boost::asio::detail::noncopyable
- {
- public:
- // Default constructor.
- implementation_type()
- : descriptor_(-1),
- flags_(0)
- {
- }
- private:
- // Only this service will have access to the internal values.
- friend class reactive_descriptor_service<Reactor>;
- // The native descriptor representation.
- int descriptor_;
- enum
- {
- user_set_non_blocking = 1, // The user wants a non-blocking descriptor.
- internal_non_blocking = 2 // The descriptor has been set non-blocking.
- };
- // Flags indicating the current state of the descriptor.
- unsigned char flags_;
- // Per-descriptor data used by the reactor.
- typename Reactor::per_descriptor_data reactor_data_;
- };
- // The maximum number of buffers to support in a single operation.
- enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len };
- // Constructor.
- reactive_descriptor_service(boost::asio::io_service& io_service)
- : boost::asio::detail::service_base<
- reactive_descriptor_service<Reactor> >(io_service),
- reactor_(boost::asio::use_service<Reactor>(io_service))
- {
- reactor_.init_task();
- }
- // Destroy all user-defined handler objects owned by the service.
- void shutdown_service()
- {
- }
- // Construct a new descriptor implementation.
- void construct(implementation_type& impl)
- {
- impl.descriptor_ = -1;
- impl.flags_ = 0;
- }
- // Destroy a descriptor implementation.
- void destroy(implementation_type& impl)
- {
- if (impl.descriptor_ != -1)
- {
- reactor_.close_descriptor(impl.descriptor_, impl.reactor_data_);
- if (impl.flags_ & implementation_type::internal_non_blocking)
- {
- ioctl_arg_type non_blocking = 0;
- boost::system::error_code ignored_ec;
- descriptor_ops::ioctl(impl.descriptor_,
- FIONBIO, &non_blocking, ignored_ec);
- impl.flags_ &= ~implementation_type::internal_non_blocking;
- }
- boost::system::error_code ignored_ec;
- descriptor_ops::close(impl.descriptor_, ignored_ec);
- impl.descriptor_ = -1;
- }
- }
- // Assign a native descriptor to a descriptor implementation.
- boost::system::error_code assign(implementation_type& impl,
- const native_type& native_descriptor, boost::system::error_code& ec)
- {
- if (is_open(impl))
- {
- ec = boost::asio::error::already_open;
- return ec;
- }
- if (int err = reactor_.register_descriptor(
- native_descriptor, impl.reactor_data_))
- {
- ec = boost::system::error_code(err,
- boost::asio::error::get_system_category());
- return ec;
- }
- impl.descriptor_ = native_descriptor;
- impl.flags_ = 0;
- ec = boost::system::error_code();
- return ec;
- }
- // Determine whether the descriptor is open.
- bool is_open(const implementation_type& impl) const
- {
- return impl.descriptor_ != -1;
- }
- // Destroy a descriptor implementation.
- boost::system::error_code close(implementation_type& impl,
- boost::system::error_code& ec)
- {
- if (is_open(impl))
- {
- reactor_.close_descriptor(impl.descriptor_, impl.reactor_data_);
- if (impl.flags_ & implementation_type::internal_non_blocking)
- {
- ioctl_arg_type non_blocking = 0;
- boost::system::error_code ignored_ec;
- descriptor_ops::ioctl(impl.descriptor_,
- FIONBIO, &non_blocking, ignored_ec);
- impl.flags_ &= ~implementation_type::internal_non_blocking;
- }
- if (descriptor_ops::close(impl.descriptor_, ec) == -1)
- return ec;
- impl.descriptor_ = -1;
- }
- ec = boost::system::error_code();
- return ec;
- }
- // Get the native descriptor representation.
- native_type native(const implementation_type& impl) const
- {
- return impl.descriptor_;
- }
- // Cancel all operations associated with the descriptor.
- boost::system::error_code cancel(implementation_type& impl,
- boost::system::error_code& ec)
- {
- if (!is_open(impl))
- {
- ec = boost::asio::error::bad_descriptor;
- return ec;
- }
- reactor_.cancel_ops(impl.descriptor_, impl.reactor_data_);
- ec = boost::system::error_code();
- return ec;
- }
- // Perform an IO control command on the descriptor.
- template <typename IO_Control_Command>
- boost::system::error_code io_control(implementation_type& impl,
- IO_Control_Command& command, boost::system::error_code& ec)
- {
- if (!is_open(impl))
- {
- ec = boost::asio::error::bad_descriptor;
- return ec;
- }
- if (command.name() == static_cast<int>(FIONBIO))
- {
- if (command.get())
- impl.flags_ |= implementation_type::user_set_non_blocking;
- else
- impl.flags_ &= ~implementation_type::user_set_non_blocking;
- ec = boost::system::error_code();
- }
- else
- {
- descriptor_ops::ioctl(impl.descriptor_, command.name(),
- static_cast<ioctl_arg_type*>(command.data()), ec);
- }
- return ec;
- }
- // Write some data to the descriptor.
- template <typename ConstBufferSequence>
- size_t write_some(implementation_type& impl,
- const ConstBufferSequence& buffers, boost::system::error_code& ec)
- {
- if (!is_open(impl))
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // Copy buffers into array.
- descriptor_ops::buf bufs[max_buffers];
- typename ConstBufferSequence::const_iterator iter = buffers.begin();
- typename ConstBufferSequence::const_iterator end = buffers.end();
- size_t i = 0;
- size_t total_buffer_size = 0;
- for (; iter != end && i < max_buffers; ++iter, ++i)
- {
- boost::asio::const_buffer buffer(*iter);
- descriptor_ops::init_buf(bufs[i],
- boost::asio::buffer_cast<const void*>(buffer),
- boost::asio::buffer_size(buffer));
- total_buffer_size += boost::asio::buffer_size(buffer);
- }
- // A request to read_some 0 bytes on a stream is a no-op.
- if (total_buffer_size == 0)
- {
- ec = boost::system::error_code();
- return 0;
- }
- // Make descriptor non-blocking if user wants non-blocking.
- if (impl.flags_ & implementation_type::user_set_non_blocking)
- {
- if (!(impl.flags_ & implementation_type::internal_non_blocking))
- {
- ioctl_arg_type non_blocking = 1;
- if (descriptor_ops::ioctl(impl.descriptor_,
- FIONBIO, &non_blocking, ec))
- return 0;
- impl.flags_ |= implementation_type::internal_non_blocking;
- }
- }
- // Send the data.
- for (;;)
- {
- // Try to complete the operation without blocking.
- int bytes_sent = descriptor_ops::gather_write(
- impl.descriptor_, bufs, i, ec);
- // Check if operation succeeded.
- if (bytes_sent >= 0)
- return bytes_sent;
- // Operation failed.
- if ((impl.flags_ & implementation_type::user_set_non_blocking)
- || (ec != boost::asio::error::would_block
- && ec != boost::asio::error::try_again))
- return 0;
- // Wait for descriptor to become ready.
- if (descriptor_ops::poll_write(impl.descriptor_, ec) < 0)
- return 0;
- }
- }
- // Wait until data can be written without blocking.
- size_t write_some(implementation_type& impl,
- const null_buffers&, boost::system::error_code& ec)
- {
- if (!is_open(impl))
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // Wait for descriptor to become ready.
- descriptor_ops::poll_write(impl.descriptor_, ec);
- return 0;
- }
- template <typename ConstBufferSequence, typename Handler>
- class write_operation :
- public handler_base_from_member<Handler>
- {
- public:
- write_operation(int descriptor, boost::asio::io_service& io_service,
- const ConstBufferSequence& buffers, Handler handler)
- : handler_base_from_member<Handler>(handler),
- descriptor_(descriptor),
- io_service_(io_service),
- work_(io_service),
- buffers_(buffers)
- {
- }
- bool perform(boost::system::error_code& ec,
- std::size_t& bytes_transferred)
- {
- // Check whether the operation was successful.
- if (ec)
- {
- bytes_transferred = 0;
- return true;
- }
- // Copy buffers into array.
- descriptor_ops::buf bufs[max_buffers];
- typename ConstBufferSequence::const_iterator iter = buffers_.begin();
- typename ConstBufferSequence::const_iterator end = buffers_.end();
- size_t i = 0;
- for (; iter != end && i < max_buffers; ++iter, ++i)
- {
- boost::asio::const_buffer buffer(*iter);
- descriptor_ops::init_buf(bufs[i],
- boost::asio::buffer_cast<const void*>(buffer),
- boost::asio::buffer_size(buffer));
- }
- // Write the data.
- int bytes = descriptor_ops::gather_write(descriptor_, bufs, i, ec);
- // Check if we need to run the operation again.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- return false;
- bytes_transferred = (bytes < 0 ? 0 : bytes);
- return true;
- }
- void complete(const boost::system::error_code& ec,
- std::size_t bytes_transferred)
- {
- io_service_.post(bind_handler(this->handler_, ec, bytes_transferred));
- }
- private:
- int descriptor_;
- boost::asio::io_service& io_service_;
- boost::asio::io_service::work work_;
- ConstBufferSequence buffers_;
- };
- // Start an asynchronous write. The data being sent must be valid for the
- // lifetime of the asynchronous operation.
- template <typename ConstBufferSequence, typename Handler>
- void async_write_some(implementation_type& impl,
- const ConstBufferSequence& buffers, Handler handler)
- {
- if (!is_open(impl))
- {
- this->get_io_service().post(bind_handler(handler,
- boost::asio::error::bad_descriptor, 0));
- }
- else
- {
- // Determine total size of buffers.
- typename ConstBufferSequence::const_iterator iter = buffers.begin();
- typename ConstBufferSequence::const_iterator end = buffers.end();
- size_t i = 0;
- size_t total_buffer_size = 0;
- for (; iter != end && i < max_buffers; ++iter, ++i)
- {
- boost::asio::const_buffer buffer(*iter);
- total_buffer_size += boost::asio::buffer_size(buffer);
- }
- // A request to read_some 0 bytes on a stream is a no-op.
- if (total_buffer_size == 0)
- {
- this->get_io_service().post(bind_handler(handler,
- boost::system::error_code(), 0));
- return;
- }
- // Make descriptor non-blocking.
- if (!(impl.flags_ & implementation_type::internal_non_blocking))
- {
- ioctl_arg_type non_blocking = 1;
- boost::system::error_code ec;
- if (descriptor_ops::ioctl(impl.descriptor_, FIONBIO, &non_blocking, ec))
- {
- this->get_io_service().post(bind_handler(handler, ec, 0));
- return;
- }
- impl.flags_ |= implementation_type::internal_non_blocking;
- }
- reactor_.start_write_op(impl.descriptor_, impl.reactor_data_,
- write_operation<ConstBufferSequence, Handler>(
- impl.descriptor_, this->get_io_service(), buffers, handler));
- }
- }
- template <typename Handler>
- class null_buffers_operation :
- public handler_base_from_member<Handler>
- {
- public:
- null_buffers_operation(boost::asio::io_service& io_service, Handler handler)
- : handler_base_from_member<Handler>(handler),
- work_(io_service)
- {
- }
- bool perform(boost::system::error_code&,
- std::size_t& bytes_transferred)
- {
- bytes_transferred = 0;
- return true;
- }
- void complete(const boost::system::error_code& ec,
- std::size_t bytes_transferred)
- {
- work_.get_io_service().post(bind_handler(
- this->handler_, ec, bytes_transferred));
- }
- private:
- boost::asio::io_service::work work_;
- };
- // Start an asynchronous wait until data can be written without blocking.
- template <typename Handler>
- void async_write_some(implementation_type& impl,
- const null_buffers&, Handler handler)
- {
- if (!is_open(impl))
- {
- this->get_io_service().post(bind_handler(handler,
- boost::asio::error::bad_descriptor, 0));
- }
- else
- {
- reactor_.start_write_op(impl.descriptor_, impl.reactor_data_,
- null_buffers_operation<Handler>(this->get_io_service(), handler),
- false);
- }
- }
- // Read some data from the stream. Returns the number of bytes read.
- template <typename MutableBufferSequence>
- size_t read_some(implementation_type& impl,
- const MutableBufferSequence& buffers, boost::system::error_code& ec)
- {
- if (!is_open(impl))
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // Copy buffers into array.
- descriptor_ops::buf bufs[max_buffers];
- typename MutableBufferSequence::const_iterator iter = buffers.begin();
- typename MutableBufferSequence::const_iterator end = buffers.end();
- size_t i = 0;
- size_t total_buffer_size = 0;
- for (; iter != end && i < max_buffers; ++iter, ++i)
- {
- boost::asio::mutable_buffer buffer(*iter);
- descriptor_ops::init_buf(bufs[i],
- boost::asio::buffer_cast<void*>(buffer),
- boost::asio::buffer_size(buffer));
- total_buffer_size += boost::asio::buffer_size(buffer);
- }
- // A request to read_some 0 bytes on a stream is a no-op.
- if (total_buffer_size == 0)
- {
- ec = boost::system::error_code();
- return 0;
- }
- // Make descriptor non-blocking if user wants non-blocking.
- if (impl.flags_ & implementation_type::user_set_non_blocking)
- {
- if (!(impl.flags_ & implementation_type::internal_non_blocking))
- {
- ioctl_arg_type non_blocking = 1;
- if (descriptor_ops::ioctl(impl.descriptor_, FIONBIO, &non_blocking, ec))
- return 0;
- impl.flags_ |= implementation_type::internal_non_blocking;
- }
- }
- // Read some data.
- for (;;)
- {
- // Try to complete the operation without blocking.
- int bytes_read = descriptor_ops::scatter_read(
- impl.descriptor_, bufs, i, ec);
- // Check if operation succeeded.
- if (bytes_read > 0)
- return bytes_read;
- // Check for EOF.
- if (bytes_read == 0)
- {
- ec = boost::asio::error::eof;
- return 0;
- }
- // Operation failed.
- if ((impl.flags_ & implementation_type::user_set_non_blocking)
- || (ec != boost::asio::error::would_block
- && ec != boost::asio::error::try_again))
- return 0;
- // Wait for descriptor to become ready.
- if (descriptor_ops::poll_read(impl.descriptor_, ec) < 0)
- return 0;
- }
- }
- // Wait until data can be read without blocking.
- size_t read_some(implementation_type& impl,
- const null_buffers&, boost::system::error_code& ec)
- {
- if (!is_open(impl))
- {
- ec = boost::asio::error::bad_descriptor;
- return 0;
- }
- // Wait for descriptor to become ready.
- descriptor_ops::poll_read(impl.descriptor_, ec);
- return 0;
- }
- template <typename MutableBufferSequence, typename Handler>
- class read_operation :
- public handler_base_from_member<Handler>
- {
- public:
- read_operation(int descriptor, boost::asio::io_service& io_service,
- const MutableBufferSequence& buffers, Handler handler)
- : handler_base_from_member<Handler>(handler),
- descriptor_(descriptor),
- io_service_(io_service),
- work_(io_service),
- buffers_(buffers)
- {
- }
- bool perform(boost::system::error_code& ec,
- std::size_t& bytes_transferred)
- {
- // Check whether the operation was successful.
- if (ec)
- {
- bytes_transferred = 0;
- return true;
- }
- // Copy buffers into array.
- descriptor_ops::buf bufs[max_buffers];
- typename MutableBufferSequence::const_iterator iter = buffers_.begin();
- typename MutableBufferSequence::const_iterator end = buffers_.end();
- size_t i = 0;
- for (; iter != end && i < max_buffers; ++iter, ++i)
- {
- boost::asio::mutable_buffer buffer(*iter);
- descriptor_ops::init_buf(bufs[i],
- boost::asio::buffer_cast<void*>(buffer),
- boost::asio::buffer_size(buffer));
- }
- // Read some data.
- int bytes = descriptor_ops::scatter_read(descriptor_, bufs, i, ec);
- if (bytes == 0)
- ec = boost::asio::error::eof;
- // Check if we need to run the operation again.
- if (ec == boost::asio::error::would_block
- || ec == boost::asio::error::try_again)
- return false;
- bytes_transferred = (bytes < 0 ? 0 : bytes);
- return true;
- }
- void complete(const boost::system::error_code& ec,
- std::size_t bytes_transferred)
- {
- io_service_.post(bind_handler(this->handler_, ec, bytes_transferred));
- }
- private:
- int descriptor_;
- boost::asio::io_service& io_service_;
- boost::asio::io_service::work work_;
- MutableBufferSequence buffers_;
- };
- // Start an asynchronous read. The buffer for the data being read must be
- // valid for the lifetime of the asynchronous operation.
- template <typename MutableBufferSequence, typename Handler>
- void async_read_some(implementation_type& impl,
- const MutableBufferSequence& buffers, Handler handler)
- {
- if (!is_open(impl))
- {
- this->get_io_service().post(bind_handler(handler,
- boost::asio::error::bad_descriptor, 0));
- }
- else
- {
- // Determine total size of buffers.
- typename MutableBufferSequence::const_iterator iter = buffers.begin();
- typename MutableBufferSequence::const_iterator end = buffers.end();
- size_t i = 0;
- size_t total_buffer_size = 0;
- for (; iter != end && i < max_buffers; ++iter, ++i)
- {
- boost::asio::mutable_buffer buffer(*iter);
- total_buffer_size += boost::asio::buffer_size(buffer);
- }
- // A request to read_some 0 bytes on a stream is a no-op.
- if (total_buffer_size == 0)
- {
- this->get_io_service().post(bind_handler(handler,
- boost::system::error_code(), 0));
- return;
- }
- // Make descriptor non-blocking.
- if (!(impl.flags_ & implementation_type::internal_non_blocking))
- {
- ioctl_arg_type non_blocking = 1;
- boost::system::error_code ec;
- if (descriptor_ops::ioctl(impl.descriptor_, FIONBIO, &non_blocking, ec))
- {
- this->get_io_service().post(bind_handler(handler, ec, 0));
- return;
- }
- impl.flags_ |= implementation_type::internal_non_blocking;
- }
- reactor_.start_read_op(impl.descriptor_, impl.reactor_data_,
- read_operation<MutableBufferSequence, Handler>(
- impl.descriptor_, this->get_io_service(), buffers, handler));
- }
- }
- // Wait until data can be read without blocking.
- template <typename Handler>
- void async_read_some(implementation_type& impl,
- const null_buffers&, Handler handler)
- {
- if (!is_open(impl))
- {
- this->get_io_service().post(bind_handler(handler,
- boost::asio::error::bad_descriptor, 0));
- }
- else
- {
- reactor_.start_read_op(impl.descriptor_, impl.reactor_data_,
- null_buffers_operation<Handler>(this->get_io_service(), handler),
- false);
- }
- }
- private:
- // The selector that performs event demultiplexing for the service.
- Reactor& reactor_;
- };
- } // namespace detail
- } // namespace asio
- } // namespace boost
- #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
- #include <boost/asio/detail/pop_options.hpp>
- #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
|