ESPHome 2026.11.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
23 public:
24 void set_host(const char *host) { this->host_ = StringRef(host); }
25 void set_port(uint16_t port) { this->port_ = port; }
27 const char *host() const { return this->host_.c_str(); }
28 uint16_t port() const { return this->port_; }
30
32 void begin(const char *tag);
35 void poll() {
36 if (this->connected_ || (this->sock_ == nullptr && this->in_backoff())) {
37 return;
38 }
39 this->poll_slow_();
40 }
42 void adopt(std::unique_ptr<Socket> sock);
44 ssize_t read(uint8_t *buf, size_t len);
46 size_t queue(const uint8_t *data, size_t len);
49 uint8_t *tx_tail() { return this->tx_ + this->tx_len_; }
50 void tx_commit(size_t len) { this->tx_len_ += static_cast<uint16_t>(len); }
51 size_t tx_free() const { return this->connected_ ? TX_BUFFER_SIZE - this->tx_len_ : 0; }
54 bool flush_tx() {
55 if (this->tx_len_ != 0) {
56 this->flush_tx_slow_();
57 }
58 return this->tx_len_ == 0;
59 }
61 void close();
62
63 bool connected() const { return this->connected_; }
65 bool connecting() const { return this->sock_ != nullptr && !this->connected_; }
66 bool ready() const { return this->sock_ != nullptr && this->sock_->ready(); }
72
73 protected:
74 static constexpr size_t TX_BUFFER_SIZE = 1024;
75
77 ssize_t write_(const uint8_t *buf, size_t len);
78 void flush_tx_slow_();
79 void poll_slow_();
80 void try_connect_();
82 void drop_(const LogString *what, int err);
83
85 std::unique_ptr<Socket> sock_;
86 const char *tag_{nullptr};
90 uint16_t port_{0};
91 uint16_t tx_len_{0};
92 bool connected_{false};
93 uint8_t tx_[TX_BUFFER_SIZE]{};
94};
95
96} // namespace esphome::socket
97
98#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.
static void uint32_t