38 const time::seconds& timeout)
39 : m_localEndpoint(localEndpoint)
41 , m_idleFaceTimeout(timeout)
49 return m_channelFaces.size();
58 shared_ptr<Face>
face;
60 face = createFace(remoteEndpoint, persistency).second;
62 catch (
const boost::system::system_error& e) {
63 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Connect failed: " << e.what());
65 onConnectFailed(e.what());
79 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Already listening");
83 m_socket.open(m_localEndpoint.protocol());
84 m_socket.set_option(ip::udp::socket::reuse_address(
true));
85 if (m_localEndpoint.address().is_v6())
86 m_socket.set_option(ip::v6_only(
true));
88 m_socket.bind(m_localEndpoint);
89 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
98 bind(&UdpChannel::handleNewPeer,
this,
99 boost::asio::placeholders::error,
100 boost::asio::placeholders::bytes_transferred,
101 onFaceCreated, onReceiveFailed));
105 UdpChannel::handleNewPeer(
const boost::system::error_code& error,
106 size_t nBytesReceived,
111 if (error == boost::asio::error::operation_aborted)
114 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] Receive failed: " << error.message());
116 onReceiveFailed(error.message());
120 NFD_LOG_DEBUG(
"[" << m_localEndpoint <<
"] New peer " << m_remoteEndpoint);
122 bool isCreated =
false;
123 shared_ptr<Face>
face;
127 catch (
const boost::system::system_error& e) {
128 NFD_LOG_WARN(
"[" << m_localEndpoint <<
"] Failed to create face for peer " 129 << m_remoteEndpoint <<
": " << e.what());
131 onReceiveFailed(e.what());
141 this->waitForNewPeer(onFaceCreated, onReceiveFailed);
144 std::pair<bool, shared_ptr<Face>>
147 auto it = m_channelFaces.find(remoteEndpoint);
148 if (it != m_channelFaces.end()) {
150 auto face = it->second;
152 bool isTransitionAllowed = persistency !=
face->getPersistency() &&
155 if (isTransitionAllowed) {
156 face->setPersistency(persistency);
158 return {
false,
face};
163 socket.set_option(ip::udp::socket::reuse_address(
true));
164 socket.bind(m_localEndpoint);
165 socket.connect(remoteEndpoint);
167 auto linkService = make_unique<face::GenericLinkService>();
168 auto transport = make_unique<face::UnicastUdpTransport>(std::move(socket), persistency, m_idleFaceTimeout);
169 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
171 face->setPersistency(persistency);
173 m_channelFaces[remoteEndpoint] = face;
175 [
this, remoteEndpoint] {
176 NFD_LOG_TRACE(
"Erasing " << remoteEndpoint <<
" from channel face map");
177 m_channelFaces.erase(remoteEndpoint);
UdpChannel(const udp::Endpoint &localEndpoint, const time::seconds &timeout)
Create UDP channel for the local endpoint.
#define NFD_LOG_DEBUG(expression)
function< void(const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
represents the underlying protocol and address used by a Face
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
detail::SimulatorIo & getGlobalIoService()
void connect(const udp::Endpoint &remoteEndpoint, ndn::nfd::FacePersistency persistency, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a face by establishing connection to remote endpoint.
#define NFD_LOG_WARN(expression)
Copyright (c) 2011-2015 Regents of the University of California.
size_t size() const
Get number of faces in the channel.
void setUri(const FaceUri &uri)
boost::asio::ip::udp::endpoint Endpoint
#define NFD_LOG_INIT(name)
#define NFD_LOG_TRACE(expression)
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onReceiveFailed)
Enable listening on the local endpoint, accept connections, and create faces when remote host makes a...
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when the face is created (as a response to incoming connec...
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size