14 #include <boost/enable_shared_from_this.hpp>
67 void doSend(
const void* buffer,
const size_t length,
86 void terminate(
const boost::system::error_code& ec,
106 std::string current_command_;
109 std::array<char, 32768> read_buf_;
118 ClientConnectionImpl::ClientConnectionImpl(
IOService& io_service)
119 : socket_(io_service), feed_(), current_command_(), timer_(io_service),
128 timeout_, IntervalTimer::ONE_SHOT);
147 auto self(shared_from_this());
150 [
this,
self, command, handler](
const boost::system::error_code& ec) {
155 terminate(ec, handler);
160 doSend(current_command_.c_str(), current_command_.length(),
167 ClientConnectionImpl::doSend(
const void* buffer,
const size_t length,
171 auto self(shared_from_this());
174 socket_.asyncSend(buffer, length,
175 [
this,
self, buffer, length, handler]
176 (
const boost::system::error_code& ec,
size_t bytes_transferred) {
181 terminate(ec, handler);
185 scheduleTimer(handler);
191 if (bytes_transferred < length) {
192 doSend(static_cast<const char*>(buffer) + bytes_transferred,
193 length - bytes_transferred, handler);
207 auto self(shared_from_this());
208 socket_.asyncReceive(&read_buf_[0], read_buf_.size(),
209 [
this,
self, handler]
210 (
const boost::system::error_code& ec,
size_t length) {
215 terminate(ec, handler);
219 scheduleTimer(handler);
221 std::string x(&read_buf_[0], length);
231 feed_->postBuffer(&read_buf_[0], length);
235 if (feed_->needData()) {
241 terminate(ec, handler);
248 ClientConnectionImpl::terminate(
const boost::system::error_code& ec,
253 current_command_.clear();
268 terminate(boost::asio::error::timed_out, handler);
280 impl_->start(socket_path, command, handler, timeout);
std::string control_command_
void timeoutCallback(ClientConnection::Handler handler)
Callback invoked when the timeout occurs.
boost::shared_ptr< JSONFeed > JSONFeedPtr
Pointer to the JSONFeed.
The IOService class is a wrapper for the ASIO io_service class.
The IntervalTimer class is a wrapper for the ASIO boost::asio::deadline_timer class.
Represents unix domain socket implemented in terms of boost asio.
Implementation of the ClientConnection.
void start(const ClientConnection::SocketPath &socket_path, const ClientConnection::ControlCommand &command, ClientConnection::Handler handler, const ClientConnection::Timeout &timeout)
Starts asynchronous transaction with a remote endpoint.
State model for asynchronous read of data in JSON format.
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.
void scheduleTimer(ClientConnection::Handler handler)
This method schedules timer or reschedules existing timer.
Encapsulates socket path.
Encapsulates timeout value.
void start(const SocketPath &socket_path, const ControlCommand &command, Handler handler, const Timeout &timeout=Timeout(5000))
Starts asynchronous transaction with a remote endpoint.
std::function< void(const boost::system::error_code &ec, const ConstJSONFeedPtr &feed)> Handler
Type of the callback invoked when the communication with the server is complete or an error has occur...
Encapsulates control command.
void setup(const Callback &cbfunc, const long interval, const Mode &mode=REPEATING)
Register timer callback function and interval.