io_service.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (C) 2011,2013 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 <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() { io_service_.run(); };
  56. /// \brief Run the underlying event loop for a single event.
  57. ///
  58. /// This method return control to the caller as soon as the
  59. /// first handler has completed. (If no handlers are ready when
  60. /// it is run, it will block until one is.)
  61. void run_one() { io_service_.run_one();} ;
  62. /// \brief Stop the underlying event loop.
  63. ///
  64. /// This will return the control to the caller of the \c run() method.
  65. void stop() { io_service_.stop();} ;
  66. /// \brief Return the native \c io_service object used in this wrapper.
  67. ///
  68. /// This is a short term work around to support other BIND 10 modules
  69. /// that share the same \c io_service with the authoritative server.
  70. /// It will eventually be removed once the wrapper interface is
  71. /// generalized.
  72. asio::io_service& get_io_service() { return io_service_; };
  73. void post(const boost::function<void ()>& callback) {
  74. io_service_.post(CallbackWrapper(callback));
  75. }
  76. private:
  77. asio::io_service io_service_;
  78. asio::io_service::work work_;
  79. };
  80. IOService::IOService() {
  81. io_impl_ = new IOServiceImpl();
  82. }
  83. IOService::~IOService() {
  84. delete io_impl_;
  85. }
  86. void
  87. IOService::run() {
  88. io_impl_->run();
  89. }
  90. void
  91. IOService::run_one() {
  92. io_impl_->run_one();
  93. }
  94. void
  95. IOService::stop() {
  96. io_impl_->stop();
  97. }
  98. asio::io_service&
  99. IOService::get_io_service() {
  100. return (io_impl_->get_io_service());
  101. }
  102. void
  103. IOService::post(const boost::function<void ()>& callback) {
  104. return (io_impl_->post(callback));
  105. }
  106. } // namespace asiolink
  107. } // namespace isc