NFD: Named Data Networking Forwarding Daemon 24.07-28-gdcc0e6e0
Loading...
Searching...
No Matches
transport.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_TRANSPORT_HPP
27#define NFD_DAEMON_FACE_TRANSPORT_HPP
28
29#include "face-common.hpp"
30#include "common/counter.hpp"
31
32namespace nfd::face {
33
37enum class TransportState {
38 NONE,
39 UP,
40 DOWN,
41 CLOSING,
42 FAILED,
43 CLOSED
44};
45
46std::ostream&
47operator<<(std::ostream& os, TransportState state);
48
97
101inline constexpr ssize_t MTU_UNLIMITED = -1;
102
106inline constexpr ssize_t MTU_INVALID = -2;
107
111inline constexpr ssize_t QUEUE_UNSUPPORTED = -1;
112
116inline constexpr ssize_t QUEUE_ERROR = -2;
117
122class Transport : protected virtual TransportCounters, noncopyable
123{
124public:
129
139
140 virtual
142
143public:
148 void
149 setFaceAndLinkService(Face& face, LinkService& service) noexcept;
150
154 const Face*
155 getFace() const noexcept
156 {
157 return m_face;
158 }
159
163 const LinkService*
164 getLinkService() const noexcept
165 {
166 return m_service;
167 }
168
173 getLinkService() noexcept
174 {
175 return m_service;
176 }
177
178 virtual const Counters&
180 {
181 return *this;
182 }
183
184public: // upper interface
193 void
194 close();
195
201 void
202 send(const Block& packet);
203
204public: // static properties
208 FaceUri
209 getLocalUri() const noexcept
210 {
211 return m_localUri;
212 }
213
217 FaceUri
218 getRemoteUri() const noexcept
219 {
220 return m_remoteUri;
221 }
222
226 ndn::nfd::FaceScope
227 getScope() const noexcept
228 {
229 return m_scope;
230 }
231
235 ndn::nfd::FacePersistency
236 getPersistency() const noexcept
237 {
238 return m_persistency;
239 }
240
250 bool
251 canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const;
252
256 void
257 setPersistency(ndn::nfd::FacePersistency newPersistency);
258
262 ndn::nfd::LinkType
263 getLinkType() const noexcept
264 {
265 return m_linkType;
266 }
267
278 ssize_t
279 getMtu() const noexcept
280 {
281 return m_mtu;
282 }
283
289 ssize_t
290 getSendQueueCapacity() const noexcept
291 {
292 return m_sendQueueCapacity;
293 }
294
295public: // dynamic properties
300 getState() const noexcept
301 {
302 return m_state;
303 }
304
308 signal::Signal<Transport, TransportState /*old*/, TransportState /*new*/> afterStateChange;
309
314 time::steady_clock::time_point
315 getExpirationTime() const noexcept
316 {
317 return m_expirationTime;
318 }
319
325 virtual ssize_t
327 {
328 return QUEUE_UNSUPPORTED;
329 }
330
331protected: // upper interface to be invoked by subclass
338 void
339 receive(const Block& packet, const EndpointId& endpoint = {});
340
341protected: // properties to be set by subclass
342 void
343 setLocalUri(const FaceUri& uri) noexcept
344 {
345 m_localUri = uri;
346 }
347
348 void
349 setRemoteUri(const FaceUri& uri) noexcept
350 {
351 m_remoteUri = uri;
352 }
353
354 void
355 setScope(ndn::nfd::FaceScope scope) noexcept
356 {
357 m_scope = scope;
358 }
359
360 void
361 setLinkType(ndn::nfd::LinkType linkType) noexcept
362 {
363 m_linkType = linkType;
364 }
365
366 void
367 setMtu(ssize_t mtu) noexcept;
368
369 void
370 setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
371 {
372 m_sendQueueCapacity = sendQueueCapacity;
373 }
374
382 void
383 setState(TransportState newState);
384
385 void
386 setExpirationTime(const time::steady_clock::time_point& expirationTime) noexcept
387 {
388 m_expirationTime = expirationTime;
389 }
390
391protected: // to be overridden by subclass
398 virtual bool
399 canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const;
400
407 virtual void
408 afterChangePersistency(ndn::nfd::FacePersistency oldPersistency);
409
418 virtual void
419 doClose() = 0;
420
421private: // to be overridden by subclass
426 virtual void
427 doSend(const Block& packet) = 0;
428
429private:
430 Face* m_face = nullptr;
431 LinkService* m_service = nullptr;
432 FaceUri m_localUri;
433 FaceUri m_remoteUri;
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;
440 time::steady_clock::time_point m_expirationTime = time::steady_clock::time_point::max();
441};
442
443std::ostream&
444operator<<(std::ostream& os, const FaceLogHelper<Transport>& flh);
445
446template<typename T>
447std::enable_if_t<std::is_base_of_v<Transport, T> && !std::is_same_v<Transport, T>,
448 std::ostream&>
449operator<<(std::ostream& os, const FaceLogHelper<T>& flh)
450{
451 return os << FaceLogHelper<Transport>(flh.obj);
452}
453
454} // namespace nfd::face
455
456#endif // NFD_DAEMON_FACE_TRANSPORT_HPP
Represents a counter of number of bytes.
Definition counter.hpp:90
Represents a counter of number of packets.
Definition counter.hpp:71
Generalization of a network interface.
Definition face.hpp:118
For internal use by FaceLogging macros.
Counters provided by a transport.
Definition transport.hpp:55
ByteCounter nInBytes
Total bytes received.
Definition transport.hpp:85
PacketCounter nInPackets
Count of incoming packets.
Definition transport.hpp:65
PacketCounter nOutPackets
Count of outgoing packets.
Definition transport.hpp:74
ByteCounter nOutBytes
Total bytes sent.
Definition transport.hpp:95
The lower half of a Face.
void setScope(ndn::nfd::FaceScope scope) noexcept
void receive(const Block &packet, const EndpointId &endpoint={})
Pass a received link-layer packet to the upper layer for further processing.
void setPersistency(ndn::nfd::FacePersistency newPersistency)
Changes the persistency setting of the transport.
ssize_t getMtu() const noexcept
Returns the maximum payload size.
FaceUri getLocalUri() const noexcept
Returns a FaceUri representing the local endpoint.
ndn::nfd::FacePersistency getPersistency() const noexcept
Returns the current persistency setting of the transport.
virtual void afterChangePersistency(ndn::nfd::FacePersistency oldPersistency)
Invoked after the persistency has been changed.
ndn::nfd::FaceScope getScope() const noexcept
Returns whether the transport is local or non-local for scope control purposes.
void setMtu(ssize_t mtu) noexcept
void send(const Block &packet)
Send a link-layer packet.
Definition transport.cpp:80
void setFaceAndLinkService(Face &face, LinkService &service) noexcept
Set Face and LinkService for this transport.
Definition transport.cpp:57
signal::Signal< Transport, TransportState, TransportState > afterStateChange
Called when the transport state changes.
LinkService * getLinkService() noexcept
Returns the LinkService to which this transport is attached.
virtual const Counters & getCounters() const
TransportState getState() const noexcept
Returns the current transport state.
virtual void doClose()=0
Performs Transport specific operations to close the transport.
bool canChangePersistencyTo(ndn::nfd::FacePersistency newPersistency) const
Check whether the persistency can be changed to newPersistency.
const LinkService * getLinkService() const noexcept
Returns the LinkService to which this transport is attached.
const Face * getFace() const noexcept
Returns the Face to which this transport is attached.
void setExpirationTime(const time::steady_clock::time_point &expirationTime) noexcept
TransportCounters Counters
Counters provided by a transport.
FaceUri getRemoteUri() const noexcept
Returns a FaceUri representing the remote endpoint.
Transport()
Default constructor.
virtual bool canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
Invoked by canChangePersistencyTo to perform the check.
void setSendQueueCapacity(ssize_t sendQueueCapacity) noexcept
void setState(TransportState newState)
Set transport state.
void setLocalUri(const FaceUri &uri) noexcept
time::steady_clock::time_point getExpirationTime() const noexcept
Returns the expiration time of the transport.
ssize_t getSendQueueCapacity() const noexcept
Returns the capacity of the send queue (in bytes).
void setLinkType(ndn::nfd::LinkType linkType) noexcept
void setRemoteUri(const FaceUri &uri) noexcept
ndn::nfd::LinkType getLinkType() const noexcept
Returns the link type of the transport.
virtual ssize_t getSendQueueLength()
Returns the current send queue length of the transport (in octets).
void close()
Request the transport to be closed.
Definition transport.cpp:67
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
Definition face.cpp:63
constexpr ssize_t QUEUE_UNSUPPORTED
Indicates that the transport does not support reading the queue capacity/length.
TransportState
Indicates the state of a transport.
Definition transport.hpp:37
@ CLOSED
the transport is closed, and can be safely deallocated
@ CLOSING
the transport is being closed gracefully, either by the peer or by a call to close()
@ FAILED
the transport is being closed due to a failure
@ DOWN
the transport is temporarily down, and is being recovered
@ UP
the transport is up and can transmit packets
constexpr ssize_t MTU_INVALID
(for internal use) Indicates that the MTU field is unset.
std::variant< std::monostate, ethernet::Address, udp::Endpoint > EndpointId
Identifies a remote endpoint on the link.
constexpr ssize_t QUEUE_ERROR
Indicates that the transport was unable to retrieve the queue capacity/length.
constexpr ssize_t MTU_UNLIMITED
Indicates that the transport has no limit on payload size.