libassa 3.5.1
|
#include <IPv4Socket.h>
Public Member Functions | |
IPv4Socket () | |
Default constructor. | |
IPv4Socket (const handler_t fd_) | |
Constructor from file descriptor. | |
~IPv4Socket () | |
Destructor will close connection. | |
IPv4Socket * | clone () const |
"Virtual constructor". | |
bool | open (const int domain_) |
Create socket. | |
bool | close () |
Close socket connection. | |
bool | connect (const Address &address_) |
Client makes connection with the server at address_. | |
virtual bool | bind (const Address &my_address_) |
Server binds listening socket to its local well-known port. | |
IPv4Socket * | accept () |
Accept connection on the listening socket. | |
int | read (char *buf_, const unsigned int size_) |
Read packet of specified size and save it to the given buffer. | |
int | write (const char *buf_, const unsigned int size_) |
Perform blocking write by writing packet of specified size. | |
handler_t | getHandler () const |
Get socket file descriptor. | |
const int | getDomain () const |
Get socket domain type. | |
virtual Streambuf * | rdbuf () |
Return a pointer to the Socketbuf associated with the stream. | |
virtual Streambuf * | rdbuf (Streambuf *sb_) |
Set new Socketbuf for internal IO buffering. | |
virtual int | in_avail () const |
This function returns the number of characters immediately available in the get area of the underlying Socketbuf buffer without making a system call if Socket is doing buffering I/O. | |
![]() | |
Socket () | |
Constructor. | |
virtual | ~Socket () |
Destructor. | |
int | getBytesAvail (void) const |
Return number of bytes available in socket receive buffer. | |
int | ignore (int n_=INT_MAX, int delim_=EOF) |
Extracts bytes and discards them. | |
virtual Socket & | flush () |
This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the associated streambuf object is present). | |
bool | turnOptionOn (opt_t opt_) |
Enable socket option. | |
bool | turnOptionOff (opt_t opt_) |
Disable socket option. | |
bool | setOption (opt_t opt_, int arg_) |
Set socket option to value required. | |
int | getOption (opt_t opt_) const |
Get current value of a socket option. | |
operator void * () const | |
Convertion to void* (for testing where bool is required) | |
bool | operator! () const |
Alias to fail() | |
iostate | rdstate () const |
Retrieve state of the socket. | |
void | clear (iostate state_=Socket::goodbit) |
Clear the socket state. Closed socket remains in bad state. | |
void | setstate (iostate flag_) |
Set socket state to flag_ by adding flag_ to the existing state. | |
bool | good () const |
Indicates no error on the socket. | |
bool | eof () const |
An earlier extraction operation has encountered the end of file of the input stream (peer closed its socket). | |
bool | fail () const |
Indicates that earlier extraction opeartion has failed to match the required pattern of input. | |
bool | bad () const |
Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer. | |
void | dumpState () const |
Write state bits of the socket to the log file. | |
Socket & | operator>> (char &c) |
Input of built-in char type. The value will be XDR-decoded. | |
Socket & | operator>> (unsigned char &c_) |
Input of built-in u_char type. The value will be XDR-decoded. | |
Socket & | operator>> (signed char &c_) |
Input of built-in signed char type. The value will be XDR-decoded. | |
Socket & | operator>> (std::string &s_) |
Input of STL string type. The string content will be XDR-decoded. | |
Socket & | operator>> (short &n_) |
Input of built-in short type. The value will be XDR-decoded. | |
Socket & | operator>> (unsigned short &n_) |
Input of built-in u_short type. The value will be XDR-decoded. | |
Socket & | operator>> (int &n_) |
Input of built-in integer type. The value will be XDR-decoded. | |
Socket & | operator>> (unsigned int &n_) |
Input of built-in u_int type. The value will be XDR-decoded. | |
Socket & | operator>> (long &n_) |
Input of built-in long type. The value will be XDR-decoded. | |
Socket & | operator>> (unsigned long &n_) |
Input of built-in u_long type. The value will be XDR-decoded. | |
Socket & | operator>> (float &n_) |
Input of built-in float type. The value will be XDR-decoded. | |
Socket & | operator>> (double &n_) |
Input of built-in double type. The value will be XDR-decoded. | |
Socket & | operator<< (char c) |
Output of built-in char type. The value will be XDR-encoded. | |
Socket & | operator<< (unsigned char c_) |
Output of built-in u_char type. The value will be XDR-encoded. | |
Socket & | operator<< (signed char c_) |
Output of built-in signed char type. The value will be XDR-encoded. | |
Socket & | operator<< (const std::string &s_) |
Output of STL string type. The value will be XDR-encoded. | |
Socket & | operator<< (short n_) |
Output of built-in short type. The value will be XDR-encoded. | |
Socket & | operator<< (unsigned short n_) |
Output of built-in u_short type. The value will be XDR-encoded. | |
Socket & | operator<< (int n_) |
Output of built-in integer type. The value will be XDR-encoded. | |
Socket & | operator<< (unsigned int n_) |
Output of built-in u_int type. The value will be XDR-encoded. | |
Socket & | operator<< (long n_) |
Output of built-in long type. The value will be XDR-encoded. | |
Socket & | operator<< (unsigned long n_) |
Output of built-in u_long type. The value will be XDR-encoded. | |
Socket & | operator<< (float n_) |
Output of built-in float type. The value will be XDR-encoded. | |
Socket & | operator<< (double n_) |
Output of built-in double type. The value will be XDR-encoded. | |
Socket & | operator<< (Socket &(*f)(Socket &)) |
Manipulators plug-in operator. | |
Static Public Attributes | |
static const int | MAXTCPBUFSZ |
Maximum TCP data frame (no options) | |
![]() | |
static const int | PGSIZE = 4096 |
Size of bytes of a kernel page. | |
Private Member Functions | |
IPv4Socket (const IPv4Socket &) | |
IPv4Socket & | operator= (const IPv4Socket &) |
Private Attributes | |
char * | m_path |
Path of UNIX domain socket. | |
Streambuf * | m_rdbuf |
Socketbuf. | |
Additional Inherited Members | |
![]() | |
enum | io_state_t { goodbit = 0 , eofbit = 1 , failbit = 2 , badbit = 4 } |
State bits: goodbit, eofbit, failbit, badbit. More... | |
enum | opt_t { reuseaddr , rcvlowat , sndlowat , nonblocking } |
Socket options. More... | |
typedef int | iostate |
typedef unsigned char | IOState |
![]() | |
static size_t | xdr_length (const std::string &s_) |
Give the true length of the XDR-encoded STL string. | |
static bool | is_little_endian () |
Determine the endianess of the platform we are on. | |
static void | close_handler (handler_t &socket_) |
Close socket endpoint in a portable way. | |
static string | decode_fcntl_flags (long mask_) |
Decipher flags packed into mask_ used in fcntl() call. | |
![]() | |
int | set_option (int level_, int optname_, int val_) |
Gateway method of setting socket options. | |
int | set_fd_options (long flags_) |
Gateway method for setting file descriptor options. | |
int | clear_fd_options (long flags_) |
Gateway method for clearing file descriptor options. | |
![]() | |
handler_t | m_fd |
File descriptor. | |
int | m_type |
Socket domain type. | |
IOState | m_state |
Control state of the socket. | |
Definition at line 25 of file IPv4Socket.h.
|
inline |
Default constructor.
Definition at line 32 of file IPv4Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
Constructor from file descriptor.
fd_ | file descriptor to use |
Definition at line 40 of file IPv4Socket.h.
References ASSA::Socket::m_fd, ASSA::SOCKTRACE, and trace_with_mask.
|
inline |
Destructor will close connection.
Definition at line 49 of file IPv4Socket.h.
References close(), m_rdbuf, ASSA::SOCKTRACE, and trace_with_mask.
|
private |
IPv4Socket * IPv4Socket::accept | ( | ) |
Accept connection on the listening socket.
Here's an interesting spot - because accept() suppose to work both for INET and UNIX domain socket addresses, we have to allocate enough space and pass exact size of the address type expected.
Returned is a COMPLETED connection (meaning that socket pair is ready for data transfer and doesn't need call to open()). This method will block waiting on connection to come if there is no connection requests waiting on the listenning socket queue. To avoid blocking, use select(3P) first.
Otherwise, if we use, for example, struct sockaddr_un as max. and accept returns struct sockaddr_in, we can cast back to struct sockaddr_in, but internally address data members are not guaranteed to be aligned correctly!!!
Definition at line 176 of file IPv4Socket.cpp.
References accept(), ASSA::ASSAERR, close(), ASSA::disable_handler(), DL, EL, getDomain(), IPv4Socket(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, ASSA::Socket::nonblocking, ASSA::SOCK, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by accept().
Server binds listening socket to its local well-known port.
This call should follow the call to open() and precede the call to accept().
my_address_ | address to bind to |
If UNIX domain, save the path
Implements ASSA::Socket.
Definition at line 123 of file IPv4Socket.cpp.
References Assure_return, bind(), EL, ASSA::Socket::failbit, getDomain(), ASSA::Socket::m_fd, m_path, ASSA::Socket::reuseaddr, ASSA::Socket::setstate(), ASSA::SOCK, ASSA::SOCKTRACE, trace_with_mask, and ASSA::Socket::turnOptionOn().
Referenced by bind().
IPv4Socket * IPv4Socket::clone | ( | ) | const |
"Virtual constructor".
clone() function creates an exact copy of Socket by dup(2)-ing file descriptor and copying Socket's internal state.
Definition at line 329 of file IPv4Socket.cpp.
References DL, ASSA::Socket::failbit, ASSA::Socket::good(), ASSA::Streambuf::in_avail(), IPv4Socket(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::SOCK, ASSA::SOCKTRACE, and trace_with_mask.
|
virtual |
Close socket connection.
Implements ASSA::Socket.
Definition at line 65 of file IPv4Socket.cpp.
References ASSA::Socket::close_handler(), DL, ASSA::Socket::failbit, ASSA::Socket::flush(), ASSA::Streambuf::in_avail(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::Streambuf::sbumpc(), ASSA::Socket::setstate(), ASSA::SOCK, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by accept(), and ~IPv4Socket().
Client makes connection with the server at address_.
If socket is set to non-blocking mode, most likely connect() would return false with errno set to EINPROGRESS. See connect(2) manpage for details.
address_ | peer address to connect with |
Reimplemented from ASSA::Socket.
Definition at line 92 of file IPv4Socket.cpp.
References ASSA::Socket::clear(), connect(), DL, EL, ASSA::get_errno(), getDomain(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, open(), ASSA::SOCK, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by connect().
Get socket domain type.
Implements ASSA::Socket.
Definition at line 138 of file IPv4Socket.h.
References ASSA::Socket::m_type.
|
inlinevirtual |
Get socket file descriptor.
Implements ASSA::Socket.
Definition at line 135 of file IPv4Socket.h.
References ASSA::Socket::m_fd.
This function returns the number of characters immediately available in the get area of the underlying Socketbuf buffer without making a system call if Socket is doing buffering I/O.
Implements ASSA::Socket.
Definition at line 162 of file IPv4Socket.h.
References ASSA::Streambuf::in_avail(), and m_rdbuf.
Create socket.
Socket domain type is specified as AF_INET for internet socket and AF_UNIX for UNIX domain socket (full duplex pipe).
domain_ | domain |
Implements ASSA::Socket.
Definition at line 41 of file IPv4Socket.cpp.
References ASSA::ASSAERR, ASSA::Socket::clear(), ASSA::disable_handler(), DL, EL, ASSA::Socket::failbit, ASSA::is_valid_handler(), ASSA::Socket::m_fd, ASSA::Socket::m_type, ASSA::Socket::nonblocking, ASSA::Socket::setstate(), ASSA::SOCK, ASSA::SOCKTRACE, trace_with_mask, and ASSA::Socket::turnOptionOn().
Referenced by connect().
|
private |
Return a pointer to the Socketbuf associated with the stream.
This is part of the construction of a stream, and the buffer class object is not normally changed. This function may be used to get at Socketbuf functionality directly, given a Socket object.
Reimplemented from ASSA::Socket.
Definition at line 146 of file IPv4Socket.h.
References m_rdbuf.
Set new Socketbuf for internal IO buffering.
IPv4Socket object assumes full ownership of the memory pointed by sb_ (it will be release when ~IPv4Socket destructor is called).
Reimplemented from ASSA::Socket.
Definition at line 26 of file IPv4Socket.cpp.
References m_rdbuf, ASSA::SOCKTRACE, and trace_with_mask.
Read packet of specified size and save it to the given buffer.
buf_ | buffer where packet will be stored |
size_ | size of the packet to expect |
Non-blocking socket delivered partial packet.
Reimplemented from ASSA::Socket.
Definition at line 230 of file IPv4Socket.cpp.
References ASSA::ASSAERR, DL, ASSA::MemDump::dump_to_log(), EL, ASSA::Socket::eofbit, ASSA::Socket::failbit, ASSA::get_errno(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::Streambuf::sbumpc(), ASSA::Socket::setstate(), ASSA::Streambuf::sgetn(), ASSA::SOCK, ASSA::SOCKTRACE, trace_with_mask, and ASSA::Streambuf::unbuffered().
Perform blocking write by writing packet of specified size.
buf_ | buffer to send |
size_ | packet size |
Reimplemented from ASSA::Socket.
Definition at line 293 of file IPv4Socket.cpp.
References DL, ASSA::MemDump::dump_to_log(), ASSA::is_valid_handler(), ASSA::Socket::m_fd, m_rdbuf, ASSA::SOCK, ASSA::SOCKTRACE, ASSA::Streambuf::sputc(), ASSA::Streambuf::sputn(), trace_with_mask, and ASSA::Streambuf::unbuffered().
|
private |
|
private |
Definition at line 176 of file IPv4Socket.h.
Referenced by clone(), close(), in_avail(), rdbuf(), rdbuf(), read(), write(), and ~IPv4Socket().
Maximum TCP data frame (no options)
Definition at line 29 of file IPv4Socket.h.