ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
esphome::socket::TcpClientLink Class Reference

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

#include <tcp_client_link.h>

Public Member Functions

void set_host (const char *host)
 
void set_port (uint16_t port)
 
void set_reconnect_interval (uint32_t ms)
 
const char * host () const
 
uint16_t port () const
 
uint32_t reconnect_interval () 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.
 
size_t queue (const uint8_t *data, size_t len)
 Copy into the outgoing buffer; returns how many bytes fit.
 
uint8_t * tx_tail ()
 Direct access to the buffer's free tail.
 
void tx_commit (size_t len)
 
size_t tx_free () const
 
bool flush_tx ()
 Send the front of the buffer; true once it is empty.
 
void close ()
 Close without scheduling a reconnect (shutdown).
 
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
 

Protected Member Functions

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_ ()
 
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.
 

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}
 
Ipv4Resolve resolved_
 
uint16_t port_ {0}
 
uint16_t tx_len_ {0}
 
bool connected_ {false}
 
uint8_t tx_ [TX_BUFFER_SIZE] {}
 

Static Protected Attributes

static constexpr size_t TX_BUFFER_SIZE = 1024
 

Detailed Description

A reconnecting TCP stream driven from loop().

Owns the socket, the DNS lookup, the retry backoff and the outgoing buffer. A fatal read/write error closes the link and schedules the next attempt; the caller sees the edge via connected().

Definition at line 22 of file tcp_client_link.h.

Member Function Documentation

◆ adopt()

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

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

Definition at line 100 of file tcp_client_link.cpp.

◆ begin()

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

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

Definition at line 39 of file tcp_client_link.cpp.

◆ close()

void esphome::socket::TcpClientLink::close ( )

Close without scheduling a reconnect (shutdown).

Definition at line 157 of file tcp_client_link.cpp.

◆ connected()

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

Definition at line 63 of file tcp_client_link.h.

◆ connecting()

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

A connect attempt is in flight.

Definition at line 65 of file tcp_client_link.h.

◆ drop_()

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

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

Definition at line 168 of file tcp_client_link.cpp.

◆ flush_tx()

bool esphome::socket::TcpClientLink::flush_tx ( )
inline

Send the front of the buffer; true once it is empty.

A partial write keeps the rest; inline no-op while nothing is queued.

Definition at line 54 of file tcp_client_link.h.

◆ flush_tx_slow_()

void esphome::socket::TcpClientLink::flush_tx_slow_ ( )
protected

Definition at line 147 of file tcp_client_link.cpp.

◆ host()

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

Definition at line 27 of file tcp_client_link.h.

◆ in_backoff()

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

Definition at line 69 of file tcp_client_link.h.

◆ note_attempt()

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

Shared retry clock, also usable for a listen socket.

Definition at line 68 of file tcp_client_link.h.

◆ poll()

void esphome::socket::TcpClientLink::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 35 of file tcp_client_link.h.

◆ poll_slow_()

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

Definition at line 45 of file tcp_client_link.cpp.

◆ port()

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

Definition at line 28 of file tcp_client_link.h.

◆ queue()

size_t esphome::socket::TcpClientLink::queue ( const uint8_t * data,
size_t len )

Copy into the outgoing buffer; returns how many bytes fit.

Definition at line 137 of file tcp_client_link.cpp.

◆ read()

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

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

Definition at line 107 of file tcp_client_link.cpp.

◆ ready()

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

Definition at line 66 of file tcp_client_link.h.

◆ reconnect_interval()

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

Definition at line 29 of file tcp_client_link.h.

◆ set_host()

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

Definition at line 24 of file tcp_client_link.h.

◆ set_port()

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

Definition at line 25 of file tcp_client_link.h.

◆ set_reconnect_interval()

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

Definition at line 26 of file tcp_client_link.h.

◆ try_connect_()

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

Definition at line 70 of file tcp_client_link.cpp.

◆ tx_commit()

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

Definition at line 50 of file tcp_client_link.h.

◆ tx_free()

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

Definition at line 51 of file tcp_client_link.h.

◆ tx_tail()

uint8_t * esphome::socket::TcpClientLink::tx_tail ( )
inline

Direct access to the buffer's free tail.

Fill at most tx_free() bytes, then tx_commit() the count; neither is bounds checked.

Definition at line 49 of file tcp_client_link.h.

◆ write_()

ssize_t esphome::socket::TcpClientLink::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 122 of file tcp_client_link.cpp.

Field Documentation

◆ connected_

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

Definition at line 92 of file tcp_client_link.h.

◆ host_

StringRef esphome::socket::TcpClientLink::host_
protected

Definition at line 84 of file tcp_client_link.h.

◆ last_attempt_ms_

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

Definition at line 87 of file tcp_client_link.h.

◆ port_

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

Definition at line 90 of file tcp_client_link.h.

◆ reconnect_interval_ms_

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

Definition at line 88 of file tcp_client_link.h.

◆ resolved_

Ipv4Resolve esphome::socket::TcpClientLink::resolved_
protected

Definition at line 89 of file tcp_client_link.h.

◆ sock_

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

Definition at line 85 of file tcp_client_link.h.

◆ tag_

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

Definition at line 86 of file tcp_client_link.h.

◆ tx_

uint8_t esphome::socket::TcpClientLink::tx_[TX_BUFFER_SIZE] {}
protected

Definition at line 93 of file tcp_client_link.h.

◆ TX_BUFFER_SIZE

size_t esphome::socket::TcpClientLink::TX_BUFFER_SIZE = 1024
staticconstexprprotected

Definition at line 74 of file tcp_client_link.h.

◆ tx_len_

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

Definition at line 91 of file tcp_client_link.h.


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