ESPHome  2024.11.1
socket.h
Go to the documentation of this file.
1 #pragma once
2 #include <memory>
3 #include <string>
4 
6 #include "headers.h"
7 
8 #if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
9 namespace esphome {
10 namespace socket {
11 
12 class Socket {
13  public:
14  Socket() = default;
15  virtual ~Socket();
16  Socket(const Socket &) = delete;
17  Socket &operator=(const Socket &) = delete;
18 
19  virtual std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) = 0;
20  virtual int bind(const struct sockaddr *addr, socklen_t addrlen) = 0;
21  virtual int close() = 0;
22  // not supported yet:
23  // virtual int connect(const std::string &address) = 0;
24  // virtual int connect(const struct sockaddr *addr, socklen_t addrlen) = 0;
25  virtual int shutdown(int how) = 0;
26 
27  virtual int getpeername(struct sockaddr *addr, socklen_t *addrlen) = 0;
28  virtual std::string getpeername() = 0;
29  virtual int getsockname(struct sockaddr *addr, socklen_t *addrlen) = 0;
30  virtual std::string getsockname() = 0;
31  virtual int getsockopt(int level, int optname, void *optval, socklen_t *optlen) = 0;
32  virtual int setsockopt(int level, int optname, const void *optval, socklen_t optlen) = 0;
33  virtual int listen(int backlog) = 0;
34  virtual ssize_t read(void *buf, size_t len) = 0;
35 #ifdef USE_SOCKET_IMPL_BSD_SOCKETS
36  virtual ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len) = 0;
37 #endif
38  virtual ssize_t readv(const struct iovec *iov, int iovcnt) = 0;
39  virtual ssize_t write(const void *buf, size_t len) = 0;
40  virtual ssize_t writev(const struct iovec *iov, int iovcnt) = 0;
41  virtual ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) = 0;
42 
43  virtual int setblocking(bool blocking) = 0;
44  virtual int loop() { return 0; };
45 };
46 
48 std::unique_ptr<Socket> socket(int domain, int type, int protocol);
49 
51 std::unique_ptr<Socket> socket_ip(int type, int protocol);
52 
54 socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port);
55 
57 socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port);
58 
59 } // namespace socket
60 } // namespace esphome
61 #endif
virtual int setsockopt(int level, int optname, const void *optval, socklen_t optlen)=0
std::unique_ptr< Socket > socket_ip(int type, int protocol)
Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol...
Definition: socket.cpp:13
virtual int getsockopt(int level, int optname, void *optval, socklen_t *optlen)=0
virtual ssize_t readv(const struct iovec *iov, int iovcnt)=0
virtual int bind(const struct sockaddr *addr, socklen_t addrlen)=0
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
Definition: socket.cpp:51
virtual std::string getpeername()=0
virtual std::unique_ptr< Socket > accept(struct sockaddr *addr, socklen_t *addrlen)=0
virtual ssize_t write(const void *buf, size_t len)=0
uint32_t socklen_t
Definition: headers.h:97
virtual int shutdown(int how)=0
virtual ssize_t writev(const struct iovec *iov, int iovcnt)=0
virtual std::string getsockname()=0
uint8_t type
virtual int listen(int backlog)=0
virtual ssize_t read(void *buf, size_t len)=0
Definition: headers.h:100
virtual int loop()
Definition: socket.h:44
const uint32_t flags
Definition: stm32flash.h:85
std::string size_t len
Definition: helpers.h:293
virtual int setblocking(bool blocking)=0
virtual int close()=0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port)
Set a sockaddr to the specified address and port for the IP version used by socket_ip().
Definition: socket.cpp:21
virtual ssize_t sendto(const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)=0
virtual ssize_t recvfrom(void *buf, size_t len, sockaddr *addr, socklen_t *addr_len)=0
Socket & operator=(const Socket &)=delete
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.