mirror of
https://github.com/esphome/esphome.git
synced 2026-09-14 16:48:40 +00:00
[socket] Dedupe fast_select hook logic into shared helper
Lift the lwip_sock resolve + event-callback hook sequence into socket::fast_select_hook_fd() in socket.h so the USE_LWIP_FAST_SELECT constructor blocks in lwip_sockets_impl.cpp and bsd_sockets_impl.cpp stop drifting in lockstep. Both impls now collapse to a two-line call site.
This commit is contained in:
@@ -14,13 +14,8 @@ BSDSocketImpl::BSDSocketImpl(int fd, bool monitor_loop) {
|
||||
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;
|
||||
}
|
||||
this->cached_sock_ = fast_select_hook_fd(this->fd_);
|
||||
this->loop_monitored_ = this->cached_sock_ != nullptr;
|
||||
#else
|
||||
this->loop_monitored_ = App.register_socket_fd(this->fd_);
|
||||
#endif
|
||||
|
||||
@@ -14,13 +14,8 @@ LwIPSocketImpl::LwIPSocketImpl(int fd, bool monitor_loop) {
|
||||
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;
|
||||
}
|
||||
this->cached_sock_ = fast_select_hook_fd(this->fd_);
|
||||
this->loop_monitored_ = this->cached_sock_ != nullptr;
|
||||
#else
|
||||
this->loop_monitored_ = App.register_socket_fd(this->fd_);
|
||||
#endif
|
||||
|
||||
@@ -45,6 +45,17 @@ using ListenSocket = LWIPRawListenImpl;
|
||||
inline bool socket_ready(struct lwip_sock *cached_sock, bool loop_monitored) {
|
||||
return !loop_monitored || (cached_sock != nullptr && esphome_lwip_socket_has_data(cached_sock));
|
||||
}
|
||||
|
||||
/// Resolve an fd to its lwip_sock and hook the netconn event callback so the main loop
|
||||
/// is woken by FreeRTOS task notifications. Shared between BSD and LwIP socket impls.
|
||||
/// Returns the cached lwip_sock pointer (or nullptr if fd is invalid).
|
||||
inline struct lwip_sock *fast_select_hook_fd(int fd) {
|
||||
struct lwip_sock *sock = esphome_lwip_get_sock(fd);
|
||||
if (sock != nullptr) {
|
||||
esphome_lwip_hook_socket(sock);
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
#elif defined(USE_HOST)
|
||||
/// Shared ready() helper for fd-based socket implementations.
|
||||
/// Checks if the Application's select() loop has marked this fd as ready.
|
||||
|
||||
Reference in New Issue
Block a user