ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
tcp_client_link.h
Go to the documentation of this file.
1#pragma once
2
3#include "headers.h"
4
5#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
6
7#include "ipv4_resolve.h"
8#include "socket.h"
10#include "esphome/core/log.h"
12
13#include <cstdint>
14#include <memory>
15
16namespace esphome::socket {
17
24 public:
25 static constexpr size_t TX_BUFFER_SIZE = 1024;
26
27 void set_host(const char *host) { this->host_ = StringRef(host); }
28 void set_port(uint16_t port) { this->port_ = port; }
33 const char *host() const { return this->host_.c_str(); }
34 uint16_t port() const { return this->port_; }
36 uint32_t idle_timeout() const { return this->idle_timeout_ms_; }
37
39 void begin(const char *tag);
42 void poll() {
43 if (this->connected_ || (this->sock_ == nullptr && this->in_backoff())) {
44 return;
45 }
46 this->poll_slow_();
47 }
49 void adopt(std::unique_ptr<Socket> sock);
51 ssize_t read(uint8_t *buf, size_t len);
52 void tx_commit(size_t len) { this->tx_len_ += static_cast<uint16_t>(len); }
53 size_t tx_free() const { return this->connected_ ? TX_BUFFER_SIZE - this->tx_len_ : 0; }
55 void close();
60 uint32_t last_io() const { return this->last_io_ms_; }
62 void check_idle() {
63 if (this->idle_timeout_ms_ != 0 && this->connected_ &&
64 App.get_loop_component_start_time() - this->last_io_ms_ >= this->idle_timeout_ms_) {
65 this->idle_close_();
66 }
67 }
68
69 bool connected() const { return this->connected_; }
71 bool connecting() const { return this->sock_ != nullptr && !this->connected_; }
72 bool ready() const { return this->sock_ != nullptr && this->sock_->ready(); }
78
79 protected:
81 size_t queue_(uint8_t *tx, const uint8_t *data, size_t len);
82 bool flush_tx_(uint8_t *tx) {
83 if (this->tx_len_ != 0) {
84 this->flush_tx_slow_(tx);
85 }
86 return this->tx_len_ == 0;
87 }
89 ssize_t write_(const uint8_t *buf, size_t len);
90 void flush_tx_slow_(uint8_t *tx);
91 void poll_slow_();
92 void try_connect_();
94 void drop_(const LogString *what, int err);
96 void idle_close_();
97
99 std::unique_ptr<Socket> sock_;
100 const char *tag_{nullptr};
106 uint16_t port_{0};
107 uint16_t tx_len_{0};
108 bool connected_{false};
109};
110
113 public:
115 size_t queue(const uint8_t *data, size_t len) { return this->queue_(this->tx_, data, len); }
118 uint8_t *tx_tail() { return this->tx_ + this->tx_len_; }
121 bool flush_tx() { return this->flush_tx_(this->tx_); }
122
123 protected:
124 uint8_t tx_[TX_BUFFER_SIZE]{};
125};
126
131 public:
132 size_t queue(uint8_t *tx, const uint8_t *data, size_t len) { return this->queue_(tx, data, len); }
133 bool flush_tx(uint8_t *tx) { return this->flush_tx_(tx); }
134};
135
136} // namespace esphome::socket
137
138#endif
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
One IPv4 literal or hostname. Must outlive a pending lookup.
__int64 ssize_t
Definition httplib.h:178
const void size_t len
Definition hal.h:68
Application App
Global storage of Application pointer - only one Application can exist.
void const char * tag
Definition log.h:110
static void uint32_t