scclib
Stable Cloud Computing C++ Library
socket.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_NET_SOCKET_H
32 #define _SCC_NET_SOCKET_H
33 
34 #include <string>
35 #include <system_error>
36 #include <chrono>
37 #include <util/iobase.h>
38 
39 struct sockaddr;
40 
41 namespace scc::net {
42 
60 {
61 public:
62  SockaddrBase() {}
63  virtual ~SockaddrBase() {}
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 ""; }
69 };
70 
77 {
78  int m_fd;
79 
81  SocketBase(const SocketBase&) = delete;
83  void operator=(const SocketBase&) = delete;
84 
85 protected:
86  explicit SocketBase(int fd) : m_fd{fd} {}
87  SocketBase(int, int, int);
88  void reset(int, int, int);
89  void move(SocketBase& other)
90  {
91  m_fd = other.m_fd;
92  other.m_fd = -1;
93  }
94  void get_sockaddr(sockaddr&);
95 
96 public:
97  virtual ~SocketBase();
100  {
101  move(other);
102  }
105  {
106  close();
107  move(other);
108  return *this;
109  }
110 
112  int fd() const { return m_fd; }
114  operator int() const { return m_fd; }
115 
125  void reuse_addr(bool r = true);
126 
144  void reuse_port(bool r = true);
145 
188  void non_blocking(bool b = true);
198  std::error_code error_code();
205  void recv_bufsize(unsigned);
209  unsigned recv_bufsize();
216  void send_bufsize(unsigned);
220  unsigned send_bufsize();
231  void send_timeout(std::chrono::milliseconds);
242  void recv_timeout(std::chrono::milliseconds);
243 
247  void bind(const SockaddrBase&);
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;
285  void close();
286 };
287 
291 {
292 protected:
293  explicit TcpSocket(int fd) : SocketBase(fd) { }
294  TcpSocket(int domain, int stype, int proto) : SocketBase(domain, stype, proto) { }
301  int accept(sockaddr*, int len, std::error_code&) noexcept;
302 
303 public:
304 
305  virtual void reset() = 0;
306 
313  void listen(int maxConnections = 10);
314 
318  void connect(SockaddrBase&);
322  void connect(SockaddrBase&, std::error_code&) noexcept;
323 
329  virtual size_t read(void* loc, size_t len)
330  {
331  return recv(loc, len);
332  }
338  virtual size_t write(const void* loc, size_t len)
339  {
340  return send(loc, len);
341  }
342 
343  void shutdown();
344 };
345 
348 class UdpSocket : public SocketBase
349 {
350 protected:
351  UdpSocket(int, int, int);
352 public:
353  virtual void reset() = 0;
354 
360  size_t recv(void* loc, size_t len, SockaddrBase& s);
366  size_t recv(void* loc, size_t len, SockaddrBase& s, std::error_code& e) noexcept;
372  size_t send(const void* loc, size_t len, const SockaddrBase& d);
378  size_t send(const void* loc, size_t len, const SockaddrBase& d, std::error_code& e) noexcept;
382  size_t recv_next();
383 };
384 
388 } // namespace
389 
393 std::ostream& operator<<(std::ostream&, const scc::net::SockaddrBase&);
394 
395 #endif
Socket address base class.
Definition: socket.h:60
Socket base class.
Definition: socket.h:77
int fd() const
Return the underlying socket handle.
Definition: socket.h:112
unsigned send_bufsize()
Get total send buffer size including overhead.
Definition: socket.cc:148
std::error_code error_code()
Get the current error code (status).
Definition: socket.cc:94
void recv_timeout(std::chrono::milliseconds)
Set the receive timeout.
Definition: socket.cc:232
size_t recv(void *loc, size_t len)
Receive bytes, throwing an exception on error.
Definition: socket.cc:267
void send_timeout(std::chrono::milliseconds)
Set the send timeout.
Definition: socket.cc:217
SocketBase(SocketBase &&other)
Move construct socket.
Definition: socket.h:99
void non_blocking(bool b=true)
Set the socket non-blocking.
Definition: socket.cc:188
const SocketBase & operator=(SocketBase &&other)
Move assign socket.
Definition: socket.h:104
size_t send(const void *loc, size_t len)
Send bytes, throwing an exception on error.
Definition: socket.cc:300
void bind(const SockaddrBase &)
Bind an address to the socket.
Definition: socket.cc:313
void reuse_addr(bool r=true)
Set address reusable.
Definition: socket.cc:162
unsigned recv_bufsize()
Get total receive buffer size including overhead.
Definition: socket.cc:121
void reuse_port(bool r=true)
Set port reusable.
Definition: socket.cc:175
void close()
Close the socket.
Definition: socket.cc:72
Tcp socket base class.
Definition: socket.h:291
void shutdown()
Shutdown the connection; no further reads or writes will be allowed.
Definition: socket.cc:415
void listen(int maxConnections=10)
Set to accept connections.
Definition: socket.cc:334
virtual size_t read(void *loc, size_t len)
Read bytes.
Definition: socket.h:329
virtual size_t write(const void *loc, size_t len)
Write bytes.
Definition: socket.h:338
int accept(sockaddr *, int len, std::error_code &) noexcept
Accept a connection.
Definition: socket.cc:344
void connect(SockaddrBase &)
Connect to a socket address.
Definition: socket.cc:397
Udp socket base class.
Definition: socket.h:349
size_t recv(void *loc, size_t len, SockaddrBase &s)
Receive bytes (a datagram), setting the socket address of the peer.
Definition: socket.cc:453
size_t send(const void *loc, size_t len, const SockaddrBase &d)
Send bytes (a datagram) to a peer address.
Definition: socket.cc:486
size_t recv_next()
Return the number of bytes available to read (the size of the next datagram).
Definition: socket.cc:499
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.
Definition: socket.cc:51
Interface class for objects which can be read.
Definition: iobase.h:67
Interface class for objects which can be written.
Definition: iobase.h:86