ESPHome  2022.9.1
socket.h
Go to the documentation of this file.
1 #pragma once
2 #include <string>
3 #include <memory>
4 
5 #include "headers.h"
7 
8 namespace esphome {
9 namespace socket {
10 
11 class Socket {
12  public:
13  Socket() = default;
14  virtual ~Socket() = default;
15  Socket(const Socket &) = delete;
16  Socket &operator=(const Socket &) = delete;
17 
18  virtual std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) = 0;
19  virtual int bind(const struct sockaddr *addr, socklen_t addrlen) = 0;
20  virtual int close() = 0;
21  // not supported yet:
22  // virtual int connect(const std::string &address) = 0;
23  // virtual int connect(const struct sockaddr *addr, socklen_t addrlen) = 0;
24  virtual int shutdown(int how) = 0;
25 
26  virtual int getpeername(struct sockaddr *addr, socklen_t *addrlen) = 0;
27  virtual std::string getpeername() = 0;
28  virtual int getsockname(struct sockaddr *addr, socklen_t *addrlen) = 0;
29  virtual std::string getsockname() = 0;
30  virtual int getsockopt(int level, int optname, void *optval, socklen_t *optlen) = 0;
31  virtual int setsockopt(int level, int optname, const void *optval, socklen_t optlen) = 0;
32  virtual int listen(int backlog) = 0;
33  virtual ssize_t read(void *buf, size_t len) = 0;
34  virtual ssize_t readv(const struct iovec *iov, int iovcnt) = 0;
35  virtual ssize_t write(const void *buf, size_t len) = 0;
36  virtual ssize_t writev(const struct iovec *iov, int iovcnt) = 0;
37  virtual int setblocking(bool blocking) = 0;
38  virtual int loop() { return 0; };
39 };
40 
42 std::unique_ptr<Socket> socket(int domain, int type, int protocol);
43 
45 std::unique_ptr<Socket> socket_ip(int type, int protocol);
46 
48 socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port);
49 
50 } // namespace socket
51 } // namespace esphome
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:9
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 for the IP version used by socket_ip().
Definition: socket.cpp:17
virtual std::string getpeername()=0
virtual ~Socket()=default
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:86
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:89
virtual int loop()
Definition: socket.h:38
std::string size_t len
Definition: helpers.h:281
virtual int setblocking(bool blocking)=0
virtual int close()=0
Definition: a4988.cpp:4
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.