26 #ifndef NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP
27 #define NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP
46 template<
class Protocol,
class Addressing>
67 receiveDatagram(span<const uint8_t> buffer,
const boost::system::error_code& error);
74 doSend(
const Block& packet)
override;
77 handleSend(
const boost::system::error_code& error,
size_t nBytesSent);
80 handleReceive(
const boost::system::error_code& error,
size_t nBytesReceived);
98 std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
99 bool m_hasRecentlyReceived;
103 template<
class T,
class U>
105 : m_socket(std::move(socket))
106 , m_hasRecentlyReceived(false)
108 boost::asio::socket_base::send_buffer_size sendBufferSizeOption;
109 boost::system::error_code error;
110 m_socket.get_option(sendBufferSizeOption, error);
112 NFD_LOG_FACE_WARN(
"Failed to obtain send queue capacity from socket: " << error.message());
120 [
this] (
auto&&... args) {
125 template<
class T,
class U>
131 NFD_LOG_FACE_WARN(
"Failed to obtain send queue length from socket: " << std::strerror(errno));
136 template<
class T,
class U>
142 if (m_socket.is_open()) {
145 boost::system::error_code error;
146 m_socket.cancel(error);
147 m_socket.close(error);
157 template<
class T,
class U>
163 m_socket.async_send(boost::asio::buffer(packet),
165 [
this, packet] (
auto&&... args) {
166 this->handleSend(std::forward<decltype(args)>(args)...);
170 template<
class T,
class U>
173 const boost::system::error_code& error)
176 return processErrorCode(error);
180 auto [isOk, element] = Block::fromBuffer(buffer);
186 if (element.size() != buffer.size()) {
187 NFD_LOG_FACE_WARN(
"Received datagram size and decoded element size don't match");
191 m_hasRecentlyReceived =
true;
193 if constexpr (std::is_same_v<addressing, Multicast>)
194 this->receive(element, m_sender);
196 this->receive(element);
199 template<
class T,
class U>
203 receiveDatagram(ndn::make_span(m_receiveBuffer).first(nBytesReceived), error);
205 if (m_socket.is_open())
206 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_sender,
207 [
this] (
auto&&... args) {
208 this->handleReceive(std::forward<decltype(args)>(args)...);
212 template<
class T,
class U>
217 return processErrorCode(error);
222 template<
class T,
class U>
231 error == boost::asio::error::operation_aborted) {
236 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
246 template<
class T,
class U>
250 return m_hasRecentlyReceived;
253 template<
class T,
class U>
257 m_hasRecentlyReceived =
false;
Implements Transport for datagram-based protocols.
protocol::endpoint m_sender
void processErrorCode(const boost::system::error_code &error)
ssize_t getSendQueueLength() override
Returns the current send queue length of the transport (in octets).
DatagramTransport(typename protocol::socket &&socket)
Construct datagram transport.
bool hasRecentlyReceived() const
void doSend(const Block &packet) override
Performs Transport specific operations to send a packet.
void resetRecentlyReceived()
void handleSend(const boost::system::error_code &error, size_t nBytesSent)
void receiveDatagram(span< const uint8_t > buffer, const boost::system::error_code &error)
Receive datagram, translate buffer into packet, deliver to parent class.
protocol::socket m_socket
void handleReceive(const boost::system::error_code &error, size_t nBytesReceived)
void doClose() override
Performs Transport specific operations to close the transport.
The lower half of a Face.
void setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_MEMBER_DECL()
@ CLOSED
the transport is closed, and can be safely deallocated
@ CLOSING
the transport is being closed gracefully, either by the peer or by a call to close()
@ FAILED
the transport is being closed due to a failure
constexpr ssize_t QUEUE_ERROR
Indicates that the transport was unable to retrieve the queue capacity/length.
ssize_t getTxQueueLength(int fd)
Obtain send queue length from a specified system socket.
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.