io_service.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <sys/types.h>
  16. #include <netinet/in.h>
  17. #include <sys/socket.h>
  18. #include <unistd.h> // for some IPC/network system calls
  19. #include <asio.hpp>
  20. #include <asiolink/io_service.h>
  21. namespace isc {
  22. namespace asiolink {
  23. class IOServiceImpl {
  24. private:
  25. IOServiceImpl(const IOService& source);
  26. IOServiceImpl& operator=(const IOService& source);
  27. public:
  28. /// \brief The constructor
  29. IOServiceImpl() :
  30. io_service_(),
  31. work_(io_service_)
  32. {};
  33. /// \brief The destructor.
  34. ~IOServiceImpl() {};
  35. //@}
  36. /// \brief Start the underlying event loop.
  37. ///
  38. /// This method does not return control to the caller until
  39. /// the \c stop() method is called via some handler.
  40. void run() { io_service_.run(); };
  41. /// \brief Run the underlying event loop for a single event.
  42. ///
  43. /// This method return control to the caller as soon as the
  44. /// first handler has completed. (If no handlers are ready when
  45. /// it is run, it will block until one is.)
  46. void run_one() { io_service_.run_one();} ;
  47. /// \brief Stop the underlying event loop.
  48. ///
  49. /// This will return the control to the caller of the \c run() method.
  50. void stop() { io_service_.stop();} ;
  51. /// \brief Return the native \c io_service object used in this wrapper.
  52. ///
  53. /// This is a short term work around to support other BIND 10 modules
  54. /// that share the same \c io_service with the authoritative server.
  55. /// It will eventually be removed once the wrapper interface is
  56. /// generalized.
  57. asio::io_service& get_io_service() { return io_service_; };
  58. private:
  59. asio::io_service io_service_;
  60. asio::io_service::work work_;
  61. };
  62. IOService::IOService() {
  63. io_impl_ = new IOServiceImpl();
  64. }
  65. IOService::~IOService() {
  66. delete io_impl_;
  67. }
  68. void
  69. IOService::run() {
  70. io_impl_->run();
  71. }
  72. void
  73. IOService::run_one() {
  74. io_impl_->run_one();
  75. }
  76. void
  77. IOService::stop() {
  78. io_impl_->stop();
  79. }
  80. asio::io_service&
  81. IOService::get_io_service() {
  82. return (io_impl_->get_io_service());
  83. }
  84. } // namespace asiolink
  85. } // namespace isc