blob: 33cef807eb3218f74123e4a11017d72cfca62287 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryb49313d2017-12-24 20:22:27 -07002/*
Davide Pesaventoe0b67df2024-02-17 19:14:24 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * 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 Pesaventocb425e82019-07-14 21:48:22 -040029#include "face-common.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040030#include "common/counter.hpp"
Eric Newberryb49313d2017-12-24 20:22:27 -070031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::face {
Eric Newberrya98bf932015-09-21 00:58:47 -070033
Teng Liangd94b7b32022-07-10 21:29:37 +080034/**
35 * \brief Indicates the state of a transport.
Eric Newberrya98bf932015-09-21 00:58:47 -070036 */
37enum class TransportState {
38 NONE,
Davide Pesavento68ab43d2017-07-02 13:37:35 -040039 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 Newberrya98bf932015-09-21 00:58:47 -070042 FAILED, ///< the transport is being closed due to a failure
43 CLOSED ///< the transport is closed, and can be safely deallocated
44};
45
46std::ostream&
47operator<<(std::ostream& os, TransportState state);
48
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050049/**
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 Shi57df2882015-11-11 06:12:35 -070053 */
54class TransportCounters
55{
56public:
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050057 /**
58 * \brief Count of incoming packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070059 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050060 * 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 Shi57df2882015-11-11 06:12:35 -070064 */
65 PacketCounter nInPackets;
66
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050067 /**
68 * \brief Count of outgoing packets.
Junxiao Shi57df2882015-11-11 06:12:35 -070069 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050070 * A 'packet' typically means a top-level TLV element.
71 *
72 * This counter is incremented only when the transport is UP.
Junxiao Shi57df2882015-11-11 06:12:35 -070073 */
74 PacketCounter nOutPackets;
75
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050076 /**
77 * \brief Total bytes received.
Junxiao Shi57df2882015-11-11 06:12:35 -070078 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050079 * 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 Shi57df2882015-11-11 06:12:35 -070084 */
85 ByteCounter nInBytes;
86
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050087 /**
88 * \brief Total bytes sent.
Junxiao Shi57df2882015-11-11 06:12:35 -070089 *
Davide Pesaventoe0b67df2024-02-17 19:14:24 -050090 * 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 Shi57df2882015-11-11 06:12:35 -070094 */
95 ByteCounter nOutBytes;
96};
97
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040098/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040099 * \brief Indicates that the transport has no limit on payload size.
Junxiao Shi13546112015-10-14 19:33:07 -0700100 */
Davide Pesavento20d94f62022-09-26 03:30:53 -0400101inline constexpr ssize_t MTU_UNLIMITED = -1;
Junxiao Shi13546112015-10-14 19:33:07 -0700102
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400103/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400104 * \brief (for internal use) Indicates that the MTU field is unset.
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700105 */
Davide Pesavento20d94f62022-09-26 03:30:53 -0400106inline constexpr ssize_t MTU_INVALID = -2;
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700107
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400108/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400109 * \brief Indicates that the transport does not support reading the queue capacity/length.
Eric Newberryb49313d2017-12-24 20:22:27 -0700110 */
Davide Pesavento20d94f62022-09-26 03:30:53 -0400111inline constexpr ssize_t QUEUE_UNSUPPORTED = -1;
Eric Newberryb49313d2017-12-24 20:22:27 -0700112
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400113/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400114 * \brief Indicates that the transport was unable to retrieve the queue capacity/length.
Eric Newberryb49313d2017-12-24 20:22:27 -0700115 */
Davide Pesavento20d94f62022-09-26 03:30:53 -0400116inline constexpr ssize_t QUEUE_ERROR = -2;
Eric Newberryb49313d2017-12-24 20:22:27 -0700117
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400118/**
119 * \brief The lower half of a Face.
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400120 * \sa Face, LinkService
Eric Newberrya98bf932015-09-21 00:58:47 -0700121 */
Junxiao Shi57df2882015-11-11 06:12:35 -0700122class Transport : protected virtual TransportCounters, noncopyable
Eric Newberrya98bf932015-09-21 00:58:47 -0700123{
124public:
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400125 /** \brief Counters provided by a transport.
126 * \sa TransportCounters
Eric Newberrya98bf932015-09-21 00:58:47 -0700127 */
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400128 using Counters = TransportCounters;
Eric Newberrya98bf932015-09-21 00:58:47 -0700129
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400130 /** \brief Default constructor.
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700131 *
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400132 * This constructor initializes static properties to invalid values.
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700133 * 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 Newberrya98bf932015-09-21 00:58:47 -0700138 Transport();
139
140 virtual
141 ~Transport();
142
143public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400144 /**
145 * \brief Set Face and LinkService for this transport.
146 * \pre setFaceAndLinkService() has not been called.
Eric Newberrya98bf932015-09-21 00:58:47 -0700147 */
148 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400149 setFaceAndLinkService(Face& face, LinkService& service) noexcept;
Eric Newberrya98bf932015-09-21 00:58:47 -0700150
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400151 /**
152 * \brief Returns the Face to which this transport is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -0700153 */
Junxiao Shicde37ad2015-12-24 01:02:05 -0700154 const Face*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400155 getFace() const noexcept
156 {
157 return m_face;
158 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700159
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400160 /**
161 * \brief Returns the LinkService to which this transport is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -0700162 */
163 const LinkService*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400164 getLinkService() const noexcept
165 {
166 return m_service;
167 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700168
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400169 /**
170 * \brief Returns the LinkService to which this transport is attached.
Eric Newberrya98bf932015-09-21 00:58:47 -0700171 */
172 LinkService*
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400173 getLinkService() noexcept
174 {
175 return m_service;
176 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700177
Junxiao Shi57df2882015-11-11 06:12:35 -0700178 virtual const Counters&
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400179 getCounters() const
180 {
181 return *this;
182 }
Junxiao Shi57df2882015-11-11 06:12:35 -0700183
Eric Newberrya98bf932015-09-21 00:58:47 -0700184public: // upper interface
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400185 /** \brief Request the transport to be closed.
Eric Newberrya98bf932015-09-21 00:58:47 -0700186 *
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 Pesaventoaa9e3b22022-10-21 17:00:07 -0400196 /** \brief Send a link-layer packet.
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400197 * \param packet the packet to be sent, must be a valid and well-formed TLV block
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400198 * \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 Newberrya98bf932015-09-21 00:58:47 -0700200 */
201 void
Teng Liang13d582a2020-07-21 20:23:11 -0700202 send(const Block& packet);
Eric Newberrya98bf932015-09-21 00:58:47 -0700203
Eric Newberrya98bf932015-09-21 00:58:47 -0700204public: // static properties
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400205 /**
206 * \brief Returns a FaceUri representing the local endpoint.
Eric Newberrya98bf932015-09-21 00:58:47 -0700207 */
208 FaceUri
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400209 getLocalUri() const noexcept
210 {
211 return m_localUri;
212 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700213
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400214 /**
215 * \brief Returns a FaceUri representing the remote endpoint.
Eric Newberrya98bf932015-09-21 00:58:47 -0700216 */
217 FaceUri
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400218 getRemoteUri() const noexcept
219 {
220 return m_remoteUri;
221 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700222
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400223 /**
224 * \brief Returns whether the transport is local or non-local for scope control purposes.
Eric Newberrya98bf932015-09-21 00:58:47 -0700225 */
226 ndn::nfd::FaceScope
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400227 getScope() const noexcept
228 {
229 return m_scope;
230 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700231
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400232 /**
233 * \brief Returns the current persistency setting of the transport.
Eric Newberrya98bf932015-09-21 00:58:47 -0700234 */
235 ndn::nfd::FacePersistency
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400236 getPersistency() const noexcept
237 {
238 return m_persistency;
239 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700240
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400241 /**
242 * \brief Check whether the persistency can be changed to \p newPersistency.
Yanbiao Li32dab972016-11-27 12:26:09 +0800243 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400244 * 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 Li32dab972016-11-27 12:26:09 +0800247 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400248 * \return true if the change can be performed, false otherwise
Yanbiao Li32dab972016-11-27 12:26:09 +0800249 */
250 bool
251 canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const;
252
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400253 /**
254 * \brief Changes the persistency setting of the transport.
Eric Newberrya98bf932015-09-21 00:58:47 -0700255 */
256 void
Davide Pesavento32065652017-01-15 01:52:21 -0500257 setPersistency(ndn::nfd::FacePersistency newPersistency);
Eric Newberrya98bf932015-09-21 00:58:47 -0700258
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400259 /**
260 * \brief Returns the link type of the transport.
Eric Newberrya98bf932015-09-21 00:58:47 -0700261 */
262 ndn::nfd::LinkType
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400263 getLinkType() const noexcept
264 {
265 return m_linkType;
266 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700267
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400268 /**
269 * \brief Returns the maximum payload size.
270 * \retval MTU_UNLIMITED The transport has no limit on payload size.
Junxiao Shi13546112015-10-14 19:33:07 -0700271 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400272 * This size is the maximum packet size that can be sent or received through this transport.
Junxiao Shi13546112015-10-14 19:33:07 -0700273 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400274 * 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 Shi13546112015-10-14 19:33:07 -0700277 */
278 ssize_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400279 getMtu() const noexcept
280 {
281 return m_mtu;
282 }
Junxiao Shi13546112015-10-14 19:33:07 -0700283
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400284 /**
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 Newberryb49313d2017-12-24 20:22:27 -0700288 */
289 ssize_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400290 getSendQueueCapacity() const noexcept
291 {
292 return m_sendQueueCapacity;
293 }
Eric Newberryb49313d2017-12-24 20:22:27 -0700294
Eric Newberrya98bf932015-09-21 00:58:47 -0700295public: // dynamic properties
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400296 /**
297 * \brief Returns the current transport state.
Eric Newberrya98bf932015-09-21 00:58:47 -0700298 */
299 TransportState
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400300 getState() const noexcept
301 {
302 return m_state;
303 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700304
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400305 /**
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500306 * \brief Called when the transport state changes.
Eric Newberrya98bf932015-09-21 00:58:47 -0700307 */
Davide Pesaventoe0b67df2024-02-17 19:14:24 -0500308 signal::Signal<Transport, TransportState /*old*/, TransportState /*new*/> afterStateChange;
Eric Newberrya98bf932015-09-21 00:58:47 -0700309
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400310 /**
311 * \brief Returns the expiration time of the transport.
312 * \retval time::steady_clock::time_point::max() The transport has an indefinite lifetime.
Eric Newberryc64d30a2015-12-26 11:07:27 -0700313 */
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400314 time::steady_clock::time_point
315 getExpirationTime() const noexcept
316 {
317 return m_expirationTime;
318 }
Eric Newberryc64d30a2015-12-26 11:07:27 -0700319
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400320 /**
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 Newberryb49313d2017-12-24 20:22:27 -0700324 */
325 virtual ssize_t
Eric Newberry812d6152018-06-06 15:06:01 -0700326 getSendQueueLength()
327 {
328 return QUEUE_UNSUPPORTED;
329 }
330
331protected: // upper interface to be invoked by subclass
Teng Liangd94b7b32022-07-10 21:29:37 +0800332 /**
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 Newberry812d6152018-06-06 15:06:01 -0700337 */
338 void
Teng Liangd94b7b32022-07-10 21:29:37 +0800339 receive(const Block& packet, const EndpointId& endpoint = {});
Eric Newberryb49313d2017-12-24 20:22:27 -0700340
Eric Newberrya98bf932015-09-21 00:58:47 -0700341protected: // properties to be set by subclass
342 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400343 setLocalUri(const FaceUri& uri) noexcept
344 {
345 m_localUri = uri;
346 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700347
348 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400349 setRemoteUri(const FaceUri& uri) noexcept
350 {
351 m_remoteUri = uri;
352 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700353
354 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400355 setScope(ndn::nfd::FaceScope scope) noexcept
356 {
357 m_scope = scope;
358 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700359
360 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400361 setLinkType(ndn::nfd::LinkType linkType) noexcept
362 {
363 m_linkType = linkType;
364 }
Eric Newberrya98bf932015-09-21 00:58:47 -0700365
Junxiao Shi13546112015-10-14 19:33:07 -0700366 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400367 setMtu(ssize_t mtu) noexcept;
Junxiao Shi13546112015-10-14 19:33:07 -0700368
Eric Newberryb49313d2017-12-24 20:22:27 -0700369 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400370 setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
371 {
372 m_sendQueueCapacity = sendQueueCapacity;
373 }
Eric Newberryb49313d2017-12-24 20:22:27 -0700374
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400375 /** \brief Set transport state.
Eric Newberrya98bf932015-09-21 00:58:47 -0700376 *
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 Newberryc64d30a2015-12-26 11:07:27 -0700385 void
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400386 setExpirationTime(const time::steady_clock::time_point& expirationTime) noexcept
387 {
388 m_expirationTime = expirationTime;
389 }
Eric Newberryc64d30a2015-12-26 11:07:27 -0700390
Eric Newberrya98bf932015-09-21 00:58:47 -0700391protected: // to be overridden by subclass
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400392 /** \brief Invoked by canChangePersistencyTo to perform the check.
Yanbiao Li32dab972016-11-27 12:26:09 +0800393 *
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 Pesaventoaa9e3b22022-10-21 17:00:07 -0400401 /** \brief Invoked after the persistency has been changed.
Yanbiao Li32dab972016-11-27 12:26:09 +0800402 *
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 Newberrya98bf932015-09-21 00:58:47 -0700406 */
407 virtual void
Yanbiao Li32dab972016-11-27 12:26:09 +0800408 afterChangePersistency(ndn::nfd::FacePersistency oldPersistency);
Eric Newberrya98bf932015-09-21 00:58:47 -0700409
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400410 /** \brief Performs Transport specific operations to close the transport.
Eric Newberrya98bf932015-09-21 00:58:47 -0700411 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400412 * This is invoked once by close() after changing state to CLOSING.
Junxiao Shi13546112015-10-14 19:33:07 -0700413 * It will not be invoked by Transport class if the transport is already CLOSING or CLOSED.
414 *
Eric Newberrya98bf932015-09-21 00:58:47 -0700415 * When the cleanup procedure is complete, this method should change state to CLOSED.
Junxiao Shi13546112015-10-14 19:33:07 -0700416 * This transition can happen synchronously or asynchronously.
Eric Newberrya98bf932015-09-21 00:58:47 -0700417 */
418 virtual void
419 doClose() = 0;
420
421private: // to be overridden by subclass
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400422 /** \brief Performs Transport specific operations to send a packet.
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400423 * \param packet the packet to be sent, can be assumed to be valid and well-formed
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400424 * \pre transport state is either UP or DOWN
Eric Newberrya98bf932015-09-21 00:58:47 -0700425 */
426 virtual void
Teng Liang13d582a2020-07-21 20:23:11 -0700427 doSend(const Block& packet) = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700428
429private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400430 Face* m_face = nullptr;
431 LinkService* m_service = nullptr;
Eric Newberrya98bf932015-09-21 00:58:47 -0700432 FaceUri m_localUri;
433 FaceUri m_remoteUri;
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400434 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 Newberrya98bf932015-09-21 00:58:47 -0700441};
442
Eric Newberrya98bf932015-09-21 00:58:47 -0700443std::ostream&
444operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
445
446template<typename T>
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400447std::enable_if_t<std::is_base_of_v<Transport, T> && !std::is_same_v<Transport, T>,
448 std::ostream&>
Eric Newberrya98bf932015-09-21 00:58:47 -0700449operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
450{
451 return os << FaceLogHelper<Transport>(flh.obj);
452}
453
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400454} // namespace nfd::face
Eric Newberrya98bf932015-09-21 00:58:47 -0700455
456#endif // NFD_DAEMON_FACE_TRANSPORT_HPP