Kea  1.9.9-git
io_service.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2021 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 
7 #include <config.h>
9 #include <asiolink/io_service.h>
10 
11 #include <unistd.h> // for some IPC/network system calls
12 #include <netinet/in.h>
13 #include <boost/shared_ptr.hpp>
14 #include <sys/socket.h>
15 
16 namespace isc {
17 namespace asiolink {
18 
19 namespace {
20 // A trivial wrapper for std::function. SunStudio doesn't seem to be capable
21 // of handling a std::function object if directly passed to
22 // io_service::post().
23 class CallbackWrapper {
24 public:
25 
27  CallbackWrapper(const std::function<void()>& callback) :
28  callback_(callback) {}
29 
31  void operator()() {
32  callback_();
33  }
34 
35 private:
36 
38  std::function<void()> callback_;
39 };
40 }
41 
43 private:
44  IOServiceImpl(const IOService& source);
45  IOServiceImpl& operator=(const IOService& source);
46 public:
49  io_service_(),
50  work_(new boost::asio::io_service::work(io_service_)) {
51  };
52 
56 
61  void run() {
62  io_service_.run();
63  };
64 
70  void run_one() {
71  io_service_.run_one();
72  };
73 
78  void poll() {
79  io_service_.poll();
80  };
81 
85  void stop() {
86  io_service_.stop();
87  }
88 
92  bool stopped() const {
93  return (io_service_.stopped());
94  }
95 
97  void restart() {
98  io_service_.reset();
99  }
100 
103  void stopWork() {
104  work_.reset();
105  }
106 
113  boost::asio::io_service& get_io_service() {
114  return (io_service_);
115  }
116 
120  void post(const std::function<void ()>& callback) {
121  const CallbackWrapper wrapper(callback);
122  io_service_.post(wrapper);
123  }
124 
125 private:
126  boost::asio::io_service io_service_;
127  boost::shared_ptr<boost::asio::io_service::work> work_;
128 };
129 
130 IOService::IOService() : io_impl_(new IOServiceImpl()) {
131 }
132 
134 }
135 
136 void
138  io_impl_->run();
139 }
140 
141 void
143  io_impl_->run_one();
144 }
145 
146 void
148  io_impl_->poll();
149 }
150 
151 void
153  io_impl_->stop();
154 }
155 
156 bool
158  return (io_impl_->stopped());
159 }
160 
161 void
163  io_impl_->restart();
164 }
165 
166 void
168  io_impl_->stopWork();
169 }
170 
171 boost::asio::io_service&
173  return (io_impl_->get_io_service());
174 }
175 
176 void
177 IOService::post(const std::function<void ()>& callback) {
178  return (io_impl_->post(callback));
179 }
180 
181 } // namespace asiolink
182 } // namespace isc
std::function< void()> callback_
The callback function.
Definition: io_service.cc:38
Defines the logger used by the top-level component of kea-dhcp-ddns.