sockpp
Modern C++ socket library wrapper
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Friends | List of all members
sockpp::stream_socket Class Reference

Base class for streaming sockets, such as TCP and Unix Domain. More...

#include <stream_socket.h>

Inheritance diagram for sockpp::stream_socket:
sockpp::socket sockpp::connector sockpp::stream_socket_tmpl< ADDR > sockpp::connector_tmpl< STREAM_SOCK, ADDR >

Public Member Functions

 stream_socket ()
 Creates an unconnected streaming socket.
 
 stream_socket (socket_t handle)
 Creates a streaming socket from an existing OS socket handle and claims ownership of the handle. More...
 
 stream_socket (stream_socket &&sock)
 Creates a stream socket by copying the socket handle from the specified socket object and transfers ownership of the socket.
 
stream_socketoperator= (stream_socket &&rhs)
 Move assignment. More...
 
stream_socket clone () const
 Creates a new stream_socket that refers to this one. More...
 
virtual ssize_t read (void *buf, size_t n)
 Reads from the port. More...
 
virtual ssize_t read_n (void *buf, size_t n)
 Best effort attempts to read the specified number of bytes. More...
 
ssize_t read (const std::vector< iovec > &ranges)
 Reads discontiguous memory ranges from the socket. More...
 
virtual bool read_timeout (const std::chrono::microseconds &to)
 Set a timeout for read operations. More...
 
template<class Rep , class Period >
bool read_timeout (const std::chrono::duration< Rep, Period > &to)
 Set a timeout for read operations. More...
 
virtual ssize_t write (const void *buf, size_t n)
 Writes the buffer to the socket. More...
 
virtual ssize_t write_n (const void *buf, size_t n)
 Best effort attempt to write the whole buffer to the socket. More...
 
virtual ssize_t write (const std::string &s)
 Best effort attempt to write a string to the socket. More...
 
virtual ssize_t write (const std::vector< iovec > &ranges)
 Writes discontiguous memory ranges to the socket. More...
 
virtual bool write_timeout (const std::chrono::microseconds &to)
 Set a timeout for write operations. More...
 
template<class Rep , class Period >
bool write_timeout (const std::chrono::duration< Rep, Period > &to)
 Set a timeout for write operations. More...
 
- Public Member Functions inherited from sockpp::socket
 socket ()
 Creates an unconnected (invalid) socket.
 
 socket (socket_t h)
 Creates a socket from an existing OS socket handle. More...
 
 socket (socket &&sock) noexcept
 Move constructor. More...
 
virtual ~socket ()
 Destructor closes the socket.
 
bool is_open () const
 Determines if the socket is open (valid). More...
 
bool operator! () const
 Determines if the socket is closed or in an error state. More...
 
 operator bool () const
 Determines if the socket is open and in an error-free state. More...
 
socket_t handle () const
 Get the underlying OS socket handle. More...
 
virtual sa_family_t family () const
 Gets the network family of the address to which the socket is bound. More...
 
socket clone () const
 Creates a new socket that refers to this one. More...
 
void clear (int val=0)
 Clears the error flag for the object. More...
 
socket_t release ()
 Releases ownership of the underlying socket object. More...
 
void reset (socket_t h=INVALID_SOCKET)
 Replaces the underlying managed socket object. More...
 
socketoperator= (socket &&sock) noexcept
 Move assignment. More...
 
bool bind (const sock_address &addr)
 Binds the socket to the specified address. More...
 
sock_address_any address () const
 Gets the local address to which the socket is bound. More...
 
sock_address_any peer_address () const
 Gets the address of the remote peer, if this socket is connected. More...
 
bool get_option (int level, int optname, void *optval, socklen_t *optlen) const
 Gets the value of a socket option. More...
 
template<typename T >
bool get_option (int level, int optname, T *val) const
 Gets the value of a socket option. More...
 
bool set_option (int level, int optname, const void *optval, socklen_t optlen)
 Sets the value of a socket option. More...
 
template<typename T >
bool set_option (int level, int optname, const T &val)
 Sets the value of a socket option. More...
 
bool set_non_blocking (bool on=true)
 Places the socket into or out of non-blocking mode. More...
 
int last_error () const
 Gets the code for the last errror. More...
 
std::string last_error_str () const
 Gets a string describing the last errror. More...
 
bool shutdown (int how=SHUT_RDWR)
 Shuts down all or part of the full-duplex connection. More...
 
bool close ()
 Closes the socket. More...
 

Static Public Member Functions

static stream_socket create (int domain, int protocol=0)
 Creates a socket with the specified communications characterics. More...
 
- Static Public Member Functions inherited from sockpp::socket
static void initialize ()
 Initializes the socket (sockpp) library. More...
 
static void destroy ()
 Shuts down the socket library. More...
 
static socket create (int domain, int type, int protocol=0)
 Creates a socket with the specified communications characterics. More...
 
static std::tuple< socket, socketpair (int domain, int type, int protocol=0)
 Creates a pair of connected sockets. More...
 
static std::string error_str (int errNum)
 Gets a string describing the specified error. More...
 

Static Public Attributes

static constexpr int COMM_TYPE = SOCK_STREAM
 The socket 'type' for communications semantics. More...
 

Static Protected Member Functions

static socket_t create_handle (int domain)
 Creates a streaming socket. More...
 
- Static Protected Member Functions inherited from sockpp::socket
static int get_last_error ()
 OS-specific means to retrieve the last error from an operation. More...
 

Friends

class acceptor
 Acceptor can create stream sockets. More...
 

Additional Inherited Members

- Protected Member Functions inherited from sockpp::socket
bool close_on_err ()
 Closes the socket without checking for errors or updating the last error. More...
 
void set_last_error ()
 Cache the last system error code into this object. More...
 
template<typename T >
check_ret (T ret) const
 Checks the value and if less than zero, sets last error. More...
 
template<typename T >
bool check_ret_bool (T ret) const
 Checks the value and if less than zero, sets last error. More...
 
socket_t check_socket (socket_t ret) const
 Checks the value and if it is not a valid socket, sets last error. More...
 
bool check_socket_bool (socket_t ret) const
 Checks the value and if it is INVALID_SOCKET, sets last error. More...
 

Detailed Description

Base class for streaming sockets, such as TCP and Unix Domain.

This is the streaming connection between two peers. It looks like a readable/writeable device.

Constructor & Destructor Documentation

◆ stream_socket()

sockpp::stream_socket::stream_socket ( socket_t  handle)
inlineexplicit

Creates a streaming socket from an existing OS socket handle and claims ownership of the handle.

Parameters
handleA socket handle from the operating system.

Member Function Documentation

◆ clone()

stream_socket sockpp::stream_socket::clone ( ) const
inline

Creates a new stream_socket that refers to this one.

This creates a new object with an independent lifetime, but refers back to this same socket. On most systems, this duplicates the file handle using the dup() call. A typical use of this is to have separate threads for reading and writing the socket. One thread would get the original socket and the other would get the cloned one.

Returns
A new stream socket object that refers to the same socket as this one.

◆ create()

static stream_socket sockpp::stream_socket::create ( int  domain,
int  protocol = 0 
)
static

Creates a socket with the specified communications characterics.

Not that this is not normally how a socket is creates in the sockpp library. Applications would typically create a connector (client) or acceptor (server) socket which would take care of the details.

This is included for completeness or for creating different types of sockets than are supported by the library.

Parameters
domainThe communications domain for the sockets (i.e. the address family)
protocolThe particular protocol to be used with the sockets
Returns
A stream socket with the requested communications characteristics.

◆ create_handle()

static socket_t sockpp::stream_socket::create_handle ( int  domain)
inlinestaticprotected

Creates a streaming socket.

Returns
An OS handle to a stream socket.

◆ operator=()

stream_socket& sockpp::stream_socket::operator= ( stream_socket &&  rhs)
inline

Move assignment.

Parameters
rhsThe other socket to move into this one.
Returns
A reference to this object.

◆ read() [1/2]

virtual ssize_t sockpp::stream_socket::read ( void *  buf,
size_t  n 
)
virtual

Reads from the port.

Parameters
bufBuffer to get the incoming data.
nThe number of bytes to try to read.
Returns
The number of bytes read on success, or -1 on error.

◆ read() [2/2]

ssize_t sockpp::stream_socket::read ( const std::vector< iovec > &  ranges)

Reads discontiguous memory ranges from the socket.

Parameters
rangesThe vector of memory ranges to fill
Returns
The number of bytes read, or -1 on error.

◆ read_n()

virtual ssize_t sockpp::stream_socket::read_n ( void *  buf,
size_t  n 
)
virtual

Best effort attempts to read the specified number of bytes.

This will make repeated read attempts until all the bytes are read in or until an error occurs.

Parameters
bufBuffer to get the incoming data.
nThe number of bytes to try to read.
Returns
The number of bytes read on success, or -1 on error. If successful, the number of bytes read should always be 'n'.

◆ read_timeout() [1/2]

virtual bool sockpp::stream_socket::read_timeout ( const std::chrono::microseconds &  to)
virtual

Set a timeout for read operations.

Sets the timeout that the device uses for read operations. Not all devices support timeouts, so the caller should prepare for failure.

Parameters
toThe amount of time to wait for the operation to complete.
Returns
true on success, false on failure.

◆ read_timeout() [2/2]

template<class Rep , class Period >
bool sockpp::stream_socket::read_timeout ( const std::chrono::duration< Rep, Period > &  to)
inline

Set a timeout for read operations.

Sets the timout that the device uses for read operations. Not all devices support timouts, so the caller should prepare for failure.

Parameters
toThe amount of time to wait for the operation to complete.
Returns
true on success, false on failure.

◆ write() [1/3]

virtual ssize_t sockpp::stream_socket::write ( const void *  buf,
size_t  n 
)
virtual

Writes the buffer to the socket.

Parameters
bufThe buffer to write
nThe number of bytes in the buffer.
Returns
The number of bytes written, or -1 on error.

◆ write() [2/3]

virtual ssize_t sockpp::stream_socket::write ( const std::string &  s)
inlinevirtual

Best effort attempt to write a string to the socket.

Parameters
sThe string to write.
Returns
The number of bytes written, or 1 on error. On success, the number of bytes written should always be the length of the string.

◆ write() [3/3]

virtual ssize_t sockpp::stream_socket::write ( const std::vector< iovec > &  ranges)
virtual

Writes discontiguous memory ranges to the socket.

Parameters
rangesThe vector of memory ranges to write
Returns
The number of bytes written, or -1 on error.

◆ write_n()

virtual ssize_t sockpp::stream_socket::write_n ( const void *  buf,
size_t  n 
)
virtual

Best effort attempt to write the whole buffer to the socket.

Parameters
bufThe buffer to write
nThe number of bytes in the buffer.
Returns
The number of bytes written, or -1 on error. If successful, the number of bytes written should always be 'n'.

◆ write_timeout() [1/2]

virtual bool sockpp::stream_socket::write_timeout ( const std::chrono::microseconds &  to)
virtual

Set a timeout for write operations.

Sets the timout that the device uses for write operations. Not all devices support timouts, so the caller should prepare for failure.

Parameters
toThe amount of time to wait for the operation to complete.
Returns
true on success, false on failure.

◆ write_timeout() [2/2]

template<class Rep , class Period >
bool sockpp::stream_socket::write_timeout ( const std::chrono::duration< Rep, Period > &  to)
inline

Set a timeout for write operations.

Sets the timout that the device uses for write operations. Not all devices support timouts, so the caller should prepare for failure.

Parameters
toThe amount of time to wait for the operation to complete.
Returns
true on success, false on failure.

Friends And Related Function Documentation

◆ acceptor

friend class acceptor
friend

Acceptor can create stream sockets.

Member Data Documentation

◆ COMM_TYPE

constexpr int sockpp::stream_socket::COMM_TYPE = SOCK_STREAM
static

The socket 'type' for communications semantics.


The documentation for this class was generated from the following file: