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