ESPHome 2026.11.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(TcpClientLink &link, bool may_accept) {
32 if (this->listen_ == nullptr) {
33 if (!link.in_backoff()) {
34 this->try_listen_(link);
35 }
36 return;
37 }
38 if (may_accept && !link.connected() && this->listen_->ready()) {
39 this->accept_(link);
40 }
41 }
42 void close() { this->listen_.reset(); }
44 void dump_config() const;
45
46 protected:
47 void try_listen_(TcpClientLink &link);
48 void accept_(TcpClientLink &link);
49
50 std::unique_ptr<ListenSocket> listen_;
51 const char *tag_{nullptr};
52#ifdef USE_SOCKET_IPV4_ALLOW
55#endif
56};
57
58} // namespace esphome::socket
59
60#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, accepts one peer at...
void poll(TcpClientLink &link, bool may_accept)
Server state machine; call every loop.
void begin(const char *tag)
Call from setup(); tag names the log lines.
void set_allow(const Ipv4AllowEntry *entries, size_t count)
void dump_config() const
One config line per allowed network.
std::unique_ptr< ListenSocket > listen_
void try_listen_(TcpClientLink &link)
void accept_(TcpClientLink &link)
unsigned count
Definition gif.cpp:51
static void uint32_t
One allowed IPv4 network, network byte order, host bits cleared.
Definition ipv4_allow.h:14