mirror of
https://github.com/esphome/esphome.git
synced 2026-09-14 00:28:39 +00:00
- Drop redundant cached_sock_ = nullptr assignment in close(). After closed_ = true the socket is a corpse and no ready() or other member access is valid, so the nulling is not load-bearing. The comment now explains why on both impl variants. - Reword the yield_with_select_ comment so the wake-source sentence reads as a complete list rather than a trailing fragment.
102 lines
3.0 KiB
C++
102 lines
3.0 KiB
C++
#include "esphome/core/defines.h"
|
|
#include "esphome/core/helpers.h"
|
|
#include "socket.h"
|
|
|
|
#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
|
|
|
|
#include <cstring>
|
|
#include "esphome/core/application.h"
|
|
|
|
namespace esphome::socket {
|
|
|
|
LwIPSocketImpl::LwIPSocketImpl(int fd, bool monitor_loop) {
|
|
this->fd_ = fd;
|
|
if (!monitor_loop || this->fd_ < 0)
|
|
return;
|
|
#ifdef USE_LWIP_FAST_SELECT
|
|
// Cache lwip_sock pointer (used by ready() for direct rcvevent reads) and hook the
|
|
// netconn event callback so the main loop is notified via FreeRTOS task notifications.
|
|
this->cached_sock_ = esphome_lwip_get_sock(this->fd_);
|
|
if (this->cached_sock_ != nullptr) {
|
|
esphome_lwip_hook_socket(this->cached_sock_);
|
|
this->loop_monitored_ = true;
|
|
}
|
|
#else
|
|
this->loop_monitored_ = App.register_socket_fd(this->fd_);
|
|
#endif
|
|
}
|
|
|
|
LwIPSocketImpl::~LwIPSocketImpl() {
|
|
if (!this->closed_) {
|
|
this->close();
|
|
}
|
|
}
|
|
|
|
int LwIPSocketImpl::close() {
|
|
if (!this->closed_) {
|
|
#ifndef USE_LWIP_FAST_SELECT
|
|
// All LwIP sockets share the same static event_callback, so on the fast-select path
|
|
// there is no per-socket unhook needed. cached_sock_ is not cleared because closed_
|
|
// makes the socket a corpse — no ready() or other member access is valid afterwards.
|
|
if (this->loop_monitored_) {
|
|
App.unregister_socket_fd(this->fd_);
|
|
}
|
|
#endif
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
|
|
}
|
|
|
|
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<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 make_unique<LwIPSocketImpl>(ret, loop_monitored);
|
|
}
|
|
|
|
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
|
|
return create_socket(domain, type, protocol, false);
|
|
}
|
|
|
|
std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol) {
|
|
return create_socket(domain, type, protocol, true);
|
|
}
|
|
|
|
} // namespace esphome::socket
|
|
|
|
#endif // USE_SOCKET_IMPL_LWIP_SOCKETS
|