mirror of
https://github.com/esphome/esphome.git
synced 2026-05-26 03:07:04 +08:00
[socket] Devirtualize socket abstraction layer (#14398)
This commit is contained in:
@@ -257,7 +257,7 @@ class APIServer : public Component,
|
||||
}
|
||||
void socket_failed_(const LogString *msg);
|
||||
// Pointers and pointer-like types first (4 bytes each)
|
||||
socket::Socket *socket_{nullptr};
|
||||
socket::ListenSocket *socket_{nullptr};
|
||||
#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
|
||||
Trigger<std::string, std::string> client_connected_trigger_;
|
||||
#endif
|
||||
|
||||
@@ -22,7 +22,7 @@ class DNSServer {
|
||||
}
|
||||
static constexpr size_t DNS_BUFFER_SIZE = 192;
|
||||
|
||||
socket::Socket *socket_{nullptr};
|
||||
socket::ListenSocket *socket_{nullptr};
|
||||
network::IPAddress server_ip_;
|
||||
uint8_t buffer_[DNS_BUFFER_SIZE];
|
||||
};
|
||||
|
||||
@@ -84,7 +84,7 @@ class ESPHomeOTAComponent final : public ota::OTAComponent {
|
||||
std::unique_ptr<uint8_t[]> auth_buf_;
|
||||
#endif // USE_OTA_PASSWORD
|
||||
|
||||
socket::Socket *server_{nullptr};
|
||||
socket::ListenSocket *server_{nullptr};
|
||||
std::unique_ptr<socket::Socket> client_;
|
||||
std::unique_ptr<ota::OTABackend> backend_;
|
||||
|
||||
|
||||
@@ -1,135 +1,81 @@
|
||||
#include "socket.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "socket.h"
|
||||
|
||||
#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
|
||||
#include <cstring>
|
||||
#include "esphome/core/application.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#include <esp_idf_version.h>
|
||||
#include <lwip/sockets.h>
|
||||
#endif
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
class BSDSocketImpl final : public Socket {
|
||||
public:
|
||||
BSDSocketImpl(int fd, bool monitor_loop = false) {
|
||||
this->fd_ = fd;
|
||||
// Register new socket with the application for select() if monitoring requested
|
||||
if (monitor_loop && this->fd_ >= 0) {
|
||||
// Only set loop_monitored_ to true if registration succeeds
|
||||
this->loop_monitored_ = App.register_socket_fd(this->fd_);
|
||||
}
|
||||
}
|
||||
~BSDSocketImpl() override {
|
||||
if (!this->closed_) {
|
||||
this->close(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
|
||||
}
|
||||
}
|
||||
int connect(const struct sockaddr *addr, socklen_t addrlen) override { return ::connect(this->fd_, addr, addrlen); }
|
||||
std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
int fd = ::accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<BSDSocketImpl>(fd, false);
|
||||
}
|
||||
std::unique_ptr<Socket> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
int fd = ::accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<BSDSocketImpl>(fd, true);
|
||||
BSDSocketImpl::BSDSocketImpl(int fd, bool monitor_loop) {
|
||||
this->fd_ = fd;
|
||||
// Register new socket with the application for select() if monitoring requested
|
||||
if (monitor_loop && this->fd_ >= 0) {
|
||||
// Only set loop_monitored_ to true if registration succeeds
|
||||
this->loop_monitored_ = App.register_socket_fd(this->fd_);
|
||||
}
|
||||
}
|
||||
|
||||
int bind(const struct sockaddr *addr, socklen_t addrlen) override { return ::bind(this->fd_, addr, addrlen); }
|
||||
int close() override {
|
||||
if (!this->closed_) {
|
||||
// Unregister from select() before closing if monitored
|
||||
if (this->loop_monitored_) {
|
||||
App.unregister_socket_fd(this->fd_);
|
||||
}
|
||||
int ret = ::close(this->fd_);
|
||||
this->closed_ = true;
|
||||
return ret;
|
||||
BSDSocketImpl::~BSDSocketImpl() {
|
||||
if (!this->closed_) {
|
||||
this->close();
|
||||
}
|
||||
}
|
||||
|
||||
int BSDSocketImpl::close() {
|
||||
if (!this->closed_) {
|
||||
// Unregister from select() before closing if monitored
|
||||
if (this->loop_monitored_) {
|
||||
App.unregister_socket_fd(this->fd_);
|
||||
}
|
||||
int ret = ::close(this->fd_);
|
||||
this->closed_ = true;
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BSDSocketImpl::setblocking(bool blocking) {
|
||||
int fl = ::fcntl(this->fd_, F_GETFL, 0);
|
||||
if (blocking) {
|
||||
fl &= ~O_NONBLOCK;
|
||||
} else {
|
||||
fl |= O_NONBLOCK;
|
||||
}
|
||||
::fcntl(this->fd_, F_SETFL, fl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool BSDSocketImpl::ready() const { return socket_ready_fd(this->fd_, this->loop_monitored_); }
|
||||
|
||||
size_t BSDSocketImpl::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
|
||||
buf[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
int shutdown(int how) override { return ::shutdown(this->fd_, how); }
|
||||
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
|
||||
}
|
||||
|
||||
int getpeername(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
return ::getpeername(this->fd_, addr, addrlen);
|
||||
}
|
||||
int getsockname(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
return ::getsockname(this->fd_, addr, addrlen);
|
||||
}
|
||||
int getsockopt(int level, int optname, void *optval, socklen_t *optlen) override {
|
||||
return ::getsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int setsockopt(int level, int optname, const void *optval, socklen_t optlen) override {
|
||||
return ::setsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int listen(int backlog) override { return ::listen(this->fd_, backlog); }
|
||||
ssize_t read(void *buf, size_t len) override {
|
||||
#ifdef USE_ESP32
|
||||
return ::lwip_read(this->fd_, buf, len);
|
||||
#else
|
||||
return ::read(this->fd_, buf, len);
|
||||
#endif
|
||||
}
|
||||
ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) override {
|
||||
#if defined(USE_ESP32) || defined(USE_HOST)
|
||||
return ::recvfrom(this->fd_, buf, len, 0, addr, addr_len);
|
||||
#else
|
||||
return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
|
||||
#endif
|
||||
}
|
||||
ssize_t readv(const struct iovec *iov, int iovcnt) override {
|
||||
#if defined(USE_ESP32)
|
||||
return ::lwip_readv(this->fd_, iov, iovcnt);
|
||||
#else
|
||||
return ::readv(this->fd_, iov, iovcnt);
|
||||
#endif
|
||||
}
|
||||
ssize_t write(const void *buf, size_t len) override {
|
||||
#ifdef USE_ESP32
|
||||
return ::lwip_write(this->fd_, buf, len);
|
||||
#else
|
||||
return ::write(this->fd_, buf, len);
|
||||
#endif
|
||||
}
|
||||
ssize_t send(void *buf, size_t len, int flags) { return ::send(this->fd_, buf, len, flags); }
|
||||
ssize_t writev(const struct iovec *iov, int iovcnt) override {
|
||||
#if defined(USE_ESP32)
|
||||
return ::lwip_writev(this->fd_, iov, iovcnt);
|
||||
#else
|
||||
return ::writev(this->fd_, iov, iovcnt);
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) override {
|
||||
return ::sendto(this->fd_, buf, len, flags, to, tolen); // NOLINT(readability-suspicious-call-argument)
|
||||
}
|
||||
|
||||
int setblocking(bool blocking) override {
|
||||
int fl = ::fcntl(this->fd_, F_GETFL, 0);
|
||||
if (blocking) {
|
||||
fl &= ~O_NONBLOCK;
|
||||
} else {
|
||||
fl |= O_NONBLOCK;
|
||||
}
|
||||
::fcntl(this->fd_, F_SETFL, fl);
|
||||
size_t BSDSocketImpl::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
|
||||
buf[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
|
||||
}
|
||||
|
||||
// Helper to create a socket with optional monitoring
|
||||
static std::unique_ptr<Socket> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
|
||||
static std::unique_ptr<BSDSocketImpl> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
|
||||
int ret = ::socket(domain, type, protocol);
|
||||
if (ret == -1)
|
||||
return nullptr;
|
||||
return std::unique_ptr<Socket>{new BSDSocketImpl(ret, loop_monitored)};
|
||||
return make_unique<BSDSocketImpl>(ret, loop_monitored);
|
||||
}
|
||||
|
||||
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
|
||||
@@ -140,6 +86,14 @@ std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol
|
||||
return create_socket(domain, type, protocol, true);
|
||||
}
|
||||
|
||||
std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol) {
|
||||
return create_socket(domain, type, protocol, false);
|
||||
}
|
||||
|
||||
std::unique_ptr<ListenSocket> socket_listen_loop_monitored(int domain, int type, int protocol) {
|
||||
return create_socket(domain, type, protocol, true);
|
||||
}
|
||||
|
||||
} // namespace esphome::socket
|
||||
|
||||
#endif // USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
#pragma once
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "headers.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#include <lwip/sockets.h>
|
||||
#endif
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
class BSDSocketImpl {
|
||||
public:
|
||||
BSDSocketImpl(int fd, bool monitor_loop = false);
|
||||
~BSDSocketImpl();
|
||||
BSDSocketImpl(const BSDSocketImpl &) = delete;
|
||||
BSDSocketImpl &operator=(const BSDSocketImpl &) = delete;
|
||||
|
||||
int connect(const struct sockaddr *addr, socklen_t addrlen) { return ::connect(this->fd_, addr, addrlen); }
|
||||
std::unique_ptr<BSDSocketImpl> accept(struct sockaddr *addr, socklen_t *addrlen) {
|
||||
int fd = ::accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<BSDSocketImpl>(fd, false);
|
||||
}
|
||||
std::unique_ptr<BSDSocketImpl> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) {
|
||||
int fd = ::accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<BSDSocketImpl>(fd, true);
|
||||
}
|
||||
|
||||
int bind(const struct sockaddr *addr, socklen_t addrlen) { return ::bind(this->fd_, addr, addrlen); }
|
||||
int close();
|
||||
int shutdown(int how) { return ::shutdown(this->fd_, how); }
|
||||
|
||||
int getpeername(struct sockaddr *addr, socklen_t *addrlen) { return ::getpeername(this->fd_, addr, addrlen); }
|
||||
int getsockname(struct sockaddr *addr, socklen_t *addrlen) { return ::getsockname(this->fd_, addr, addrlen); }
|
||||
|
||||
/// Format peer address into a fixed-size buffer (no heap allocation)
|
||||
size_t getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
/// Format local address into a fixed-size buffer (no heap allocation)
|
||||
size_t getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
|
||||
int getsockopt(int level, int optname, void *optval, socklen_t *optlen) {
|
||||
return ::getsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int setsockopt(int level, int optname, const void *optval, socklen_t optlen) {
|
||||
return ::setsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int listen(int backlog) { return ::listen(this->fd_, backlog); }
|
||||
ssize_t read(void *buf, size_t len) {
|
||||
#ifdef USE_ESP32
|
||||
return ::lwip_read(this->fd_, buf, len);
|
||||
#else
|
||||
return ::read(this->fd_, buf, len);
|
||||
#endif
|
||||
}
|
||||
ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) {
|
||||
#if defined(USE_ESP32) || defined(USE_HOST)
|
||||
return ::recvfrom(this->fd_, buf, len, 0, addr, addr_len);
|
||||
#else
|
||||
return ::lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
|
||||
#endif
|
||||
}
|
||||
ssize_t readv(const struct iovec *iov, int iovcnt) {
|
||||
#if defined(USE_ESP32)
|
||||
return ::lwip_readv(this->fd_, iov, iovcnt);
|
||||
#else
|
||||
return ::readv(this->fd_, iov, iovcnt);
|
||||
#endif
|
||||
}
|
||||
ssize_t write(const void *buf, size_t len) {
|
||||
#ifdef USE_ESP32
|
||||
return ::lwip_write(this->fd_, buf, len);
|
||||
#else
|
||||
return ::write(this->fd_, buf, len);
|
||||
#endif
|
||||
}
|
||||
ssize_t send(void *buf, size_t len, int flags) { return ::send(this->fd_, buf, len, flags); }
|
||||
ssize_t writev(const struct iovec *iov, int iovcnt) {
|
||||
#if defined(USE_ESP32)
|
||||
return ::lwip_writev(this->fd_, iov, iovcnt);
|
||||
#else
|
||||
return ::writev(this->fd_, iov, iovcnt);
|
||||
#endif
|
||||
}
|
||||
|
||||
ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) {
|
||||
return ::sendto(this->fd_, buf, len, flags, to, tolen); // NOLINT(readability-suspicious-call-argument)
|
||||
}
|
||||
|
||||
int setblocking(bool blocking);
|
||||
int loop() { return 0; }
|
||||
|
||||
bool ready() const;
|
||||
|
||||
int get_fd() const { return this->fd_; }
|
||||
|
||||
protected:
|
||||
int fd_{-1};
|
||||
bool closed_{false};
|
||||
bool loop_monitored_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::socket
|
||||
|
||||
#endif // USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
@@ -183,3 +183,20 @@ using socklen_t = uint32_t;
|
||||
#endif
|
||||
|
||||
#endif // USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
|
||||
#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
// Maximum length for formatted socket address string (IP address without port)
|
||||
// IPv4: "255.255.255.255" = 15 chars + null = 16
|
||||
// IPv6: full address = 45 chars + null = 46
|
||||
#if USE_NETWORK_IPV6
|
||||
static constexpr size_t SOCKADDR_STR_LEN = 46; // INET6_ADDRSTRLEN
|
||||
#else
|
||||
static constexpr size_t SOCKADDR_STR_LEN = 16; // INET_ADDRSTRLEN
|
||||
#endif
|
||||
|
||||
} // namespace esphome::socket
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,200 @@
|
||||
#pragma once
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_SOCKET_IMPL_LWIP_TCP
|
||||
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "headers.h"
|
||||
#include "lwip/ip.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/tcp.h"
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
// Forward declaration
|
||||
class LWIPRawImpl;
|
||||
|
||||
/// Non-virtual common base for LWIP raw TCP sockets.
|
||||
/// Provides shared fields and methods for both connected and listening sockets.
|
||||
/// No virtual methods — pure code sharing.
|
||||
class LWIPRawCommon {
|
||||
public:
|
||||
LWIPRawCommon(sa_family_t family, struct tcp_pcb *pcb) : pcb_(pcb), family_(family) {}
|
||||
~LWIPRawCommon();
|
||||
LWIPRawCommon(const LWIPRawCommon &) = delete;
|
||||
LWIPRawCommon &operator=(const LWIPRawCommon &) = delete;
|
||||
|
||||
int bind(const struct sockaddr *name, socklen_t addrlen);
|
||||
int close();
|
||||
int shutdown(int how);
|
||||
|
||||
int getpeername(struct sockaddr *name, socklen_t *addrlen);
|
||||
int getsockname(struct sockaddr *name, socklen_t *addrlen);
|
||||
|
||||
/// Format peer address into a fixed-size buffer (no heap allocation)
|
||||
size_t getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
/// Format local address into a fixed-size buffer (no heap allocation)
|
||||
size_t getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
|
||||
int getsockopt(int level, int optname, void *optval, socklen_t *optlen);
|
||||
int setsockopt(int level, int optname, const void *optval, socklen_t optlen);
|
||||
|
||||
int get_fd() const { return -1; }
|
||||
|
||||
protected:
|
||||
int ip2sockaddr_(ip_addr_t *ip, uint16_t port, struct sockaddr *name, socklen_t *addrlen);
|
||||
|
||||
// Member ordering optimized to minimize padding on 32-bit systems
|
||||
struct tcp_pcb *pcb_;
|
||||
// don't use lwip nodelay flag, it sometimes causes reconnect
|
||||
// instead use it for determining whether to call lwip_output
|
||||
bool nodelay_ = false;
|
||||
sa_family_t family_ = 0;
|
||||
};
|
||||
|
||||
/// Connected socket implementation for LWIP raw TCP.
|
||||
/// No virtual methods — callers always use the concrete type.
|
||||
class LWIPRawImpl : public LWIPRawCommon {
|
||||
public:
|
||||
using LWIPRawCommon::LWIPRawCommon;
|
||||
~LWIPRawImpl();
|
||||
|
||||
void init();
|
||||
|
||||
// Non-listening sockets return error
|
||||
std::unique_ptr<LWIPRawImpl> accept(struct sockaddr *, socklen_t *) {
|
||||
errno = EINVAL;
|
||||
return nullptr;
|
||||
}
|
||||
std::unique_ptr<LWIPRawImpl> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) {
|
||||
return this->accept(addr, addrlen);
|
||||
}
|
||||
// Regular sockets can't be converted to listening - this shouldn't happen
|
||||
// as listen() should only be called on sockets created for listening
|
||||
int listen(int) {
|
||||
errno = EOPNOTSUPP;
|
||||
return -1;
|
||||
}
|
||||
ssize_t read(void *buf, size_t len);
|
||||
ssize_t readv(const struct iovec *iov, int iovcnt);
|
||||
ssize_t recvfrom(void *, size_t, sockaddr *, socklen_t *) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
ssize_t write(const void *buf, size_t len);
|
||||
ssize_t writev(const struct iovec *iov, int iovcnt);
|
||||
ssize_t sendto(const void *, size_t, int, const struct sockaddr *, socklen_t) {
|
||||
// return ::sendto(fd_, buf, len, flags, to, tolen);
|
||||
errno = ENOSYS;
|
||||
return -1;
|
||||
}
|
||||
bool ready() const { return this->rx_buf_ != nullptr || this->rx_closed_ || this->pcb_ == nullptr; }
|
||||
|
||||
int setblocking(bool blocking) {
|
||||
if (this->pcb_ == nullptr) {
|
||||
errno = ECONNRESET;
|
||||
return -1;
|
||||
}
|
||||
if (blocking) {
|
||||
// blocking operation not supported
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int loop() { return 0; }
|
||||
|
||||
err_t recv_fn(struct pbuf *pb, err_t err);
|
||||
|
||||
static void s_err_fn(void *arg, err_t err);
|
||||
static err_t s_recv_fn(void *arg, struct tcp_pcb *pcb, struct pbuf *pb, err_t err);
|
||||
|
||||
protected:
|
||||
ssize_t internal_write_(const void *buf, size_t len);
|
||||
int internal_output_();
|
||||
|
||||
pbuf *rx_buf_ = nullptr;
|
||||
size_t rx_buf_offset_ = 0;
|
||||
bool rx_closed_ = false;
|
||||
};
|
||||
|
||||
/// Listening socket implementation for LWIP raw TCP.
|
||||
/// Separate from LWIPRawImpl — no virtual dispatch needed.
|
||||
class LWIPRawListenImpl : public LWIPRawCommon {
|
||||
public:
|
||||
using LWIPRawCommon::LWIPRawCommon;
|
||||
~LWIPRawListenImpl();
|
||||
|
||||
void init();
|
||||
|
||||
bool ready() const { return this->accepted_socket_count_ > 0; }
|
||||
|
||||
std::unique_ptr<LWIPRawImpl> accept(struct sockaddr *addr, socklen_t *addrlen);
|
||||
std::unique_ptr<LWIPRawImpl> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) {
|
||||
return this->accept(addr, addrlen);
|
||||
}
|
||||
int listen(int backlog);
|
||||
|
||||
// Listening sockets don't do I/O
|
||||
ssize_t read(void *, size_t) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
ssize_t write(const void *, size_t) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
ssize_t readv(const struct iovec *, int) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
ssize_t writev(const struct iovec *, int) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
ssize_t recvfrom(void *, size_t, sockaddr *, socklen_t *) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
ssize_t sendto(const void *, size_t, int, const struct sockaddr *, socklen_t) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
int setblocking(bool) { return 0; }
|
||||
int loop() { return 0; }
|
||||
|
||||
static void s_err_fn(void *arg, err_t err);
|
||||
|
||||
private:
|
||||
err_t accept_fn_(struct tcp_pcb *newpcb, err_t err);
|
||||
static err_t s_accept_fn(void *arg, struct tcp_pcb *newpcb, err_t err);
|
||||
|
||||
// Accept queue - holds incoming connections briefly until the event loop calls accept()
|
||||
// This is NOT a connection pool - just a temporary queue between LWIP callbacks and the main loop
|
||||
// 3 slots is plenty since connections are pulled out quickly by the event loop
|
||||
//
|
||||
// Memory analysis: std::array<3> vs original std::queue implementation:
|
||||
// - std::queue uses std::deque internally which on 32-bit systems needs:
|
||||
// 24 bytes (deque object) + 32+ bytes (map array) + heap allocations
|
||||
// Total: ~56+ bytes minimum, plus heap fragmentation
|
||||
// - std::array<3>: 12 bytes fixed (3 pointers × 4 bytes)
|
||||
// Saves ~44+ bytes RAM per listening socket + avoids ALL heap allocations
|
||||
// Used on ESP8266 and RP2040 (platforms using LWIP_TCP implementation)
|
||||
//
|
||||
// By using a separate listening socket class, regular connected sockets save
|
||||
// 16 bytes (12 bytes array + 1 byte count + 3 bytes padding) of memory overhead on 32-bit systems
|
||||
static constexpr size_t MAX_ACCEPTED_SOCKETS = 3;
|
||||
std::array<std::unique_ptr<LWIPRawImpl>, MAX_ACCEPTED_SOCKETS> accepted_sockets_;
|
||||
uint8_t accepted_socket_count_ = 0; // Number of sockets currently in queue
|
||||
};
|
||||
|
||||
} // namespace esphome::socket
|
||||
|
||||
#endif // USE_SOCKET_IMPL_LWIP_TCP
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "socket.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "socket.h"
|
||||
|
||||
#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
|
||||
|
||||
@@ -9,94 +9,73 @@
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
class LwIPSocketImpl final : public Socket {
|
||||
public:
|
||||
LwIPSocketImpl(int fd, bool monitor_loop = false) {
|
||||
this->fd_ = fd;
|
||||
// Register new socket with the application for select() if monitoring requested
|
||||
if (monitor_loop && this->fd_ >= 0) {
|
||||
// Only set loop_monitored_ to true if registration succeeds
|
||||
this->loop_monitored_ = App.register_socket_fd(this->fd_);
|
||||
}
|
||||
}
|
||||
~LwIPSocketImpl() override {
|
||||
if (!this->closed_) {
|
||||
this->close(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall)
|
||||
}
|
||||
}
|
||||
int connect(const struct sockaddr *addr, socklen_t addrlen) override {
|
||||
return lwip_connect(this->fd_, addr, addrlen);
|
||||
}
|
||||
std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
int fd = lwip_accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<LwIPSocketImpl>(fd, false);
|
||||
}
|
||||
std::unique_ptr<Socket> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
int fd = lwip_accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<LwIPSocketImpl>(fd, true);
|
||||
LwIPSocketImpl::LwIPSocketImpl(int fd, bool monitor_loop) {
|
||||
this->fd_ = fd;
|
||||
// Register new socket with the application for select() if monitoring requested
|
||||
if (monitor_loop && this->fd_ >= 0) {
|
||||
// Only set loop_monitored_ to true if registration succeeds
|
||||
this->loop_monitored_ = App.register_socket_fd(this->fd_);
|
||||
}
|
||||
}
|
||||
|
||||
int bind(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_bind(this->fd_, addr, addrlen); }
|
||||
int close() override {
|
||||
if (!this->closed_) {
|
||||
// Unregister from select() before closing if monitored
|
||||
if (this->loop_monitored_) {
|
||||
App.unregister_socket_fd(this->fd_);
|
||||
}
|
||||
int ret = lwip_close(this->fd_);
|
||||
this->closed_ = true;
|
||||
return ret;
|
||||
LwIPSocketImpl::~LwIPSocketImpl() {
|
||||
if (!this->closed_) {
|
||||
this->close();
|
||||
}
|
||||
}
|
||||
|
||||
int LwIPSocketImpl::close() {
|
||||
if (!this->closed_) {
|
||||
// Unregister from select() before closing if monitored
|
||||
if (this->loop_monitored_) {
|
||||
App.unregister_socket_fd(this->fd_);
|
||||
}
|
||||
int ret = lwip_close(this->fd_);
|
||||
this->closed_ = true;
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LwIPSocketImpl::setblocking(bool blocking) {
|
||||
int fl = lwip_fcntl(this->fd_, F_GETFL, 0);
|
||||
if (blocking) {
|
||||
fl &= ~O_NONBLOCK;
|
||||
} else {
|
||||
fl |= O_NONBLOCK;
|
||||
}
|
||||
lwip_fcntl(this->fd_, F_SETFL, fl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool LwIPSocketImpl::ready() const { return socket_ready_fd(this->fd_, this->loop_monitored_); }
|
||||
|
||||
size_t LwIPSocketImpl::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
|
||||
buf[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
int shutdown(int how) override { return lwip_shutdown(this->fd_, how); }
|
||||
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
|
||||
}
|
||||
|
||||
int getpeername(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
return lwip_getpeername(this->fd_, addr, addrlen);
|
||||
}
|
||||
int getsockname(struct sockaddr *addr, socklen_t *addrlen) override {
|
||||
return lwip_getsockname(this->fd_, addr, addrlen);
|
||||
}
|
||||
int getsockopt(int level, int optname, void *optval, socklen_t *optlen) override {
|
||||
return lwip_getsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int setsockopt(int level, int optname, const void *optval, socklen_t optlen) override {
|
||||
return lwip_setsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int listen(int backlog) override { return lwip_listen(this->fd_, backlog); }
|
||||
ssize_t read(void *buf, size_t len) override { return lwip_read(this->fd_, buf, len); }
|
||||
ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) override {
|
||||
return lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
|
||||
}
|
||||
ssize_t readv(const struct iovec *iov, int iovcnt) override { return lwip_readv(this->fd_, iov, iovcnt); }
|
||||
ssize_t write(const void *buf, size_t len) override { return lwip_write(this->fd_, buf, len); }
|
||||
ssize_t send(void *buf, size_t len, int flags) { return lwip_send(this->fd_, buf, len, flags); }
|
||||
ssize_t writev(const struct iovec *iov, int iovcnt) override { return lwip_writev(this->fd_, iov, iovcnt); }
|
||||
ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) override {
|
||||
return lwip_sendto(this->fd_, buf, len, flags, to, tolen);
|
||||
}
|
||||
int setblocking(bool blocking) override {
|
||||
int fl = lwip_fcntl(this->fd_, F_GETFL, 0);
|
||||
if (blocking) {
|
||||
fl &= ~O_NONBLOCK;
|
||||
} else {
|
||||
fl |= O_NONBLOCK;
|
||||
}
|
||||
lwip_fcntl(this->fd_, F_SETFL, fl);
|
||||
size_t LwIPSocketImpl::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
|
||||
buf[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
|
||||
}
|
||||
|
||||
// Helper to create a socket with optional monitoring
|
||||
static std::unique_ptr<Socket> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
|
||||
static std::unique_ptr<LwIPSocketImpl> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
|
||||
int ret = lwip_socket(domain, type, protocol);
|
||||
if (ret == -1)
|
||||
return nullptr;
|
||||
return std::unique_ptr<Socket>{new LwIPSocketImpl(ret, loop_monitored)};
|
||||
return make_unique<LwIPSocketImpl>(ret, loop_monitored);
|
||||
}
|
||||
|
||||
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
|
||||
@@ -107,6 +86,14 @@ std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol
|
||||
return create_socket(domain, type, protocol, true);
|
||||
}
|
||||
|
||||
std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol) {
|
||||
return create_socket(domain, type, protocol, false);
|
||||
}
|
||||
|
||||
std::unique_ptr<ListenSocket> socket_listen_loop_monitored(int domain, int type, int protocol) {
|
||||
return create_socket(domain, type, protocol, true);
|
||||
}
|
||||
|
||||
} // namespace esphome::socket
|
||||
|
||||
#endif // USE_SOCKET_IMPL_LWIP_SOCKETS
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
|
||||
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "headers.h"
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
class LwIPSocketImpl {
|
||||
public:
|
||||
LwIPSocketImpl(int fd, bool monitor_loop = false);
|
||||
~LwIPSocketImpl();
|
||||
LwIPSocketImpl(const LwIPSocketImpl &) = delete;
|
||||
LwIPSocketImpl &operator=(const LwIPSocketImpl &) = delete;
|
||||
|
||||
int connect(const struct sockaddr *addr, socklen_t addrlen) { return lwip_connect(this->fd_, addr, addrlen); }
|
||||
std::unique_ptr<LwIPSocketImpl> accept(struct sockaddr *addr, socklen_t *addrlen) {
|
||||
int fd = lwip_accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<LwIPSocketImpl>(fd, false);
|
||||
}
|
||||
std::unique_ptr<LwIPSocketImpl> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) {
|
||||
int fd = lwip_accept(this->fd_, addr, addrlen);
|
||||
if (fd == -1)
|
||||
return {};
|
||||
return make_unique<LwIPSocketImpl>(fd, true);
|
||||
}
|
||||
|
||||
int bind(const struct sockaddr *addr, socklen_t addrlen) { return lwip_bind(this->fd_, addr, addrlen); }
|
||||
int close();
|
||||
int shutdown(int how) { return lwip_shutdown(this->fd_, how); }
|
||||
|
||||
int getpeername(struct sockaddr *addr, socklen_t *addrlen) { return lwip_getpeername(this->fd_, addr, addrlen); }
|
||||
int getsockname(struct sockaddr *addr, socklen_t *addrlen) { return lwip_getsockname(this->fd_, addr, addrlen); }
|
||||
|
||||
/// Format peer address into a fixed-size buffer (no heap allocation)
|
||||
size_t getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
/// Format local address into a fixed-size buffer (no heap allocation)
|
||||
size_t getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
|
||||
int getsockopt(int level, int optname, void *optval, socklen_t *optlen) {
|
||||
return lwip_getsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int setsockopt(int level, int optname, const void *optval, socklen_t optlen) {
|
||||
return lwip_setsockopt(this->fd_, level, optname, optval, optlen);
|
||||
}
|
||||
int listen(int backlog) { return lwip_listen(this->fd_, backlog); }
|
||||
ssize_t read(void *buf, size_t len) { return lwip_read(this->fd_, buf, len); }
|
||||
ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) {
|
||||
return lwip_recvfrom(this->fd_, buf, len, 0, addr, addr_len);
|
||||
}
|
||||
ssize_t readv(const struct iovec *iov, int iovcnt) { return lwip_readv(this->fd_, iov, iovcnt); }
|
||||
ssize_t write(const void *buf, size_t len) { return lwip_write(this->fd_, buf, len); }
|
||||
ssize_t send(void *buf, size_t len, int flags) { return lwip_send(this->fd_, buf, len, flags); }
|
||||
ssize_t writev(const struct iovec *iov, int iovcnt) { return lwip_writev(this->fd_, iov, iovcnt); }
|
||||
ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) {
|
||||
return lwip_sendto(this->fd_, buf, len, flags, to, tolen);
|
||||
}
|
||||
int setblocking(bool blocking);
|
||||
int loop() { return 0; }
|
||||
|
||||
bool ready() const;
|
||||
|
||||
int get_fd() const { return this->fd_; }
|
||||
|
||||
protected:
|
||||
int fd_{-1};
|
||||
bool closed_{false};
|
||||
bool loop_monitored_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::socket
|
||||
|
||||
#endif // USE_SOCKET_IMPL_LWIP_SOCKETS
|
||||
@@ -8,10 +8,10 @@
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
Socket::~Socket() {}
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
bool Socket::ready() const { return !this->loop_monitored_ || App.is_socket_ready_(this->fd_); }
|
||||
// Shared ready() implementation for fd-based socket implementations (BSD and LWIP sockets).
|
||||
// Checks if the Application's select() loop has marked this fd as ready.
|
||||
bool socket_ready_fd(int fd, bool loop_monitored) { return !loop_monitored || App.is_socket_ready_(fd); }
|
||||
#endif
|
||||
|
||||
// Platform-specific inet_ntop wrappers
|
||||
@@ -81,26 +81,6 @@ size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::s
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t Socket::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
|
||||
buf[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
|
||||
}
|
||||
|
||||
size_t Socket::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
|
||||
buf[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
|
||||
}
|
||||
|
||||
std::unique_ptr<Socket> socket_ip(int type, int protocol) {
|
||||
#if USE_NETWORK_IPV6
|
||||
return socket(AF_INET6, type, protocol);
|
||||
@@ -109,11 +89,11 @@ std::unique_ptr<Socket> socket_ip(int type, int protocol) {
|
||||
#endif /* USE_NETWORK_IPV6 */
|
||||
}
|
||||
|
||||
std::unique_ptr<Socket> socket_ip_loop_monitored(int type, int protocol) {
|
||||
std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol) {
|
||||
#if USE_NETWORK_IPV6
|
||||
return socket_loop_monitored(AF_INET6, type, protocol);
|
||||
return socket_listen_loop_monitored(AF_INET6, type, protocol);
|
||||
#else
|
||||
return socket_loop_monitored(AF_INET, type, protocol);
|
||||
return socket_listen_loop_monitored(AF_INET, type, protocol);
|
||||
#endif /* USE_NETWORK_IPV6 */
|
||||
}
|
||||
|
||||
|
||||
@@ -7,87 +7,41 @@
|
||||
#include "headers.h"
|
||||
|
||||
#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
|
||||
|
||||
// Include only the active implementation's header.
|
||||
// SOCKADDR_STR_LEN is defined in headers.h.
|
||||
#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
#include "bsd_sockets_impl.h"
|
||||
#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||
#include "lwip_sockets_impl.h"
|
||||
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||
#include "lwip_raw_tcp_impl.h"
|
||||
#endif
|
||||
|
||||
namespace esphome::socket {
|
||||
|
||||
// Maximum length for formatted socket address string (IP address without port)
|
||||
// IPv4: "255.255.255.255" = 15 chars + null = 16
|
||||
// IPv6: full address = 45 chars + null = 46
|
||||
#if USE_NETWORK_IPV6
|
||||
static constexpr size_t SOCKADDR_STR_LEN = 46; // INET6_ADDRSTRLEN
|
||||
#else
|
||||
static constexpr size_t SOCKADDR_STR_LEN = 16; // INET_ADDRSTRLEN
|
||||
// Type aliases — only one implementation is active per build.
|
||||
// Socket is the concrete type for connected sockets.
|
||||
// ListenSocket is the concrete type for listening/server sockets.
|
||||
// On BSD and LWIP_SOCKETS, both aliases resolve to the same type.
|
||||
// On LWIP_TCP, they are different types (no virtual dispatch between them).
|
||||
#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
using Socket = BSDSocketImpl;
|
||||
using ListenSocket = BSDSocketImpl;
|
||||
#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||
using Socket = LwIPSocketImpl;
|
||||
using ListenSocket = LwIPSocketImpl;
|
||||
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||
using Socket = LWIPRawImpl;
|
||||
using ListenSocket = LWIPRawListenImpl;
|
||||
#endif
|
||||
|
||||
class Socket {
|
||||
public:
|
||||
Socket() = default;
|
||||
virtual ~Socket();
|
||||
Socket(const Socket &) = delete;
|
||||
Socket &operator=(const Socket &) = delete;
|
||||
|
||||
virtual std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) = 0;
|
||||
/// Accept a connection and monitor it in the main loop
|
||||
/// NOTE: This function is NOT thread-safe and must only be called from the main loop
|
||||
virtual std::unique_ptr<Socket> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) {
|
||||
return accept(addr, addrlen); // Default implementation for backward compatibility
|
||||
}
|
||||
virtual int bind(const struct sockaddr *addr, socklen_t addrlen) = 0;
|
||||
virtual int close() = 0;
|
||||
// not supported yet:
|
||||
// virtual int connect(const std::string &address) = 0;
|
||||
#if defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
|
||||
virtual int connect(const struct sockaddr *addr, socklen_t addrlen) = 0;
|
||||
#endif
|
||||
virtual int shutdown(int how) = 0;
|
||||
|
||||
virtual int getpeername(struct sockaddr *addr, socklen_t *addrlen) = 0;
|
||||
virtual int getsockname(struct sockaddr *addr, socklen_t *addrlen) = 0;
|
||||
|
||||
/// Format peer address into a fixed-size buffer (no heap allocation)
|
||||
/// Non-virtual wrapper around getpeername() - can be optimized away if unused
|
||||
/// Returns number of characters written (excluding null terminator), or 0 on error
|
||||
size_t getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
/// Format local address into a fixed-size buffer (no heap allocation)
|
||||
/// Non-virtual wrapper around getsockname() - can be optimized away if unused
|
||||
size_t getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf);
|
||||
virtual int getsockopt(int level, int optname, void *optval, socklen_t *optlen) = 0;
|
||||
virtual int setsockopt(int level, int optname, const void *optval, socklen_t optlen) = 0;
|
||||
virtual int listen(int backlog) = 0;
|
||||
virtual ssize_t read(void *buf, size_t len) = 0;
|
||||
virtual ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) = 0;
|
||||
virtual ssize_t readv(const struct iovec *iov, int iovcnt) = 0;
|
||||
virtual ssize_t write(const void *buf, size_t len) = 0;
|
||||
virtual ssize_t writev(const struct iovec *iov, int iovcnt) = 0;
|
||||
virtual ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) = 0;
|
||||
|
||||
virtual int setblocking(bool blocking) = 0;
|
||||
virtual int loop() { return 0; };
|
||||
|
||||
/// Get the underlying file descriptor (returns -1 if not supported)
|
||||
/// Non-virtual: only one socket implementation is active per build.
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
int get_fd() const { return this->fd_; }
|
||||
#else
|
||||
int get_fd() const { return -1; }
|
||||
/// Shared ready() helper for fd-based socket implementations.
|
||||
/// Checks if the Application's select() loop has marked this fd as ready.
|
||||
bool socket_ready_fd(int fd, bool loop_monitored);
|
||||
#endif
|
||||
|
||||
/// Check if socket has data ready to read. Must only be called from the main loop thread.
|
||||
/// For select()-based sockets: non-virtual, checks Application's select() results
|
||||
/// For LWIP raw TCP sockets: virtual, checks internal buffer state
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
bool ready() const;
|
||||
#else
|
||||
virtual bool ready() const { return true; }
|
||||
#endif
|
||||
|
||||
protected:
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
int fd_{-1};
|
||||
bool closed_{false};
|
||||
bool loop_monitored_{false};
|
||||
#endif
|
||||
};
|
||||
|
||||
/// Create a socket of the given domain, type and protocol.
|
||||
std::unique_ptr<Socket> socket(int domain, int type, int protocol);
|
||||
/// Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol.
|
||||
@@ -100,7 +54,13 @@ std::unique_ptr<Socket> socket_ip(int type, int protocol);
|
||||
/// NOTE: On ESP platforms, FD_SETSIZE is typically 10, limiting the number of monitored sockets.
|
||||
/// File descriptors >= FD_SETSIZE will not be monitored and will log an error.
|
||||
std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol);
|
||||
std::unique_ptr<Socket> socket_ip_loop_monitored(int type, int protocol);
|
||||
|
||||
/// Create a listening socket of the given domain, type and protocol.
|
||||
std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol);
|
||||
/// Create a listening socket and monitor it for data in the main loop.
|
||||
std::unique_ptr<ListenSocket> socket_listen_loop_monitored(int domain, int type, int protocol);
|
||||
/// Create a listening socket in the newest available IP domain and monitor it.
|
||||
std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol);
|
||||
|
||||
/// Set a sockaddr to the specified address and port for the IP version used by socket_ip().
|
||||
/// @param addr Destination sockaddr structure
|
||||
|
||||
@@ -105,7 +105,10 @@
|
||||
#endif
|
||||
|
||||
namespace esphome::socket {
|
||||
class Socket;
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
/// Shared ready() helper for fd-based socket implementations.
|
||||
bool socket_ready_fd(int fd, bool loop_monitored); // NOLINT(readability-redundant-declaration)
|
||||
#endif
|
||||
} // namespace esphome::socket
|
||||
|
||||
// Forward declarations for friend access from codegen-generated setup()
|
||||
@@ -520,7 +523,9 @@ class Application {
|
||||
|
||||
protected:
|
||||
friend Component;
|
||||
friend class socket::Socket;
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
friend bool socket::socket_ready_fd(int fd, bool loop_monitored);
|
||||
#endif
|
||||
friend void ::setup();
|
||||
friend void ::original_setup();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user