|
ESPHome 2026.11.0b6
|
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) |
| 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. | |
| 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). | |
| void | note_io () |
| Restart the idle clock. | |
| 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 |
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. | |
| 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} |
| 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 102 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 40 of file tcp_client_link.cpp.
|
inline |
Close when no byte has moved for idle_timeout(). Inline: the close runs only once that time has passed.
Definition at line 70 of file tcp_client_link.h.
| void esphome::socket::TcpClientLink::close | ( | ) |
Close without scheduling a reconnect (shutdown).
Definition at line 164 of file tcp_client_link.cpp.
|
inline |
Definition at line 77 of file tcp_client_link.h.
|
inline |
A connect attempt is in flight.
Definition at line 79 of file tcp_client_link.h.
|
protected |
Close after a failure, log what and errno, schedule the next attempt.
Definition at line 175 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 58 of file tcp_client_link.h.
|
protected |
Definition at line 154 of file tcp_client_link.cpp.
|
inline |
Definition at line 30 of file tcp_client_link.h.
|
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.
|
inline |
Definition at line 33 of file tcp_client_link.h.
|
inline |
Definition at line 83 of file tcp_client_link.h.
|
inline |
Shared retry clock, also usable for a listen socket.
Definition at line 82 of file tcp_client_link.h.
|
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 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 39 of file tcp_client_link.h.
|
protected |
Definition at line 46 of file tcp_client_link.cpp.
|
inline |
Definition at line 31 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 144 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 110 of file tcp_client_link.cpp.
|
inline |
Definition at line 80 of file tcp_client_link.h.
|
inline |
Definition at line 32 of file tcp_client_link.h.
|
inline |
Definition at line 24 of file tcp_client_link.h.
|
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 29 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 72 of file tcp_client_link.cpp.
|
inline |
Definition at line 54 of file tcp_client_link.h.
|
inline |
Definition at line 55 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 53 of file tcp_client_link.h.
|
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.
|
protected |
Definition at line 110 of file tcp_client_link.h.
|
protected |
Definition at line 100 of file tcp_client_link.h.
|
protected |
Definition at line 106 of file tcp_client_link.h.
|
protected |
Definition at line 103 of file tcp_client_link.h.
|
protected |
Definition at line 105 of file tcp_client_link.h.
|
protected |
Definition at line 108 of file tcp_client_link.h.
|
protected |
Definition at line 104 of file tcp_client_link.h.
|
protected |
Definition at line 107 of file tcp_client_link.h.
|
protected |
Definition at line 101 of file tcp_client_link.h.
|
protected |
Definition at line 102 of file tcp_client_link.h.
|
protected |
Definition at line 111 of file tcp_client_link.h.
|
staticconstexprprotected |
Definition at line 88 of file tcp_client_link.h.
|
protected |
Definition at line 109 of file tcp_client_link.h.