udp-channel.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef NFD_DAEMON_FACE_UDP_CHANNEL_HPP
27 #define NFD_DAEMON_FACE_UDP_CHANNEL_HPP
28 
29 #include "channel.hpp"
30 #include "udp-protocol.hpp"
31 
32 #include <array>
33 #include <map>
34 
35 namespace nfd::face {
36 
40 class UdpChannel final : public Channel
41 {
42 public:
49  UdpChannel(const udp::Endpoint& localEndpoint,
50  time::nanoseconds idleTimeout,
51  bool wantCongestionMarking,
52  size_t defaultMtu);
53 
54  bool
55  isListening() const final
56  {
57  return m_socket.is_open();
58  }
59 
60  size_t
61  size() const final
62  {
63  return m_channelFaces.size();
64  }
65 
69  void
70  connect(const udp::Endpoint& remoteEndpoint,
71  const FaceParams& params,
72  const FaceCreatedCallback& onFaceCreated,
73  const FaceCreationFailedCallback& onConnectFailed);
74 
86  void
87  listen(const FaceCreatedCallback& onFaceCreated,
88  const FaceCreationFailedCallback& onFaceCreationFailed);
89 
90 private:
91  void
92  waitForNewPeer(const FaceCreatedCallback& onFaceCreated,
93  const FaceCreationFailedCallback& onReceiveFailed);
94 
95  void
96  handleNewPeer(const boost::system::error_code& error,
97  size_t nBytesReceived,
98  const FaceCreatedCallback& onFaceCreated,
99  const FaceCreationFailedCallback& onReceiveFailed);
100 
101  std::pair<bool, shared_ptr<Face>>
102  createFace(const udp::Endpoint& remoteEndpoint,
103  const FaceParams& params);
104 
105 private:
106  const udp::Endpoint m_localEndpoint;
107  udp::Endpoint m_remoteEndpoint;
108  boost::asio::ip::udp::socket m_socket;
109  std::array<uint8_t, ndn::MAX_NDN_PACKET_SIZE> m_receiveBuffer;
110  std::map<udp::Endpoint, shared_ptr<Face>> m_channelFaces;
111  const time::nanoseconds m_idleFaceTimeout;
112  const bool m_wantCongestionMarking;
113 };
114 
115 } // namespace nfd::face
116 
117 #endif // NFD_DAEMON_FACE_UDP_CHANNEL_HPP
Represents a channel that listens on a local endpoint.
Definition: channel.hpp:43
Class implementing a UDP-based channel to create faces.
Definition: udp-channel.hpp:41
void listen(const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onFaceCreationFailed)
Start listening.
Definition: udp-channel.cpp:77
size_t size() const final
Returns the number of faces in the channel.
Definition: udp-channel.hpp:61
void connect(const udp::Endpoint &remoteEndpoint, const FaceParams &params, const FaceCreatedCallback &onFaceCreated, const FaceCreationFailedCallback &onConnectFailed)
Create a unicast UDP face toward remoteEndpoint.
Definition: udp-channel.cpp:55
UdpChannel(const udp::Endpoint &localEndpoint, time::nanoseconds idleTimeout, bool wantCongestionMarking, size_t defaultMtu)
Create a UDP channel on the given localEndpoint.
Definition: udp-channel.cpp:40
bool isListening() const final
Returns whether the channel is listening.
Definition: udp-channel.hpp:55
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.
Definition: channel.hpp:94
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...
Definition: channel.hpp:90
boost::asio::ip::udp::endpoint Endpoint
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:87