32 #include <boost/asio/ip/v6_only.hpp>
36 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(boost::asio::socket_base::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, [=] (
auto&&... args) {
101 handleNewPeer(std::forward<decltype(args)>(args)..., onFaceCreated, onReceiveFailed);
106 UdpChannel::handleNewPeer(
const boost::system::error_code& error,
107 size_t nBytesReceived,
112 if (error != boost::asio::error::operation_aborted) {
115 onReceiveFailed(500,
"Receive failed: " + error.message());
122 bool isCreated =
false;
123 shared_ptr<Face> face;
126 params.persistency = ndn::nfd::FACE_PERSISTENCY_ON_DEMAND;
128 std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
130 catch (
const boost::system::system_error& e) {
131 NFD_LOG_CHAN_DEBUG(
"Face creation for " << m_remoteEndpoint <<
" failed: " << e.what());
133 onReceiveFailed(504,
"Face creation failed: "s + e.what());
143 auto* transport =
static_cast<UnicastUdpTransport*
>(face->getTransport());
144 transport->receiveDatagram(ndn::span(m_receiveBuffer).first(nBytesReceived), error);
146 waitForNewPeer(onFaceCreated, onReceiveFailed);
149 std::pair<bool, shared_ptr<Face>>
151 const FaceParams& params)
153 auto it = m_channelFaces.find(remoteEndpoint);
154 if (it != m_channelFaces.end()) {
157 return {
false, it->second};
162 socket.set_option(boost::asio::socket_base::reuse_address(
true));
163 socket.bind(m_localEndpoint);
164 socket.connect(remoteEndpoint);
168 options.allowReassembly =
true;
169 options.reliabilityOptions.isEnabled = params.wantLpReliability;
171 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
173 options.allowCongestionMarking = m_wantCongestionMarking;
176 options.allowCongestionMarking = bool(params.wantCongestionMarking);
179 if (params.baseCongestionMarkingInterval) {
180 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
182 if (params.defaultCongestionThreshold) {
183 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
188 auto linkService = make_unique<GenericLinkService>(options);
189 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
191 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
192 face->setChannel(weak_from_this());
194 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
GenericLinkServiceOptions Options
Options for GenericLinkService.
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_context & getGlobalIoService()
Returns the global io_context instance for the calling thread.
Parameters used to set Transport properties or LinkService options on a newly created face.
bool allowFragmentation
Enables fragmentation.