26 #ifndef NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP 27 #define NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP 44 template<
class Protocol,
class Addressing = Unicast>
60 receiveDatagram(
const uint8_t* buffer,
size_t nBytesReceived,
61 const boost::system::error_code& error);
71 handleSend(
const boost::system::error_code& error,
72 size_t nBytesSent,
const Block& payload);
75 handleReceive(
const boost::system::error_code& error,
76 size_t nBytesReceived);
79 processErrorCode(
const boost::system::error_code& error);
82 hasBeenUsedRecently()
const;
88 makeEndpointId(
const typename protocol::endpoint& ep);
97 std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
98 bool m_hasBeenUsedRecently;
102 template<
class T,
class U>
104 : m_socket(
std::move(socket))
105 , m_hasBeenUsedRecently(false)
107 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_sender,
109 boost::asio::placeholders::error,
110 boost::asio::placeholders::bytes_transferred));
113 template<
class T,
class U>
119 if (m_socket.is_open()) {
122 boost::system::error_code error;
123 m_socket.cancel(error);
124 m_socket.close(error);
130 this->setState(TransportState::CLOSED);
134 template<
class T,
class U>
140 m_socket.async_send(boost::asio::buffer(packet.packet),
142 boost::asio::placeholders::error,
143 boost::asio::placeholders::bytes_transferred,
147 template<
class T,
class U>
150 const boost::system::error_code& error)
153 return processErrorCode(error);
159 std::tie(isOk, element) = Block::fromBuffer(buffer, nBytesReceived);
165 if (element.
size() != nBytesReceived) {
166 NFD_LOG_FACE_WARN(
"Received datagram size and decoded element size don't match");
170 m_hasBeenUsedRecently =
true;
174 this->receive(std::move(tp));
177 template<
class T,
class U>
180 size_t nBytesReceived)
182 receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
184 if (m_socket.is_open())
185 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_sender,
187 boost::asio::placeholders::error,
188 boost::asio::placeholders::bytes_transferred));
191 template<
class T,
class U>
194 size_t nBytesSent,
const Block& payload)
198 return processErrorCode(error);
203 template<
class T,
class U>
209 if (getState() == TransportState::CLOSING ||
210 getState() == TransportState::FAILED ||
211 getState() == TransportState::CLOSED ||
212 error == boost::asio::error::operation_aborted)
221 if (error != boost::asio::error::eof)
224 this->setState(TransportState::FAILED);
228 template<
class T,
class U>
232 return m_hasBeenUsedRecently;
235 template<
class T,
class U>
239 m_hasBeenUsedRecently =
false;
242 template<
class T,
class U>
252 #endif // NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP #define NFD_LOG_INCLASS_DECLARE()
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
protocol::socket m_socket
stores a packet along with the remote endpoint
detail::SimulatorIo & getGlobalIoService()
Class representing a wire element of NDN-TLV packet format.
Implements Transport for datagram-based protocols.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
protocol::endpoint m_sender
Copyright (c) 2011-2015 Regents of the University of California.
EndpointId remoteEndpoint
identifies the remote endpoint
void post(const std::function< void()> &callback)
DatagramTransport(typename protocol::socket &&socket)
Construct datagram transport.
uint64_t EndpointId
identifies an endpoint on the link
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.