Class Connections
#include <Connections.h >
This class contains all the OS-specific details needed to make various connections. Presumably there is one primary listening connection and possibly a series of additional connections to it. The primary connection is special. This class is used for both the primary connection and each additional connection.
The class accommodates two listening connections - a TCP fd and a Unix domain socket fd.
Each connection is given an connection number (or id) that is unique. Note that fd's are recycled and therefore not unique across connections.
Constructor. Prepare to make connections.
Return the OS file descriptor for the primary conneciton.
Return value: file descriptor, -1 if no connection
Return the IP address of the remote end of the connection
Return value: the IP address as N.N.N.N; 0.0.0.0 if none
Determine if the primary connection is line oriented or not. If line
oriented, all traffic is transmitted in bursts separated by \n.
Return value: true if the connection is line oriented
Determinen if the primary connection is byte-oriented or not.
Return value: true if connection is byte oriented
Get the primary remote connection's IPv4 address.
Return value: ip address as unsigned int
Get the current count on the number of additional connections.
Return value: the count of the number of additional connections; does not include the primary connection
int connNo |
the index into the table of additional connections |
Get the connection information for an additional connection.
Return value: a Connections structure (check eof() if invalid)
int newNumber |
Set the connections cache size |
Set the total number of connections. (Not sure why!)
GetConnectionFN fn |
the new method to get a connection |
Assign a new method to get a connection. This is handy when
a derived class wants to get its full class, not just this one.
AllocConnectionFN fn |
new conneciton allocation method |
Setup a new connection allocation method. This is handy when
a derived class wants to get its full class, not just this one.
int listenFd |
a valid OS file descriptor from listen() |
When the daemon listens on a particular fd for new TCP connections,
report that fd here so new connection requests can be handled.
Return value: the fd from input
int listenFd |
a valid OS file descriptor from listen() |
When the daemon listens on a particular fd for new Unix socket connections,
report that fd here so new connection requests can be handled.
Return value: the fd from input
Specify this this connection is line oriented.
Specify that this connection is byte oriented.
unsigned int newIpAddr |
the IPv4 address |
Set the remote connection's IPv4 address
Determine if the connection has terminated (reached EOF).
Return value: true if EOF reached
const string& service_name |
the name of the TCP service (in /etc/services) |
int defaultport |
the port number to return if service name not found |
Given a TCP service name, return its port number. If not found, return the default one provided.
Return value: the TCP service port
const string& tcpServiceName |
the name of the TCP service |
int tcpserviceport |
If less than 0, then use default port; If =0, then do not open a port; If >0, then open the specified port. |
string& errmsg |
(output) if failed, a description of why it failed |
Open a TCP and listen to it for connections.
Return value: 1 on error (see errmsg), otherwise new listen() fd
const string& socketName |
filename in filesystem to use for socket |
string& errmsg |
(output) if failed, a description of why it failed |
Open the Unix domain socket to listen for local connections.
Return value: 1 on error (see errmsg), otherwise new listen() fd
Close this conneciton.
Get the hostname of the remote host connected on this connection.
Return value: remote hostname
const char* text |
the series of bytes to transmit |
unsigned int size |
the number of bytes to transmit |
bool debug_out |
true if debug output is desired Default value: false |
Send the provided text through the connection.
Return value: number of bytes transmitted
const string& text |
text to send |
Transmit a string of text through the connection
Return value: number of bytes transmitted
const string& text |
text to send |
Transmit a string of text through the connection with debug output enabled.
Return value: number of bytes transmitted
int fd |
the OS file descriptor |
Given a fd, locate the connection.
Return value: a Connections instance; check getFd() to make sure its right
Class all maintained connections
Run a loop to wait for input on a connection and then service it.
int fd |
the OS file descriptor |
Add a new additional connection, using the Connection allocation method, if set, to return the app's instance.
Return value: the new connection
Before a connection is destroyed, unallocate it.
const string& cmd |
the input from the connection |
Upon receiving input from a connection, process it.
Return the total number of bytes/octets received
Return value: input byte count
Return the total number of bytes/octets transmitted
Return value: output byte count
Return the number of bytes pending to be transmitted.
Return value: # of bytes pending
string& cmd |
(output) the input line |
int maxSize |
if non-0, the maximum number of bytes to fetch Default value: 0 |
Get a line of text from the input buffer of text received.
static int noConnections |
Count of number of (active and inactive) connections |
static vector <Connections > conn |
Data for each connection |
static GetConnectionFN getConnFN |
If non-NULL, this is the app's function to find a connection; any superclass of this one should use this |
static AllocConnectionFN allocConnFN |
If non-NULL, this is the app's funciton to allocate a new connection; any superclass of this one should use this |
int fd |
socket fd for this connection |
bool lineOriented |
Flag: is this connection line oriented (so \n is separator) |
unsigned int remoteIpAddr |
Remote IP address of connection of this connection |
bool eof |
Flag: true if eof was found on this connection |
unsigned char* cache |
read cache for this conneciton |
int cachesize |
read cache circular queue indices for this conneciton |
int cachepos |
read cache circular queue indices for this conneciton |
unsigned long inOctets |
used for management - byte I/O count for this connection |
unsigned long outOctets |
used for management - byte I/O count for this connection |
static int tcpListenFd |
fd for listening for new TCP network connections |
static int unixListenFd |
fd for listening for new TCP network connections |
unixListenFd; unixListenFd |
fd for listenting for new socket connections |
static unsigned long totalSessionCount |
Management: session counters |
static unsigned long totalUnixConnCount |
Management: session counters |
static unsigned long totalTcpConnCount |
Management: session counters |
static unsigned long totalInOctets |
Management: total in/out bytes |
static unsigned long totalOutOctets |
Management: total in/out bytes |
int connNo |
Given a connection id/number, find the connection; used internally
Connections& newConnection |
Allocate a new connection
int tcpserviceport |
unsigned char* buf |
|
int len |
|
char* text |
char* text |
|
unsigned char* buf |