37 const time::seconds& timeout)
38 : m_localEndpoint(localEndpoint)
40 , m_idleFaceTimeout(timeout)
50 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Already listening");
54 m_socket.open(m_localEndpoint.protocol());
55 m_socket.set_option(ip::udp::socket::reuse_address(
true));
56 if (m_localEndpoint.address().is_v6())
57 m_socket.set_option(ip::v6_only(
true));
59 m_socket.bind(m_localEndpoint);
62 bind(&UdpChannel::handleNewPeer,
this,
63 boost::asio::placeholders::error,
64 boost::asio::placeholders::bytes_transferred,
65 onFaceCreated, onReceiveFailed));
74 shared_ptr<UdpFace> face;
76 face = createFace(remoteEndpoint, persistency).second;
78 catch (
const boost::system::system_error& e) {
79 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Connect failed: " << e.what());
81 onConnectFailed(e.what());
93 return m_channelFaces.size();
96 std::pair<bool, shared_ptr<UdpFace>>
99 auto it = m_channelFaces.find(remoteEndpoint);
100 if (it != m_channelFaces.end()) {
102 auto face = it->second;
104 bool isTransitionAllowed = persistency != face->getPersistency() &&
107 if (isTransitionAllowed) {
108 face->setPersistency(persistency);
110 return {
false, face};
115 socket.set_option(ip::udp::socket::reuse_address(
true));
116 socket.bind(m_localEndpoint);
117 socket.connect(remoteEndpoint);
119 auto face = make_shared<UdpFace>(
FaceUri(remoteEndpoint),
FaceUri(m_localEndpoint),
120 std::move(socket), persistency, m_idleFaceTimeout);
122 face->onFail.connectSingleShot([
this, remoteEndpoint] (
const std::string&) {
123 NFD_LOG_TRACE(
"Erasing " << remoteEndpoint <<
" from channel face map");
124 m_channelFaces.erase(remoteEndpoint);
126 m_channelFaces[remoteEndpoint] = face;
132 UdpChannel::handleNewPeer(
const boost::system::error_code& error,
133 size_t nBytesReceived,
138 if (error == boost::asio::error::operation_aborted)
141 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Receive failed: " << error.message());
143 onReceiveFailed(error.message());
147 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] New peer " << m_remoteEndpoint);
150 shared_ptr<UdpFace> face;
154 catch (
const boost::system::system_error& e) {
155 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Failed to create face for peer " 156 << m_remoteEndpoint <<
": " << e.what());
158 onReceiveFailed(e.what());
166 face->receiveDatagram(m_inputBuffer, nBytesReceived, error);
170 bind(&UdpChannel::handleNewPeer,
this,
171 boost::asio::placeholders::error,
172 boost::asio::placeholders::bytes_transferred,
173 onFaceCreated, onReceiveFailed));
UdpChannel(const udp::Endpoint &localEndpoint, const time::seconds &timeout)
Create UDP channel for the local endpoint.
#define NFD_LOG_DEBUG(expression)
represents the underlying protocol and address used by a Face
void connect(const udp::Endpoint &remoteEndpoint, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &onConnectFailed)
Create a face by establishing connection to remote endpoint.
void listen(const FaceCreatedCallback &onFaceCreated, const ConnectFailedCallback &onReceiveFailed)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
#define NFD_LOG_WARN(expression)
Copyright (c) 2011-2015 Regents of the University of California.
void setUri(const FaceUri &uri)
function< void(const std::string &reason)> ConnectFailedCallback
Prototype for the callback that is called when face is failed to get created.
boost::asio::ip::udp::endpoint Endpoint
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
boost::asio::io_service & getGlobalIoService()
size_t size() const
Get number of faces in the channel.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback called when face is created (as a response to incoming connection or after...
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size