Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 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_TRANSPORT_HPP |
| 27 | #define NFD_DAEMON_FACE_TRANSPORT_HPP |
| 28 | |
Davide Pesavento | cb425e8 | 2019-07-14 21:48:22 -0400 | [diff] [blame] | 29 | #include "face-common.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 30 | #include "common/counter.hpp" |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::face { |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 33 | |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame] | 34 | /** |
| 35 | * \brief Indicates the state of a transport. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 36 | */ |
| 37 | enum class TransportState { |
| 38 | NONE, |
Davide Pesavento | 68ab43d | 2017-07-02 13:37:35 -0400 | [diff] [blame] | 39 | UP, ///< the transport is up and can transmit packets |
| 40 | DOWN, ///< the transport is temporarily down, and is being recovered |
| 41 | CLOSING, ///< the transport is being closed gracefully, either by the peer or by a call to close() |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 42 | FAILED, ///< the transport is being closed due to a failure |
| 43 | CLOSED ///< the transport is closed, and can be safely deallocated |
| 44 | }; |
| 45 | |
| 46 | std::ostream& |
| 47 | operator<<(std::ostream& os, TransportState state); |
| 48 | |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 49 | /** |
| 50 | * \brief Counters provided by a transport. |
| 51 | * \note The type name TransportCounters is an implementation detail. |
| 52 | * Use Transport::Counters in public API. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 53 | */ |
| 54 | class TransportCounters |
| 55 | { |
| 56 | public: |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 57 | /** |
| 58 | * \brief Count of incoming packets. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 59 | * |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 60 | * A 'packet' typically means a top-level TLV element. |
| 61 | * |
| 62 | * For a datagram-based transport, an incoming packet that cannot be parsed as TLV |
| 63 | * will not be counted. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 64 | */ |
| 65 | PacketCounter nInPackets; |
| 66 | |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 67 | /** |
| 68 | * \brief Count of outgoing packets. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 69 | * |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 70 | * A 'packet' typically means a top-level TLV element. |
| 71 | * |
| 72 | * This counter is incremented only when the transport is UP. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 73 | */ |
| 74 | PacketCounter nOutPackets; |
| 75 | |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 76 | /** |
| 77 | * \brief Total bytes received. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 78 | * |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 79 | * This counter includes headers imposed by NFD (such as NDNLP), but excludes the |
| 80 | * overhead of the underlying protocol (such as IP header). |
| 81 | * |
| 82 | * For a datagram-based transport, an incoming packet that cannot be parsed as TLV |
| 83 | * will not be counted. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 84 | */ |
| 85 | ByteCounter nInBytes; |
| 86 | |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 87 | /** |
| 88 | * \brief Total bytes sent. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 89 | * |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 90 | * This counter includes headers imposed by NFD (such as NDNLP), but excludes the |
| 91 | * overhead of the underlying protocol (such as IP header). |
| 92 | * |
| 93 | * This counter is increased only when the transport is UP. |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 94 | */ |
| 95 | ByteCounter nOutBytes; |
| 96 | }; |
| 97 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 98 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 99 | * \brief Indicates that the transport has no limit on payload size. |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 100 | */ |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 101 | inline constexpr ssize_t MTU_UNLIMITED = -1; |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 102 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 103 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 104 | * \brief (for internal use) Indicates that the MTU field is unset. |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 105 | */ |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 106 | inline constexpr ssize_t MTU_INVALID = -2; |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 107 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 108 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 109 | * \brief Indicates that the transport does not support reading the queue capacity/length. |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 110 | */ |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 111 | inline constexpr ssize_t QUEUE_UNSUPPORTED = -1; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 112 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 113 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 114 | * \brief Indicates that the transport was unable to retrieve the queue capacity/length. |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 115 | */ |
Davide Pesavento | 20d94f6 | 2022-09-26 03:30:53 -0400 | [diff] [blame] | 116 | inline constexpr ssize_t QUEUE_ERROR = -2; |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 117 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 118 | /** |
| 119 | * \brief The lower half of a Face. |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 120 | * \sa Face, LinkService |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 121 | */ |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 122 | class Transport : protected virtual TransportCounters, noncopyable |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 123 | { |
| 124 | public: |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 125 | /** \brief Counters provided by a transport. |
| 126 | * \sa TransportCounters |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 127 | */ |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 128 | using Counters = TransportCounters; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 129 | |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 130 | /** \brief Default constructor. |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 131 | * |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 132 | * This constructor initializes static properties to invalid values. |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 133 | * Subclass constructor must explicitly set every static property. |
| 134 | * |
| 135 | * This constructor initializes TransportState to UP; |
| 136 | * subclass constructor can rely on this default value. |
| 137 | */ |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 138 | Transport(); |
| 139 | |
| 140 | virtual |
| 141 | ~Transport(); |
| 142 | |
| 143 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 144 | /** |
| 145 | * \brief Set Face and LinkService for this transport. |
| 146 | * \pre setFaceAndLinkService() has not been called. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 147 | */ |
| 148 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 149 | setFaceAndLinkService(Face& face, LinkService& service) noexcept; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 150 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 151 | /** |
| 152 | * \brief Returns the Face to which this transport is attached. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 153 | */ |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 154 | const Face* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 155 | getFace() const noexcept |
| 156 | { |
| 157 | return m_face; |
| 158 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 159 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 160 | /** |
| 161 | * \brief Returns the LinkService to which this transport is attached. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 162 | */ |
| 163 | const LinkService* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 164 | getLinkService() const noexcept |
| 165 | { |
| 166 | return m_service; |
| 167 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 168 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 169 | /** |
| 170 | * \brief Returns the LinkService to which this transport is attached. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 171 | */ |
| 172 | LinkService* |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 173 | getLinkService() noexcept |
| 174 | { |
| 175 | return m_service; |
| 176 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 178 | virtual const Counters& |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 179 | getCounters() const |
| 180 | { |
| 181 | return *this; |
| 182 | } |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 183 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 184 | public: // upper interface |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 185 | /** \brief Request the transport to be closed. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 186 | * |
| 187 | * This operation is effective only if transport is in UP or DOWN state, |
| 188 | * otherwise it has no effect. |
| 189 | * The transport changes state to CLOSING, and performs cleanup procedure. |
| 190 | * The state will be changed to CLOSED when cleanup is complete, which may |
| 191 | * happen synchronously or asynchronously. |
| 192 | */ |
| 193 | void |
| 194 | close(); |
| 195 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 196 | /** \brief Send a link-layer packet. |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 197 | * \param packet the packet to be sent, must be a valid and well-formed TLV block |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 198 | * \note This operation has no effect if getState() is neither UP nor DOWN |
| 199 | * \warning Behavior is undefined if packet size exceeds the MTU limit |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 200 | */ |
| 201 | void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 202 | send(const Block& packet); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 203 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 204 | public: // static properties |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 205 | /** |
| 206 | * \brief Returns a FaceUri representing the local endpoint. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 207 | */ |
| 208 | FaceUri |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 209 | getLocalUri() const noexcept |
| 210 | { |
| 211 | return m_localUri; |
| 212 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 213 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 214 | /** |
| 215 | * \brief Returns a FaceUri representing the remote endpoint. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 216 | */ |
| 217 | FaceUri |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 218 | getRemoteUri() const noexcept |
| 219 | { |
| 220 | return m_remoteUri; |
| 221 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 222 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 223 | /** |
| 224 | * \brief Returns whether the transport is local or non-local for scope control purposes. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 225 | */ |
| 226 | ndn::nfd::FaceScope |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 227 | getScope() const noexcept |
| 228 | { |
| 229 | return m_scope; |
| 230 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 231 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 232 | /** |
| 233 | * \brief Returns the current persistency setting of the transport. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 234 | */ |
| 235 | ndn::nfd::FacePersistency |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 236 | getPersistency() const noexcept |
| 237 | { |
| 238 | return m_persistency; |
| 239 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 240 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 241 | /** |
| 242 | * \brief Check whether the persistency can be changed to \p newPersistency. |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 243 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 244 | * This function serves as the external API, and invokes the protected function |
| 245 | * canChangePersistencyToImpl() to perform further checks if \p newPersistency differs |
| 246 | * from the current persistency. |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 247 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 248 | * \return true if the change can be performed, false otherwise |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 249 | */ |
| 250 | bool |
| 251 | canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const; |
| 252 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 253 | /** |
| 254 | * \brief Changes the persistency setting of the transport. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 255 | */ |
| 256 | void |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 257 | setPersistency(ndn::nfd::FacePersistency newPersistency); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 258 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 259 | /** |
| 260 | * \brief Returns the link type of the transport. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 261 | */ |
| 262 | ndn::nfd::LinkType |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 263 | getLinkType() const noexcept |
| 264 | { |
| 265 | return m_linkType; |
| 266 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 267 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 268 | /** |
| 269 | * \brief Returns the maximum payload size. |
| 270 | * \retval MTU_UNLIMITED The transport has no limit on payload size. |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 271 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 272 | * This size is the maximum packet size that can be sent or received through this transport. |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 273 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 274 | * For a datagram-based transport, this is typically the Maximum Transmission Unit (MTU), |
| 275 | * after the overhead of headers introduced by the transport has been accounted for. |
| 276 | * For a stream-based transport, this is typically unlimited (MTU_UNLIMITED). |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 277 | */ |
| 278 | ssize_t |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 279 | getMtu() const noexcept |
| 280 | { |
| 281 | return m_mtu; |
| 282 | } |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 283 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 284 | /** |
| 285 | * \brief Returns the capacity of the send queue (in bytes). |
| 286 | * \retval QUEUE_UNSUPPORTED The transport does not support queue capacity retrieval. |
| 287 | * \retval QUEUE_ERROR The transport was unable to retrieve the queue capacity. |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 288 | */ |
| 289 | ssize_t |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 290 | getSendQueueCapacity() const noexcept |
| 291 | { |
| 292 | return m_sendQueueCapacity; |
| 293 | } |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 294 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 295 | public: // dynamic properties |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 296 | /** |
| 297 | * \brief Returns the current transport state. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 298 | */ |
| 299 | TransportState |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 300 | getState() const noexcept |
| 301 | { |
| 302 | return m_state; |
| 303 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 304 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 305 | /** |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 306 | * \brief Called when the transport state changes. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 307 | */ |
Davide Pesavento | e0b67df | 2024-02-17 19:14:24 -0500 | [diff] [blame^] | 308 | signal::Signal<Transport, TransportState /*old*/, TransportState /*new*/> afterStateChange; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 309 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 310 | /** |
| 311 | * \brief Returns the expiration time of the transport. |
| 312 | * \retval time::steady_clock::time_point::max() The transport has an indefinite lifetime. |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 313 | */ |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 314 | time::steady_clock::time_point |
| 315 | getExpirationTime() const noexcept |
| 316 | { |
| 317 | return m_expirationTime; |
| 318 | } |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 319 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 320 | /** |
| 321 | * \brief Returns the current send queue length of the transport (in octets). |
| 322 | * \retval QUEUE_UNSUPPORTED The transport does not support queue length retrieval. |
| 323 | * \retval QUEUE_ERROR The transport was unable to retrieve the queue length. |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 324 | */ |
| 325 | virtual ssize_t |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 326 | getSendQueueLength() |
| 327 | { |
| 328 | return QUEUE_UNSUPPORTED; |
| 329 | } |
| 330 | |
| 331 | protected: // upper interface to be invoked by subclass |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame] | 332 | /** |
| 333 | * \brief Pass a received link-layer packet to the upper layer for further processing. |
| 334 | * \param packet The received packet, must be a valid and well-formed TLV block |
| 335 | * \param endpoint The source endpoint, optional for unicast transports |
| 336 | * \warning Behavior is undefined if packet size exceeds the MTU limit. |
Eric Newberry | 812d615 | 2018-06-06 15:06:01 -0700 | [diff] [blame] | 337 | */ |
| 338 | void |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame] | 339 | receive(const Block& packet, const EndpointId& endpoint = {}); |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 340 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 341 | protected: // properties to be set by subclass |
| 342 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 343 | setLocalUri(const FaceUri& uri) noexcept |
| 344 | { |
| 345 | m_localUri = uri; |
| 346 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 347 | |
| 348 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 349 | setRemoteUri(const FaceUri& uri) noexcept |
| 350 | { |
| 351 | m_remoteUri = uri; |
| 352 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 353 | |
| 354 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 355 | setScope(ndn::nfd::FaceScope scope) noexcept |
| 356 | { |
| 357 | m_scope = scope; |
| 358 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 359 | |
| 360 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 361 | setLinkType(ndn::nfd::LinkType linkType) noexcept |
| 362 | { |
| 363 | m_linkType = linkType; |
| 364 | } |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 365 | |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 366 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 367 | setMtu(ssize_t mtu) noexcept; |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 368 | |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 369 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 370 | setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept |
| 371 | { |
| 372 | m_sendQueueCapacity = sendQueueCapacity; |
| 373 | } |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 374 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 375 | /** \brief Set transport state. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 376 | * |
| 377 | * Only the following transitions are valid: |
| 378 | * UP->DOWN, DOWN->UP, UP/DOWN->CLOSING/FAILED, CLOSING/FAILED->CLOSED |
| 379 | * |
| 380 | * \throw std::runtime_error transition is invalid. |
| 381 | */ |
| 382 | void |
| 383 | setState(TransportState newState); |
| 384 | |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 385 | void |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 386 | setExpirationTime(const time::steady_clock::time_point& expirationTime) noexcept |
| 387 | { |
| 388 | m_expirationTime = expirationTime; |
| 389 | } |
Eric Newberry | c64d30a | 2015-12-26 11:07:27 -0700 | [diff] [blame] | 390 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 391 | protected: // to be overridden by subclass |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 392 | /** \brief Invoked by canChangePersistencyTo to perform the check. |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 393 | * |
| 394 | * Base class implementation returns false. |
| 395 | * |
| 396 | * \param newPersistency the new persistency, guaranteed to be different from current persistency |
| 397 | */ |
| 398 | virtual bool |
| 399 | canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const; |
| 400 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 401 | /** \brief Invoked after the persistency has been changed. |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 402 | * |
| 403 | * The base class implementation does nothing. |
| 404 | * When overridden in a subclass, the function should update internal states |
| 405 | * after persistency setting has been changed. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 406 | */ |
| 407 | virtual void |
Yanbiao Li | 32dab97 | 2016-11-27 12:26:09 +0800 | [diff] [blame] | 408 | afterChangePersistency(ndn::nfd::FacePersistency oldPersistency); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 409 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 410 | /** \brief Performs Transport specific operations to close the transport. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 411 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 412 | * This is invoked once by close() after changing state to CLOSING. |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 413 | * It will not be invoked by Transport class if the transport is already CLOSING or CLOSED. |
| 414 | * |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 415 | * When the cleanup procedure is complete, this method should change state to CLOSED. |
Junxiao Shi | 1354611 | 2015-10-14 19:33:07 -0700 | [diff] [blame] | 416 | * This transition can happen synchronously or asynchronously. |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 417 | */ |
| 418 | virtual void |
| 419 | doClose() = 0; |
| 420 | |
| 421 | private: // to be overridden by subclass |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 422 | /** \brief Performs Transport specific operations to send a packet. |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 423 | * \param packet the packet to be sent, can be assumed to be valid and well-formed |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 424 | * \pre transport state is either UP or DOWN |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 425 | */ |
| 426 | virtual void |
Teng Liang | 13d582a | 2020-07-21 20:23:11 -0700 | [diff] [blame] | 427 | doSend(const Block& packet) = 0; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 428 | |
| 429 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 430 | Face* m_face = nullptr; |
| 431 | LinkService* m_service = nullptr; |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 432 | FaceUri m_localUri; |
| 433 | FaceUri m_remoteUri; |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 434 | ndn::nfd::FaceScope m_scope = ndn::nfd::FACE_SCOPE_NONE; |
| 435 | ndn::nfd::FacePersistency m_persistency = ndn::nfd::FACE_PERSISTENCY_NONE; |
| 436 | ndn::nfd::LinkType m_linkType = ndn::nfd::LINK_TYPE_NONE; |
| 437 | ssize_t m_mtu = MTU_INVALID; |
| 438 | ssize_t m_sendQueueCapacity = QUEUE_UNSUPPORTED; |
| 439 | TransportState m_state = TransportState::UP; |
| 440 | time::steady_clock::time_point m_expirationTime = time::steady_clock::time_point::max(); |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 441 | }; |
| 442 | |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 443 | std::ostream& |
| 444 | operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh); |
| 445 | |
| 446 | template<typename T> |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 447 | std::enable_if_t<std::is_base_of_v<Transport, T> && !std::is_same_v<Transport, T>, |
| 448 | std::ostream&> |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 449 | operator<<(std::ostream& os, const FaceLogHelper<T>& flh) |
| 450 | { |
| 451 | return os << FaceLogHelper<Transport>(flh.obj); |
| 452 | } |
| 453 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 454 | } // namespace nfd::face |
Eric Newberry | a98bf93 | 2015-09-21 00:58:47 -0700 | [diff] [blame] | 455 | |
| 456 | #endif // NFD_DAEMON_FACE_TRANSPORT_HPP |