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;
64 receiveDatagram(
const uint8_t* buffer,
size_t nBytesReceived,
65 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>
106 : m_socket(
std::move(socket))
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);
183 std::tie(isOk, element) = Block::fromBuffer(buffer, nBytesReceived);
189 if (element.size() != nBytesReceived) {
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(m_receiveBuffer.data(), 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 ||
231 error == boost::asio::error::operation_aborted) {
236 if (getPersistency() == ndn::nfd::FACE_PERSISTENCY_PERMANENT) {
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_DEBUG(msg)
Log a message at DEBUG level.
const ssize_t QUEUE_ERROR
indicates that the transport was unable to retrieve the queue capacity/length
#define NFD_LOG_MEMBER_DECL()
The lower half of a Face.
uint64_t EndpointId
Identifies a remote endpoint on the link.
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
ssize_t getTxQueueLength(int fd)
obtain send queue length from a specified system socket
Implements Transport for datagram-based protocols.
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
protocol::endpoint m_sender
DatagramTransport(typename protocol::socket &&socket)
Construct datagram transport.
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.