ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::socket::TcpClientLinkBase Class Reference

A reconnecting TCP stream driven from loop(). More...

#include <tcp_client_link.h>

Inheritance diagram for esphome::socket::TcpClientLinkBase:
esphome::socket::TcpClientLink esphome::socket::TcpClientLinkSharedTx

Public Member Functions

void set_host (const char *host)
 
void set_port (uint16_t port)
 
void set_reconnect_interval (uint32_t ms)
 
void set_idle_timeout (uint32_t ms)
 0 leaves the link up.
 
const char * host () const
 
uint16_t port () const
 
uint32_t reconnect_interval () const
 
uint32_t idle_timeout () const
 
void begin (const char *tag)
 Call from setup(). tag names this link's log lines.
 
void poll ()
 Connect state machine; call every loop while acting as a client.
 
void adopt (std::unique_ptr< Socket > sock)
 Take over an accepted socket (the server side of a bridge).
 
ssize_t read (uint8_t *buf, size_t len)
 Returns bytes moved, 0 when nothing can move now, -1 when the link dropped.
 
void tx_commit (size_t len)
 
size_t tx_free () const
 
void close ()
 Close without scheduling a reconnect (shutdown).
 
void note_io ()
 Restart the idle clock.
 
uint32_t last_io () const
 When a byte last moved, or note_io() last ran.
 
void check_idle ()
 Close when no byte has moved for idle_timeout(). Inline: the close runs only once that time has passed.
 
bool connected () const
 
bool connecting () const
 A connect attempt is in flight.
 
bool ready () const
 
void note_attempt ()
 Shared retry clock, also usable for a listen socket.
 
bool in_backoff () const
 

Static Public Attributes

static constexpr size_t TX_BUFFER_SIZE = 1024
 

Protected Member Functions

size_t queue_ (uint8_t *tx, const uint8_t *data, size_t len)
 queue() and flush_tx() of the subclasses, on the buffer they keep.
 
bool flush_tx_ (uint8_t *tx)
 
ssize_t write_ (const uint8_t *buf, size_t len)
 The raw stream write behind flush_tx(); drops the link on a fatal error.
 
void flush_tx_slow_ (uint8_t *tx)
 
void poll_slow_ ()
 
void try_connect_ ()
 
void drop_ (const LogString *what, int err)
 Close after a failure, log what and errno, schedule the next attempt.
 
void idle_close_ ()
 Log and close after check_idle() saw the timeout pass; schedules the next attempt like a failure.
 

Protected Attributes

StringRef host_
 
std::unique_ptr< Socket > sock_
 
const char * tag_ {nullptr}
 
uint32_t last_attempt_ms_ {0}
 
uint32_t reconnect_interval_ms_ {5000}
 
uint32_t last_io_ms_ {0}
 
uint32_t idle_timeout_ms_ {0}
 
Ipv4Resolve resolved_
 
uint16_t port_ {0}
 
uint16_t tx_len_ {0}
 
bool connected_ {false}
 

Detailed Description

A reconnecting TCP stream driven from loop().

Owns the socket, the DNS lookup, the retry backoff and the fill level of the outgoing buffer; TcpClientLink and TcpClientLinkSharedTx add where its bytes live. A fatal read/write error closes the link and schedules the next attempt; the caller sees the edge via connected().

Definition at line 23 of file tcp_client_link.h.

Member Function Documentation

◆ adopt()

void esphome::socket::TcpClientLinkBase::adopt ( std::unique_ptr< Socket > sock)

Take over an accepted socket (the server side of a bridge).

Definition at line 102 of file tcp_client_link.cpp.

◆ begin()

void esphome::socket::TcpClientLinkBase::begin ( const char * tag)

Call from setup(). tag names this link's log lines.

Definition at line 40 of file tcp_client_link.cpp.

◆ check_idle()

void esphome::socket::TcpClientLinkBase::check_idle ( )
inline

Close when no byte has moved for idle_timeout(). Inline: the close runs only once that time has passed.

Definition at line 62 of file tcp_client_link.h.

◆ close()

void esphome::socket::TcpClientLinkBase::close ( )

Close without scheduling a reconnect (shutdown).

Definition at line 164 of file tcp_client_link.cpp.

◆ connected()

bool esphome::socket::TcpClientLinkBase::connected ( ) const
inline

Definition at line 69 of file tcp_client_link.h.

◆ connecting()

bool esphome::socket::TcpClientLinkBase::connecting ( ) const
inline

A connect attempt is in flight.

Definition at line 71 of file tcp_client_link.h.

◆ drop_()

void esphome::socket::TcpClientLinkBase::drop_ ( const LogString * what,
int err )
protected

Close after a failure, log what and errno, schedule the next attempt.

Definition at line 175 of file tcp_client_link.cpp.

◆ flush_tx_()

bool esphome::socket::TcpClientLinkBase::flush_tx_ ( uint8_t * tx)
inlineprotected

Definition at line 82 of file tcp_client_link.h.

◆ flush_tx_slow_()

void esphome::socket::TcpClientLinkBase::flush_tx_slow_ ( uint8_t * tx)
protected

Definition at line 154 of file tcp_client_link.cpp.

◆ host()

const char * esphome::socket::TcpClientLinkBase::host ( ) const
inline

Definition at line 33 of file tcp_client_link.h.

◆ idle_close_()

void esphome::socket::TcpClientLinkBase::idle_close_ ( )
protected

Log and close after check_idle() saw the timeout pass; schedules the next attempt like a failure.

Definition at line 181 of file tcp_client_link.cpp.

◆ idle_timeout()

uint32_t esphome::socket::TcpClientLinkBase::idle_timeout ( ) const
inline

Definition at line 36 of file tcp_client_link.h.

◆ in_backoff()

bool esphome::socket::TcpClientLinkBase::in_backoff ( ) const
inline

Definition at line 75 of file tcp_client_link.h.

◆ last_io()

uint32_t esphome::socket::TcpClientLinkBase::last_io ( ) const
inline

When a byte last moved, or note_io() last ran.

Definition at line 60 of file tcp_client_link.h.

◆ note_attempt()

void esphome::socket::TcpClientLinkBase::note_attempt ( )
inline

Shared retry clock, also usable for a listen socket.

Definition at line 74 of file tcp_client_link.h.

◆ note_io()

void esphome::socket::TcpClientLinkBase::note_io ( )
inline

Restart the idle clock.

A consumer that cannot take more bytes calls this, so a full buffer is not treated as a quiet peer.

Definition at line 58 of file tcp_client_link.h.

◆ poll()

void esphome::socket::TcpClientLinkBase::poll ( )
inline

Connect state machine; call every loop while acting as a client.

Inline no-op while connected or waiting out the backoff.

Definition at line 42 of file tcp_client_link.h.

◆ poll_slow_()

void esphome::socket::TcpClientLinkBase::poll_slow_ ( )
protected

Definition at line 46 of file tcp_client_link.cpp.

◆ port()

uint16_t esphome::socket::TcpClientLinkBase::port ( ) const
inline

Definition at line 34 of file tcp_client_link.h.

◆ queue_()

size_t esphome::socket::TcpClientLinkBase::queue_ ( uint8_t * tx,
const uint8_t * data,
size_t len )
protected

queue() and flush_tx() of the subclasses, on the buffer they keep.

Definition at line 144 of file tcp_client_link.cpp.

◆ read()

ssize_t esphome::socket::TcpClientLinkBase::read ( uint8_t * buf,
size_t len )

Returns bytes moved, 0 when nothing can move now, -1 when the link dropped.

Definition at line 110 of file tcp_client_link.cpp.

◆ ready()

bool esphome::socket::TcpClientLinkBase::ready ( ) const
inline

Definition at line 72 of file tcp_client_link.h.

◆ reconnect_interval()

uint32_t esphome::socket::TcpClientLinkBase::reconnect_interval ( ) const
inline

Definition at line 35 of file tcp_client_link.h.

◆ set_host()

void esphome::socket::TcpClientLinkBase::set_host ( const char * host)
inline

Definition at line 27 of file tcp_client_link.h.

◆ set_idle_timeout()

void esphome::socket::TcpClientLinkBase::set_idle_timeout ( uint32_t ms)
inline

0 leaves the link up.

A full local buffer is not idle: the caller notes that with note_io(), because the peer may still be sending.

Definition at line 32 of file tcp_client_link.h.

◆ set_port()

void esphome::socket::TcpClientLinkBase::set_port ( uint16_t port)
inline

Definition at line 28 of file tcp_client_link.h.

◆ set_reconnect_interval()

void esphome::socket::TcpClientLinkBase::set_reconnect_interval ( uint32_t ms)
inline

Definition at line 29 of file tcp_client_link.h.

◆ try_connect_()

void esphome::socket::TcpClientLinkBase::try_connect_ ( )
protected

Definition at line 72 of file tcp_client_link.cpp.

◆ tx_commit()

void esphome::socket::TcpClientLinkBase::tx_commit ( size_t len)
inline

Definition at line 52 of file tcp_client_link.h.

◆ tx_free()

size_t esphome::socket::TcpClientLinkBase::tx_free ( ) const
inline

Definition at line 53 of file tcp_client_link.h.

◆ write_()

ssize_t esphome::socket::TcpClientLinkBase::write_ ( const uint8_t * buf,
size_t len )
protected

The raw stream write behind flush_tx(); drops the link on a fatal error.

Definition at line 126 of file tcp_client_link.cpp.

Field Documentation

◆ connected_

bool esphome::socket::TcpClientLinkBase::connected_ {false}
protected

Definition at line 108 of file tcp_client_link.h.

◆ host_

StringRef esphome::socket::TcpClientLinkBase::host_
protected

Definition at line 98 of file tcp_client_link.h.

◆ idle_timeout_ms_

uint32_t esphome::socket::TcpClientLinkBase::idle_timeout_ms_ {0}
protected

Definition at line 104 of file tcp_client_link.h.

◆ last_attempt_ms_

uint32_t esphome::socket::TcpClientLinkBase::last_attempt_ms_ {0}
protected

Definition at line 101 of file tcp_client_link.h.

◆ last_io_ms_

uint32_t esphome::socket::TcpClientLinkBase::last_io_ms_ {0}
protected

Definition at line 103 of file tcp_client_link.h.

◆ port_

uint16_t esphome::socket::TcpClientLinkBase::port_ {0}
protected

Definition at line 106 of file tcp_client_link.h.

◆ reconnect_interval_ms_

uint32_t esphome::socket::TcpClientLinkBase::reconnect_interval_ms_ {5000}
protected

Definition at line 102 of file tcp_client_link.h.

◆ resolved_

Ipv4Resolve esphome::socket::TcpClientLinkBase::resolved_
protected

Definition at line 105 of file tcp_client_link.h.

◆ sock_

std::unique_ptr<Socket> esphome::socket::TcpClientLinkBase::sock_
protected

Definition at line 99 of file tcp_client_link.h.

◆ tag_

const char* esphome::socket::TcpClientLinkBase::tag_ {nullptr}
protected

Definition at line 100 of file tcp_client_link.h.

◆ TX_BUFFER_SIZE

size_t esphome::socket::TcpClientLinkBase::TX_BUFFER_SIZE = 1024
staticconstexpr

Definition at line 25 of file tcp_client_link.h.

◆ tx_len_

uint16_t esphome::socket::TcpClientLinkBase::tx_len_ {0}
protected

Definition at line 107 of file tcp_client_link.h.


The documentation for this class was generated from the following files: