smtk::connectionless_socket Class Reference

connectionless socket base class More...

Inheritance diagram for smtk::connectionless_socket:

Inheritance graph
[legend]
List of all members.

Public Member Functions

ssize_t aread (void **buf, size_t *size, long double timeout=INFINITY)
 allocate memory and read from the socket
bool compare_address (const char *address_port) const
 compare the address and port to another
void disable_address_cash (void)
 disable local address and port cashing
void enable_address_cash (void)
 enable local address and port cashing
void free_socket_address_lists (void)
 Free memory of socket address and port list.
ssize_t ssize_t ssize_t ssize_t fwrite (const char *format,...) __attribute__((format(printf
 formated write to the socket
ssize_t ssize_t ssize_t fwrite (long double timeout, const char *format,...) __attribute__((format(printf
 formated write to the socket
ssize_t ssize_t fwrite (void **buf, size_t *buf_len, const char *format,...) __attribute__((format(printf
 formated write to the socket
ssize_t fwrite (void **buf, size_t *buf_len, long double timeout, const char *format,...) __attribute__((format(printf
 formated write to the socket
int get_file_descriptor (void)
 get file descriptor
ssize_t ssize_t ssize_t ssize_t
int char * 
get_remote_address (void) const
 get the write address and port
bool have_data (void) const
 does the socket have data to read?
bool is_invalid (void) const
 see if the object is invalid
bool is_valid (void) const
 see if the object is valid
ssize_t read (void *buf, size_t size, long double timeout=INFINITY)
 read from the socket
ssize_t ssize_t ssize_t ssize_t
int 
set_remote_address (const char *address_port,...) __attribute__((format(printf
 set the write address and port
ssize_t write (const void *buf, size_t size, long double timeout=INFINITY)
 write to the socket

Static Public Attributes

static const long double INFINITY = static_cast<long double>(-1.0)
 time out infinite or block indefinitely

Protected Member Functions

int bind (const char *port)
 used by connected_server and connectionless_server
template<class T>
invalidate (T t)
 invalidate the object
void invalidate (void)
 invalidate the object
template<class T>
validate (T t)
 validate the object
void validate (void)
 validate the object

Protected Attributes

address bind_address_port
int domain
 socket domain
int fd
 socket file descriptor
address remote_address_port

Detailed Description

connectionless socket base class

Connectionless socket class that supports datagrams (UDP/IP) and other maybe connectionless transmission protocols. To contrast see smtk::connected_socket, smtk::connected_server and smtk::connected_client.


Member Function Documentation

ssize_t smtk::socket::aread ( void **  buf,
size_t *  size,
long double  timeout = INFINITY 
) [inherited]

allocate memory and read from the socket

Allocating read. Allocates a buffer as it reads.

This will make a select(2) call if timeout not set to INFINITY. Then depending on that results of the select(2) call or if timeout is set to INFINITY, this will call read(2) (connected_socket) or recvfrom(2) (connectionless_socket). If you ready need a nonblocking read, don't use this. This will allocate a buffer large enough to hold all the data using realloc(3). On success the allocated buffer will always be at least one byte longer than the data size read into it.

Parameters:
buf pointer to a pointer to a buffer to read into. This must point to 0 if memory has not been allocated yet. This functions simular to GNU's getline(3).
size will point to the size that the buffer upon return. size must point to a size_t. The value that size points to must be the size of the memory pointed to by *buf. This value will be changed to reflect the size of the buffer upon return. This size will likely be larger then the number of bytes read.
timeout time out time in seconds. Set timeout to INFINITY for blocking read, 0 for nonblocking read, and a time in seconds for a time out in waiting for data.
Returns:
number of bytes read, -2 if timeout seconds expired with no data to read, and -1 on error.

int smtk::socket::bind ( const char *  port  )  [protected, inherited]

used by connected_server and connectionless_server

Todo:
make smtk::socket::bind() work with more socket domains, types and protocols.

bool smtk::socket::compare_address ( const char *  address_port  )  const [inherited]

compare the address and port to another

This will make a system call in order to find a common address and port for the argument passed in. It will do a DNS (domain name service) lookup or whatever the operating system tells it to do.

Parameters:
address_port is address and port to compared to.
Returns:
true if the socket object remote address and port are the same as that producted from the string pointed to by address_port, otherwise this returns false.

Todo:
this needs to be made more general

void smtk::socket::disable_address_cash ( void   )  [inline, inherited]

disable local address and port cashing

Call disable_address_cash() to disable this local address and port cashing. By default local address and port cashing is enabled. This will not free the memory in the address port cash, it just turns off the cashing that happens in calls to get_remote_address().

This only effects Internet domain sockets, there is no need to cash UNIX domain sockets file names, since there is no address lookup needed to use them.

See also:
enable_address_cash(), free_socket_address_lists()

void smtk::socket::enable_address_cash ( void   )  [inline, inherited]

enable local address and port cashing

Call enable_address_cash() to enable local address and port cashing, in calls to get_remote_address(). The default is enabled.

This only effects Internet domain sockets, there is no need to cash UNIX domain sockets file names, since there is no address lookup needed to use them.

See also:
disable_address_cash, free_socket_address_lists

void smtk::socket::free_socket_address_lists ( void   )  [inherited]

Free memory of socket address and port list.

This only effects Internet domain sockets, UNIX domain sockets file names are not cashed, because they don't have to be looked up; with DNS (domain name service) for example.

The smtk::socket class keeps a list of all addresses and ports for all smtk::socket objects that are created in a process. This list contains the user address and port string and the corresponding socket struct that the socket uses. This list is mostly used by connectionless (UDP) sockets so the when the user changes the remote address where will not need to be a slow DNS lookup or like system call each time the remote address and port is changed. free_socket_address_lists() removes this list of addresses. This is for extreme cases that use a lot of smth::sockets are used and the addresses and port are varying.

See also:
disable_address_port_cash, enable_address_port_cash
Returns:
true if there is data to be read from the socket and false otherwise.

ssize_t smtk::socket::fwrite ( const char *  format,
  ... 
) [inherited]

formated write to the socket

Write data to the socket. By default this call will block and wait for the socket to be ready to write.

This will make a select(2) call if timeout not set to INFINITY. Then depending on that results of the select(2) call or if timeout is set to INFINITY, this will call write(2) (connected_socket) or sendto(2) (connectionless_socket). This will send the string null terminator too.

Parameters:
format is a pointer to a printf(3) like format string.
Returns:
number of bytes written on success, and -1 on error. If not all the data requested to be written is written this will be considered and error and -1 will be returned.

ssize_t smtk::socket::fwrite ( long double  timeout,
const char *  format,
  ... 
) [inherited]

formated write to the socket

Write data to the socket. By default this call will block and wait for the socket to be ready to write.

This will make a select(2) call if timeout not set to INFINITY. Then depending on that results of the select(2) call or if timeout is set to INFINITY, this will call write(2) (connected_socket) or sendto(2) (connectionless_socket). This will send the string null terminator too.

Parameters:
timeout time out time in seconds. If timeout is not INFINITY this will not check if the socket is ready for writing. If timeout is any other value select(2) will be called and this write will time out if the socket is not ready for writing in timeout seconds.
format is a pointer to a printf(3) like format string.
Returns:
number of bytes written on success,-2 if timeout seconds expired with no data to written, and -1 on error. If not all the data requested to be written is written this will be considered and error and -1 will be returned.

ssize_t smtk::socket::fwrite ( void **  buf,
size_t *  buf_len,
const char *  format,
  ... 
) [inherited]

formated write to the socket

Write data to the socket. By default this call will block and wait for the socket to be ready to write.

This will make a select(2) call if timeout not set to INFINITY. Then depending on that results of the select(2) call or if timeout is set to INFINITY, this will call write(2) (connected_socket) or sendto(2) (connectionless_socket). This will send the string null terminator too.

Parameters:
buf If buff is set this will return a malloc(3) allocated memory used to write to the socket.
buf_len if set should be the length of the buffer, and it will be set to the length of the buffer in bytes.
format is a pointer to a printf(3) like format string.
Returns:
number of bytes written on success, and -1 on error. If not all the data requested to be written is written this will be considered and error and -1 will be returned.

ssize_t smtk::socket::fwrite ( void **  buf,
size_t *  buf_len,
long double  timeout,
const char *  format,
  ... 
) [inherited]

formated write to the socket

Write data to the socket. By default this call will block and wait for the socket to be ready to write.

This will make a select(2) call if timeout not set to INFINITY. Then depending on that results of the select(2) call or if timeout is set to INFINITY, this will call write(2) (connected_socket) or sendto(2) (connectionless_socket). This will send the string null terminator too.

Parameters:
buf If buff is set this will return a malloc(3) allocated memory used to write to the socket.
buf_len if set should be the length of the buffer, and it will be set to the length of the buffer in bytes.
timeout time out time in seconds. If timeout is not INFINITY this will not check if the socket is ready for writing. If timeout is any other value select(2) will be called and this write will time out if the socket is not ready for writing in timeout seconds.
format is a pointer to a printf(3) like format string.
Returns:
number of bytes written on success,-2 if timeout seconds expired with no data to written, and -1 on error. If not all the data requested to be written is written this will be considered and error and -1 will be returned.

int smtk::socket::get_file_descriptor ( void   )  [inline, inherited]

get file descriptor

Get the socket file descriptor to do multiplexing with select(2), doing configuration of the socket, sending and receiving out-of-band data, or doing your own read(2) or write(2) calls.

Returns:
the file descriptor that is associated with the socket.

char * smtk::socket::get_remote_address ( void   )  const [inherited]

get the write address and port

Get the remote address and port where the socket will write to. For a connected_socket that is acting as a client this will get the address and port that the socket will connect to or is connected to. For a connectionless_socket this will get the address and port that the socket will write to.

This will not make a system call. This just returns the address and port that was set in set_remote_address().

The string returned will not necessarily be the same as a string that was passed to set_remote_address(). This string will be the address and port that the OS says is equivalent to a string that was passed to set_remote_address(). If an address port like "www.google.com:http" or "www.l.google.com:80" is passed to set_remote_address() the address returned by get_remote_address() will be a unique string like 64.233.187.104:80. Getting the first time will require a system call so the result is cashed in a list that is shared by all smtk::sockets, and subsequent calls to get_remote_address() and set_remote_address() will use the cashed value.

See also:
set_remote_address_port, disable_address_port_cash
Returns:
a pointer to memory that is part of this object. The string is like "128.34.56.5:80", or "" on error. Use strdup() if you need to keep a copy of this string.

bool smtk::socket::have_data ( void   )  const [inherited]

does the socket have data to read?

This is a wrapper of the select(2) function.

Returns:
true if there is data to be read from the socket and false otherwise.

template<class T>
T smtk::validation::invalidate ( t  )  [inline, protected, inherited]

invalidate the object

Make the object invalid.

Returns:
t the argument the user passed.

void smtk::validation::invalidate ( void   )  [inline, protected, inherited]

invalidate the object

Make the object invalid.

bool smtk::validation::is_invalid ( void   )  const [inline, inherited]

see if the object is invalid

Returns:
true if the object is invalid, and false if not.

bool smtk::validation::is_valid ( void   )  const [inline, inherited]

see if the object is valid

Returns:
true if the object is valid, and false if not.

ssize_t connectionless_socket::read ( void *  buf,
size_t  size,
long double  timeout = INFINITY 
) [virtual]

read from the socket

Read data from a socket. By default this call will block and wait for data to read.

This will make a select(2) call if timeout not set to INFINITY. Then depending on that results of the select(2) call or if timeout is set to INFINITY, this will call read(2) (connected_socket) or recvfrom(2) (connectionless_socket).

Parameters:
buf pointer to a buffer to read into.
size the size that the buffer, buf, can hold.
timeout time out time in seconds. Set timeout to INFINITY for blocking read, 0 for nonblocking read, and a time in seconds for a time out in waiting for data.
Returns:
number of bytes read, -2 if timeout seconds expired with no data to read, and -1 on error.

Implements smtk::socket.

int smtk::socket::set_remote_address ( const char *  address_port,
  ... 
) [inherited]

set the write address and port

Set the write (remote) address and port where the socket will write to. For a connected_socket that is acting as a client this will set the address and port that the socket will connect to. set_remote_address() is intended for connectionless_socket, but may be used in connected_socket though using the connected client constructor like in connected_client may be preferred (currently the only way).

This will make a system call to lookup the address and port.

The form of the address can be like for example "127.64.56.33:1235", "foo.com:telnet", or "foo.com:80". This will do a DNS lookup and other stuff like that if this is the first time with that address, otherwise this will get a object copy of the address information from previous calls to this method. So using one smtk::connectionless_socket to write to many addresses can be done without incurring an additional system call when the remote address is changed with set_remote_address_port.

See also:
get_remote_address_port, disable_address_port_cash
Returns:
0 on success and -1 on error, and spews at level warn

Todo:
this needs to be made more general

template<class T>
T smtk::validation::validate ( t  )  [inline, protected, inherited]

validate the object

Make the object valid.

Returns:
t the argument the user passed.

void smtk::validation::validate ( void   )  [inline, protected, inherited]

validate the object

Make the object valid.

ssize_t connectionless_socket::write ( const void *  buf,
size_t  size,
long double  timeout = INFINITY 
) [virtual]

write to the socket

Write data to the socket. By default this call will block and wait for the socket to be ready to write.

This will make a select(2) call if timeout not set to INFINITY. Then depending on that results of the select(2) call or if timeout is set to INFINITY, this will call write(2) (connected_socket) or sendto(2) (connectionless_socket).

Parameters:
buf is a pointer to a buffer with data to write.
size is the number of bytes to write.
timeout time out time in seconds. If timeout is not INFINITY this will not check if the socket is ready for writing. If timeout is any other value select(2) will be called and this write will time out if the socket is not ready for writing in timeout seconds.
Returns:
number of bytes written on success,-2 if timeout seconds expired with no data to written, and -1 on error. If not all the data requested to be written is written this will be considered and error and -1 will be returned.

Implements smtk::socket.


Member Data Documentation

address smtk::socket::bind_address_port [protected, inherited]

Stores binding port.

int smtk::socket::domain [protected, inherited]

socket domain

like: PF_INET, PF_INET6, PF_UNIX. See `man socket' for details.

address smtk::socket::remote_address_port [protected, inherited]

Stores remote address and port. Needed for connected_socket before they connect, and connectionless_socket for a place to write to.


The documentation for this class was generated from the following files:
Generated on Sat Aug 11 22:25:57 2007 for Simulation Toolkit (SmTk) by  doxygen 1.5.2