26 #ifndef NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP 27 #define NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP 45 template<
class Protocol,
class Addressing = Unicast>
65 const boost::system::error_code& error);
75 handleSend(
const boost::system::error_code& error,
76 size_t nBytesSent,
const Block& payload);
80 size_t nBytesReceived);
101 std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
102 bool m_hasRecentlyReceived;
106 template<
class T,
class U>
108 : m_socket(
std::move(socket))
109 , m_hasRecentlyReceived(false)
111 boost::asio::socket_base::send_buffer_size sendBufferSizeOption;
112 boost::system::error_code error;
113 m_socket.get_option(sendBufferSizeOption, error);
115 NFD_LOG_FACE_WARN(
"Failed to obtain send queue capacity from socket: " << error.message());
119 this->setSendQueueCapacity(sendBufferSizeOption.value());
122 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_sender,
124 boost::asio::placeholders::error,
125 boost::asio::placeholders::bytes_transferred));
128 template<
class T,
class U>
134 NFD_LOG_FACE_WARN(
"Failed to obtain send queue length from socket: " << std::strerror(errno));
139 template<
class T,
class U>
145 if (m_socket.is_open()) {
148 boost::system::error_code error;
149 m_socket.cancel(error);
150 m_socket.close(error);
156 this->setState(TransportState::CLOSED);
160 template<
class T,
class U>
166 m_socket.async_send(boost::asio::buffer(packet.packet),
168 boost::asio::placeholders::error,
169 boost::asio::placeholders::bytes_transferred,
173 template<
class T,
class U>
176 const boost::system::error_code& error)
179 return processErrorCode(error);
185 std::tie(isOk, element) = Block::fromBuffer(buffer, nBytesReceived);
191 if (element.
size() != nBytesReceived) {
192 NFD_LOG_FACE_WARN(
"Received datagram size and decoded element size don't match");
196 m_hasRecentlyReceived =
true;
200 this->receive(std::move(tp));
203 template<
class T,
class U>
206 size_t nBytesReceived)
208 receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
210 if (m_socket.is_open())
211 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_sender,
213 boost::asio::placeholders::error,
214 boost::asio::placeholders::bytes_transferred));
217 template<
class T,
class U>
220 size_t nBytesSent,
const Block& payload)
224 return processErrorCode(error);
229 template<
class T,
class U>
235 if (getState() == TransportState::CLOSING ||
236 getState() == TransportState::FAILED ||
237 getState() == TransportState::CLOSED ||
238 error == boost::asio::error::operation_aborted) {
249 this->setState(TransportState::FAILED);
253 template<
class T,
class U>
257 return m_hasRecentlyReceived;
260 template<
class T,
class U>
264 m_hasRecentlyReceived =
false;
267 template<
class T,
class U>
277 #endif // NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP void doSend(Transport::Packet &&packet) override
performs Transport specific operations to send a packet
void handleSend(const boost::system::error_code &error, size_t nBytesSent, const Block &payload)
#define NFD_LOG_FACE_TRACE(msg)
Log a message at TRACE level.
protocol::socket m_socket
void doClose() override
performs Transport specific operations to close the transport
#define NFD_LOG_FACE_ERROR(msg)
Log a message at ERROR level.
void handleReceive(const boost::system::error_code &error, size_t nBytesReceived)
stores a packet along with the remote endpoint
const ssize_t QUEUE_ERROR
indicates that the transport was unable to retrieve the queue capacity/length
detail::SimulatorIo & getGlobalIoService()
Represents a TLV element of NDN packet format.
Implements Transport for datagram-based protocols.
static EndpointId makeEndpointId(const typename protocol::endpoint &ep)
void receiveDatagram(const uint8_t *buffer, size_t nBytesReceived, const boost::system::error_code &error)
Receive datagram, translate buffer into packet, deliver to parent class.
#define NFD_LOG_FACE_DEBUG(msg)
Log a message at DEBUG level.
size_t size() const
Get size of encoded wire, including Type-Length-Value.
bool hasRecentlyReceived() const
protocol::endpoint m_sender
Copyright (c) 2011-2015 Regents of the University of California.
void processErrorCode(const boost::system::error_code &error)
EndpointId remoteEndpoint
identifies the remote endpoint
void post(const std::function< void()> &callback)
void resetRecentlyReceived()
ssize_t getTxQueueLength(int fd)
obtain send queue length from a specified system socket
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.
ssize_t getSendQueueLength() override
NFD_LOG_INCLASS_DECLARE()