psocksxx
1.0.0
|
Socket stream buffer class. More...
#include <sockstreambuf.h>
Public Types | |
enum | eof_t { eof = -1 } |
enum | socket_domain_t { pf_local = PF_LOCAL, pf_inet = PF_INET, pf_route = PF_ROUTE, pf_key = PF_KEY, pf_inet6 = PF_INET6 } |
enum | socket_type_t { sock_stream = SOCK_STREAM, sock_dgram = SOCK_DGRAM, sock_raw = SOCK_RAW, sock_rdm = SOCK_RDM, sock_seqpacket = SOCK_SEQPACKET } |
enum | socket_protocol_t { proto_unspec = 0, ipproto_ip = IPPROTO_IP, ipproto_ipv6 = IPPROTO_IPV6, ipproto_icmp = IPPROTO_ICMP, ipproto_raw = IPPROTO_RAW, ipproto_tcp = IPPROTO_TCP, ipproto_udp = IPPROTO_UDP } |
typedef int | socket_t |
Public Member Functions | |
sockstreambuf () throw () | |
constructor | |
virtual | ~sockstreambuf () |
destructor | |
sockstreambuf (socket_t socket) throw () | |
overloaded constructor More... | |
const socket_t & | socket () const throw () |
get internal socket data More... | |
void | open (socket_domain_t domain, socket_type_t type, socket_protocol_t proto=proto_unspec) throw ( sockexception ) |
open a socket More... | |
void | close () throw () |
close open sockets More... | |
virtual int | flush () throw ( socktimeoutexception ) |
flush the socket output buffer More... | |
void | connect (const sockaddr *dest_addr, unsigned int timeout=0) throw ( sockexception, socktimeoutexception ) |
initiate a connection on a socket More... | |
void | connect (const sockaddr *dest_addr, timeval *timeout) throw ( sockexception, socktimeoutexception ) |
initiate a connection on a socket More... | |
void | bind (const sockaddr *bind_addr, bool reuse_addr=false) throw ( sockexception ) |
bind the socket to a specified address More... | |
void | listen (int backlog=SOMAXCONN) throw ( sockexception ) |
make the socket passive and capable of accepting connections More... | |
socket_t | accept () throw ( sockexception ) |
accept a connection on a listening (passive) socket More... | |
const timeval * | timeout (time_t sec, suseconds_t usec) throw () |
set the timeout value for the socket More... | |
void * | clear_timeout () throw () |
clear the timeout value for the socket More... | |
bool | timedout () const throw () |
get the timed-out status More... | |
Protected Member Functions | |
void | init_buffers () throw () |
initialise internal buffers | |
void | cleanup_buffers () throw () |
cleanup internal buffers | |
virtual int | sync () throw () |
sync data with the socket More... | |
virtual int | overflow (int c=eof) throw ( socktimeoutexception ) |
consumes the buffer by writing the contents to the socket More... | |
virtual int | underflow () throw ( socktimeoutexception ) |
read more data into the buffer from the socket More... | |
bool | ready (timeval *timeout, bool chk_read=true, bool chk_write=true) throw ( sockexception ) |
check for the read/write availability on the socket More... | |
Socket stream buffer class.
This buffer class associates its both input and output sequences with an external POSIX socket.
typedef int psocksxx::sockstreambuf::socket_t |
socket data type definition
socket protocols type definition
socket types type definition
psocksxx::sockstreambuf::sockstreambuf | ( | socket_t | socket | ) | |
throw | ( | ||||
) |
overloaded constructor
socket | socket data |
Create an instance with the passed in sockstreambuf::socket_t type socket. It is assumed that the socket is initialised and ready to use.
sockstreambuf::socket_t psocksxx::sockstreambuf::accept | ( | ) | ||
throw | ( | sockexception | ||
) |
accept a connection on a listening (passive) socket
psocksxx::sockexception | socket exception |
This method will accept incoming connections on a socket set to be passive using the listen() method. Upon success this will return the peer socket data structure that can be used to create a socket stream buffer instance to communicate with the accepted socket connection.
void psocksxx::sockstreambuf::bind | ( | const sockaddr * | bind_addr, |
bool | reuse_addr = false |
||
) | |||
throw | ( | sockexception | |
) |
bind the socket to a specified address
bind_addr | address to bind to |
reuse_addr | allow address to be re-used |
psocksxx::sockexception | socket exception |
After a socket is configured using open() this method can be used to assign an address to it. If reuse_addr
is set to true
then this will try to re-use the address unless the address is actively listening.
void * psocksxx::sockstreambuf::clear_timeout | ( | ) | ||
throw | ( | |||
) |
clear the timeout value for the socket
0
) after clearing the timeoutThis will clear any timeout values set for the socket affectively making this a blocking socket by default.
void psocksxx::sockstreambuf::close | ( | ) | ||
throw | ( | |||
) |
close open sockets
Close any open socket connections used by this buffer. This will also flush any data in the buffer before closing.
void psocksxx::sockstreambuf::connect | ( | const sockaddr * | dest_addr, |
unsigned int | timeout = 0 |
||
) | |||
throw | ( | sockexception, | |
socktimeoutexception | |||
) |
initiate a connection on a socket
dest_addr | destination address to connect to |
timeout | connection timeout value in seconds |
psocksxx::sockexception | socket exception |
psocksxx::socktimeoutexception | connection timeout exception |
Initiate a connection on a socket previously opened using open() method. If the timeout value is 0 (default) then the timeouts are ignored.
void psocksxx::sockstreambuf::connect | ( | const sockaddr * | dest_addr, |
timeval * | timeout | ||
) | |||
throw | ( | sockexception, | |
socktimeoutexception | |||
) |
initiate a connection on a socket
dest_addr | destination address to connect to |
timeout | connection timeout value as a reference to a timeval structure |
psocksxx::sockexception | socket exception |
psocksxx::socktimeoutexception | connection timeout exception |
Initiate a connection on a socket previously opened using open() method.
|
virtual |
flush the socket output buffer
psocksxx::socktimeoutexception | on socket timeout |
Flush the socket buffer by writing date into the socket and returns the number of characters flushed. If the output buffer is empty sockstreambuf::eof is returned.
void psocksxx::sockstreambuf::listen | ( | int | backlog = SOMAXCONN | ) | |
throw | ( | sockexception | |||
) |
make the socket passive and capable of accepting connections
backlog | maximum length of the queue for pending connections and defaults to SOMAXCONN (128) defined in <sys/socket.h> |
psocksxx::sockexception | socket exception |
This method will make the currently opened socket connection to passive and capable of accepting client connections using accept() method.
void psocksxx::sockstreambuf::open | ( | socket_domain_t | domain, |
socket_type_t | type, | ||
socket_protocol_t | proto = proto_unspec |
||
) | |||
throw | ( | sockexception | |
) |
open a socket
domain | communications domain for the socket |
type | socket communications type |
proto | socket communications protocol |
psocksxx::sockexception | socket exception |
Open a socket and initialise socket communications.
|
protectedvirtual |
consumes the buffer by writing the contents to the socket
c | additional character to consume |
psocksxx::socktimeoutexception | on socket timeout |
Consumes the buffer contents and writes to the opened socket. If c is not sockstreambuf::eof then c is also written out.
|
protected |
check for the read/write availability on the socket
timeout | timeout value reference to a timeval structure |
chk_read | check for read availability |
chk_write | check for write availability |
psocksxx::sockexception | socket exception |
true
to denote availability or false
if none of the checked actions are available.This will check the socket for read and/or write availability.
const sockstreambuf::socket_t & psocksxx::sockstreambuf::socket | ( | ) | const | |
throw | ( | |||
) |
get internal socket data
Returns a read-only reference to the internal POSIX socket data.
|
protectedvirtual |
sync data with the socket
Synchronise the buffer with the associated socket by flushing data from the buffer to the socket.
bool psocksxx::sockstreambuf::timedout | ( | ) | const | |
throw | ( | |||
) |
get the timed-out status
true
if timed-out flag is set or false
otherwise.Returns the timed-out status.
const timeval * psocksxx::sockstreambuf::timeout | ( | time_t | sec, |
suseconds_t | usec | ||
) | |||
throw | ( | ||
) |
set the timeout value for the socket
sec | seconds |
usec | microseconds |
This method will set the timeout for the socket and make this a non-blocking socket. Note that you cannot clear the timeout by passing in a 0 timeout, use clear_timeout() method instead.
|
protectedvirtual |
read more data into the buffer from the socket
psocksxx::socktimeoutexception | on socket timeout |
This reads more data into the buffer from the socket when the input buffer is empty and returns the next readable character from the buffer. If the buffer is empty and no data is available through the socket, this returns sockstreambuf::eof.