io_service.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <asiolink/asio_wrapper.h>
  8. #include <asiolink/io_service.h>
  9. #include <unistd.h> // for some IPC/network system calls
  10. #include <netinet/in.h>
  11. #include <sys/socket.h>
  12. namespace isc {
  13. namespace asiolink {
  14. namespace {
  15. // A trivial wrapper for boost::function. SunStudio doesn't seem to be capable
  16. // of handling a boost::function object if directly passed to
  17. // io_service::post().
  18. class CallbackWrapper {
  19. public:
  20. CallbackWrapper(const boost::function<void()>& callback) :
  21. callback_(callback)
  22. {}
  23. void operator()() {
  24. callback_();
  25. }
  26. private:
  27. boost::function<void()> callback_;
  28. };
  29. }
  30. class IOServiceImpl {
  31. private:
  32. IOServiceImpl(const IOService& source);
  33. IOServiceImpl& operator=(const IOService& source);
  34. public:
  35. /// \brief The constructor
  36. IOServiceImpl() :
  37. io_service_(),
  38. work_(io_service_)
  39. {};
  40. /// \brief The destructor.
  41. ~IOServiceImpl() {};
  42. //@}
  43. /// \brief Start the underlying event loop.
  44. ///
  45. /// This method does not return control to the caller until
  46. /// the \c stop() method is called via some handler.
  47. void run() {
  48. io_service_.run();
  49. };
  50. /// \brief Run the underlying event loop for a single event.
  51. ///
  52. /// This method return control to the caller as soon as the
  53. /// first handler has completed. (If no handlers are ready when
  54. /// it is run, it will block until one is.)
  55. void run_one() {
  56. io_service_.run_one();
  57. };
  58. /// \brief Run the underlying event loop for a ready events.
  59. ///
  60. /// This method executes handlers for all ready events and returns.
  61. /// It will return immediately if there are no ready events.
  62. void poll() {
  63. io_service_.poll();
  64. };
  65. /// \brief Stop the underlying event loop.
  66. ///
  67. /// This will return the control to the caller of the \c run() method.
  68. void stop() { io_service_.stop();} ;
  69. /// \brief Return the native \c io_service object used in this wrapper.
  70. ///
  71. /// This is a short term work around to support other Kea modules
  72. /// that share the same \c io_service with the authoritative server.
  73. /// It will eventually be removed once the wrapper interface is
  74. /// generalized.
  75. boost::asio::io_service& get_io_service() { return io_service_; };
  76. void post(const boost::function<void ()>& callback) {
  77. const CallbackWrapper wrapper(callback);
  78. io_service_.post(wrapper);
  79. }
  80. private:
  81. boost::asio::io_service io_service_;
  82. boost::asio::io_service::work work_;
  83. };
  84. IOService::IOService() {
  85. io_impl_ = new IOServiceImpl();
  86. }
  87. IOService::~IOService() {
  88. delete io_impl_;
  89. }
  90. void
  91. IOService::run() {
  92. io_impl_->run();
  93. }
  94. void
  95. IOService::run_one() {
  96. io_impl_->run_one();
  97. }
  98. void
  99. IOService::poll() {
  100. io_impl_->poll();
  101. }
  102. void
  103. IOService::stop() {
  104. io_impl_->stop();
  105. }
  106. boost::asio::io_service&
  107. IOService::get_io_service() {
  108. return (io_impl_->get_io_service());
  109. }
  110. void
  111. IOService::post(const boost::function<void ()>& callback) {
  112. return (io_impl_->post(callback));
  113. }
  114. } // namespace asiolink
  115. } // namespace isc