blob: 8c96b095c99de90d3164b3ff259d1c5e1340bc46 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NFD_DAEMON_FACE_UDP_CHANNEL_HPP
#define NFD_DAEMON_FACE_UDP_CHANNEL_HPP
#include "channel.hpp"
#include "udp-protocol.hpp"
#include <array>
namespace nfd::face {
/**
* \brief Class implementing a UDP-based channel to create faces.
*/
class UdpChannel final : public Channel
{
public:
/**
* \brief Create a UDP channel on the given \p localEndpoint.
*
* To enable the creation of faces upon incoming connections, one needs to
* explicitly call listen(). The created socket is bound to \p localEndpoint.
*/
UdpChannel(const udp::Endpoint& localEndpoint,
time::nanoseconds idleTimeout,
bool wantCongestionMarking,
size_t defaultMtu);
bool
isListening() const final
{
return m_socket.is_open();
}
size_t
size() const final
{
return m_channelFaces.size();
}
/**
* \brief Create a unicast UDP face toward \p remoteEndpoint.
*/
void
connect(const udp::Endpoint& remoteEndpoint,
const FaceParams& params,
const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onConnectFailed);
/**
* \brief Start listening.
*
* Enable listening on the local endpoint, waiting for incoming datagrams,
* and creating a face when a datagram is received from a new remote host.
*
* Faces created in this way will have on-demand persistency.
*
* \param onFaceCreated Callback to notify successful creation of a face
* \param onFaceCreationFailed Callback to notify errors
*/
void
listen(const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onFaceCreationFailed);
private:
void
waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onReceiveFailed);
void
handleNewPeer(const boost::system::error_code& error,
size_t nBytesReceived,
const FaceCreatedCallback& onFaceCreated,
const FaceCreationFailedCallback& onReceiveFailed);
std::pair<bool, shared_ptr<Face>>
createFace(const udp::Endpoint& remoteEndpoint,
const FaceParams& params);
private:
const udp::Endpoint m_localEndpoint;
udp::Endpoint m_remoteEndpoint; ///< The latest peer that started communicating with us
boost::asio::ip::udp::socket m_socket; ///< Socket used to "accept" new peers
std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
std::map<udp::Endpoint, shared_ptr<Face>> m_channelFaces;
const time::nanoseconds m_idleFaceTimeout; ///< Timeout for automatic closure of idle on-demand faces
const bool m_wantCongestionMarking;
};
} // namespace nfd::face
#endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP