MLIB
|
Provide functions required by streambuf
interface using an underlying socket.
More...
#include <sockbuf.h>
Public Member Functions | |
sockbuf () | |
Default constructor. | |
sockbuf (int type, int domain, int proto) | |
Build a sockbuf object and the attached socket with the given parameters. | |
sockbuf (const sock &s) | |
Build a sockbuf object from a sock base. | |
sockbuf (const sockbuf< T > &sb) | |
Copy constructor. | |
sockbuf< T > & | operator= (const sockbuf< T > &rhs) |
~sockbuf () | |
Destructor. | |
![]() | |
sock (SOCKET soc) | |
Create a sock object from a socket handle. | |
sock (int type, int domain=AF_INET, int proto=0) | |
Construct a sock object for the specified domain. | |
sock (const sock &) | |
Copy constructor. | |
sock (sock &&) | |
Move constructor. | |
virtual | ~sock () |
Destructor. | |
sock & | operator= (const sock &) |
Assignment operator. | |
sock & | operator= (sock &&) |
Move assignment. | |
virtual erc | open (int type, int domain=AF_INET, int proto=0) |
Open the socket. | |
virtual erc | close () |
Close socket. | |
virtual erc | shutdown (shuthow sh) const |
Disables sends or receives on socket. | |
size_t | recv (void *buf, size_t maxlen, mflags msgf=mflags::none) const |
Receives data from socket. | |
size_t | recvfrom (sockaddr &sa, void *buf, size_t maxlen, mflags msgf=mflags::none) const |
Receives data from socket. | |
size_t | send (const void *buf, size_t len, mflags msgf=mflags::none) const |
Send data to the connected peer. | |
size_t | sendto (const sockaddr &sa, const void *buf, size_t len, mflags msgf=mflags::none) const |
Send data to a peer. | |
bool | is_readready (const timeval &tv={0, 0}) const |
Check if socket is "readable". | |
bool | is_writeready (const timeval &tv={0, 0}) const |
Check if socket is "writable". | |
bool | is_exceptionpending (const timeval &tv={0, 0}) const |
Check if socket has OOB data or any exceptional error conditions. | |
unsigned int | nread () const |
Return number of characters waiting in socket's buffer. | |
erc | bind (const inaddr &) const |
Associates a local address with the socket. | |
erc | bind () const |
Associates a local address with the socket. | |
erc | connect (const inaddr &peer, const timeval &tv) const |
Establishes a connection to a specified address. | |
erc | accept (sock &client, const timeval &tv, inaddr *sa=nullptr) const |
Permits an incoming connection attempt on the socket. | |
checked< inaddr > | name () const |
Return the local name for the object. | |
checked< inaddr > | peer () const |
Retrieves the name of the peer to which the socket is connected. | |
sock () | |
Default constructor creates a closed socket. | |
HANDLE | handle () const |
Retrieve Windows socket handle. | |
bool | operator== (const sock &other) const |
Equality comparison operator. | |
bool | operator!= (const sock &other) const |
Inequality comparison operator. | |
operator SOCKET () const | |
Conversion operator. | |
virtual bool | is_open () const |
Check if socket is opened. | |
template<typename T> | |
size_t | send (std::basic_string< T > buf, mflags msgf=mflags::none) const |
Send data to connected peer. | |
template<typename T> | |
size_t | sendto (const sockaddr &sa, std::basic_string< T > buf, mflags msgf=mflags::none) const |
Send data to a peer. | |
int | sendtimeout (int wp_sec) const |
Set send timeout value. | |
int | sendtimeout () const |
Returns the send timeout value. | |
int | recvtimeout (int wp_sec) const |
Set receive timeout value. | |
int | recvtimeout () const |
Returns the send timeout value. | |
erc | connect (const inaddr &peer) const |
Establishes a connection to specified peer. | |
bool | connected () const |
Check is socket is connected. | |
erc | listen (int num=SOMAXCONN) const |
Places the socket in a state in which it is listening for incoming connections. | |
erc | accept (sock &client, inaddr *sa=nullptr) const |
Permits an incoming connection attempt on the socket. | |
int | getopt (int op, void *buf, int len, int level=SOL_SOCKET) const |
Returns a socket option. | |
erc | setopt (int op, void *buf, int len, int level=SOL_SOCKET) const |
Set a socket option. | |
int | gettype () const |
Return socket type (SOCK_DGRAM or SOCK_STREAM) | |
int | clearerror () const |
Return and clear the socket error flag. | |
bool | debug () const |
Return the debug flag. | |
void | debug (bool opt) const |
Set the debug flag. | |
bool | reuseaddr () const |
Return the "reuse address" flag. | |
void | reuseaddr (bool opt) const |
Set the "reuse address" flag. | |
bool | keepalive () const |
Return "keep alive" flag. | |
void | keepalive (bool opt) const |
Set "keep alive" flag. | |
bool | dontroute () const |
Return status of "don't route" flag. | |
void | dontroute (bool opt) const |
Turn on or off the "don't route" flag. | |
bool | broadcast () const |
Return "broadcast" option. | |
void | broadcast (bool opt) const |
Turn on or off the "broadcast" option. | |
bool | oobinline () const |
Return the status of the OOB_INLINE flag. | |
void | oobinline (bool opt) const |
Set the status of the OOB_INLINE flag. | |
int | sendbufsz () const |
Return buffer size for send operations. | |
void | sendbufsz (size_t sz) const |
Set buffer size for send operations. | |
int | recvbufsz () const |
Return buffer size for receive operations. | |
void | recvbufsz (size_t sz) const |
Set buffer size for receive operations. | |
void | blocking (bool on_off) |
Change blocking mode. | |
erc | setevent (HANDLE evt, long mask) const |
Associate an event object with this socket. | |
long | enumevents () const |
Indicates which of the FD_XXX network events have occurred. | |
void | linger (bool on_off, unsigned short seconds) const |
Turn on or off linger mode and lingering timeout. | |
bool | linger (unsigned short *seconds=0) const |
Return linger mode and lingering timeout. | |
void | nodelay (bool on_off) |
Set TCP_NODELAY option. | |
bool | nodelay () const |
Return status of TCP_NODELAY option. | |
Protected Types | |
using | int_type = std::char_traits<T>::int_type |
Protected Member Functions | |
Redefined virtuals from parent streambuf | |
int_type | underflow () override |
If c == EOF or buffer full, return sync(); otherwise insert c into the buffer and return c | |
int_type | overflow (int_type c=EOF) override |
If c == EOF or buffer full, return sync(); otherwise insert c into the buffer and return c | |
int | sync () override |
Return 0 if all chars flushed or -1 if error. | |
std::basic_streambuf< T > * | setbuf (T *buf, std::streamsize sz) override |
Change buffering mode. | |
std::streamsize | showmanyc () override |
Return number of characters available in socket buffer. | |
Additional Inherited Members | |
![]() | |
enum | shuthow { shut_read = 0 , shut_write = 1 , shut_readwrite = 2 } |
operation blocked by shutdown function More... | |
enum | mflags { none = 0 , out_of_band = MSG_OOB , peek = MSG_PEEK , dont_route = MSG_DONTROUTE , wait_all = MSG_WAITALL } |
Flags for send/receive operations. More... | |
![]() | |
static errfac & | Errors () |
Return error facility used by all sock-derived classes. | |
static void | Errors (errfac &facitlity) |
Set the error facility use by all sock-derived classes. | |
static erc | last_error () |
Return an error code with the value returned by WSAGetLastError. | |
Provide functions required by streambuf
interface using an underlying socket.
You can simultaneously read and write into a sockbuf
just like you can listen and talk through a telephone. Hence, the read and the write buffers are different.
eback()
points to the start of the get area. The unread chars are gptr()
to egptr()
.
eback()
is set to base()
so that pbackfail()
is called only when there is no place to putback a char. And pbackfail()
always returns EOF.
pbase()
points to the start of the put area The unflushed chars are pbase() - pptr()
epptr()
points to the end of the write buffer.
Output is flushed whenever one of the following conditions holds: (1) pptr() == epptr()
(2) EOF is written
Input buffer size is assumed to be of size 1 and output buffer is of size 0. That is, egptr() <= base()+1
and epptr() == pbase()
.
|
inline |
Assignment operator. Maintains current buffering mode
|
inlineoverrideprotected |
Change buffering mode.
If buf
is NULL, switches to automatic buffering mode with separate buffers for input and output. Otherwise the user supplied buffer is used for output only.