36 namespace ip = boost::asio::ip;
39 time::nanoseconds idleTimeout)
40 : m_localEndpoint(localEndpoint)
42 , m_idleFaceTimeout(idleTimeout)
44 setUri(FaceUri(m_localEndpoint));
50 ndn::nfd::FacePersistency persistency,
51 bool wantLpReliability,
55 shared_ptr<Face> face;
57 face = createFace(remoteEndpoint, persistency, wantLpReliability).second;
59 catch (
const boost::system::system_error& e) {
62 onConnectFailed(504, std::string(
"Face creation failed: ") + e.what());
80 m_socket.open(m_localEndpoint.protocol());
81 m_socket.set_option(ip::udp::socket::reuse_address(
true));
82 if (m_localEndpoint.address().is_v6()) {
83 m_socket.set_option(ip::v6_only(
true));
85 m_socket.bind(m_localEndpoint);
87 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
95 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
96 bind(&UdpChannel::handleNewPeer,
this,
97 boost::asio::placeholders::error,
98 boost::asio::placeholders::bytes_transferred,
99 onFaceCreated, onReceiveFailed));
103 UdpChannel::handleNewPeer(
const boost::system::error_code& error,
104 size_t nBytesReceived,
109 if (error != boost::asio::error::operation_aborted) {
112 onReceiveFailed(500,
"Receive failed: " + error.message());
119 bool isCreated =
false;
120 shared_ptr<Face> face;
122 std::tie(isCreated, face) = createFace(m_remoteEndpoint, ndn::nfd::FACE_PERSISTENCY_ON_DEMAND,
125 catch (
const boost::system::system_error& e) {
126 NFD_LOG_CHAN_DEBUG(
"Face creation for " << m_remoteEndpoint <<
" failed: " << e.what());
128 onReceiveFailed(504, std::string(
"Face creation failed: ") + e.what());
139 transport->
receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
141 waitForNewPeer(onFaceCreated, onReceiveFailed);
144 std::pair<bool, shared_ptr<Face>>
146 ndn::nfd::FacePersistency persistency,
147 bool wantLpReliability)
149 auto it = m_channelFaces.find(remoteEndpoint);
150 if (it != m_channelFaces.end()) {
153 return {
false, it->second};
158 socket.set_option(ip::udp::socket::reuse_address(
true));
159 socket.bind(m_localEndpoint);
160 socket.connect(remoteEndpoint);
164 auto linkService = make_unique<GenericLinkService>(options);
165 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
166 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
168 m_channelFaces[remoteEndpoint] = face;
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
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.
UdpChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout)
Create a UDP channel on the given localEndpoint.
bool isEnabled
enables link-layer reliability
A Transport that communicates on a unicast UDP socket.
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
void connect(const udp::Endpoint &remoteEndpoint, ndn::nfd::FacePersistency persistency, bool wantLpReliability, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast UDP face toward remoteEndpoint.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
LpReliability::Options reliabilityOptions
options for reliability
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
void setUri(const FaceUri &uri)
boost::asio::ip::udp::endpoint Endpoint
Options that control the behavior of GenericLinkService.
#define NFD_LOG_INIT(name)
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
bool isListening() const override
Returns whether the channel is listening.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
boost::asio::io_service & getGlobalIoService()