libsocket
|
Generic socket for active stream sockets (TCP client sockets, UNIX Stream clients) More...
#include <streamclient.hpp>
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_socket & | operator<< (stream_client_socket &sock, const char *str) |
Send data to socket. More... | |
stream_client_socket & | operator<< (stream_client_socket &sock, const string &str) |
Send data to socket. More... | |
stream_client_socket & | operator>> (stream_client_socket &sock, string &dest) |
Receive data from socket to a string. More... | |
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.
libsocket::stream_client_socket::stream_client_socket | ( | ) |
Void constructor.
Definition at line 54 of file streamclient.cpp.
ssize_t libsocket::stream_client_socket::snd | ( | const void * | buf, |
size_t | len, | ||
int | flags = 0 |
||
) |
Send data to socket.
buf | Data to be sent |
len | Length of buf |
flags | Flags for send(2) . WARNING: Throws an exception if send() returns -1; this may be the case if the flag MSG_DONTWAIT is used. |
Definition at line 243 of file streamclient.cpp.
ssize_t libsocket::stream_client_socket::rcv | ( | void * | buf, |
size_t | len, | ||
int | flags = 0 |
||
) |
Receive data from socket.
...and puts it in buf
.
buf | A writable memory buffer of length len |
len | Length of buf |
flags | Flags for recv(2) . WARNING: Throws an exception if recv() returns -1; this may be the case if the flag MSG_DONTWAIT is used. |
Definition at line 68 of file streamclient.cpp.
void libsocket::stream_client_socket::shutdown | ( | int | method = LIBSOCKET_WRITE | ) |
Shut a socket down.
Shuts a socket down using shutdown(2)
.
method | LIBSOCKET_READ/LIBSOCKET_WRITE or an OR ed combination. |
Definition at line 279 of file streamclient.cpp.
|
virtualinherited |
Destroys a socket.
0 | Fine! |
<0 | Most likely the socket was already closed before. |
Definition at line 72 of file socket.cpp.
|
inherited |
Returns the socket file descriptor.
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.
|
inherited |
Set socket options on the underlying socket.
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.
|
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.
|
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.!
sock | A socket. |
str | Data to be sent; do a static_cast<>() to send raw data. |
Definition at line 174 of file streamclient.cpp.
|
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.!
sock | A socket. |
str | Data. |
Definition at line 212 of file streamclient.cpp.
|
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...];
sock | the socket. |
dest | the destination string. Its length determines how much data is received. |
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.
|
protected |
If the socket was shut down for reading (-> no reads anymore)
Definition at line 54 of file streamclient.hpp.
|
protected |
If the socket was shut down for writing (-> no writes anymore)
Definition at line 56 of file streamclient.hpp.
|
protectedinherited |
sfd is the sockets API file descriptor
Definition at line 74 of file socket.hpp.
|
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.