15 namespace ph = std::placeholders;
24 : handler_(handler), data_(new
Data(buffer, buf_size, data_source)) {
36 const size_t bytes_transferred) {
47 handler_(!error_code,
this);
56 if (len > data_->buf_size_) {
60 memcpy (data_->buffer_.get(), src, len);
61 data_->put_len_ = len;
70 const bool reuse_address)
72 port_(port), format_(
format), reuse_address_(reuse_address) {
80 this, ph::_1, ph::_2)));
95 asio_socket_.reset(
new boost::asio::ip::udp::
97 (ip_address_.
isV4() ? boost::asio::ip::udp::v4() :
98 boost::asio::ip::udp::v6())));
101 if (reuse_address_) {
102 asio_socket_->set_option(boost::asio::socket_base::reuse_address(
true));
107 }
catch (
const boost::system::system_error& ex) {
108 asio_socket_.reset();
121 socket_->asyncReceive(recv_buffer.get(), recv_callback_->getBufferSize(),
122 0, recv_callback_->getDataSource().get(),
134 if (asio_socket_->is_open()) {
136 asio_socket_->close();
137 }
catch (
const boost::system::system_error& ex) {
146 asio_socket_.reset();
166 static_cast<int64_t
>(1));
171 static_cast<int64_t
>(1));
179 boost::system::error_code error_code = callback->
getErrorCode();
180 if (error_code.value() == boost::asio::error::operation_aborted) {
188 .arg(error_code.message());
190 static_cast<int64_t
>(1));
208 const size_t send_que_max,
const bool reuse_address)
210 ip_address_(ip_address), port_(port), server_address_(server_address),
211 server_port_(server_port), format_(
format),
212 reuse_address_(reuse_address) {
220 this, ph::_1, ph::_2)));
235 asio_socket_.reset(
new boost::asio::ip::udp::
237 (ip_address_.
isV4() ? boost::asio::ip::udp::v4() :
238 boost::asio::ip::udp::v6())));
241 if (reuse_address_) {
242 asio_socket_->set_option(boost::asio::socket_base::reuse_address(
true));
247 }
catch (
const boost::system::system_error& ex) {
256 UDPEndpoint(server_address_, server_port_));
258 send_callback_->setDataSource(server_endpoint_);
272 if (asio_socket_->is_open()) {
274 asio_socket_->close();
275 }
catch (
const boost::system::system_error& ex) {
284 asio_socket_.reset();
290 watch_socket_.reset();
297 ncr->toFormat(format_, ncr_buffer);
301 send_callback_->putData(
static_cast<const uint8_t*
>(ncr_buffer.
getData()),
305 socket_->asyncSend(send_callback_->getData(), send_callback_->getPutLen(),
306 send_callback_->getDataSource().get(), *send_callback_);
313 watch_socket_->markReady();
321 watch_socket_->clearReady();
322 }
catch (
const std::exception& ex) {
338 boost::system::error_code error_code = send_callback->
getErrorCode();
339 if (error_code.value() == boost::asio::error::operation_aborted) {
341 .arg(error_code.message());
345 .arg(error_code.message());
358 " not in send mode");
361 return(watch_socket_->getSelectFd());
367 return (watch_socket_->isReady());
374 NameChangeUDPSender::closeWatchSocket() {
376 std::string error_string;
377 watch_socket_->closeSocket(error_string);
378 if (!error_string.empty()) {
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when a function is not implemented.
The IOAddress class represents an IP addresses (version agnostic)
bool isV4() const
Convenience function to check for an IPv4 address.
The IOService class is a wrapper for the ASIO io_service class.
boost::asio::io_service & get_io_service()
Return the native io_service object used in this wrapper.
The UDPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a UDP ...
const boost::asio::ip::udp::endpoint & getASIOEndpoint() const
Abstract class for defining application layer receive callbacks.
Abstract interface for receiving NameChangeRequests.
void stopListening()
Closes the IO source and stops listen logic.
void invokeRecvHandler(const Result result, NameChangeRequestPtr &ncr)
Calls the NCR receive handler registered with the listener.
Result
Defines the outcome of an asynchronous NCR receive.
void receiveNext()
Initiates an asynchronous receive.
static NameChangeRequestPtr fromFormat(const NameChangeFormat format, isc::util::InputBuffer &buffer)
Static method for creating a NameChangeRequest from a buffer containing a marshalled request in a giv...
Abstract class for defining application layer send callbacks.
Abstract interface for sending NameChangeRequests.
void stopSending()
Closes the IO sink and stops send logic.
bool amSending() const
Returns true if the sender is in send mode, false otherwise.
void invokeSendHandler(const NameChangeSender::Result result)
Calls the NCR send completion handler registered with the sender.
Result
Defines the outcome of an asynchronous NCR send.
virtual void close()
Closes the UDPSocket.
virtual ~NameChangeUDPListener()
Destructor.
static const size_t RECV_BUF_MAX
Defines the maximum size packet that can be received.
virtual void open(isc::asiolink::IOService &io_service)
Opens a UDP socket using the given IOService.
void receiveCompletionHandler(const bool successful, const UDPCallback *recv_callback)
Implements the NameChangeRequest level receive completion handler.
void doReceive()
Initiates an asynchronous read on the socket.
NameChangeUDPListener(const isc::asiolink::IOAddress &ip_address, const uint32_t port, const NameChangeFormat format, RequestReceiveHandler &ncr_recv_handler, const bool reuse_address=false)
Constructor.
void sendCompletionHandler(const bool successful, const UDPCallback *send_callback)
Implements the NameChangeRequest level send completion handler.
virtual void open(isc::asiolink::IOService &io_service)
Opens a UDP socket using the given IOService.
virtual bool ioReady()
Returns whether or not the sender has IO ready to process.
static const size_t SEND_BUF_MAX
Defines the maximum size packet that can be sent.
virtual int getSelectFd()
Returns a file descriptor suitable for use with select.
virtual void close()
Closes the UDPSocket.
NameChangeUDPSender(const isc::asiolink::IOAddress &ip_address, const uint32_t port, const isc::asiolink::IOAddress &server_address, const uint32_t server_port, const NameChangeFormat format, RequestSendHandler &ncr_send_handler, const size_t send_que_max=NameChangeSender::MAX_QUEUE_DEFAULT, const bool reuse_address=false)
Constructor.
virtual void doSend(NameChangeRequestPtr &ncr)
Sends a given request asynchronously over the socket.
virtual ~NameChangeUDPSender()
Destructor.
Exception thrown when NameChangeRequest marshalling error occurs.
Thrown when a UDP level exception occurs.
Implements the callback class passed into UDPSocket calls.
size_t getBytesTransferred() const
Returns the number of bytes transferred by the completed IO service.
void setErrorCode(const boost::system::error_code value)
Sets the completed IO layer service outcome status.
void putData(const uint8_t *src, size_t len)
Copies data into the data transfer buffer.
UDPCallback(RawBufferPtr &buffer, const size_t buf_size, UDPEndpointPtr &data_source, const UDPCompletionHandler &handler)
Used as the callback object for UDPSocket services.
const uint8_t * getData() const
Returns a pointer the data transfer buffer content.
void setBytesTransferred(const size_t value)
Sets the number of bytes transferred.
void operator()(const boost::system::error_code error_code, const size_t bytes_transferred)
Operator that will be invoked by the asiolink layer.
boost::system::error_code getErrorCode() const
Returns the completed IO layer service outcome status.
static StatsMgr & instance()
Statistics Manager accessor method.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
const void * getData() const
Return a pointer to the head of the data stored in the buffer.
size_t getLength() const
Return the length of data written in the buffer.
Provides an IO "ready" semaphore for use with select() or poll() WatchSocket exposes a single open fi...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
void addValue(const std::string &name, const int64_t value)
Records incremental integer observation.
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
const isc::log::MessageID DHCP_DDNS_NCR_UDP_RECV_ERROR
isc::log::Logger dhcp_ddns_logger("libdhcp-ddns")
Defines the logger used within lib dhcp_ddns.
NameChangeFormat
Defines the list of data wire formats supported.
const isc::log::MessageID DHCP_DDNS_NCR_UDP_SEND_CANCELED
const isc::log::MessageID DHCP_DDNS_NCR_UDP_SEND_ERROR
const isc::log::MessageID DHCP_DDNS_NCR_UDP_CLEAR_READY_ERROR
std::function< void(const bool, const UDPCallback *)> UDPCompletionHandler
Defines a function pointer for NameChangeRequest completion handlers.
const isc::log::MessageID DHCP_DDNS_NCR_UDP_RECV_CANCELED
isc::asiolink::UDPSocket< UDPCallback > NameChangeUDPSocket
Convenience type for UDP socket based listener.
boost::shared_ptr< NameChangeRequest > NameChangeRequestPtr
Defines a pointer to a NameChangeRequest.
const isc::log::MessageID DHCP_DDNS_INVALID_NCR
boost::shared_array< uint8_t > RawBufferPtr
Defines a dynamically allocated shared array.
boost::shared_ptr< asiolink::UDPEndpoint > UDPEndpointPtr
const isc::log::MessageID DHCP_DDNS_UDP_SENDER_WATCH_SOCKET_CLOSE_ERROR
const int DBGLVL_TRACE_BASIC
Trace basic operations.
std::string format(const std::string &format, const std::vector< std::string > &args)
Apply Formatting.
Defines the logger used by the top-level component of kea-lfc.
This file provides UDP socket based implementation for sending and receiving NameChangeRequests.
Container class which stores service invocation related data.