11 #include <boost/enable_shared_from_this.hpp>
16 namespace ph = std::placeholders;
29 : socket_(io_service.get_io_service()) {
47 void asyncConnect(
const stream_protocol::endpoint& endpoint,
63 const boost::system::error_code& ec);
74 void asyncSend(
const void* data,
const size_t length,
86 void doSend(
const boost::asio::const_buffers_1& buffer,
106 const boost::asio::const_buffers_1& buffer,
107 const boost::system::error_code& ec,
119 void asyncReceive(
void* data,
const size_t length,
130 void doReceive(
const boost::asio::mutable_buffers_1& buffer,
149 const boost::asio::mutable_buffers_1& buffer,
150 const boost::system::error_code& ec,
167 UnixDomainSocketImpl::asyncConnect(
const stream_protocol::endpoint& endpoint,
169 auto local_handler = std::bind(&UnixDomainSocketImpl::connectHandler,
172 socket_.async_connect(endpoint, local_handler);
177 const boost::system::error_code& ec) {
183 if (ec.value() == boost::asio::error::in_progress) {
184 remote_handler(boost::system::error_code());
191 UnixDomainSocketImpl::asyncSend(
const void* data,
const size_t length,
193 doSend(boost::asio::buffer(data, length), handler);
197 UnixDomainSocketImpl::doSend(
const boost::asio::const_buffers_1& buffer,
199 auto local_handler = std::bind(&UnixDomainSocketImpl::sendHandler,
201 handler, buffer, ph::_1, ph::_2);
202 socket_.async_send(buffer, local_handler);
207 const boost::asio::const_buffers_1& buffer,
208 const boost::system::error_code& ec,
214 if ((ec.value() == boost::asio::error::would_block) ||
215 (ec.value() == boost::asio::error::try_again)) {
216 doSend(buffer, remote_handler);
219 remote_handler(ec, length);
224 UnixDomainSocketImpl::asyncReceive(
void* data,
const size_t length,
226 doReceive(boost::asio::buffer(data, length), handler);
230 UnixDomainSocketImpl::doReceive(
const boost::asio::mutable_buffers_1& buffer,
232 auto local_handler = std::bind(&UnixDomainSocketImpl::receiveHandler,
234 handler, buffer, ph::_1, ph::_2);
235 socket_.async_receive(buffer, 0, local_handler);
240 const boost::asio::mutable_buffers_1& buffer,
241 const boost::system::error_code& ec,
247 if ((ec.value() == boost::asio::error::would_block) ||
248 (ec.value() == boost::asio::error::try_again)) {
249 doReceive(buffer, remote_handler);
252 remote_handler(ec, length);
257 UnixDomainSocketImpl::shutdown() {
258 boost::system::error_code ec;
259 static_cast<void>(socket_.shutdown(stream_protocol::socket::shutdown_both, ec));
266 UnixDomainSocketImpl::cancel() {
267 boost::system::error_code ec;
268 static_cast<void>(socket_.cancel(ec));
275 UnixDomainSocketImpl::close() {
276 boost::system::error_code ec;
277 static_cast<void>(socket_.close(ec));
283 UnixDomainSocket::UnixDomainSocket(
IOService& io_service)
289 #if BOOST_VERSION < 106600
290 return (impl_->socket_.native());
292 return (impl_->socket_.native_handle());
303 boost::system::error_code ec;
304 impl_->socket_.connect(stream_protocol::endpoint(path.c_str()), ec);
312 impl_->asyncConnect(stream_protocol::endpoint(path.c_str()), handler);
317 boost::system::error_code ec;
318 size_t res = boost::asio::write(impl_->socket_,
319 boost::asio::buffer(data, length),
320 boost::asio::transfer_all(),
331 impl_->asyncSend(data, length, handler);
336 boost::system::error_code ec;
337 size_t res = impl_->socket_.receive(boost::asio::buffer(data, length), 0, ec);
347 impl_->asyncReceive(data, length, handler);
365 boost::asio::local::stream_protocol::socket&
367 return (impl_->socket_);
void cancel()
Cancels scheduled asynchronous operations on the socket.
void shutdown()
Disables read and write operations on the socket.
size_t write(const void *data, size_t length)
Writes specified amount of data to a socket.
stream_protocol::socket socket_
Instance of the boost asio unix domain socket.
void close()
Closes the socket.
Implementation of the unix domain socket.
std::function< void(const boost::system::error_code &)> ConnectHandler
Callback type used in call to UnixDomainSocket::asyncConnect.
std::function< void(const boost::system::error_code &, size_t)> Handler
Callback type used in calls to UnixDomainSocket::asyncSend and UnixDomainSocket::asyncReceive.
~UnixDomainSocketImpl()
Destructor.
The IOService class is a wrapper for the ASIO io_service class.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
virtual int getProtocol() const
Always returns 0.
UnixDomainSocketImpl(IOService &io_service)
Constructor.
void asyncSend(const void *data, const size_t length, const Handler &handler)
Asynchronously sends data over the socket.
Defines the logger used by the top-level component of kea-dhcp-ddns.
void asyncConnect(const std::string &path, const ConnectHandler &handler)
Asynchronously connects the socket to the specified endpoint.
Exception thrown upon socket error.
size_t receive(void *data, size_t length)
Receives data from a socket.
A wrapper interface for the ASIO library.
void connect(const std::string &path)
Connects the socket to the specified endpoint.
virtual int getNative() const
Returns native socket representation.
void asyncReceive(void *data, const size_t length, const Handler &handler)
Asynchronously receives data over the socket.
virtual boost::asio::local::stream_protocol::socket & getASIOSocket() const
Returns reference to the underlying ASIO socket.