Rudiments
Public Member Functions | List of all members
inetsocketclient Class Reference

Inherits socketclient, and inetsocketutil.

Public Member Functions

 inetsocketclient ()
 
 inetsocketclient (const inetsocketclient &i)
 
inetsocketclientoperator= (const inetsocketclient &i)
 
virtual ~inetsocketclient ()
 
int32_t connect (const char *host, uint16_t port, int32_t timeoutsec, int32_t timeoutusec, uint32_t retrywait, uint32_t tries)
 
void initialize (constnamevaluepairs *cd)
 
void initialize (const char *host, uint16_t port, int32_t timeoutsec, int32_t timeoutusec, uint32_t retrywait, uint32_t tries)
 
int32_t connect ()
 
void randomizeAddresses (uint32_t seed)
 
void dontRandomizeAddresses ()
 
- Public Member Functions inherited from socketclient
 socketclient ()
 
 socketclient (const socketclient &c)
 
socketclientoperator= (const socketclient &c)
 
virtual ~socketclient ()
 
virtual bool supportsBlockingNonBlockingModes ()
 
virtual bool useNonBlockingMode () const
 
virtual bool useBlockingMode () const
 
virtual bool isUsingNonBlockingMode () const
 
virtual int32_t ioCtl (int32_t command, void *arg) const
 
- Public Member Functions inherited from client
 client ()
 
 client (const client &c)
 
clientoperator= (const client &c)
 
virtual ~client ()
 
virtual const char * getVerboseConnectError ()
 
virtual void setVerboseConnectError (const char *error)
 
- Public Member Functions inherited from filedescriptor
 filedescriptor ()
 
 filedescriptor (const filedescriptor &f)
 
filedescriptoroperator= (const filedescriptor &f)
 
virtual ~filedescriptor ()
 
virtual bool close ()
 
int32_t getFileDescriptor () const
 
void setFileDescriptor (int32_t filedesc)
 
int32_t duplicate () const
 
bool duplicate (int32_t newfd) const
 
void setSecurityContext (securitycontext *ctx)
 
securitycontextgetSecurityContext ()
 
ssize_t write (uint16_t number) const
 
ssize_t write (uint32_t number) const
 
ssize_t write (uint64_t number) const
 
ssize_t write (int16_t number) const
 
ssize_t write (int32_t number) const
 
ssize_t write (int64_t number) const
 
ssize_t write (float number) const
 
ssize_t write (double number) const
 
ssize_t write (unsigned char character) const
 
ssize_t write (char character) const
 
ssize_t write (bool value) const
 
ssize_t write (const unsigned char *string) const
 
ssize_t write (const char *string) const
 
ssize_t write (const unsigned char *string, size_t size) const
 
ssize_t write (const char *string, size_t size) const
 
ssize_t write (const void *buffer, size_t size) const
 
ssize_t write (uint16_t number, int32_t sec, int32_t usec) const
 
ssize_t write (uint32_t number, int32_t sec, int32_t usec) const
 
ssize_t write (uint64_t number, int32_t sec, int32_t usec) const
 
ssize_t write (int16_t number, int32_t sec, int32_t usec) const
 
ssize_t write (int32_t number, int32_t sec, int32_t usec) const
 
ssize_t write (int64_t number, int32_t sec, int32_t usec) const
 
ssize_t write (float number, int32_t sec, int32_t usec) const
 
ssize_t write (double number, int32_t sec, int32_t usec) const
 
ssize_t write (unsigned char character, int32_t sec, int32_t usec) const
 
ssize_t write (char character, int32_t sec, int32_t usec) const
 
ssize_t write (bool value, int32_t sec, int32_t usec) const
 
ssize_t write (const unsigned char *string, int32_t sec, int32_t usec) const
 
ssize_t write (const char *string, int32_t sec, int32_t usec) const
 
ssize_t write (const unsigned char *string, size_t size, int32_t sec, int32_t usec) const
 
ssize_t write (const char *string, size_t size, int32_t sec, int32_t usec) const
 
ssize_t write (const void *buffer, size_t size, int32_t sec, int32_t usec) const
 
size_t printf (const char *format,...)
 
size_t printf (const char *format, va_list *argp)
 
void safePrint (unsigned char c)
 
void safePrint (const unsigned char *str)
 
void safePrint (const unsigned char *str, int32_t length)
 
void safePrint (char c)
 
void safePrint (const char *str)
 
void safePrint (const char *str, int32_t length)
 
void printBits (unsigned char value)
 
void printBits (uint16_t value)
 
void printBits (uint32_t value)
 
void printBits (uint64_t value)
 
void printBits (char value)
 
void printBits (int16_t value)
 
void printBits (int32_t value)
 
void printBits (int64_t value)
 
void printBits (const unsigned char *bits, uint64_t size)
 
ssize_t read (uint16_t *buffer)
 
ssize_t read (uint32_t *buffer)
 
ssize_t read (uint64_t *buffer)
 
ssize_t read (int16_t *buffer)
 
ssize_t read (int32_t *buffer)
 
ssize_t read (int64_t *buffer)
 
ssize_t read (float *buffer)
 
ssize_t read (double *buffer)
 
ssize_t read (unsigned char *buffer)
 
ssize_t read (char *buffer)
 
ssize_t read (bool *buffer)
 
ssize_t read (unsigned char *buffer, size_t size)
 
ssize_t read (char *buffer, size_t size)
 
ssize_t read (void *buf, size_t size)
 
ssize_t read (char **buffer, const char *terminator)
 
ssize_t read (char **buffer, const char *terminator, size_t maxbytes)
 
ssize_t read (uint16_t *buffer, int32_t sec, int32_t usec)
 
ssize_t read (uint32_t *buffer, int32_t sec, int32_t usec)
 
ssize_t read (uint64_t *buffer, int32_t sec, int32_t usec)
 
ssize_t read (int16_t *buffer, int32_t sec, int32_t usec)
 
ssize_t read (int32_t *buffer, int32_t sec, int32_t usec)
 
ssize_t read (int64_t *buffer, int32_t sec, int32_t usec)
 
ssize_t read (float *buffer, int32_t sec, int32_t usec)
 
ssize_t read (double *buffer, int32_t sec, int32_t usec)
 
ssize_t read (unsigned char *buffer, int32_t sec, int32_t usec)
 
ssize_t read (char *buffer, int32_t sec, int32_t usec)
 
ssize_t read (bool *buffer, int32_t sec, int32_t usec)
 
ssize_t read (unsigned char *buffer, size_t size, int32_t sec, int32_t usec)
 
ssize_t read (char *buffer, size_t size, int32_t sec, int32_t usec)
 
ssize_t read (void *buf, size_t size, int32_t sec, int32_t usec)
 
ssize_t read (char **buffer, const char *terminator, int32_t sec, int32_t usec)
 
ssize_t read (char **buffer, const char *terminator, size_t maxbytes, int32_t sec, int32_t usec)
 
ssize_t read (char **buffer, const char *terminator, size_t maxbytes, char escapechar, int32_t sec, int32_t usec)
 
int32_t waitForNonBlockingRead (int32_t sec, int32_t usec) const
 
int32_t waitForNonBlockingWrite (int32_t sec, int32_t usec) const
 
void retryInterruptedReads ()
 
void dontRetryInterruptedReads ()
 
bool getRetryInterruptedReads () const
 
void retryInterruptedWrites ()
 
void dontRetryInterruptedWrites ()
 
bool getRetryInterruptedWrites () const
 
void retryInterruptedWaits ()
 
void dontRetryInterruptedWaits ()
 
bool getRetryInterruptedWaits () const
 
void retryInterruptedFcntl ()
 
void dontRetryInterruptedFcntl ()
 
bool getRetryInterruptedFcntl () const
 
void retryInterruptedIoctl ()
 
void dontRetryInterruptedIoctl ()
 
bool getRetryInterruptedIoctl () const
 
void allowShortReads ()
 
void dontAllowShortReads ()
 
void allowShortWrites ()
 
void dontAllowShortWrites ()
 
virtual bool passFileDescriptor (int32_t fd)
 
virtual bool receiveFileDescriptor (int32_t *fd)
 
virtual bool passSocket (int32_t sock)
 
virtual bool receiveSocket (int32_t *sock)
 
void translateByteOrder ()
 
void dontTranslateByteOrder ()
 
virtual int32_t fCntl (int32_t command, long arg) const
 
bool useNaglesAlgorithm ()
 
bool dontUseNaglesAlgorithm ()
 
bool setSocketWriteBufferSize (int32_t size)
 
bool getSocketWriteBufferSize (int32_t *size)
 
bool setSocketReadBufferSize (int32_t size)
 
bool getSocketReadBufferSize (int32_t *size)
 
bool disableIPv4 ()
 
bool enableIPv4 ()
 
const char * getType () const
 
char * getPeerAddress () const
 
bool setWriteBufferSize (ssize_t size) const
 
bool flushWriteBuffer (int32_t sec, int32_t usec) const
 
bool setReadBufferSize (ssize_t size) const
 
bool closeOnExec ()
 
bool dontCloseOnExec ()
 
bool getCloseOnExec ()
 

Additional Inherited Members

- Static Public Member Functions inherited from filedescriptor
static bool createPipe (filedescriptor *readfd, filedescriptor *writefd)
 
static uint16_t hostToNet (uint16_t value)
 
static uint32_t hostToNet (uint32_t value)
 
static uint64_t hostToNet (uint64_t value)
 
static uint16_t netToHost (uint16_t value)
 
static uint32_t netToHost (uint32_t value)
 
static uint64_t netToHost (uint64_t value)
 
static uint16_t hostToLittleEndian (uint16_t value)
 
static uint32_t hostToLittleEndian (uint32_t value)
 
static uint64_t hostToLittleEndian (uint64_t value)
 
static uint16_t littleEndianToHost (uint16_t value)
 
static uint32_t littleEndianToHost (uint32_t value)
 
static uint64_t littleEndianToHost (uint64_t value)
 

Detailed Description

The inetsocketclient class allows you to write programs that can talk to other programs across a network over TCP stream sockets.

The inetsocketclient class provides methods for connecting to servers. Its ultimate parent class: filedescriptor provides methods for reading and writing data and closing connections.

Constructor & Destructor Documentation

◆ inetsocketclient() [1/2]

inetsocketclient::inetsocketclient ( )

Creates an instance of the inetsocketclient class.

◆ inetsocketclient() [2/2]

inetsocketclient::inetsocketclient ( const inetsocketclient i)

Creates an instance of the inetsocketclient class that is a copy of "i".

◆ ~inetsocketclient()

virtual inetsocketclient::~inetsocketclient ( )
virtual

Deletes this instance of the inetsocketclient class.

Member Function Documentation

◆ connect() [1/2]

int32_t inetsocketclient::connect ( const char *  host,
uint16_t  port,
int32_t  timeoutsec,
int32_t  timeoutusec,
uint32_t  retrywait,
uint32_t  tries 
)

This convenience method calls the initialize() and connect() methods of the class.

Returns RESULT_SUCCESS on success and RESULT_ERROR on failure.

See the other connect() method for a note about use in multithreaded applications.

◆ connect() [2/2]

int32_t inetsocketclient::connect ( )
virtual

Attempts to connect to the "host" and "port" set earlier using one of the initialize() methods. If the connection fails, it will retry, for a total of "tries" attempts, waiting "retrywait" seconds between each attempt.

If "host" resolves to multiple addresses (ie. if round-robin DNS or another DNS-based load-balancing strategy is used), then an attempt will be made to connect to each address until the attempt succeeds or there are no more addresses left to try. This process will be tried "tries" times.

Setting "tries" to 0 will cause it to try to connect indefinitely. Setting "retrywait" to 0 will cause it to try to connect over and over as fast as possible (not recommended).

Each attempt to connect will wait "timeoutsec" seconds and "timeoutusec" microseconds for the connect to succeed. Specifying -1 for either parameter will cause the attempt to wait until the underlying protocol times out which may be up to 2 minutes.

Returns RESULT_SUCCESS on success and RESULT_ERROR on failure.

Note that if your system does not support getaddrinfo() then connect() uses the hostentry and protocolentry classes. If you are using this method in a multithreaded application, you may need to supply those classes with mutexes. See hostentry.h and protocolentry.h for more detail.

Notes on round-robin DNS and DNS-based load-balancing:

By default, the connect() method randomizes the list of addresses that "host" resolves to. This is intended to work around two issues.

Issue 1: When round-robin DNS is used, if one host is down, then the next host in the list receives all of the traffic that would have gone to the downed host. It is usually prefereable for the traffic to be distributed among the remaining hosts instead. Randomization of the list of addresses accomplishes this.

Issue 2: Rudiments uses getaddrinfo() if it is available, but implementations of getaddrinfo() vary widely.

Some implementations sort the list of addresses that "host" resolves to rather than returning them in the order returned by the DNS server, defeating any attempt at DNS-based load-balancing.

Some implementations support a sortv4=no directive in /etc/gai.conf that instructs getaddrinfo() not to sort the addresses but many do not.

Some implementations randomize the list.

Some implementations return the list in the order returned by DNS.

Rudiments doesn't have a good way of knowing how getaddrinfo() is implemented or configured, so to err on the side of caution, connect() randomizes the list of addresses that "host" resolves to.

Randomization can be disabled and configured using dontRandomizeAddresses() and randomizeAddresses().

Reimplemented from client.

◆ dontRandomizeAddresses()

void inetsocketclient::dontRandomizeAddresses ( )

Instructs connect() not to randomize the list of addresses that "host" resolves to.

See connect() for issues that this can cause on some platforms or environments.

◆ initialize() [1/2]

void inetsocketclient::initialize ( constnamevaluepairs *  cd)
virtual

Queries constnamevaluepairs "cd" for "host", "port", "timeoutsec", "timeoutusec", "retrywait" and "tries". Initializes the class to use the result when connect() is called.

Reimplemented from client.

◆ initialize() [2/2]

void inetsocketclient::initialize ( const char *  host,
uint16_t  port,
int32_t  timeoutsec,
int32_t  timeoutusec,
uint32_t  retrywait,
uint32_t  tries 
)

Initializes the class to use "host", "port", "timeoutsec", "timeoutusec", "retrywait" and "tries" when connect() is called.

◆ operator=()

inetsocketclient& inetsocketclient::operator= ( const inetsocketclient i)

Makes this instance of the inetsocketclient class identical to "i".

◆ randomizeAddresses()

void inetsocketclient::randomizeAddresses ( uint32_t  seed)

Instructs connect() to randomize the list of addresses that "host" resolves to using "seed".

Randomization of addresses is the default behavior, but by default the random number generator used internally is seeded using randomnumber::getSeed(). This may or may not be suitable as it will fall back to seeding the generator with the time of day on platforms that don't support any other method.

This method allows you to provide a more suitable seed if necessary.