Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
sockpp::stream_socket Class Reference

#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 ()
 
 stream_socket (socket_t handle)
 
 stream_socket (stream_socket &&sock)
 
stream_socketoperator= (stream_socket &&rhs)
 
stream_socket clone () const
 
virtual ssize_t read (void *buf, size_t n)
 
virtual ssize_t read_n (void *buf, size_t n)
 
ssize_t read (const std::vector< iovec > &ranges)
 
virtual bool read_timeout (const std::chrono::microseconds &to)
 
template<class Rep , class Period >
bool read_timeout (const std::chrono::duration< Rep, Period > &to)
 
virtual ssize_t write (const void *buf, size_t n)
 
virtual ssize_t write_n (const void *buf, size_t n)
 
virtual ssize_t write (const std::string &s)
 
virtual ssize_t write (const std::vector< iovec > &ranges)
 
virtual bool write_timeout (const std::chrono::microseconds &to)
 
template<class Rep , class Period >
bool write_timeout (const std::chrono::duration< Rep, Period > &to)
 
- Public Member Functions inherited from sockpp::socket
 socket ()
 
 socket (socket_t h)
 
 socket (socket &&sock) noexcept
 
virtual ~socket ()
 
bool is_open () const
 
bool operator! () const
 
 operator bool () const
 
socket_t handle () const
 
virtual sa_family_t family () const
 
socket clone () const
 
void clear (int val=0)
 
socket_t release ()
 
void reset (socket_t h=INVALID_SOCKET)
 
socketoperator= (socket &&sock) noexcept
 
bool bind (const sock_address &addr)
 
sock_address_any address () const
 
sock_address_any peer_address () const
 
bool get_option (int level, int optname, void *optval, socklen_t *optlen) const
 
template<typename T >
bool get_option (int level, int optname, T *val) const
 
bool set_option (int level, int optname, const void *optval, socklen_t optlen)
 
template<typename T >
bool set_option (int level, int optname, const T &val)
 
bool set_non_blocking (bool on=true)
 
int last_error () const
 
std::string last_error_str () const
 
bool shutdown (int how=SHUT_RDWR)
 
bool close ()
 

Static Public Member Functions

static stream_socket create (int domain, int protocol=0)
 
- Static Public Member Functions inherited from sockpp::socket
static void initialize ()
 
static void destroy ()
 
static socket create (int domain, int type, int protocol=0)
 
static std::tuple< socket, socketpair (int domain, int type, int protocol=0)
 
static std::string error_str (int errNum)
 

Static Public Attributes

static constexpr int COMM_TYPE = SOCK_STREAM
 

Static Protected Member Functions

static socket_t create_handle (int domain)
 
- Static Protected Member Functions inherited from sockpp::socket
static int get_last_error ()
 

Friends

class acceptor
 

Additional Inherited Members

- Protected Member Functions inherited from sockpp::socket
bool close_on_err ()
 
void set_last_error ()
 
template<typename T >
check_ret (T ret) const
 
template<typename T >
bool check_ret_bool (T ret) const
 
socket_t check_socket (socket_t ret) const
 
bool check_socket_bool (socket_t ret) const
 

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() [1/3]

sockpp::stream_socket::stream_socket ( )
inline

Creates an unconnected streaming socket.

◆ stream_socket() [2/3]

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.

◆ stream_socket() [3/3]

sockpp::stream_socket::stream_socket ( stream_socket && sock)
inline

Creates a stream socket by copying the socket handle from the specified socket object and transfers ownership of the socket.

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()

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]

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() [2/2]

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_n()

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]

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.

◆ read_timeout() [2/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.

◆ write() [1/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() [2/3]

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() [3/3]

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_n()

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]

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.

◆ write_timeout() [2/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.

Friends And Related Symbol Documentation

◆ acceptor

friend class acceptor
friend

Acceptor can create stream sockets.

Member Data Documentation

◆ COMM_TYPE

int sockpp::stream_socket::COMM_TYPE = SOCK_STREAM
staticconstexpr

The socket 'type' for communications semantics.


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