ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
tcp_listener.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#ifdef USE_SOCKET_IPV4_ALLOW
8#include "ipv4_allow.h"
9#endif
10#include "socket.h"
11#include "tcp_client_link.h"
12
13#include <cstdint>
14#include <memory>
15
16namespace esphome::socket {
17
22 public:
23#ifdef USE_SOCKET_IPV4_ALLOW
24 void set_allow(const Ipv4AllowEntry *entries, size_t count) { this->allow_.set(entries, count); }
25#endif
26
28 void begin(const char *tag) { this->tag_ = tag; }
31 void poll(TcpClientLinkBase &link, bool may_accept) {
32 if (this->poll_listen(link) && may_accept && !link.connected()) {
33 if (auto client = this->accept(link)) {
34 link.adopt(std::move(client));
35 }
36 }
37 }
40 bool poll_listen(TcpClientLinkBase &link, uint8_t backlog = 1) {
41 if (this->listen_ == nullptr) {
42 if (!link.in_backoff()) {
43 this->try_listen_(link, backlog);
44 }
45 return false;
46 }
47 return this->listen_->ready();
48 }
51 std::unique_ptr<Socket> accept(TcpClientLinkBase &link);
52 void close() { this->listen_.reset(); }
54 void dump_config() const;
55
56 protected:
57 void try_listen_(TcpClientLinkBase &link, uint8_t backlog);
58
59 std::unique_ptr<ListenSocket> listen_;
60 const char *tag_{nullptr};
61#ifdef USE_SOCKET_IPV4_ALLOW
63#endif
64};
65
66} // namespace esphome::socket
67
68#endif
IPv4 peers that may connect. An empty list allows every peer.
Definition ipv4_allow.h:20
void set(const Ipv4AllowEntry *entries, size_t count)
Definition ipv4_allow.h:22
The server side of a bridged TCP link: owns the listen socket and the allow list.
std::unique_ptr< Socket > accept(TcpClientLinkBase &link)
Accepts a waiting connection that the allow list admits, or returns nullptr.
void begin(const char *tag)
Call from setup(); tag names the log lines.
void try_listen_(TcpClientLinkBase &link, uint8_t backlog)
void set_allow(const Ipv4AllowEntry *entries, size_t count)
void dump_config() const
One config line per allowed network.
std::unique_ptr< ListenSocket > listen_
bool poll_listen(TcpClientLinkBase &link, uint8_t backlog=1)
Opens the listen socket on link's port after its backoff; true when a connection waits.
void poll(TcpClientLinkBase &link, bool may_accept)
Server state machine; call every loop.
unsigned count
Definition gif.cpp:51
void const char * tag
Definition log.h:110
One allowed IPv4 network, network byte order, host bits cleared.
Definition ipv4_allow.h:14