📄 io_service.hpp
字号:
//// io_service.hpp// ~~~~~~~~~~~~~~//// Copyright (c) 2003-2007 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 ASIO_IO_SERVICE_HPP#define ASIO_IO_SERVICE_HPP#if defined(_MSC_VER) && (_MSC_VER >= 1200)# pragma once#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)#include "asio/detail/push_options.hpp"#include "asio/detail/push_options.hpp"#include <cstddef>#include <stdexcept>#include <typeinfo>#include <boost/config.hpp>#include <boost/throw_exception.hpp>#include "asio/detail/pop_options.hpp"#include "asio/error_code.hpp"#include "asio/detail/dev_poll_reactor_fwd.hpp"#include "asio/detail/epoll_reactor_fwd.hpp"#include "asio/detail/kqueue_reactor_fwd.hpp"#include "asio/detail/noncopyable.hpp"#include "asio/detail/select_reactor_fwd.hpp"#include "asio/detail/service_registry_fwd.hpp"#include "asio/detail/signal_init.hpp"#include "asio/detail/task_io_service_fwd.hpp"#include "asio/detail/win_iocp_io_service_fwd.hpp"#include "asio/detail/winsock_init.hpp"#include "asio/detail/wrapped_handler.hpp"namespace asio {class io_service;template <typename Service> Service& use_service(io_service& ios);template <typename Service> void add_service(io_service& ios, Service* svc);template <typename Service> bool has_service(io_service& ios);/// Provides core I/O functionality./** * The io_service class provides the core I/O functionality for users of the * asynchronous I/O objects, including: * * @li asio::ip::tcp::socket * @li asio::ip::tcp::acceptor * @li asio::ip::udp::socket * @li asio::deadline_timer. * * The io_service class also includes facilities intended for developers of * custom asynchronous services. * * @par Thread Safety * @e Distinct @e objects: Safe.@n * @e Shared @e objects: Safe, with the exception that calling reset() * while there are unfinished run() calls results in undefined behaviour. * * @par Concepts: * Dispatcher. * * @par Effect of exceptions thrown from handlers * * If an exception is thrown from a handler, the exception is allowed to * propagate through the throwing thread's invocation of * asio::io_service::run(), asio::io_service::run_one(), * asio::io_service::poll() or asio::io_service::poll_one(). * No other threads that are calling any of these functions are affected. It is * then the responsibility of the application to catch the exception. * * After the exception has been caught, the * asio::io_service::run(), asio::io_service::run_one(), * asio::io_service::poll() or asio::io_service::poll_one() * call may be restarted @em without the need for an intervening call to * asio::io_service::reset(). This allows the thread to rejoin the * io_service's thread pool without impacting any other threads in the pool. * * For example: * * @code * asio::io_service io_service; * ... * for (;;) * { * try * { * io_service.run(); * break; // run() exited normally * } * catch (my_exception& e) * { * // Deal with exception as appropriate. * } * } * @endcode */class io_service : private noncopyable{private: // The type of the platform-specific implementation.#if defined(ASIO_HAS_IOCP) typedef detail::win_iocp_io_service impl_type;#elif defined(ASIO_HAS_EPOLL) typedef detail::task_io_service<detail::epoll_reactor<false> > impl_type;#elif defined(ASIO_HAS_KQUEUE) typedef detail::task_io_service<detail::kqueue_reactor<false> > impl_type;#elif defined(ASIO_HAS_DEV_POLL) typedef detail::task_io_service<detail::dev_poll_reactor<false> > impl_type;#else typedef detail::task_io_service<detail::select_reactor<false> > impl_type;#endifpublic: class work; friend class work; class id; class service; class strand; /// Constructor. io_service(); /// Constructor. /** * Construct with a hint about the required level of concurrency. * * @param concurrency_hint A suggestion to the implementation on how many * threads it should allow to run simultaneously. */ explicit io_service(std::size_t concurrency_hint); /// Destructor. ~io_service(); /// Run the io_service's event processing loop. /** * The run() function blocks until all work has finished and there are no * more handlers to be dispatched, or until the io_service has been stopped. * * Multiple threads may call the run() function to set up a pool of threads * from which the io_service may execute handlers. All threads that are * waiting in the pool are equivalent and the io_service may choose any one * of them to invoke a handler. * * The run() function may be safely called again once it has completed only * after a call to reset(). * * @return The number of handlers that were executed. * * @throws asio::system_error Thrown on failure. */ std::size_t run(); /// Run the io_service's event processing loop. /** * The run() function blocks until all work has finished and there are no * more handlers to be dispatched, or until the io_service has been stopped. * * Multiple threads may call the run() function to set up a pool of threads * from which the io_service may execute handlers. All threads that are * waiting in the pool are equivalent and the io_service may choose any one * of them to invoke a handler. * * The run() function may be safely called again once it has completed only * after a call to reset(). * * @param ec Set to indicate what error occurred, if any. * * @return The number of handlers that were executed. */ std::size_t run(asio::error_code& ec); /// Run the io_service's event processing loop to execute at most one handler. /** * The run_one() function blocks until one handler has been dispatched, or * until the io_service has been stopped. * * @return The number of handlers that were executed. * * @throws asio::system_error Thrown on failure. */ std::size_t run_one(); /// Run the io_service's event processing loop to execute at most one handler. /** * The run_one() function blocks until one handler has been dispatched, or * until the io_service has been stopped. * * @param ec Set to indicate what error occurred, if any. * * @return The number of handlers that were executed. */ std::size_t run_one(asio::error_code& ec); /// Run the io_service's event processing loop to execute ready handlers. /** * The poll() function runs handlers that are ready to run, without blocking, * until the io_service has been stopped or there are no more ready handlers. * * @return The number of handlers that were executed. * * @throws asio::system_error Thrown on failure. */ std::size_t poll(); /// Run the io_service's event processing loop to execute ready handlers. /** * The poll() function runs handlers that are ready to run, without blocking, * until the io_service has been stopped or there are no more ready handlers. * * @param ec Set to indicate what error occurred, if any. * * @return The number of handlers that were executed. */ std::size_t poll(asio::error_code& ec); /// Run the io_service's event processing loop to execute one ready handler. /** * The poll_one() function runs at most one handler that is ready to run, * without blocking. * * @return The number of handlers that were executed. * * @throws asio::system_error Thrown on failure. */ std::size_t poll_one(); /// Run the io_service's event processing loop to execute one ready handler. /** * The poll_one() function runs at most one handler that is ready to run, * without blocking. * * @param ec Set to indicate what error occurred, if any. * * @return The number of handlers that were executed. */ std::size_t poll_one(asio::error_code& ec); /// Stop the io_service's event processing loop. /** * This function does not block, but instead simply signals the io_service to * stop. All invocations of its run() or run_one() member functions should * return as soon as possible. Subsequent calls to run(), run_one(), poll() * or poll_one() will return immediately until reset() is called. */ void stop(); /// Reset the io_service in preparation for a subsequent run() invocation. /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -