32 #include <boost/asio/ip/v6_only.hpp>
38 namespace ip = boost::asio::ip;
41 time::nanoseconds idleTimeout,
42 bool wantCongestionMarking,
44 : m_localEndpoint(localEndpoint)
46 , m_idleFaceTimeout(idleTimeout)
47 , m_wantCongestionMarking(wantCongestionMarking)
49 setUri(FaceUri(m_localEndpoint));
60 shared_ptr<Face> face;
62 face = createFace(remoteEndpoint, params).second;
64 catch (
const boost::system::system_error& e) {
67 onConnectFailed(504,
"Face creation failed: "s + e.what());
85 m_socket.open(m_localEndpoint.protocol());
86 m_socket.set_option(ip::udp::socket::reuse_address(
true));
87 if (m_localEndpoint.address().is_v6()) {
88 m_socket.set_option(ip::v6_only(
true));
90 m_socket.bind(m_localEndpoint);
92 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
100 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
101 [=] (
auto&&... args) {
102 this->handleNewPeer(std::forward<decltype(args)>(args)..., onFaceCreated, onReceiveFailed);
107 UdpChannel::handleNewPeer(
const boost::system::error_code& error,
108 size_t nBytesReceived,
113 if (error != boost::asio::error::operation_aborted) {
116 onReceiveFailed(500,
"Receive failed: " + error.message());
123 bool isCreated =
false;
124 shared_ptr<Face> face;
127 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
129 std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
131 catch (
const boost::system::system_error& e) {
132 NFD_LOG_CHAN_DEBUG(
"Face creation for " << m_remoteEndpoint <<
" failed: " << e.what());
134 onReceiveFailed(504,
"Face creation failed: "s + e.what());
144 auto* transport =
static_cast<UnicastUdpTransport*
>(face->getTransport());
145 transport->receiveDatagram(ndn::make_span(m_receiveBuffer).first(nBytesReceived), error);
147 waitForNewPeer(onFaceCreated, onReceiveFailed);
150 std::pair<bool, shared_ptr<Face>>
152 const FaceParams& params)
154 auto it = m_channelFaces.find(remoteEndpoint);
155 if (it != m_channelFaces.end()) {
158 return {
false, it->second};
163 socket.set_option(ip::udp::socket::reuse_address(
true));
164 socket.bind(m_localEndpoint);
165 socket.connect(remoteEndpoint);
167 GenericLinkService::Options options;
168 options.allowFragmentation =
true;
169 options.allowReassembly =
true;
170 options.reliabilityOptions.isEnabled = params.wantLpReliability;
172 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
174 options.allowCongestionMarking = m_wantCongestionMarking;
177 options.allowCongestionMarking = bool(params.wantCongestionMarking);
180 if (params.baseCongestionMarkingInterval) {
181 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
183 if (params.defaultCongestionThreshold) {
184 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
189 auto linkService = make_unique<GenericLinkService>(options);
190 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
192 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
193 face->setChannel(weak_from_this());
195 m_channelFaces[remoteEndpoint] = face;
void setUri(const FaceUri &uri) noexcept
size_t getDefaultMtu() const noexcept
Returns the default MTU for all faces created by this channel.
void setDefaultMtu(size_t mtu) noexcept
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
void connect(const udp::Endpoint &remoteEndpoint, const FaceParams ¶ms, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast UDP face toward remoteEndpoint.
UdpChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout, bool wantCongestionMarking, size_t defaultMtu)
Create a UDP channel on the given localEndpoint.
bool isListening() const final
Returns whether the channel is listening.
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_INIT(name)
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
void connectFaceClosedSignal(Face &face, std::function< void()> f)
Invokes a callback when a face is closed.
boost::asio::ip::udp::endpoint Endpoint
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.
Parameters used to set Transport properties or LinkService options on a newly created face.