|
ESPHome 2026.11.0-dev
|
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 |
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.
| 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.
| 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.
| void esphome::socket::TcpClientLink::close | ( | ) |
Close without scheduling a reconnect (shutdown).
Definition at line 157 of file tcp_client_link.cpp.
|
inline |
Definition at line 63 of file tcp_client_link.h.
|
inline |
A connect attempt is in flight.
Definition at line 65 of file tcp_client_link.h.
|
protected |
Close after a failure, log what and errno, schedule the next attempt.
Definition at line 168 of file tcp_client_link.cpp.
|
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.
|
protected |
Definition at line 147 of file tcp_client_link.cpp.
|
inline |
Definition at line 27 of file tcp_client_link.h.
|
inline |
Definition at line 69 of file tcp_client_link.h.
|
inline |
Shared retry clock, also usable for a listen socket.
Definition at line 68 of file tcp_client_link.h.
|
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.
|
protected |
Definition at line 45 of file tcp_client_link.cpp.
|
inline |
Definition at line 28 of file tcp_client_link.h.
| 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.
| 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.
|
inline |
Definition at line 66 of file tcp_client_link.h.
|
inline |
Definition at line 29 of file tcp_client_link.h.
|
inline |
Definition at line 24 of file tcp_client_link.h.
|
inline |
Definition at line 25 of file tcp_client_link.h.
|
inline |
Definition at line 26 of file tcp_client_link.h.
|
protected |
Definition at line 70 of file tcp_client_link.cpp.
|
inline |
Definition at line 50 of file tcp_client_link.h.
|
inline |
Definition at line 51 of file tcp_client_link.h.
|
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.
|
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.
|
protected |
Definition at line 92 of file tcp_client_link.h.
|
protected |
Definition at line 84 of file tcp_client_link.h.
|
protected |
Definition at line 87 of file tcp_client_link.h.
|
protected |
Definition at line 90 of file tcp_client_link.h.
|
protected |
Definition at line 88 of file tcp_client_link.h.
|
protected |
Definition at line 89 of file tcp_client_link.h.
|
protected |
Definition at line 85 of file tcp_client_link.h.
|
protected |
Definition at line 86 of file tcp_client_link.h.
|
protected |
Definition at line 93 of file tcp_client_link.h.
|
staticconstexprprotected |
Definition at line 74 of file tcp_client_link.h.
|
protected |
Definition at line 91 of file tcp_client_link.h.