io_service.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. //
  2. // io_service.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_IO_SERVICE_HPP
  11. #define ASIO_IO_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include <stdexcept>
  18. #include <typeinfo>
  19. #include "asio/detail/noncopyable.hpp"
  20. #include "asio/detail/service_registry_fwd.hpp"
  21. #include "asio/detail/wrapped_handler.hpp"
  22. #include "asio/error_code.hpp"
  23. #if defined(ASIO_HAS_IOCP)
  24. # include "asio/detail/win_iocp_io_service_fwd.hpp"
  25. #else
  26. # include "asio/detail/task_io_service_fwd.hpp"
  27. #endif
  28. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  29. # include "asio/detail/winsock_init.hpp"
  30. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  31. || defined(__osf__)
  32. # include "asio/detail/signal_init.hpp"
  33. #endif
  34. #include "asio/detail/push_options.hpp"
  35. namespace asio {
  36. class io_service;
  37. template <typename Service> Service& use_service(io_service& ios);
  38. template <typename Service> void add_service(io_service& ios, Service* svc);
  39. template <typename Service> bool has_service(io_service& ios);
  40. #if defined(ASIO_HAS_IOCP)
  41. namespace detail { typedef win_iocp_io_service io_service_impl; }
  42. #else
  43. namespace detail { typedef task_io_service io_service_impl; }
  44. #endif
  45. /// Provides core I/O functionality.
  46. /**
  47. * The io_service class provides the core I/O functionality for users of the
  48. * asynchronous I/O objects, including:
  49. *
  50. * @li asio::ip::tcp::socket
  51. * @li asio::ip::tcp::acceptor
  52. * @li asio::ip::udp::socket
  53. * @li asio::deadline_timer.
  54. *
  55. * The io_service class also includes facilities intended for developers of
  56. * custom asynchronous services.
  57. *
  58. * @par Thread Safety
  59. * @e Distinct @e objects: Safe.@n
  60. * @e Shared @e objects: Safe, with the exception that calling reset() while
  61. * there are unfinished run(), run_one(), poll() or poll_one() calls results in
  62. * undefined behaviour.
  63. *
  64. * @par Concepts:
  65. * Dispatcher.
  66. *
  67. * @par Synchronous and asynchronous operations
  68. *
  69. * Synchronous operations on I/O objects implicitly run the io_service object
  70. * for an individual operation. The io_service functions run(), run_one(),
  71. * poll() or poll_one() must be called for the io_service to perform
  72. * asynchronous operations on behalf of a C++ program. Notification that an
  73. * asynchronous operation has completed is delivered by invocation of the
  74. * associated handler. Handlers are invoked only by a thread that is currently
  75. * calling any overload of run(), run_one(), poll() or poll_one() for the
  76. * io_service.
  77. *
  78. * @par Effect of exceptions thrown from handlers
  79. *
  80. * If an exception is thrown from a handler, the exception is allowed to
  81. * propagate through the throwing thread's invocation of run(), run_one(),
  82. * poll() or poll_one(). No other threads that are calling any of these
  83. * functions are affected. It is then the responsibility of the application to
  84. * catch the exception.
  85. *
  86. * After the exception has been caught, the run(), run_one(), poll() or
  87. * poll_one() call may be restarted @em without the need for an intervening
  88. * call to reset(). This allows the thread to rejoin the io_service object's
  89. * thread pool without impacting any other threads in the pool.
  90. *
  91. * For example:
  92. *
  93. * @code
  94. * asio::io_service io_service;
  95. * ...
  96. * for (;;)
  97. * {
  98. * try
  99. * {
  100. * io_service.run();
  101. * break; // run() exited normally
  102. * }
  103. * catch (my_exception& e)
  104. * {
  105. * // Deal with exception as appropriate.
  106. * }
  107. * }
  108. * @endcode
  109. *
  110. * @par Stopping the io_service from running out of work
  111. *
  112. * Some applications may need to prevent an io_service object's run() call from
  113. * returning when there is no more work to do. For example, the io_service may
  114. * be being run in a background thread that is launched prior to the
  115. * application's asynchronous operations. The run() call may be kept running by
  116. * creating an object of type asio::io_service::work:
  117. *
  118. * @code asio::io_service io_service;
  119. * asio::io_service::work work(io_service);
  120. * ... @endcode
  121. *
  122. * To effect a shutdown, the application will then need to call the io_service
  123. * object's stop() member function. This will cause the io_service run() call
  124. * to return as soon as possible, abandoning unfinished operations and without
  125. * permitting ready handlers to be dispatched.
  126. *
  127. * Alternatively, if the application requires that all operations and handlers
  128. * be allowed to finish normally, the work object may be explicitly destroyed.
  129. *
  130. * @code asio::io_service io_service;
  131. * auto_ptr<asio::io_service::work> work(
  132. * new asio::io_service::work(io_service));
  133. * ...
  134. * work.reset(); // Allow run() to exit. @endcode
  135. *
  136. * @par The io_service class and I/O services
  137. *
  138. * Class io_service implements an extensible, type-safe, polymorphic set of I/O
  139. * services, indexed by service type. An object of class io_service must be
  140. * initialised before I/O objects such as sockets, resolvers and timers can be
  141. * used. These I/O objects are distinguished by having constructors that accept
  142. * an @c io_service& parameter.
  143. *
  144. * I/O services exist to manage the logical interface to the operating system on
  145. * behalf of the I/O objects. In particular, there are resources that are shared
  146. * across a class of I/O objects. For example, timers may be implemented in
  147. * terms of a single timer queue. The I/O services manage these shared
  148. * resources.
  149. *
  150. * Access to the services of an io_service is via three function templates,
  151. * use_service(), add_service() and has_service().
  152. *
  153. * In a call to @c use_service<Service>(), the type argument chooses a service,
  154. * making available all members of the named type. If @c Service is not present
  155. * in an io_service, an object of type @c Service is created and added to the
  156. * io_service. A C++ program can check if an io_service implements a
  157. * particular service with the function template @c has_service<Service>().
  158. *
  159. * Service objects may be explicitly added to an io_service using the function
  160. * template @c add_service<Service>(). If the @c Service is already present, the
  161. * service_already_exists exception is thrown. If the owner of the service is
  162. * not the same object as the io_service parameter, the invalid_service_owner
  163. * exception is thrown.
  164. *
  165. * Once a service reference is obtained from an io_service object by calling
  166. * use_service(), that reference remains usable as long as the owning io_service
  167. * object exists.
  168. *
  169. * All I/O service implementations have io_service::service as a public base
  170. * class. Custom I/O services may be implemented by deriving from this class and
  171. * then added to an io_service using the facilities described above.
  172. */
  173. class io_service
  174. : private noncopyable
  175. {
  176. private:
  177. typedef detail::io_service_impl impl_type;
  178. #if defined(ASIO_HAS_IOCP)
  179. friend class detail::win_iocp_overlapped_ptr;
  180. #endif
  181. public:
  182. class work;
  183. friend class work;
  184. class id;
  185. class service;
  186. class strand;
  187. /// Constructor.
  188. ASIO_DECL io_service();
  189. /// Constructor.
  190. /**
  191. * Construct with a hint about the required level of concurrency.
  192. *
  193. * @param concurrency_hint A suggestion to the implementation on how many
  194. * threads it should allow to run simultaneously.
  195. */
  196. ASIO_DECL explicit io_service(std::size_t concurrency_hint);
  197. /// Destructor.
  198. /**
  199. * On destruction, the io_service performs the following sequence of
  200. * operations:
  201. *
  202. * @li For each service object @c svc in the io_service set, in reverse order
  203. * of the beginning of service object lifetime, performs
  204. * @c svc->shutdown_service().
  205. *
  206. * @li Uninvoked handler objects that were scheduled for deferred invocation
  207. * on the io_service, or any associated strand, are destroyed.
  208. *
  209. * @li For each service object @c svc in the io_service set, in reverse order
  210. * of the beginning of service object lifetime, performs
  211. * <tt>delete static_cast<io_service::service*>(svc)</tt>.
  212. *
  213. * @note The destruction sequence described above permits programs to
  214. * simplify their resource management by using @c shared_ptr<>. Where an
  215. * object's lifetime is tied to the lifetime of a connection (or some other
  216. * sequence of asynchronous operations), a @c shared_ptr to the object would
  217. * be bound into the handlers for all asynchronous operations associated with
  218. * it. This works as follows:
  219. *
  220. * @li When a single connection ends, all associated asynchronous operations
  221. * complete. The corresponding handler objects are destroyed, and all
  222. * @c shared_ptr references to the objects are destroyed.
  223. *
  224. * @li To shut down the whole program, the io_service function stop() is
  225. * called to terminate any run() calls as soon as possible. The io_service
  226. * destructor defined above destroys all handlers, causing all @c shared_ptr
  227. * references to all connection objects to be destroyed.
  228. */
  229. ASIO_DECL ~io_service();
  230. /// Run the io_service object's event processing loop.
  231. /**
  232. * The run() function blocks until all work has finished and there are no
  233. * more handlers to be dispatched, or until the io_service has been stopped.
  234. *
  235. * Multiple threads may call the run() function to set up a pool of threads
  236. * from which the io_service may execute handlers. All threads that are
  237. * waiting in the pool are equivalent and the io_service may choose any one
  238. * of them to invoke a handler.
  239. *
  240. * The run() function may be safely called again once it has completed only
  241. * after a call to reset().
  242. *
  243. * @return The number of handlers that were executed.
  244. *
  245. * @throws asio::system_error Thrown on failure.
  246. *
  247. * @note The run() function must not be called from a thread that is currently
  248. * calling one of run(), run_one(), poll() or poll_one() on the same
  249. * io_service object.
  250. *
  251. * The poll() function may also be used to dispatch ready handlers, but
  252. * without blocking.
  253. */
  254. ASIO_DECL std::size_t run();
  255. /// Run the io_service object's event processing loop.
  256. /**
  257. * The run() function blocks until all work has finished and there are no
  258. * more handlers to be dispatched, or until the io_service has been stopped.
  259. *
  260. * Multiple threads may call the run() function to set up a pool of threads
  261. * from which the io_service may execute handlers. All threads that are
  262. * waiting in the pool are equivalent and the io_service may choose any one
  263. * of them to invoke a handler.
  264. *
  265. * The run() function may be safely called again once it has completed only
  266. * after a call to reset().
  267. *
  268. * @param ec Set to indicate what error occurred, if any.
  269. *
  270. * @return The number of handlers that were executed.
  271. *
  272. * @note The run() function must not be called from a thread that is currently
  273. * calling one of run(), run_one(), poll() or poll_one() on the same
  274. * io_service object.
  275. *
  276. * The poll() function may also be used to dispatch ready handlers, but
  277. * without blocking.
  278. */
  279. ASIO_DECL std::size_t run(asio::error_code& ec);
  280. /// Run the io_service object's event processing loop to execute at most one
  281. /// handler.
  282. /**
  283. * The run_one() function blocks until one handler has been dispatched, or
  284. * until the io_service has been stopped.
  285. *
  286. * @return The number of handlers that were executed.
  287. *
  288. * @throws asio::system_error Thrown on failure.
  289. */
  290. ASIO_DECL std::size_t run_one();
  291. /// Run the io_service object's event processing loop to execute at most one
  292. /// handler.
  293. /**
  294. * The run_one() function blocks until one handler has been dispatched, or
  295. * until the io_service has been stopped.
  296. *
  297. * @param ec Set to indicate what error occurred, if any.
  298. *
  299. * @return The number of handlers that were executed.
  300. */
  301. ASIO_DECL std::size_t run_one(asio::error_code& ec);
  302. /// Run the io_service object's event processing loop to execute ready
  303. /// handlers.
  304. /**
  305. * The poll() function runs handlers that are ready to run, without blocking,
  306. * until the io_service has been stopped or there are no more ready handlers.
  307. *
  308. * @return The number of handlers that were executed.
  309. *
  310. * @throws asio::system_error Thrown on failure.
  311. */
  312. ASIO_DECL std::size_t poll();
  313. /// Run the io_service object's event processing loop to execute ready
  314. /// handlers.
  315. /**
  316. * The poll() function runs handlers that are ready to run, without blocking,
  317. * until the io_service has been stopped or there are no more ready handlers.
  318. *
  319. * @param ec Set to indicate what error occurred, if any.
  320. *
  321. * @return The number of handlers that were executed.
  322. */
  323. ASIO_DECL std::size_t poll(asio::error_code& ec);
  324. /// Run the io_service object's event processing loop to execute one ready
  325. /// handler.
  326. /**
  327. * The poll_one() function runs at most one handler that is ready to run,
  328. * without blocking.
  329. *
  330. * @return The number of handlers that were executed.
  331. *
  332. * @throws asio::system_error Thrown on failure.
  333. */
  334. ASIO_DECL std::size_t poll_one();
  335. /// Run the io_service object's event processing loop to execute one ready
  336. /// handler.
  337. /**
  338. * The poll_one() function runs at most one handler that is ready to run,
  339. * without blocking.
  340. *
  341. * @param ec Set to indicate what error occurred, if any.
  342. *
  343. * @return The number of handlers that were executed.
  344. */
  345. ASIO_DECL std::size_t poll_one(asio::error_code& ec);
  346. /// Stop the io_service object's event processing loop.
  347. /**
  348. * This function does not block, but instead simply signals the io_service to
  349. * stop. All invocations of its run() or run_one() member functions should
  350. * return as soon as possible. Subsequent calls to run(), run_one(), poll()
  351. * or poll_one() will return immediately until reset() is called.
  352. */
  353. ASIO_DECL void stop();
  354. /// Reset the io_service in preparation for a subsequent run() invocation.
  355. /**
  356. * This function must be called prior to any second or later set of
  357. * invocations of the run(), run_one(), poll() or poll_one() functions when a
  358. * previous invocation of these functions returned due to the io_service
  359. * being stopped or running out of work. This function allows the io_service
  360. * to reset any internal state, such as a "stopped" flag.
  361. *
  362. * This function must not be called while there are any unfinished calls to
  363. * the run(), run_one(), poll() or poll_one() functions.
  364. */
  365. ASIO_DECL void reset();
  366. /// Request the io_service to invoke the given handler.
  367. /**
  368. * This function is used to ask the io_service to execute the given handler.
  369. *
  370. * The io_service guarantees that the handler will only be called in a thread
  371. * in which the run(), run_one(), poll() or poll_one() member functions is
  372. * currently being invoked. The handler may be executed inside this function
  373. * if the guarantee can be met.
  374. *
  375. * @param handler The handler to be called. The io_service will make
  376. * a copy of the handler object as required. The function signature of the
  377. * handler must be: @code void handler(); @endcode
  378. *
  379. * @note This function throws an exception only if:
  380. *
  381. * @li the handler's @c asio_handler_allocate function; or
  382. *
  383. * @li the handler's copy constructor
  384. *
  385. * throws an exception.
  386. */
  387. template <typename CompletionHandler>
  388. void dispatch(CompletionHandler handler);
  389. /// Request the io_service to invoke the given handler and return immediately.
  390. /**
  391. * This function is used to ask the io_service to execute the given handler,
  392. * but without allowing the io_service to call the handler from inside this
  393. * function.
  394. *
  395. * The io_service guarantees that the handler will only be called in a thread
  396. * in which the run(), run_one(), poll() or poll_one() member functions is
  397. * currently being invoked.
  398. *
  399. * @param handler The handler to be called. The io_service will make
  400. * a copy of the handler object as required. The function signature of the
  401. * handler must be: @code void handler(); @endcode
  402. *
  403. * @note This function throws an exception only if:
  404. *
  405. * @li the handler's @c asio_handler_allocate function; or
  406. *
  407. * @li the handler's copy constructor
  408. *
  409. * throws an exception.
  410. */
  411. template <typename CompletionHandler>
  412. void post(CompletionHandler handler);
  413. /// Create a new handler that automatically dispatches the wrapped handler
  414. /// on the io_service.
  415. /**
  416. * This function is used to create a new handler function object that, when
  417. * invoked, will automatically pass the wrapped handler to the io_service
  418. * object's dispatch function.
  419. *
  420. * @param handler The handler to be wrapped. The io_service will make a copy
  421. * of the handler object as required. The function signature of the handler
  422. * must be: @code void handler(A1 a1, ... An an); @endcode
  423. *
  424. * @return A function object that, when invoked, passes the wrapped handler to
  425. * the io_service object's dispatch function. Given a function object with the
  426. * signature:
  427. * @code R f(A1 a1, ... An an); @endcode
  428. * If this function object is passed to the wrap function like so:
  429. * @code io_service.wrap(f); @endcode
  430. * then the return value is a function object with the signature
  431. * @code void g(A1 a1, ... An an); @endcode
  432. * that, when invoked, executes code equivalent to:
  433. * @code io_service.dispatch(boost::bind(f, a1, ... an)); @endcode
  434. */
  435. template <typename Handler>
  436. #if defined(GENERATING_DOCUMENTATION)
  437. unspecified
  438. #else
  439. detail::wrapped_handler<io_service&, Handler>
  440. #endif
  441. wrap(Handler handler);
  442. /// Obtain the service object corresponding to the given type.
  443. /**
  444. * This function is used to locate a service object that corresponds to
  445. * the given service type. If there is no existing implementation of the
  446. * service, then the io_service will create a new instance of the service.
  447. *
  448. * @param ios The io_service object that owns the service.
  449. *
  450. * @return The service interface implementing the specified service type.
  451. * Ownership of the service interface is not transferred to the caller.
  452. */
  453. template <typename Service>
  454. friend Service& use_service(io_service& ios);
  455. /// Add a service object to the io_service.
  456. /**
  457. * This function is used to add a service to the io_service.
  458. *
  459. * @param ios The io_service object that owns the service.
  460. *
  461. * @param svc The service object. On success, ownership of the service object
  462. * is transferred to the io_service. When the io_service object is destroyed,
  463. * it will destroy the service object by performing:
  464. * @code delete static_cast<io_service::service*>(svc) @endcode
  465. *
  466. * @throws asio::service_already_exists Thrown if a service of the
  467. * given type is already present in the io_service.
  468. *
  469. * @throws asio::invalid_service_owner Thrown if the service's owning
  470. * io_service is not the io_service object specified by the ios parameter.
  471. */
  472. template <typename Service>
  473. friend void add_service(io_service& ios, Service* svc);
  474. /// Determine if an io_service contains a specified service type.
  475. /**
  476. * This function is used to determine whether the io_service contains a
  477. * service object corresponding to the given service type.
  478. *
  479. * @param ios The io_service object that owns the service.
  480. *
  481. * @return A boolean indicating whether the io_service contains the service.
  482. */
  483. template <typename Service>
  484. friend bool has_service(io_service& ios);
  485. private:
  486. #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
  487. detail::winsock_init<> init_;
  488. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  489. || defined(__osf__)
  490. detail::signal_init<> init_;
  491. #endif
  492. // The service registry.
  493. asio::detail::service_registry* service_registry_;
  494. // The implementation.
  495. impl_type& impl_;
  496. };
  497. /// Class to inform the io_service when it has work to do.
  498. /**
  499. * The work class is used to inform the io_service when work starts and
  500. * finishes. This ensures that the io_service object's run() function will not
  501. * exit while work is underway, and that it does exit when there is no
  502. * unfinished work remaining.
  503. *
  504. * The work class is copy-constructible so that it may be used as a data member
  505. * in a handler class. It is not assignable.
  506. */
  507. class io_service::work
  508. {
  509. public:
  510. /// Constructor notifies the io_service that work is starting.
  511. /**
  512. * The constructor is used to inform the io_service that some work has begun.
  513. * This ensures that the io_service object's run() function will not exit
  514. * while the work is underway.
  515. */
  516. explicit work(asio::io_service& io_service);
  517. /// Copy constructor notifies the io_service that work is starting.
  518. /**
  519. * The constructor is used to inform the io_service that some work has begun.
  520. * This ensures that the io_service object's run() function will not exit
  521. * while the work is underway.
  522. */
  523. work(const work& other);
  524. /// Destructor notifies the io_service that the work is complete.
  525. /**
  526. * The destructor is used to inform the io_service that some work has
  527. * finished. Once the count of unfinished work reaches zero, the io_service
  528. * object's run() function is permitted to exit.
  529. */
  530. ~work();
  531. /// (Deprecated: use get_io_service().) Get the io_service associated with the
  532. /// work.
  533. asio::io_service& io_service();
  534. /// Get the io_service associated with the work.
  535. asio::io_service& get_io_service();
  536. private:
  537. // Prevent assignment.
  538. void operator=(const work& other);
  539. // The io_service.
  540. asio::io_service& io_service_;
  541. };
  542. /// Class used to uniquely identify a service.
  543. class io_service::id
  544. : private noncopyable
  545. {
  546. public:
  547. /// Constructor.
  548. id() {}
  549. };
  550. /// Base class for all io_service services.
  551. class io_service::service
  552. : private noncopyable
  553. {
  554. public:
  555. /// (Deprecated: use get_io_service().) Get the io_service object that owns
  556. /// the service.
  557. asio::io_service& io_service();
  558. /// Get the io_service object that owns the service.
  559. asio::io_service& get_io_service();
  560. protected:
  561. /// Constructor.
  562. /**
  563. * @param owner The io_service object that owns the service.
  564. */
  565. ASIO_DECL service(asio::io_service& owner);
  566. /// Destructor.
  567. ASIO_DECL virtual ~service();
  568. private:
  569. /// Destroy all user-defined handler objects owned by the service.
  570. virtual void shutdown_service() = 0;
  571. friend class asio::detail::service_registry;
  572. struct key
  573. {
  574. key() : type_info_(0), id_(0) {}
  575. const std::type_info* type_info_;
  576. const asio::io_service::id* id_;
  577. } key_;
  578. asio::io_service& owner_;
  579. service* next_;
  580. };
  581. /// Exception thrown when trying to add a duplicate service to an io_service.
  582. class service_already_exists
  583. : public std::logic_error
  584. {
  585. public:
  586. ASIO_DECL service_already_exists();
  587. };
  588. /// Exception thrown when trying to add a service object to an io_service where
  589. /// the service has a different owner.
  590. class invalid_service_owner
  591. : public std::logic_error
  592. {
  593. public:
  594. ASIO_DECL invalid_service_owner();
  595. };
  596. namespace detail {
  597. // Special derived service id type to keep classes header-file only.
  598. template <typename Type>
  599. class service_id
  600. : public asio::io_service::id
  601. {
  602. };
  603. // Special service base class to keep classes header-file only.
  604. template <typename Type>
  605. class service_base
  606. : public asio::io_service::service
  607. {
  608. public:
  609. static asio::detail::service_id<Type> id;
  610. // Constructor.
  611. service_base(asio::io_service& io_service)
  612. : asio::io_service::service(io_service)
  613. {
  614. }
  615. };
  616. template <typename Type>
  617. asio::detail::service_id<Type> service_base<Type>::id;
  618. } // namespace detail
  619. } // namespace asio
  620. #include "asio/detail/pop_options.hpp"
  621. #include "asio/impl/io_service.hpp"
  622. #if defined(ASIO_HEADER_ONLY)
  623. # include "asio/impl/io_service.ipp"
  624. #endif // defined(ASIO_HEADER_ONLY)
  625. #endif // ASIO_IO_SERVICE_HPP