ESPHome 2026.2.3
Loading...
Searching...
No Matches
esphome::socket::Socket Class Referenceabstract

#include <socket.h>

Public Member Functions

 Socket ()=default
 
virtual ~Socket ()
 
 Socket (const Socket &)=delete
 
Socketoperator= (const Socket &)=delete
 
virtual std::unique_ptr< Socketaccept (struct sockaddr *addr, socklen_t *addrlen)=0
 
virtual std::unique_ptr< Socketaccept_loop_monitored (struct sockaddr *addr, socklen_t *addrlen)
 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 int bind (const struct sockaddr *addr, socklen_t addrlen)=0
 
virtual int close ()=0
 
virtual int connect (const struct sockaddr *addr, socklen_t addrlen)=0
 
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
 
size_t getpeername_to (std::span< char, SOCKADDR_STR_LEN > buf)
 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 getsockname_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.
 
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 ()
 
int get_fd () const
 Get the underlying file descriptor (returns -1 if not supported) Non-virtual: only one socket implementation is active per build.
 
int get_fd () const
 
bool ready () const
 Check if socket has data ready to read For select()-based sockets: non-virtual, checks Application's select() results For LWIP raw TCP sockets: virtual, checks internal buffer state.
 
virtual bool ready () const
 

Protected Attributes

int fd_ {-1}
 
bool closed_ {false}
 
bool loop_monitored_ {false}
 

Detailed Description

Definition at line 21 of file socket.h.

Constructor & Destructor Documentation

◆ Socket() [1/2]

esphome::socket::Socket::Socket ( )
default

◆ ~Socket()

esphome::socket::Socket::~Socket ( )
virtual

Definition at line 11 of file socket.cpp.

◆ Socket() [2/2]

esphome::socket::Socket::Socket ( const Socket & )
delete

Member Function Documentation

◆ accept()

virtual std::unique_ptr< Socket > esphome::socket::Socket::accept ( struct sockaddr * addr,
socklen_t * addrlen )
pure virtual

◆ accept_loop_monitored()

virtual std::unique_ptr< Socket > esphome::socket::Socket::accept_loop_monitored ( struct sockaddr * addr,
socklen_t * addrlen )
inlinevirtual

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.

Definition at line 31 of file socket.h.

◆ bind()

virtual int esphome::socket::Socket::bind ( const struct sockaddr * addr,
socklen_t addrlen )
pure virtual

◆ close()

virtual int esphome::socket::Socket::close ( )
pure virtual

◆ connect()

virtual int esphome::socket::Socket::connect ( const struct sockaddr * addr,
socklen_t addrlen )
pure virtual

◆ get_fd() [1/2]

int esphome::socket::Socket::get_fd ( ) const
inline

Get the underlying file descriptor (returns -1 if not supported) Non-virtual: only one socket implementation is active per build.

Definition at line 69 of file socket.h.

◆ get_fd() [2/2]

int esphome::socket::Socket::get_fd ( ) const
inline

Definition at line 71 of file socket.h.

◆ getpeername()

virtual int esphome::socket::Socket::getpeername ( struct sockaddr * addr,
socklen_t * addrlen )
pure virtual

◆ getpeername_to()

size_t esphome::socket::Socket::getpeername_to ( std::span< char, SOCKADDR_STR_LEN > buf)

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.

Definition at line 84 of file socket.cpp.

◆ getsockname()

virtual int esphome::socket::Socket::getsockname ( struct sockaddr * addr,
socklen_t * addrlen )
pure virtual

◆ getsockname_to()

size_t esphome::socket::Socket::getsockname_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.

Definition at line 94 of file socket.cpp.

◆ getsockopt()

virtual int esphome::socket::Socket::getsockopt ( int level,
int optname,
void * optval,
socklen_t * optlen )
pure virtual

◆ listen()

virtual int esphome::socket::Socket::listen ( int backlog)
pure virtual

◆ loop()

virtual int esphome::socket::Socket::loop ( )
inlinevirtual

Definition at line 64 of file socket.h.

◆ operator=()

Socket & esphome::socket::Socket::operator= ( const Socket & )
delete

◆ read()

virtual ssize_t esphome::socket::Socket::read ( void * buf,
size_t len )
pure virtual

◆ readv()

virtual ssize_t esphome::socket::Socket::readv ( const struct iovec * iov,
int iovcnt )
pure virtual

◆ ready() [1/2]

bool esphome::socket::Socket::ready ( ) const

Check if socket has data ready to read For select()-based sockets: non-virtual, checks Application's select() results For LWIP raw TCP sockets: virtual, checks internal buffer state.

Definition at line 14 of file socket.cpp.

◆ ready() [2/2]

virtual bool esphome::socket::Socket::ready ( ) const
inlinevirtual

Definition at line 80 of file socket.h.

◆ recvfrom()

virtual ssize_t esphome::socket::Socket::recvfrom ( void * buf,
size_t len,
sockaddr * addr,
socklen_t * addr_len )
pure virtual

◆ sendto()

virtual ssize_t esphome::socket::Socket::sendto ( const void * buf,
size_t len,
int flags,
const struct sockaddr * to,
socklen_t tolen )
pure virtual

◆ setblocking()

virtual int esphome::socket::Socket::setblocking ( bool blocking)
pure virtual

◆ setsockopt()

virtual int esphome::socket::Socket::setsockopt ( int level,
int optname,
const void * optval,
socklen_t optlen )
pure virtual

◆ shutdown()

virtual int esphome::socket::Socket::shutdown ( int how)
pure virtual

◆ write()

virtual ssize_t esphome::socket::Socket::write ( const void * buf,
size_t len )
pure virtual

◆ writev()

virtual ssize_t esphome::socket::Socket::writev ( const struct iovec * iov,
int iovcnt )
pure virtual

Field Documentation

◆ closed_

bool esphome::socket::Socket::closed_ {false}
protected

Definition at line 86 of file socket.h.

◆ fd_

int esphome::socket::Socket::fd_ {-1}
protected

Definition at line 85 of file socket.h.

◆ loop_monitored_

bool esphome::socket::Socket::loop_monitored_ {false}
protected

Definition at line 87 of file socket.h.


The documentation for this class was generated from the following files: