[socket] Move thin UDP and listen factory wrappers inline into socket.h

All the _ip_ variants and non-LWIP_TCP delegators are one-liners
that just forward to other factory functions. Move them inline to
reduce socket.cpp and keep the dispatch logic visible in one place.
This commit is contained in:
J. Nick Koston
2026-04-09 09:09:17 -10:00
parent 41a8e7f61b
commit c3827423ba
2 changed files with 30 additions and 58 deletions
-50
View File
@@ -89,56 +89,6 @@ std::unique_ptr<Socket> socket_ip(int type, int protocol) {
#endif /* USE_NETWORK_IPV6 */
}
#ifdef USE_SOCKET_IMPL_LWIP_TCP
// LWIP_TCP has separate Socket/ListenSocket types — needs out-of-line factory.
// BSD and LWIP_SOCKETS define this inline in socket.h.
std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol) {
#if USE_NETWORK_IPV6
return socket_listen_loop_monitored(AF_INET6, type, protocol);
#else
return socket_listen_loop_monitored(AF_INET, type, protocol);
#endif /* USE_NETWORK_IPV6 */
}
#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) {
return esphome::socket::socket(domain, SOCK_DGRAM, protocol);
}
std::unique_ptr<UDPRecvSocket> socket_udp_recv(int domain, int protocol) {
return esphome::socket::socket(domain, SOCK_DGRAM, protocol);
}
#endif
std::unique_ptr<UDPSocket> socket_ip_udp(int protocol) {
#if USE_NETWORK_IPV6
return socket_udp(AF_INET6, protocol);
#else
return socket_udp(AF_INET, protocol);
#endif
}
std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv(int protocol) {
#if USE_NETWORK_IPV6
return socket_udp_recv(AF_INET6, protocol);
#else
return socket_udp_recv(AF_INET, protocol);
#endif
}
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port) {
#if USE_NETWORK_IPV6
if (strchr(ip_address, ':') != nullptr) {
+30 -8
View File
@@ -89,25 +89,49 @@ std::unique_ptr<Socket> socket(int domain, int type, int protocol);
std::unique_ptr<Socket> socket_ip(int type, int protocol);
/// Create a send-only UDP socket of the given domain and protocol.
#ifdef USE_SOCKET_IMPL_LWIP_TCP
std::unique_ptr<UDPSocket> socket_udp(int domain, int protocol);
#else
inline std::unique_ptr<UDPSocket> socket_udp(int domain, int protocol) {
return esphome::socket::socket(domain, SOCK_DGRAM, protocol);
}
#endif
/// Create a send-only UDP socket in the newest available IP domain.
std::unique_ptr<UDPSocket> socket_ip_udp(int protocol);
inline std::unique_ptr<UDPSocket> socket_ip_udp(int protocol) {
#if USE_NETWORK_IPV6
return socket_udp(AF_INET6, protocol);
#else
return socket_udp(AF_INET, protocol);
#endif
}
/// Create a UDP socket with receive support of the given domain and protocol.
#ifdef USE_SOCKET_IMPL_LWIP_TCP
std::unique_ptr<UDPRecvSocket> socket_udp_recv(int domain, int protocol);
#else
inline std::unique_ptr<UDPRecvSocket> socket_udp_recv(int domain, int protocol) {
return esphome::socket::socket(domain, SOCK_DGRAM, protocol);
}
#endif
/// Create a UDP socket with receive support in the newest available IP domain.
std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv(int protocol);
inline std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv(int protocol) {
#if USE_NETWORK_IPV6
return socket_udp_recv(AF_INET6, protocol);
#else
return socket_udp_recv(AF_INET, protocol);
#endif
}
/// 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);
}
#endif
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);
@@ -115,7 +139,6 @@ inline std::unique_ptr<UDPRecvSocket> socket_ip_udp_recv_loop_monitored(int prot
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.
@@ -132,7 +155,6 @@ std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol
// LWIP_TCP has separate Socket/ListenSocket types — needs distinct factory functions.
std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol);
std::unique_ptr<ListenSocket> socket_listen_loop_monitored(int domain, int type, int protocol);
std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol);
#else
// BSD and LWIP_SOCKETS: Socket == ListenSocket, so listen variants just delegate.
inline std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int protocol) {
@@ -141,14 +163,14 @@ inline std::unique_ptr<ListenSocket> socket_listen(int domain, int type, int pro
inline std::unique_ptr<ListenSocket> socket_listen_loop_monitored(int domain, int type, int protocol) {
return socket_loop_monitored(domain, type, protocol);
}
#endif
inline 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
}
#endif
/// Set a sockaddr to the specified address and port for the IP version used by socket_ip().
/// @param addr Destination sockaddr structure