36 namespace ip = boost::asio::ip;
39 time::nanoseconds idleTimeout,
40 bool wantCongestionMarking)
41 : m_localEndpoint(localEndpoint)
43 , m_idleFaceTimeout(idleTimeout)
44 , m_wantCongestionMarking(wantCongestionMarking)
56 shared_ptr<Face> face;
58 face = createFace(remoteEndpoint, params).second;
60 catch (
const boost::system::system_error& e) {
63 onConnectFailed(504,
"Face creation failed: "s + e.what());
81 m_socket.open(m_localEndpoint.protocol());
82 m_socket.set_option(ip::udp::socket::reuse_address(
true));
83 if (m_localEndpoint.address().is_v6()) {
84 m_socket.set_option(ip::v6_only(
true));
86 m_socket.bind(m_localEndpoint);
88 waitForNewPeer(onFaceCreated, onFaceCreationFailed);
96 m_socket.async_receive_from(boost::asio::buffer(m_receiveBuffer), m_remoteEndpoint,
97 [=] (
auto&&... args) {
98 this->handleNewPeer(std::forward<decltype(args)>(args)..., 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;
124 std::tie(isCreated, face) = createFace(m_remoteEndpoint, params);
126 catch (
const boost::system::system_error& e) {
127 NFD_LOG_CHAN_DEBUG(
"Face creation for " << m_remoteEndpoint <<
" failed: " << e.what());
129 onReceiveFailed(504,
"Face creation failed: "s + e.what());
139 auto* transport = static_cast<UnicastUdpTransport*>(face->getTransport());
140 transport->receiveDatagram(m_receiveBuffer.data(), nBytesReceived, error);
142 waitForNewPeer(onFaceCreated, onReceiveFailed);
145 std::pair<bool, shared_ptr<Face>>
147 const FaceParams& params)
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);
162 GenericLinkService::Options options;
163 options.allowFragmentation =
true;
164 options.allowReassembly =
true;
165 options.reliabilityOptions.isEnabled = params.wantLpReliability;
167 if (boost::logic::indeterminate(params.wantCongestionMarking)) {
169 options.allowCongestionMarking = m_wantCongestionMarking;
172 options.allowCongestionMarking = params.wantCongestionMarking;
175 if (params.baseCongestionMarkingInterval) {
176 options.baseCongestionMarkingInterval = *params.baseCongestionMarkingInterval;
178 if (params.defaultCongestionThreshold) {
179 options.defaultCongestionThreshold = *params.defaultCongestionThreshold;
182 auto linkService = make_unique<GenericLinkService>(options);
183 auto transport = make_unique<UnicastUdpTransport>(std::move(socket), params.persistency,
184 m_idleFaceTimeout, params.mtu);
185 auto face = make_shared<Face>(std::move(linkService), std::move(transport));
187 m_channelFaces[remoteEndpoint] = face;
bool isListening() const override
Returns whether the channel is listening.
void connect(const udp::Endpoint &remoteEndpoint, const FaceParams ¶ms, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast UDP face toward remoteEndpoint.
void setUri(const FaceUri &uri)
detail::SimulatorIo & getGlobalIoService()
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 > &face)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
void connectFaceClosedSignal(Face &face, const std::function< void()> &f)
invokes a callback when the face is closed
#define NFD_LOG_CHAN_DEBUG(msg)
Log a message at DEBUG level.
Copyright (c) 2011-2015 Regents of the University of California.
#define NFD_LOG_CHAN_INFO(msg)
Log a message at INFO level.
#define NFD_LOG_CHAN_TRACE(msg)
Log a message at TRACE level.
#define NFD_LOG_CHAN_WARN(msg)
Log a message at WARN level.
UdpChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout, bool wantCongestionMarking)
Create a UDP channel on the given localEndpoint.
represents the underlying protocol and address used by a Face
boost::asio::ip::udp::endpoint Endpoint
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
#define NFD_LOG_INIT(name)
Parameters used to set Transport properties or LinkService options on a newly created face.
Class implementing UDP-based channel to create faces.