SatCat5
tcp_socket.h
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 // Connect a SatCat5 byte-stream to a Linux or Windows TCP socket.
6 
7 #pragma once
8 
9 #include <satcat5/io_buffer.h>
10 #include <satcat5/polling.h>
11 #include <satcat5/timeref.h>
12 
13 namespace satcat5 {
14  namespace tcp {
25  , public satcat5::poll::Timer
26  {
27  public:
28  explicit SocketPosix(unsigned txbytes=8192, unsigned rxbytes=8192);
29  virtual ~SocketPosix();
30 
32  void close();
33 
35  bool bind(const satcat5::ip::Port& port);
36 
39  bool connect(
40  const char* hostname,
41  const satcat5::ip::Port& port);
42  bool connect(
43  const satcat5::ip::Addr& addr,
44  const satcat5::ip::Port& port);
46 
48  bool ready();
49 
51  inline void set_rate_kbps(unsigned kbps)
52  { m_rate_kbps = kbps; }
53 
54  private:
55  // Internal event handlers:
56  void data_rcvd(satcat5::io::Readable* src) override;
57  void timer_event() override;
58  int open_nonblock_socket();
59  unsigned rate_limit(satcat5::util::TimeVal& tv);
60 
61  // Internal state:
62  u32 m_flags;
67  unsigned m_rate_kbps;
68  };
69  }
70 }
Extensible transmit and receive buffer.
Definition: io_buffer.h:42
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Connect a SatCat5 byte-stream to a Linux or Windows TCP socket.
Definition: tcp_socket.h:26
bool ready()
Is this connection ready to send and receive data?
Definition: tcp_socket.cc:259
void close()
Close any open sockets and return to idle.
Definition: tcp_socket.cc:151
int m_sock_data
Socket for data transfer, if connected.
Definition: tcp_socket.h:66
util::TimeVal m_last_rx
Time since last receive event.
Definition: tcp_socket.h:63
unsigned m_rate_kbps
Maximum bytes/msec, if applicable.
Definition: tcp_socket.h:67
void set_rate_kbps(unsigned kbps)
Set a Tx/Rx rate-limit in kilobits-per-second, or zero to disable.
Definition: tcp_socket.h:51
bool bind(const satcat5::ip::Port &port)
Prepare to accept connection from a remote client endpoint.
Definition: tcp_socket.cc:164
u32 m_flags
Additional status flags.
Definition: tcp_socket.h:62
void data_rcvd(satcat5::io::Readable *src) override
The data_rcvd() callback is polled whenever data is available.
Definition: tcp_socket.cc:263
void timer_event() override
Child class MUST override this method.
Definition: tcp_socket.cc:280
bool connect(const char *hostname, const satcat5::ip::Port &port)
Attempt connection to a remote server endpoint.
Definition: tcp_socket.cc:202
int m_sock_listen
Socket for accept/bind, if applicable.
Definition: tcp_socket.h:65
util::TimeVal m_last_tx
Time since last transmit event.
Definition: tcp_socket.h:64
Buffered I/O wrappers for PacketBuffer.
Core event-processing loop for SatCat5 software.
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
Timestamp for measuring elapsed time.
Definition: timeref.h:48
TimeRef and TimeVal define the API for monotonic timers.