io_service.cc 3.8 KB

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