UCommon
Public Member Functions | Protected Member Functions
ucommon::TCPBuffer Class Reference

A generic tcp socket class that offers i/o buffering. More...

#include <buffer.h>

Inheritance diagram for ucommon::TCPBuffer:
Inheritance graph
[legend]
Collaboration diagram for ucommon::TCPBuffer:
Collaboration graph
[legend]

Public Member Functions

void close (void)
 Close active connection.
void open (TCPServer *server, size_t size=536)
 Connect a tcp socket to a client from a listener.
void open (char *host, char *service, size_t size=536)
 Connect a tcp client session to a specific host uri.
 TCPBuffer ()
 Construct an unconnected tcp client and specify our service profile.
 TCPBuffer (TCPServer *server, size_t size=536)
 Construct a tcp server session from a listening socket.
 TCPBuffer (char *host, char *service, size_t size=536)
 Construct a tcp client session connected to a specific host uri.
virtual ~TCPBuffer ()
 Destroy the tcp socket and release all resources.
- Public Member Functions inherited from ucommon::BufferProtocol
int _getch (void)
 Get a character from the buffer.
int _putch (int ch)
 Put a character into the buffer.
bool eof (void)
 Check if at end of input.
int err (void)
bool flush (void)
 Flush buffered memory to physical I/O.
size_t get (char *address, size_t count)
 Get memory from the buffer.
size_t getline (char *string, size_t size)
 Get text as a line of input from the buffer.
size_t getline (string &buffer)
 Get a string as a line of input from the buffer.
bool is_input (void)
 See if input active.
bool is_open (void)
 See if buffer open.
bool is_output (void)
 See if output active.
bool is_pending (void)
 See if pending input.
 operator bool ()
 See if buffer open.
bool operator! ()
 See if buffer closed.
size_t printf (const char *format,...)
 Print formatted string to the buffer.
void purge (void)
 Purge any pending input or output buffer data.
size_t put (const char *address, size_t count=0)
 Put memory into the buffer.
size_t putline (const char *string)
 Put a string as a line of output to the buffer.
void reset (void)
 Reset input buffer state.
void seteof (void)
 Set eof flag.
- Public Member Functions inherited from ucommon::CharacterProtocol
int get (void)
 Get the next character.
int put (int code)
 Put the next character.

Protected Member Functions

bool _blocking (void)
 Return true if blocking.
void _buffer (size_t size)
void _clear (void)
 Method to clear low level i/o error.
int _err (void)
virtual bool _pending (void)
 Check for pending tcp or ssl data.
virtual size_t _pull (char *address, size_t size)
 Method to pull buffer from physical i/o (read).
virtual size_t _push (char *address, size_t size)
socket_t getsocket (void)
 Get the low level socket object.
- Protected Member Functions inherited from ucommon::BufferProtocol
virtual int _err (void) const =0
 Method to get low level i/o error.
virtual bool _flush (void)
 Flush buffer to physical i/o.
virtual size_t _push (const char *address, size_t size)=0
 Method to push buffer into physical i/o (write).
void allocate (size_t size, type_t access=BUF_RDWR)
 Allocate I/O buffer(s) of specified size.
 BufferProtocol ()
 Construct an empty (unallocated) buffer.
 BufferProtocol (size_t size, type_t access=BUF_RDWR)
 Construct a buffer of pre-allocated size and access type.
char * gather (size_t size)
 Gather returns a pointer to contiguous input of specified size.
void release (void)
 Release (free) buffer memory.
char * request (size_t size)
 Request workspace in output buffer.
void seteol (const char *string)
 Set end of line marker.
size_t unread (void)
 Get current input position.
size_t unsaved (void)
 Get current output position.
virtual ~BufferProtocol ()
 Destroy object by releasing buffer memory.
- Protected Member Functions inherited from ucommon::Socket
int blocking (bool enable)
 Set socket blocking I/O mode.
int broadcast (bool enable)
 Set socket for unicast mode broadcasts.
void cancel (void)
 Cancel pending i/o by shutting down the socket.
int connectto (struct addrinfo *list)
 Connect our socket to a remote host from an address list.
int disconnect (void)
 Disconnect a connected socket.
int drop (struct addrinfo *list)
 Drop socket from multicast group.
int err (void)
 Get error code.
int getError (void)
 Get socket error code.
unsigned getPending (void)
 Get the number of bytes of data in the socket receive buffer.
int gettype (void)
 Get the type of a socket.
bool isConnected (void)
 Test if socket is connected.
bool isPending (unsigned value)
 See the number of bytes in the receive queue.
int join (struct addrinfo *list)
 Join socket to multicast group.
int keepalive (bool enable)
 Set socket for keepalive packets.
int loopback (bool enable)
 Set loopback to read multicast packets we broadcast.
int multicast (unsigned ttl=1)
 Set multicast mode and multicast broadcast range.
int nodelay (void)
 Set nodelay option for tcp socket.
 operator bool ()
 Test if socket is valid.
 operator socket_t ()
 Get the socket descriptor by casting.
bool operator! ()
 Test if socket is invalid.
socket_t operator* ()
 Get the socket descriptor by pointer reference.
Socketoperator= (socket_t socket)
 Assign socket from a socket descriptor.
size_t peek (void *data, size_t number)
 Peek at data waiting in the socket receive buffer.
size_t printf (char *format,...)
 Print formatted string to socket.
int priority (int scheduling)
 Set packet priority, 0 to 6 unless privileged.
size_t readfrom (void *data, size_t number, struct sockaddr_storage *address=((void *) 0))
 Read data from the socket receive buffer.
size_t readline (char *data, size_t size)
 Read a newline of text data from the socket and save in NULL terminated string.
size_t readline (string &buffer)
 Read a string of input from the socket and strip trailing newline.
int recvsize (unsigned size)
 Set the size of the socket receive buffer.
void release (void)
 Shutdown and close the socket.
unsigned segsize (unsigned size)
 Set segment size and get mtu of a socket.
int sendsize (unsigned size)
 Set the size of the socket send buffer.
int sendwait (unsigned size)
 Set the size to wait before sending.
bool setccid (uint8_t ccid)
 Set ccid of dccp socket.
void shutdown (void)
 Shutdown the socket communication channel.
 Socket ()
 Create a socket object for use.
 Socket (Socket &existing)
 Create socket as duped handle of existing socket.
 Socket (socket_t socket)
 Create socket from existing socket descriptor.
 Socket (struct addrinfo *address)
 Create and connect a socket to an address from an address list.
 Socket (int family, int type, int protocol=0)
 Create an unbound socket of a specific type.
 Socket (char *address, char *port, int family=AF_UNSPEC, int type=0, int protocol=0)
 Create a bound socket.
int tos (int type)
 Set the type of service field of outgoing packets.
int ttl (unsigned char time)
 Set the time to live before packets expire.
int wait (timeout_t timeout=Timer::inf)
 Socket i/o timer setting.
bool waitPending (timeout_t timeout=0)
 Test for pending input data.
bool waitSending (timeout_t timeout=0)
 Test for output data sent.
size_t writes (char *string)
 Write a null terminated string to the socket.
size_t writeto (void *data, size_t number, struct sockaddr *address=((void *) 0))
 Write data to the socket send buffer.
virtual ~Socket ()
 Shutdown, close, and destroy socket.

Additional Inherited Members

- Public Types inherited from ucommon::BufferProtocol
enum  type_t { BUF_RD, BUF_WR, BUF_RDWR }
- Protected Types inherited from ucommon::Socket
typedef void * set_t
 External definition of fd_set type.
- Static Protected Member Functions inherited from ucommon::Socket
static socket_t acceptfrom (socket_t socket, struct sockaddr_storage *address=((void *) 0))
 Accept a socket connection from a remote host.
static Socket acceptfrom (Socket &socket, struct sockaddr_storage *address)
 Accept connection through socket.
static int bindto (socket_t socket, char *address, char *service, int protocol=0)
 Bind the socket descriptor to a known interface and service port.
static int bindto (socket_t socket, struct sockaddr *address)
 Bind the socket descriptor to a known interface.
static int blocking (socket_t socket, bool enable)
 Set socket blocking I/O mode of socket descriptor.
static int broadcast (socket_t socket, bool enable)
 Set socket for unicast mode broadcasts on socket descriptor.
static void cancel (socket_t socket)
 Cancel pending i/o by shutting down the socket.
static void clear (set_t mask)
 Clear a mask, setting all entries to zero.
static void clear (socket_t socket, set_t mask)
 Clear a socket entry in a mask.
static int connectto (socket_t socket, struct addrinfo *list)
 Connect socket descriptor to a remote host from an address list.
static void connectto (Socket &socket, Socket::address &address)
 Connect a socket.
static unsigned copy (struct sockaddr *target, struct sockaddr *origin)
 Copy a socket address.
static socket_t create (int family, int type, int protocol)
 Create a socket object unbound.
static socket_t create (struct addrinfo *address, int type, int protocol)
 Create a connected socket.
static socket_t create (char *iface, char *service, int family=AF_UNSPEC, int type=0, int protocol=0)
 Create a bound socket for a service.
static socket_t create (Socket::address &address)
 Create a connected socket for a service.
static int disconnect (socket_t socket)
 Disconnect a connected socket descriptor.
static void disconnect (Socket &socket)
 Disconnect a connected socket.
static int drop (socket_t socket, struct addrinfo *list)
 Drop socket descriptor from multicast group.
static bool equal (struct sockaddr *address1, struct sockaddr *address2)
 Compare socket addresses.
static bool equalfrom (struct sockaddr_storage *address1, struct sockaddr_storage *address2)
 Compare socket addresses.
static bool equalhost (struct sockaddr *address1, struct sockaddr *address2)
 Compare socket host addresses.
static bool equalinet (struct sockaddr_internet *address1, struct sockaddr_internet *address2)
 Compare socket addresses.
static int error (socket_t socket)
 Get socket error code of socket descriptor.
static int error (void)
 Return error code of last socket operation,.
static void family (int query)
 Set default socket family preference for query options when the socket type is otherwise not specified.
static socklen_t getaddr (socket_t socket, struct sockaddr_storage *address, char *hostname, char *service)
 Lookup a host name and service address based on the addressing family and socket type of a socket descriptor.
static struct addrinfogetaddress (char *host, char *service, int type=SOCK_STREAM, int protocol=0)
 Get an address list directly.
static char * getaddress (struct sockaddr *address, char *buffer, socklen_t size)
 Get the hostname of a socket address.
static int getfamily (socket_t socket)
 Get the address family of the socket descriptor.
static int getfamily (struct sockaddr_storage &address)
 Get the address family of a socket address object.
static int getfamily (struct sockaddr_internet &address)
 Get the address family of an internet socket address object.
static struct addrinfogethint (socket_t socket, struct addrinfo *hint)
 Create an address info lookup hint based on the family and type properties of a socket descriptor.
static char * gethostname (struct sockaddr *address, char *buffer, size_t size)
 Lookup and return the host name associated with a socket address.
static int getinterface (struct sockaddr *address, struct sockaddr *destination)
 Get the socket address of the interface needed to reach a destination address.
static socklen_t getlen (struct sockaddr *address)
 Get the size of a socket address.
static int getlocal (socket_t socket, struct sockaddr_storage *address)
 Get local address to which the socket is bound.
static set_t getmask (void)
 Get a mask from the heap.
static int getremote (socket_t socket, struct sockaddr_storage *address)
 Get remote address to which the socket is connected.
static short getservice (struct sockaddr *address)
 Get the service port of a socket.
static int gettype (socket_t socket)
 Get the type of a socket.
static short inetservice (struct sockaddr_internet *address)
 Get the service port of an inet socket.
static void init (void)
 Initialize socket subsystem.
static void init (char *program)
 Initialize with program name.
static bool isNull (char *string)
 Simple function to validate that a given IP address string is a "zero" address.
static bool isNumeric (char *string)
 Simple function to validate that a given IP address string is a numeric address.
static int join (socket_t socket, struct addrinfo *list)
 Join socket descriptor to multicast group.
static int keepalive (socket_t socket, bool enable)
 Set socket for keepalive packets for socket descriptor.
static unsigned keyhost (struct sockaddr *address, unsigned size)
 Convert a socket host address into a hash map index.
static unsigned keyindex (struct sockaddr *address, unsigned size)
 Convert a socket address and service into a hash map index.
static int listento (socket_t socket, struct sockaddr *address, int backlog=5)
 Bind the socket descriptor to a known interface listen on service port.
static int loopback (socket_t socket, bool enable)
 Set loopback to read multicast packets socket descriptor broadcasts.
static int multicast (socket_t socket, unsigned ttl=1)
 Set multicast mode and multicast broadcast range for socket descriptor.
static int nodelay (socket_t socket)
 Set tcp nodelay option on socket descriptor.
static unsigned pending (socket_t socket)
 Get the number of bytes pending in the receive buffer of a socket descriptor.
static ssize_t printf (socket_t socket, char *format,...)
 Print formatted string to socket.
static int priority (socket_t socket, int scheduling)
 Set packet priority of socket descriptor.
static size_t readfrom (Socket &socket, char *buffer, size_t size, struct sockaddr_storage *address)
 receive from socket object.
static ssize_t readline (socket_t socket, char *data, size_t size, timeout_t timeout=Timer::inf)
 Read a newline of text data from the socket and save in NULL terminated string.
static ssize_t recvfrom (socket_t socket, void *buffer, size_t size, int flags=0, struct sockaddr_storage *address=((void *) 0))
 Get data waiting in receive queue.
static ssize_t recvinet (socket_t socket, void *buffer, size_t size, int flags=0, struct sockaddr_internet *address=((void *) 0))
 Get internet data waiting in receive queue.
static int recvsize (socket_t socket, unsigned size)
 Set the receive size of a socket descriptor.
static void release (struct addrinfo *list)
 Release an address list directly.
static void release (socket_t socket)
 Release (close) a socket.
static void release (set_t mask)
 Release a heap allocated mask.
static ssize_t replyto (socket_t socket, void *buffer, size_t size, int flags, struct sockaddr_storage *address)
 Send reply on socket.
static unsigned segsize (socket_t socket, unsigned size=0)
 Set segment size and get MTU.
static int select (int max, set_t read, set_t write, set_t error)
 Select without timeout.
static int select (int max, set_t read, set_t write, set_t error, timeout_t timeout)
 Select with timeout.
static ssize_t sendinet (socket_t socket, void *buffer, size_t size, int flags, struct sockaddr_internet *address)
 Send to internet socket.
static int sendsize (socket_t socket, unsigned size)
 Set the send size of a socket descriptor.
static ssize_t sendto (socket_t socket, void *buffer, size_t size, int flags=0, struct sockaddr *address=((void *) 0))
 Send data on socket.
static int sendwait (socket_t socket, unsigned size)
 Set the size to wait before sending.
static void set (socket_t socket, set_t mask)
 Set a socket entry in a mask.
static bool setccid (socket_t socket, uint8_t ccid)
 Set congestion control id.
static unsigned store (struct sockaddr_storage *storage, struct sockaddr *address)
 Store an address into an address object.
static unsigned store (struct sockaddr_internet *storage, struct sockaddr *address)
 Store an address into an internet address object.
static bool subnet (struct sockaddr *address1, struct sockaddr *address2)
 See if both addresses are in the same subnet.
static bool test (socket_t socket, set_t mask)
 Test a socket entry in a mask.
static int tos (socket_t socket, int type)
 Set type of service of socket descriptor.
static int ttl (socket_t socket, unsigned char time)
 Set the time to live for the socket descriptor.
static void v4mapping (bool enable)
 Set the default socket behavior for v6-v4 mapping.
static bool wait (socket_t socket, timeout_t timeout=0)
 Test for pending input data.
static size_t writeto (Socket &socket, char *buffer, size_t size, struct sockaddr *address)
 Send to socket object.
- Protected Attributes inherited from ucommon::BufferProtocol
const char * format
- Static Protected Attributes inherited from ucommon::Socket
static size_t masksize

Detailed Description

A generic tcp socket class that offers i/o buffering.

All user i/o operations are directly inherited from the IOBuffer base class public members. Some additional members are added for layering ssl services.

Author:
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 151 of file buffer.h.


Constructor & Destructor Documentation

ucommon::TCPBuffer::TCPBuffer ( TCPServer server,
size_t  size = 536 
)

Construct a tcp server session from a listening socket.

Parameters:
serversocket we are created from.
sizeof buffer and tcp fragments.
ucommon::TCPBuffer::TCPBuffer ( char *  host,
char *  service,
size_t  size = 536 
)

Construct a tcp client session connected to a specific host uri.

Parameters:
hostand optional :port we are connecting to.
serviceidentifier of our client.
sizeof buffer and tcp fragments.

Member Function Documentation

virtual bool ucommon::TCPBuffer::_pending ( void  )
protectedvirtual

Check for pending tcp or ssl data.

Returns:
true if data pending.

Reimplemented from ucommon::BufferProtocol.

Reimplemented in ucommon::SSLBuffer.

virtual size_t ucommon::TCPBuffer::_pull ( char *  address,
size_t  size 
)
protectedvirtual

Method to pull buffer from physical i/o (read).

The address is passed to this virtual since it is hidden as private.

Parameters:
addressof buffer to pull data into.
sizeof buffer area being pulled..
Returns:
number of read written, 0 on error or end of data.

Implements ucommon::BufferProtocol.

Reimplemented in ucommon::SSLBuffer.

socket_t ucommon::TCPBuffer::getsocket ( void  )
inlineprotected

Get the low level socket object.

Returns:
socket we are using.

Definition at line 166 of file buffer.h.

void ucommon::TCPBuffer::open ( TCPServer server,
size_t  size = 536 
)

Connect a tcp socket to a client from a listener.

If the socket was already connected, it is automatically closed first.

Parameters:
serverwe are connected from.
sizeof buffer and tcp fragments.
void ucommon::TCPBuffer::open ( char *  host,
char *  service,
size_t  size = 536 
)

Connect a tcp client session to a specific host uri.

If the socket was already connected, it is automatically closed first.

Parameters:
hostwe are connecting.
serviceto connect to.
sizeof buffer and tcp fragments.

Reimplemented in ucommon::SSLBuffer.


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