libsocket
Public Member Functions | Protected Attributes | Friends | List of all members
libsocket::stream_client_socket Class Reference

Generic socket for active stream sockets (TCP client sockets, UNIX Stream clients) More...

#include <streamclient.hpp>

Inheritance diagram for libsocket::stream_client_socket:
Inheritance graph
[legend]

Public Member Functions

 stream_client_socket ()
 Void constructor. More...
 
 stream_client_socket (const stream_client_socket &)=delete
 
 stream_client_socket (stream_client_socket &&other)
 
ssize_t snd (const void *buf, size_t len, int flags=0)
 Send data to socket. More...
 
ssize_t rcv (void *buf, size_t len, int flags=0)
 Receive data from socket. More...
 
void shutdown (int method=LIBSOCKET_WRITE)
 Shut a socket down. More...
 
virtual int destroy (void)
 Destroys a socket. More...
 
int getfd (void) const
 Returns the socket file descriptor. More...
 
int set_sock_opt (int level, int optname, const char *optval, socklen_t optlen) const
 Set socket options on the underlying socket. More...
 
void set_close_on_destructor (bool cod)
 

Protected Attributes

bool shut_rd
 
bool shut_wr
 
int sfd
 sfd is the sockets API file descriptor More...
 
bool is_nonblocking
 
bool close_on_destructor
 

Friends

class dgram_over_stream
 
stream_client_socketoperator<< (stream_client_socket &sock, const char *str)
 Send data to socket. More...
 
stream_client_socketoperator<< (stream_client_socket &sock, const string &str)
 Send data to socket. More...
 
stream_client_socketoperator>> (stream_client_socket &sock, string &dest)
 Receive data from socket to a string. More...
 

Detailed Description

Generic socket for active stream sockets (TCP client sockets, UNIX Stream clients)

This class defines the basic i/o operations for all stream-based sockets.

Definition at line 52 of file streamclient.hpp.

Constructor & Destructor Documentation

◆ stream_client_socket()

libsocket::stream_client_socket::stream_client_socket ( )

Void constructor.

Definition at line 54 of file streamclient.cpp.

Member Function Documentation

◆ snd()

ssize_t libsocket::stream_client_socket::snd ( const void *  buf,
size_t  len,
int  flags = 0 
)

Send data to socket.

Parameters
bufData to be sent
lenLength of buf
flagsFlags for send(2). WARNING: Throws an exception if send() returns -1; this may be the case if the flag MSG_DONTWAIT is used.
Returns
The number of bytes sent to the peer. -1 if the socket is non-blocking and no data was sent.

Definition at line 243 of file streamclient.cpp.

◆ rcv()

ssize_t libsocket::stream_client_socket::rcv ( void *  buf,
size_t  len,
int  flags = 0 
)

Receive data from socket.

...and puts it in buf.

Parameters
bufA writable memory buffer of length len
lenLength of buf
flagsFlags for recv(2). WARNING: Throws an exception if recv() returns -1; this may be the case if the flag MSG_DONTWAIT is used.
Returns
The length of received data

Definition at line 68 of file streamclient.cpp.

◆ shutdown()

void libsocket::stream_client_socket::shutdown ( int  method = LIBSOCKET_WRITE)

Shut a socket down.

Shuts a socket down using shutdown(2).

Parameters
methodLIBSOCKET_READ/LIBSOCKET_WRITE or an ORed combination.

Definition at line 279 of file streamclient.cpp.

◆ destroy()

int libsocket::socket::destroy ( void  )
virtualinherited

Destroys a socket.

Return values
0Fine!
<0Most likely the socket was already closed before.

Definition at line 72 of file socket.cpp.

Here is the caller graph for this function:

◆ getfd()

int libsocket::socket::getfd ( void  ) const
inherited

Returns the socket file descriptor.

Returns
The socket file descriptor of the class.

getfd() is a getter you may use to obtain the file descriptor for raw operations on it. It's relatively uncritical as libsocket just wraps other syscalls and doesn't manipulate it using unusual ways.

Definition at line 91 of file socket.cpp.

◆ set_sock_opt()

int libsocket::socket::set_sock_opt ( int  level,
int  optname,
const char *  optval,
socklen_t  optlen 
) const
inherited

Set socket options on the underlying socket.

Returns
The return value of setsockopt(2).

Sets socket options using setsockopt(2). See setsockopt(2), tcp(7), udp(7), unix(7) for documentation on how to use this function.

Definition at line 101 of file socket.cpp.

◆ set_close_on_destructor()

void libsocket::socket::set_close_on_destructor ( bool  cod)
inlineinherited

close_on_destructor is true by default. If set to false, do not call close(2) on the underlying socket in the destructor.

Definition at line 95 of file socket.hpp.

Friends And Related Function Documentation

◆ operator<< [1/2]

stream_client_socket& operator<< ( stream_client_socket sock,
const char *  str 
)
friend

Send data to socket.

Sends data to socket using stream-like syntax:

socket << "Hello " << "World" << "\n";

Important: Only overloaded for C and C++ strings, not for numbers, chars etc.!

Parameters
sockA socket.
strData to be sent; do a static_cast<>() to send raw data.

Definition at line 174 of file streamclient.cpp.

◆ operator<< [2/2]

stream_client_socket& operator<< ( stream_client_socket sock,
const string &  str 
)
friend

Send data to socket.

Sends data to socket using stream-like syntax:

socket << "Hello " << "World" << "\n";

Important: Only overloaded for C and C++ strings, not for numbers, chars etc.!

Parameters
sockA socket.
strData.

Definition at line 212 of file streamclient.cpp.

◆ operator>>

stream_client_socket& operator>> ( stream_client_socket sock,
string &  dest 
)
friend

Receive data from socket to a string.

Receives n bytes of data (where n == dest.size()) and writes it to a string.

Application: infix; sock >> dest1 [>> dest2...];

Parameters
sockthe socket.
destthe destination string. Its length determines how much data is received.
Returns
the same socket which was given as parameter. (See "Application")

The dest string is resized to 0 if the socket is non-blocking and no data could be received.

Definition at line 118 of file streamclient.cpp.

Member Data Documentation

◆ shut_rd

bool libsocket::stream_client_socket::shut_rd
protected

If the socket was shut down for reading (-> no reads anymore)

Definition at line 54 of file streamclient.hpp.

◆ shut_wr

bool libsocket::stream_client_socket::shut_wr
protected

If the socket was shut down for writing (-> no writes anymore)

Definition at line 56 of file streamclient.hpp.

◆ sfd

int libsocket::socket::sfd
protectedinherited

sfd is the sockets API file descriptor

Definition at line 74 of file socket.hpp.

◆ close_on_destructor

bool libsocket::socket::close_on_destructor
protectedinherited

Default is true; if set to false, the file descriptor is not closed when the destructor is called.

Definition at line 78 of file socket.hpp.


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