00001 // 00002 // io_service.hpp 00003 // ~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_IO_SERVICE_HPP 00012 #define ASIO_IO_SERVICE_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/detail/push_options.hpp" 00021 #include <cstddef> 00022 #include <stdexcept> 00023 #include <typeinfo> 00024 #include <boost/config.hpp> 00025 #include <boost/throw_exception.hpp> 00026 #include "asio/detail/pop_options.hpp" 00027 00028 #include "asio/error_code.hpp" 00029 #include "asio/detail/epoll_reactor_fwd.hpp" 00030 #include "asio/detail/kqueue_reactor_fwd.hpp" 00031 #include "asio/detail/noncopyable.hpp" 00032 #include "asio/detail/select_reactor_fwd.hpp" 00033 #include "asio/detail/service_registry_fwd.hpp" 00034 #include "asio/detail/signal_init.hpp" 00035 #include "asio/detail/task_io_service_fwd.hpp" 00036 #include "asio/detail/win_iocp_io_service_fwd.hpp" 00037 #include "asio/detail/winsock_init.hpp" 00038 #include "asio/detail/wrapped_handler.hpp" 00039 00040 namespace asio { 00041 00043 00065 class io_service 00066 : private noncopyable 00067 { 00068 private: 00069 // The type of the platform-specific implementation. 00070 #if defined(ASIO_HAS_IOCP) 00071 typedef detail::win_iocp_io_service impl_type; 00072 #elif defined(ASIO_HAS_EPOLL) 00073 typedef detail::task_io_service<detail::epoll_reactor<false> > impl_type; 00074 #elif defined(ASIO_HAS_KQUEUE) 00075 typedef detail::task_io_service<detail::kqueue_reactor<false> > impl_type; 00076 #else 00077 typedef detail::task_io_service<detail::select_reactor<false> > impl_type; 00078 #endif 00079 00080 public: 00081 class work; 00082 friend class work; 00083 00084 class id; 00085 00086 class service; 00087 00088 class strand; 00089 00091 io_service(); 00092 00094 00100 explicit io_service(std::size_t concurrency_hint); 00101 00103 ~io_service(); 00104 00106 00120 std::size_t run(); 00121 00123 00137 std::size_t run(asio::error_code& ec); 00138 00140 00148 std::size_t run_one(); 00149 00151 00159 std::size_t run_one(asio::error_code& ec); 00160 00162 00170 std::size_t poll(); 00171 00173 00181 std::size_t poll(asio::error_code& ec); 00182 00184 00192 std::size_t poll_one(); 00193 00195 00203 std::size_t poll_one(asio::error_code& ec); 00204 00206 00212 void stop(); 00213 00215 00225 void reset(); 00226 00228 00240 template <typename CompletionHandler> 00241 void dispatch(CompletionHandler handler); 00242 00244 00257 template <typename CompletionHandler> 00258 void post(CompletionHandler handler); 00259 00262 00282 template <typename Handler> 00283 #if defined(GENERATING_DOCUMENTATION) 00284 unspecified 00285 #else 00286 detail::wrapped_handler<io_service, Handler> 00287 #endif 00288 wrap(Handler handler); 00289 00291 00301 template <typename Service> 00302 friend Service& use_service(io_service& ios); 00303 00305 00321 template <typename Service> 00322 friend void add_service(io_service& ios, Service* svc); 00323 00325 00333 template <typename Service> 00334 friend bool has_service(io_service& ios); 00335 00336 private: 00337 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__) 00338 detail::winsock_init<> init_; 00339 #elif defined(__sun) || defined(__QNX__) 00340 detail::signal_init<> init_; 00341 #endif 00342 00343 // The service registry. 00344 asio::detail::service_registry* service_registry_; 00345 00346 // The implementation. 00347 impl_type& impl_; 00348 }; 00349 00351 00360 class io_service::work 00361 { 00362 public: 00364 00369 explicit work(asio::io_service& io_service); 00370 00372 00377 work(const work& other); 00378 00380 00385 ~work(); 00386 00388 asio::io_service& io_service(); 00389 00390 private: 00391 // Prevent assignment. 00392 void operator=(const work& other); 00393 00394 // The io_service. 00395 asio::io_service& io_service_; 00396 }; 00397 00399 class io_service::id 00400 : private noncopyable 00401 { 00402 public: 00404 id() {} 00405 }; 00406 00408 class io_service::service 00409 : private noncopyable 00410 { 00411 public: 00413 asio::io_service& io_service(); 00414 00415 protected: 00417 00420 service(asio::io_service& owner); 00421 00423 virtual ~service(); 00424 00425 private: 00427 virtual void shutdown_service() = 0; 00428 00429 friend class asio::detail::service_registry; 00430 asio::io_service& owner_; 00431 const std::type_info* type_info_; 00432 const asio::io_service::id* id_; 00433 service* next_; 00434 }; 00435 00437 class service_already_exists 00438 : public std::logic_error 00439 { 00440 public: 00441 service_already_exists() 00442 : std::logic_error("Service already exists.") 00443 { 00444 } 00445 }; 00446 00449 class invalid_service_owner 00450 : public std::logic_error 00451 { 00452 public: 00453 invalid_service_owner() 00454 : std::logic_error("Invalid service owner.") 00455 { 00456 } 00457 }; 00458 00495 } // namespace asio 00496 00497 #include "asio/impl/io_service.ipp" 00498 00499 #include "asio/detail/pop_options.hpp" 00500 00501 #endif // ASIO_IO_SERVICE_HPP
1.5.6