io_service.cc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <netinet/in.h>
  15. #include <sys/socket.h>
  16. #include <unistd.h> // for some IPC/network system calls
  17. #include <config.h>
  18. #include <asio.hpp>
  19. #include <asiolink/io_service.h>
  20. namespace asiolink {
  21. class IOServiceImpl {
  22. private:
  23. IOServiceImpl(const IOService& source);
  24. IOServiceImpl& operator=(const IOService& source);
  25. public:
  26. /// \brief The constructor
  27. IOServiceImpl() :
  28. io_service_(),
  29. work_(io_service_)
  30. {};
  31. /// \brief The destructor.
  32. ~IOServiceImpl() {};
  33. //@}
  34. /// \brief Start the underlying event loop.
  35. ///
  36. /// This method does not return control to the caller until
  37. /// the \c stop() method is called via some handler.
  38. void run() { io_service_.run(); };
  39. /// \brief Run the underlying event loop for a single event.
  40. ///
  41. /// This method return control to the caller as soon as the
  42. /// first handler has completed. (If no handlers are ready when
  43. /// it is run, it will block until one is.)
  44. void run_one() { io_service_.run_one();} ;
  45. /// \brief Stop the underlying event loop.
  46. ///
  47. /// This will return the control to the caller of the \c run() method.
  48. void stop() { io_service_.stop();} ;
  49. /// \brief Return the native \c io_service object used in this wrapper.
  50. ///
  51. /// This is a short term work around to support other BIND 10 modules
  52. /// that share the same \c io_service with the authoritative server.
  53. /// It will eventually be removed once the wrapper interface is
  54. /// generalized.
  55. asio::io_service& get_io_service() { return io_service_; };
  56. private:
  57. asio::io_service io_service_;
  58. asio::io_service::work work_;
  59. };
  60. IOService::IOService() {
  61. io_impl_ = new IOServiceImpl();
  62. }
  63. IOService::~IOService() {
  64. delete io_impl_;
  65. }
  66. void
  67. IOService::run() {
  68. io_impl_->run();
  69. }
  70. void
  71. IOService::run_one() {
  72. io_impl_->run_one();
  73. }
  74. void
  75. IOService::stop() {
  76. io_impl_->stop();
  77. }
  78. asio::io_service&
  79. IOService::get_io_service() {
  80. return (io_impl_->get_io_service());
  81. }
  82. } // namepsace asiolink