mirror of
https://github.com/esphome/esphome.git
synced 2026-09-15 17:18:40 +00:00
[socket] Add unified UDP recv loop-monitored factory functions
Add socket_udp_recv_loop_monitored() and socket_ip_udp_recv_loop_monitored() so consumers can create UDP recv sockets with loop wake support using a single API across all platforms, without #ifdef guards.
This commit is contained in:
@@ -1249,6 +1249,11 @@ std::unique_ptr<UDPRecvSocket> socket_udp_recv(int domain, int protocol) {
|
||||
return sock;
|
||||
}
|
||||
|
||||
std::unique_ptr<UDPRecvSocket> socket_udp_recv_loop_monitored(int domain, int protocol) {
|
||||
// LWIPRawUDPRecvImpl has wake built into the recv callback, so no extra monitoring needed
|
||||
return socket_udp_recv(domain, protocol);
|
||||
}
|
||||
|
||||
std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol) {
|
||||
if (type != SOCK_STREAM) {
|
||||
ESP_LOGE(TAG, "Use socket_udp_recv() for UDP sockets on this platform");
|
||||
|
||||
@@ -101,6 +101,18 @@ std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_SOCKET_IMPL_LWIP_TCP
|
||||
// LWIP_TCP has separate UDPRecvSocket type — needs out-of-line factory.
|
||||
// BSD and LWIP_SOCKETS define this inline in socket.h.
|
||||
std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv_loop_monitored(int protocol) {
|
||||
#if USE_NETWORK_IPV6
|
||||
return socket_udp_recv_loop_monitored(AF_INET6, protocol);
|
||||
#else
|
||||
return socket_udp_recv_loop_monitored(AF_INET, protocol);
|
||||
#endif /* USE_NETWORK_IPV6 */
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||
// BSD and LWIP_SOCKETS: UDPSocket == UDPRecvSocket == Socket, so these just delegate.
|
||||
std::unique_ptr<UDPSocket> socket_udp(int domain, int protocol) {
|
||||
|
||||
@@ -98,6 +98,25 @@ std::unique_ptr<UDPRecvSocket> socket_udp_recv(int domain, int protocol);
|
||||
/// Create a UDP socket with receive support in the newest available IP domain.
|
||||
std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv(int protocol);
|
||||
|
||||
/// Create a UDP recv socket and monitor it for data in the main loop.
|
||||
/// On LWIP_TCP platforms, wake is built into the recv callback so this just delegates to socket_udp_recv().
|
||||
/// On BSD/LWIP_SOCKETS platforms, this registers the socket with the Application's select() loop.
|
||||
#ifdef USE_SOCKET_IMPL_LWIP_TCP
|
||||
std::unique_ptr<UDPRecvSocket> socket_udp_recv_loop_monitored(int domain, int protocol);
|
||||
std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv_loop_monitored(int protocol);
|
||||
#else
|
||||
inline std::unique_ptr<UDPRecvSocket> socket_udp_recv_loop_monitored(int domain, int protocol) {
|
||||
return socket_loop_monitored(domain, SOCK_DGRAM, protocol);
|
||||
}
|
||||
inline std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv_loop_monitored(int protocol) {
|
||||
#if USE_NETWORK_IPV6
|
||||
return socket_udp_recv_loop_monitored(AF_INET6, protocol);
|
||||
#else
|
||||
return socket_udp_recv_loop_monitored(AF_INET, protocol);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Create a socket and monitor it for data in the main loop.
|
||||
/// Like socket() but also registers the socket with the Application's select() loop.
|
||||
/// WARNING: These functions are NOT thread-safe. They must only be called from the main loop
|
||||
|
||||
Reference in New Issue
Block a user