26 #ifndef NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP 27 #define NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP 45 template<
class Protocol,
class Addressing = Unicast>
59 getSendQueueLength()
override;
65 receiveDatagram(span<const uint8_t> buffer,
const boost::system::error_code& error);
72 doSend(
const Block& packet)
override;
75 handleSend(
const boost::system::error_code& error,
size_t nBytesSent);
78 handleReceive(
const boost::system::error_code& error,
size_t nBytesReceived);
81 processErrorCode(
const boost::system::error_code& error);
84 hasRecentlyReceived()
const;
87 resetRecentlyReceived();
90 makeEndpointId(
const typename protocol::endpoint& ep);
99 std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
100 bool m_hasRecentlyReceived;
104 template<
class T,
class U>
107 , m_hasRecentlyReceived(false)
109 boost::asio::socket_base::send_buffer_size sendBufferSizeOption;
110 boost::system::error_code error;
111 m_socket.get_option(sendBufferSizeOption, error);
113 NFD_LOG_FACE_WARN(
"Failed to obtain send queue capacity from socket: " << error.message());
117 this->setSendQueueCapacity(sendBufferSizeOption.value());
120 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_sender,
121 [
this] (
auto&&... args) {
122 this->handleReceive(std::forward<decltype(args)>(args)...);
126 template<
class T,
class U>
132 NFD_LOG_FACE_WARN(
"Failed to obtain send queue length from socket: " << std::strerror(errno));
137 template<
class T,
class U>
143 if (m_socket.is_open()) {
146 boost::system::error_code error;
147 m_socket.cancel(error);
148 m_socket.close(error);
154 this->setState(TransportState::CLOSED);
158 template<
class T,
class U>
164 m_socket.async_send(boost::asio::buffer(packet),
166 [
this, packet] (
auto&&... args) {
167 this->handleSend(std::forward<decltype(args)>(args)...);
171 template<
class T,
class U>
174 const boost::system::error_code& error)
177 return processErrorCode(error);
189 if (element.
size() != buffer.size()) {
190 NFD_LOG_FACE_WARN(
"Received datagram size and decoded element size don't match");
194 m_hasRecentlyReceived =
true;
196 this->receive(element, makeEndpointId(m_sender));
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>
228 if (getState() == TransportState::CLOSING ||
229 getState() == TransportState::FAILED ||
230 getState() == TransportState::CLOSED ||
242 this->setState(TransportState::FAILED);
246 template<
class T,
class U>
250 return m_hasRecentlyReceived;
253 template<
class T,
class U>
257 m_hasRecentlyReceived =
false;
260 template<
class T,
class U>
270 #endif // NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
protocol::socket m_socket
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
const ssize_t QUEUE_ERROR
indicates that the transport was unable to retrieve the queue capacity/length
detail::SimulatorIo & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Represents a TLV element of the NDN packet format.
uint64_t EndpointId
Identifies a remote endpoint on the link.
Implements Transport for datagram-based protocols.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
size_t size() const
Return the size of the encoded wire, i.e., of the whole TLV.
protocol::endpoint m_sender
Copyright (c) 2011-2015 Regents of the University of California.
void post(const std::function< void()> &callback)
#define NFD_LOG_MEMBER_DECL()
ssize_t getTxQueueLength(int fd)
obtain send queue length from a specified system socket
boost::asio::ip::udp protocol
CFReleaser< CFStringRef > fromBuffer(const uint8_t *buf, size_t buflen)
Create a CFString by copying bytes from a raw buffer.
Catch-all error for socket component errors that don't fit in other categories.
DatagramTransport(typename protocol::socket &&socket)
Construct datagram transport.
The lower half of a Face.
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.