SatCat5
net_tpipe.h
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // "Tpipe" protocol for reliable byte-streams over UDP or Ethernet.
6 
7 #pragma once
8 
9 #include <satcat5/eth_address.h>
10 #include <satcat5/io_buffer.h>
11 #include <satcat5/net_core.h>
12 #include <satcat5/polling.h>
13 #include <satcat5/timeref.h>
14 #include <satcat5/udp_core.h>
15 
16 namespace satcat5 {
17  namespace net {
40  class Tpipe
42  , public satcat5::net::Protocol
43  , protected satcat5::poll::Timer
44  {
45  public:
49  void close();
50 
52  bool completed() const;
53 
55  inline void set_retransmit(u16 msec) {m_retransmit = msec;}
56 
58  inline void set_timeout(u16 msec) {m_timeout = msec;}
59 
65  void set_txonly();
66 
67  protected:
70  explicit Tpipe(satcat5::net::Address* dst);
71  ~Tpipe() SATCAT5_OPTIONAL_DTOR;
72 
73  // Internal event callbacks.
74  void data_rcvd(satcat5::io::Readable* src) override;
75  void frame_rcvd(satcat5::io::LimitedRead& src) override;
76  void timer_event() override;
77 
87  void send_block();
88 
92  void send_start();
93 
95  static constexpr unsigned MAX_WINDOW = 512;
96 
97  // Other protocol constants:
98  static constexpr u16
99  FLAG_START = 0x8000,
100  FLAG_STOP = 0x4000,
101  FLAG_LEN = 0x03FF,
102  STATE_OPENREQ = 0x0001,
103  STATE_READY = 0x0002,
104  STATE_TXBUSY = 0x0004,
105  STATE_CLOSING = 0x0008,
106  STATE_TXONLY = 0x0010;
107 
108  // Protocol state.
109  satcat5::net::Address* const m_iface;
110  u16 m_retry;
111  u16 m_state;
113  u16 m_timeout;
114  u16 m_txpos;
115  u16 m_txref;
116  u16 m_rxpos;
117  u16 m_rxref;
120  };
121  }
122 
123  // Wrappers for commonly used network interfaces:
124  namespace eth {
126  class Tpipe final
128  , public satcat5::net::Tpipe
129  {
130  public:
132  explicit Tpipe(satcat5::eth::Dispatch* iface);
133 
135  void bind(
136  const satcat5::eth::MacType& etype,
137  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
138 
140  void connect(
141  const satcat5::eth::MacAddr& addr,
142  const satcat5::eth::MacType& etype,
143  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
144 
146  inline bool ready() const {return m_addr.ready();}
147  };
148  }
149 
150  namespace udp {
152  class Tpipe final
154  , public satcat5::net::Tpipe
155  {
156  public:
158  explicit Tpipe(satcat5::udp::Dispatch* iface);
159 
161  void bind(const satcat5::udp::Port& port);
162 
164  void connect(
165  const satcat5::ip::Addr& addr,
166  const satcat5::udp::Port& port,
167  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
168 
170  inline bool ready() const {return m_addr.ready();}
171  };
172  }
173 }
Inheritable container for a eth::Address.
Definition: eth_address.h:60
Implemention of "net::Dispatch" for Ethernet frames.
Definition: eth_dispatch.h:21
Simple network pipe service over raw Ethernet.
Definition: net_tpipe.h:129
bool ready() const
Is this connection ready to send and receive?
Definition: net_tpipe.h:146
Extensible transmit and receive buffer.
Definition: io_buffer.h:42
constexpr Readable(satcat5::io::EventListener *callback=0)
Only children should create or destroy the base class.
Definition: io_readable.h:176
Defines a generic API for sending data to a specific destination, such as a MAC address,...
Definition: net_address.h:30
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Simple network pipe service for reliable byte-streams.
Definition: net_tpipe.h:44
bool completed() const
Has all queued data been acknowledged?
Definition: net_tpipe.cc:51
u16 m_txref
Transmit reference.
Definition: net_tpipe.h:115
u8 m_txbuff[MAX_WINDOW]
Working buffer (transmit)
Definition: net_tpipe.h:118
void timer_event() override
Child class MUST override this method.
Definition: net_tpipe.cc:155
void set_retransmit(u16 msec)
Adjust retransmit interval.
Definition: net_tpipe.h:55
void close()
Close the active connection.
Definition: net_tpipe.cc:42
u16 m_rxpos
Receive position.
Definition: net_tpipe.h:116
u8 m_rxbuff[MAX_WINDOW]
Working buffer (receive)
Definition: net_tpipe.h:119
u16 m_state
Status flags.
Definition: net_tpipe.h:111
static constexpr u16 STATE_READY
Connection acknowledged.
Definition: net_tpipe.h:103
u16 m_rxref
Receive reference.
Definition: net_tpipe.h:117
static constexpr u16 STATE_TXBUSY
Tx sent, wait for ack.
Definition: net_tpipe.h:104
u16 m_retransmit
Retransmit timeout (msec)
Definition: net_tpipe.h:112
void send_start()
Special case of send_block used to open a new connection.
Definition: net_tpipe.cc:205
u16 m_txpos
Transmit position.
Definition: net_tpipe.h:114
static constexpr u16 FLAG_STOP
Connection is closing.
Definition: net_tpipe.h:100
static constexpr unsigned MAX_WINDOW
Buffer size is set by the maximum transmit window.
Definition: net_tpipe.h:95
static constexpr u16 FLAG_LEN
Data length.
Definition: net_tpipe.h:101
static constexpr u16 FLAG_START
Open new connection.
Definition: net_tpipe.h:99
u16 m_timeout
Connection timeout (msec)
Definition: net_tpipe.h:113
u16 m_retry
Retry elapsed time (msec)
Definition: net_tpipe.h:110
Tpipe(satcat5::net::Address *dst)
Create link and set the transport service.
Definition: net_tpipe.cc:17
satcat5::net::Address *const m_iface
Network interface.
Definition: net_tpipe.h:109
void set_txonly()
Enable unidirectional transmission? Transmit-only endpoints do not wait for acknowledgements.
Definition: net_tpipe.cc:56
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: net_tpipe.cc:62
static constexpr u16 STATE_OPENREQ
Opening new connection.
Definition: net_tpipe.h:102
void set_timeout(u16 msec)
Adjust lost-connection timeout.
Definition: net_tpipe.h:58
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: net_tpipe.cc:68
static constexpr u16 STATE_TXONLY
Unidirectional streaming.
Definition: net_tpipe.h:106
void send_block()
Send a synchronization packet, with data if applicable.
Definition: net_tpipe.cc:164
static constexpr u16 STATE_CLOSING
Closing connection.
Definition: net_tpipe.h:105
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Inheritable container for a udp::Address.
Definition: udp_core.h:149
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
Simple network pipe service over raw Ethernet.
Definition: net_tpipe.h:155
bool ready() const
Is this connection ready to send and receive?
Definition: net_tpipe.h:170
Buffered I/O wrappers for PacketBuffer.
Generic network Dispatch API.
Core event-processing loop for SatCat5 software.
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
EtherType field (uint16) is used a protocol-ID [1536..65535].
Definition: eth_header.h:96
Header contents for an 802.1Q Virtual-LAN tag.
Definition: eth_header.h:124
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
TimeRef and TimeVal define the API for monotonic timers.