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,
size_t nBytesSent);
78 handleReceive(
const boost::system::error_code& error,
size_t nBytesReceived);
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.packet),
166 [
this, p = packet.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() != nBytesReceived) {
190 NFD_LOG_FACE_WARN(
"Received datagram size and decoded element size don't match");
194 m_hasRecentlyReceived =
true;
198 this->receive(std::move(tp));
201 template<
class T,
class U>
205 receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
207 if (m_socket.is_open())
208 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_sender,
209 [
this] (
auto&&... args) {
210 this->handleReceive(std::forward<decltype(args)>(args)...);
214 template<
class T,
class U>
219 return processErrorCode(error);
224 template<
class T,
class U>
230 if (getState() == TransportState::CLOSING ||
231 getState() == TransportState::FAILED ||
232 getState() == TransportState::CLOSED ||
233 error == boost::asio::error::operation_aborted) {
244 this->setState(TransportState::FAILED);
248 template<
class T,
class U>
252 return m_hasRecentlyReceived;
255 template<
class T,
class U>
259 m_hasRecentlyReceived =
false;
262 template<
class T,
class U>
272 #endif // NFD_DAEMON_FACE_DATAGRAM_TRANSPORT_HPP void doSend(Transport::Packet &&packet) override
performs Transport specific operations to send a packet
#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
void handleSend(const boost::system::error_code &error, size_t nBytesSent)
CFReleaser< CFStringRef > fromBuffer(const uint8_t *buf, size_t buflen)
Create a CFString by copying bytes from a raw buffer.
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