sockpp
Modern C++ socket library wrapper
stream_socket.h
Go to the documentation of this file.
1 
13 // --------------------------------------------------------------------------
14 // This file is part of the "sockpp" C++ socket library.
15 //
16 // Copyright (c) 2014-2017 Frank Pagliughi
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are
21 // met:
22 //
23 // 1. Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
25 //
26 // 2. Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimer in the
28 // documentation and/or other materials provided with the distribution.
29 //
30 // 3. Neither the name of the copyright holder nor the names of its
31 // contributors may be used to endorse or promote products derived from this
32 // software without specific prior written permission.
33 //
34 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
36 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
38 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 // --------------------------------------------------------------------------
46 
47 #ifndef __sockpp_stream_socket_h
48 #define __sockpp_stream_socket_h
49 
50 #include "sockpp/socket.h"
51 #include <vector>
52 
53 namespace sockpp {
54 
56 
62 class stream_socket : public socket
63 {
65  using base = socket;
66 
67 protected:
69  friend class acceptor;
70 
75  static socket_t create_handle(int domain) {
76  return (socket_t) ::socket(domain, COMM_TYPE, 0);
77  }
78 
79 public:
81  static constexpr int COMM_TYPE = SOCK_STREAM;
91  explicit stream_socket(socket_t handle) : base(handle) {}
96  stream_socket(stream_socket&& sock) : base(std::move(sock)) {}
97 
98 
115  static stream_socket create(int domain, int protocol=0);
116 
123  base::operator=(std::move(rhs));
124  return *this;
125  }
138  auto h = base::clone().release();
139  return stream_socket(h);
140  }
147  virtual ssize_t read(void *buf, size_t n);
157  virtual ssize_t read_n(void *buf, size_t n);
163  ssize_t read(const std::vector<iovec>& ranges);
171  virtual bool read_timeout(const std::chrono::microseconds& to);
179  template<class Rep, class Period>
180  bool read_timeout(const std::chrono::duration<Rep,Period>& to) {
181  return read_timeout(std::chrono::duration_cast<std::chrono::microseconds>(to));
182  }
189  virtual ssize_t write(const void *buf, size_t n);
197  virtual ssize_t write_n(const void *buf, size_t n);
205  virtual ssize_t write(const std::string& s) {
206  return write_n(s.data(), s.size());
207  }
213  virtual ssize_t write(const std::vector<iovec> &ranges);
221  virtual bool write_timeout(const std::chrono::microseconds& to);
229  template<class Rep, class Period>
230  bool write_timeout(const std::chrono::duration<Rep,Period>& to) {
231  return write_timeout(std::chrono::duration_cast<std::chrono::microseconds>(to));
232  }
233 };
234 
236 
244 template <typename ADDR>
246 {
248  using base = stream_socket;
249 
250 public:
252  static constexpr sa_family_t ADDRESS_FAMILY = ADDR::ADDRESS_FAMILY;
254  using addr_t = ADDR;
264  explicit stream_socket_tmpl(socket_t handle) : base(handle) {}
271  : base(std::move(sock)) {}
277  : base(std::move(sock)) {}
284  base::operator=(std::move(rhs));
285  return *this;
286  }
292  stream_socket_tmpl create(int protocol=0) {
293  return stream_socket_tmpl(std::move(base::create(ADDRESS_FAMILY, protocol)));
294  }
307  static std::tuple<stream_socket_tmpl, stream_socket_tmpl> pair(int protocol=0) {
308  auto pr = base::pair(ADDRESS_FAMILY, COMM_TYPE, protocol);
309  return std::make_tuple<stream_socket_tmpl, stream_socket_tmpl>(
310  stream_socket_tmpl{std::get<0>(pr).release()},
311  stream_socket_tmpl{std::get<1>(pr).release()});
312  }
318  addr_t address() const { return addr_t(socket::address()); }
325 };
326 
328 // end namespace sockpp
329 }
330 
331 #endif // __sockpp_socket_h
332 
stream_socket_tmpl()
Creates an unconnected streaming socket.
Definition: stream_socket.h:258
static std::tuple< socket, socket > pair(int domain, int type, int protocol=0)
Creates a pair of connected sockets.
virtual ssize_t write_n(const void *buf, size_t n)
Best effort attempt to write the whole buffer to the socket.
stream_socket & operator=(stream_socket &&rhs)
Move assignment.
Definition: stream_socket.h:122
sock_address_any address() const
Gets the local address to which the socket is bound.
addr_t peer_address() const
Gets the address of the remote peer, if this socket is connected.
Definition: stream_socket.h:324
stream_socket(stream_socket &&sock)
Creates a stream socket by copying the socket handle from the specified socket object and transfers o...
Definition: stream_socket.h:96
Base class for streaming sockets, such as TCP and Unix Domain.
Definition: stream_socket.h:62
virtual bool write_timeout(const std::chrono::microseconds &to)
Set a timeout for write operations.
virtual ssize_t read(void *buf, size_t n)
Reads from the port.
stream_socket_tmpl(stream_socket &&sock)
Move constructor.
Definition: stream_socket.h:270
static socket_t create_handle(int domain)
Creates a streaming socket.
Definition: stream_socket.h:75
stream_socket clone() const
Creates a new stream_socket that refers to this one.
Definition: stream_socket.h:137
stream_socket()
Creates an unconnected streaming socket.
Definition: stream_socket.h:85
virtual bool read_timeout(const std::chrono::microseconds &to)
Set a timeout for read operations.
bool write_timeout(const std::chrono::duration< Rep, Period > &to)
Set a timeout for write operations.
Definition: stream_socket.h:230
ADDR addr_t
The type of network address used with this socket.
Definition: stream_socket.h:254
static std::tuple< stream_socket_tmpl, stream_socket_tmpl > pair(int protocol=0)
Creates a pair of connected stream sockets.
Definition: stream_socket.h:307
Class for creating a streaming server.
Definition: acceptor.h:63
socket()
Creates an unconnected (invalid) socket.
Definition: socket.h:183
stream_socket_tmpl(stream_socket_tmpl &&sock)
Creates a stream socket by copying the socket handle from the specified socket object and transfers o...
Definition: stream_socket.h:276
Definition: acceptor.h:51
socket clone() const
Creates a new socket that refers to this one.
static socket create(int domain, int type, int protocol=0)
Creates a socket with the specified communications characterics.
stream_socket_tmpl create(int protocol=0)
Cretates a stream socket.
Definition: stream_socket.h:292
Base class for socket objects.
Definition: socket.h:84
int socket_t
The OS socket handle.
Definition: socket.h:60
stream_socket_tmpl & operator=(stream_socket_tmpl &&rhs)
Move assignment.
Definition: stream_socket.h:283
sock_address_any peer_address() const
Gets the address of the remote peer, if this socket is connected.
socket_t release()
Releases ownership of the underlying socket object.
Definition: socket.h:311
stream_socket_tmpl(socket_t handle)
Creates a streaming socket from an existing OS socket handle and claims ownership of the handle...
Definition: stream_socket.h:264
Template for creating specific stream types (IPv4, IPv6, etc).
Definition: stream_socket.h:245
static constexpr int COMM_TYPE
The socket &#39;type&#39; for communications semantics.
Definition: stream_socket.h:81
Classes for TCP & UDP socket.
bool read_timeout(const std::chrono::duration< Rep, Period > &to)
Set a timeout for read operations.
Definition: stream_socket.h:180
stream_socket(socket_t handle)
Creates a streaming socket from an existing OS socket handle and claims ownership of the handle...
Definition: stream_socket.h:91
socket_t handle() const
Get the underlying OS socket handle.
Definition: socket.h:259
virtual ssize_t write(const void *buf, size_t n)
Writes the buffer to the socket.
static stream_socket create(int domain, int protocol=0)
Creates a socket with the specified communications characterics.
addr_t address() const
Gets the local address to which the socket is bound.
Definition: stream_socket.h:318
virtual ssize_t write(const std::string &s)
Best effort attempt to write a string to the socket.
Definition: stream_socket.h:205
virtual ssize_t read_n(void *buf, size_t n)
Best effort attempts to read the specified number of bytes.