SatCat5
udp_socket.h
1 // Copyright 2021-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // BufferedIO wrapper for two-way UDP communication
6 
7 #pragma once
8 
9 #include <satcat5/net_socket.h>
10 #include <satcat5/udp_core.h>
11 #include <satcat5/udp_dispatch.h>
12 
13 // Default size parameters for the fixed-size buffer.
14 // Override with a larger buffer size to support jumbo Ethernet+UDP frames.
15 // (i.e., Add gcc argument "-DSATCAT5_UDP_BUFFSIZE=16384" etc.)
16 // The default is large enough for any regular Ethernet+UDP frame.
17 #ifndef SATCAT5_UDP_BUFFSIZE
18 #define SATCAT5_UDP_BUFFSIZE 1600 // One full-size UDP frame
19 #endif
20 
21 #ifndef SATCAT5_UDP_PACKETS
22 #define SATCAT5_UDP_PACKETS 32 // ...or many smaller frames
23 #endif
24 
25 namespace satcat5 {
26  namespace udp {
29  class SocketCore
32  {
33  public:
35  void bind(const satcat5::udp::Port& port);
36 
40  void connect(
41  const satcat5::udp::Addr& dstaddr,
42  const satcat5::eth::MacAddr& dstmac,
43  const satcat5::udp::Port& dstport,
44  satcat5::udp::Port srcport = satcat5::udp::PORT_NONE,
45  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
46 
52  void connect(
53  const satcat5::udp::Addr& dstaddr,
54  const satcat5::udp::Port& dstport,
55  satcat5::udp::Port srcport = satcat5::udp::PORT_NONE,
56  const satcat5::eth::VlanTag& vtag = satcat5::eth::VTAG_NONE);
57 
59  void reconnect() {m_addr.retry();}
60 
61  // Useful inherited methods from net::SocketCore:
62  // close(), ready_rx(), ready_tx()
63 
64  protected:
65  SocketCore(
67  u8* txbuff, unsigned txbytes, unsigned txpkt,
68  u8* rxbuff, unsigned rxbytes, unsigned rxpkt);
69  ~SocketCore() {}
70  };
71 
73  class Socket final : public satcat5::udp::SocketCore {
74  public:
76  explicit Socket(satcat5::udp::Dispatch* iface);
77  ~Socket() {}
78 
79  // Useful inherited methods from udp::SocketCore:
80  // bind(...), connect(...), close(), ready_rx(), ready_tx()
81 
82  private:
83  u8 m_txbuff[SATCAT5_UDP_BUFFSIZE];
84  u8 m_rxbuff[SATCAT5_UDP_BUFFSIZE];
85  };
86 
88  class SocketRx final : public satcat5::udp::SocketCore {
89  public:
91  explicit SocketRx(satcat5::udp::Dispatch* iface);
92  ~SocketRx() {}
93 
94  // Useful inherited methods from udp::SocketCore:
95  // bind(...), connect(...), close(), ready_rx(), ready_tx()
96 
97  private:
98  u8 m_rxbuff[SATCAT5_UDP_BUFFSIZE];
99  };
100 
102  class SocketTx final : public satcat5::udp::SocketCore {
103  public:
105  explicit SocketTx(satcat5::udp::Dispatch* iface);
106  ~SocketTx() {}
107 
108  // Useful inherited methods from udp::SocketCore:
109  // bind(...), connect(...), close(), ready_rx(), ready_tx()
110 
111  private:
112  u8 m_txbuff[SATCAT5_UDP_BUFFSIZE];
113  };
114  }
115 }
Buffered I/O for sending and receiving messages over a network.
Definition: net_socket.h:24
Inheritable container for a udp::Address.
Definition: udp_core.h:149
void retry() override
If this Address is not in the ready() state, reattempt any steps required to do so,...
Definition: udp_core.h:104
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
BufferedIO wrapper for two-way UDP communication.
Definition: udp_socket.h:32
void connect(const satcat5::udp::Addr &dstaddr, const satcat5::eth::MacAddr &dstmac, const satcat5::udp::Port &dstport, satcat5::udp::Port srcport=satcat5::udp::PORT_NONE, const satcat5::eth::VlanTag &vtag=satcat5::eth::VTAG_NONE)
Connect with manual address resolution.
Definition: udp_socket.cc:52
void bind(const satcat5::udp::Port &port)
Listening mode only (no remote address).
Definition: udp_socket.cc:47
void reconnect()
Retry automatic address resolution.
Definition: udp_socket.h:59
SocketCore with fixed-size transmit and receive buffers.
Definition: udp_socket.h:73
Socket(satcat5::udp::Dispatch *iface)
Connect this Socket to a network interface.
Definition: udp_socket.cc:11
SocketCore with fixed-size receive buffer only.
Definition: udp_socket.h:88
SocketRx(satcat5::udp::Dispatch *iface)
Connect this Socket to a network interface.
Definition: udp_socket.cc:19
SocketCore with fixed-size transmit buffer only.
Definition: udp_socket.h:102
SocketTx(satcat5::udp::Dispatch *iface)
Connect this Socket to a network interface.
Definition: udp_socket.cc:27
Buffered wrappers for generic network communication.
An Ethernet MAC address (with serializable interface).
Definition: eth_header.h:29
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