31 #ifndef _SCC_NET_SOCKET_H
32 #define _SCC_NET_SOCKET_H
35 #include <system_error>
64 virtual operator const sockaddr*()
const {
return nullptr; }
65 virtual operator sockaddr*() {
return nullptr; }
66 virtual unsigned len()
const {
return 0; }
67 virtual std::string str()
const {
return ""; }
68 virtual std::string host()
const {
return ""; }
88 void reset(
int,
int,
int);
94 void get_sockaddr(sockaddr&);
112 int fd()
const {
return m_fd; }
114 operator int()
const {
return m_fd; }
254 size_t recv(
void* loc,
size_t len);
265 size_t recv(
void* loc,
size_t len, std::error_code& e) noexcept;
271 size_t send(
const void* loc,
size_t len);
281 size_t send(
const void* loc,
size_t len, std::error_code& e) noexcept;
301 int accept(sockaddr*,
int len, std::error_code&) noexcept;
305 virtual void reset() = 0;
313 void listen(
int maxConnections = 10);
329 virtual size_t read(
void* loc,
size_t len)
331 return recv(loc, len);
338 virtual size_t write(
const void* loc,
size_t len)
340 return send(loc, len);
353 virtual void reset() = 0;
366 size_t recv(
void* loc,
size_t len,
SockaddrBase& s, std::error_code& e) noexcept;
378 size_t send(
const void* loc,
size_t len,
const SockaddrBase& d, std::error_code& e) noexcept;
Socket address base class.
int fd() const
Return the underlying socket handle.
unsigned send_bufsize()
Get total send buffer size including overhead.
std::error_code error_code()
Get the current error code (status).
void recv_timeout(std::chrono::milliseconds)
Set the receive timeout.
size_t recv(void *loc, size_t len)
Receive bytes, throwing an exception on error.
void send_timeout(std::chrono::milliseconds)
Set the send timeout.
SocketBase(SocketBase &&other)
Move construct socket.
void non_blocking(bool b=true)
Set the socket non-blocking.
const SocketBase & operator=(SocketBase &&other)
Move assign socket.
size_t send(const void *loc, size_t len)
Send bytes, throwing an exception on error.
void bind(const SockaddrBase &)
Bind an address to the socket.
void reuse_addr(bool r=true)
Set address reusable.
unsigned recv_bufsize()
Get total receive buffer size including overhead.
void reuse_port(bool r=true)
Set port reusable.
void close()
Close the socket.
void shutdown()
Shutdown the connection; no further reads or writes will be allowed.
void listen(int maxConnections=10)
Set to accept connections.
virtual size_t read(void *loc, size_t len)
Read bytes.
virtual size_t write(const void *loc, size_t len)
Write bytes.
int accept(sockaddr *, int len, std::error_code &) noexcept
Accept a connection.
void connect(SockaddrBase &)
Connect to a socket address.
size_t recv(void *loc, size_t len, SockaddrBase &s)
Receive bytes (a datagram), setting the socket address of the peer.
size_t send(const void *loc, size_t len, const SockaddrBase &d)
Send bytes (a datagram) to a peer address.
size_t recv_next()
Return the number of bytes available to read (the size of the next datagram).
Input/output stream base reader/writer interface classes.
std::ostream & operator<<(std::ostream &, const scc::net::SockaddrBase &)
Helper to print socket address to output stream.
Interface class for objects which can be read.
Interface class for objects which can be written.