libassa 3.5.1
|
#include <Socket.h>
Public Types | |
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 |
Public Member Functions | |
Socket () | |
Constructor. | |
virtual | ~Socket () |
Destructor. | |
virtual bool | open (const int domain_)=0 |
Open socket. | |
virtual bool | close ()=0 |
Close socket. | |
virtual bool | connect (const Address &address_) |
Make a connection. | |
virtual bool | bind (const Address &my_address_)=0 |
Server binds listening socket to its local well-known port. | |
virtual int | write (const char *buf_, const u_int size_) |
Write specified number of bytes to the socket. | |
int | getBytesAvail (void) const |
Return number of bytes available in socket receive buffer. | |
virtual int | read (char *buf_, const u_int size_) |
Read expected number of bytes from the socket. | |
int | ignore (int n_=INT_MAX, int delim_=EOF) |
Extracts bytes and discards them. | |
virtual handler_t | getHandler () const =0 |
Get file descriptor. | |
virtual const int | getDomain () const =0 |
Get socket domain. | |
virtual Streambuf * | rdbuf () |
Return a pointer to the Streambuf associated with the stream. | |
virtual Streambuf * | rdbuf (Streambuf *) |
Virtual function that sets new socket buffer and returns the old one. | |
virtual int | in_avail () const =0 |
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. | |
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 Member Functions | |
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. | |
Static Public Attributes | |
static const int | PGSIZE = 4096 |
Size of bytes of a kernel page. | |
Protected Member Functions | |
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. | |
Protected Attributes | |
handler_t | m_fd |
File descriptor. | |
int | m_type |
Socket domain type. | |
IOState | m_state |
Control state of the socket. | |
Private Member Functions | |
Socket (const Socket &) | |
The copy constructor and assignment operator are private to prevent copying of Socket objects, since the effect of such copying is not well defined. | |
Socket & | operator= (const Socket &) |
State bits: goodbit, eofbit, failbit, badbit.
Meaning to these is explained in class' documentation.
Definition at line 80 of file Socket.h.
Socket options.
Enumerator | |
---|---|
reuseaddr | Allow local address reuse. |
rcvlowat | The receiver low-water mark is the amount of data that must be in the socket receive buffer for select(3) to return "readable". It defaults to 1 for a TCP and UDP socket (NOTE: Posix.1g does not require support for this option). |
sndlowat | The send low-water mark si the amount of available space that must exist in the socket send buffer for select to return "writable". This low-water mark normally defaults to 2048 for TCP socket. UDP socket is always writable (NOTE: Posix.1g does not require support for this option) |
nonblocking | Set Socket to a non-blocking mode (O_RDWR|O_NONBLOCK). The default setup for a socket is BLOCKING (O_RDWR), Blocking implies being suspended while waiting for data to arrive on read. On write, it means that there is no room to send all data out and the process is being suspended until more room in the output buffer becomes available. Use nonblocking option to set the socket to non-blocking mode. For input operations, if an operation cannot be satisfied (at least 1 byte of data for a TCP socket or a complete datagram for a UDP socket), return is made immediately with an error of EWOULDBLOCK. For output operations, if there is no room at all in the socket send buffer, return is made immediately with an error of EWOULDBLOCK. If, however, there is some room in the socket send buffer, the return value will be the number of bytes that the kernel was able to copy into the buffer (This is called a short count). NOTE: To go back to blocking mode, clear nonblocking with turnOptionOff(). |
Definition at line 98 of file Socket.h.
|
inline |
Constructor.
Definition at line 518 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
|
inlinevirtual |
Destructor.
Definition at line 531 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
The copy constructor and assignment operator are private to prevent copying of Socket objects, since the effect of such copying is not well defined.
Usually you want to copy a pointer to the object, or pass a reference to a function.
|
inline |
Server binds listening socket to its local well-known port.
my_address_ | address to bind to |
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
|
inline |
Clear the socket state. Closed socket remains in bad state.
Definition at line 571 of file Socket.h.
References badbit, ASSA::is_valid_handler(), m_fd, and m_state.
Referenced by ASSA::IPv4Socket::connect(), ASSA::IPv4Socket::open(), and ASSA::UDPSocket::open().
Gateway method for clearing file descriptor options.
This is a private function used only by class Socket.
The only value for flags_ is O_NONBLOCK - set the socket back to the blocking mode.
Definition at line 146 of file Socket.cpp.
References decode_fcntl_flags(), DL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by setOption(), and turnOptionOff().
Close socket endpoint in a portable way.
Socket is also set to an invalid value.
Definition at line 448 of file Socket.h.
References close(), and ASSA::disable_handler().
Referenced by ASSA::IPv4Socket::close().
Make a connection.
address_ | address of the server to connect to |
Reimplemented in ASSA::IPv4Socket, and ASSA::ConUDPSocket.
Definition at line 537 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
|
static |
Decipher flags packed into mask_ used in fcntl() call.
Definition at line 712 of file Socket.cpp.
Referenced by clear_fd_options(), and set_fd_options().
Write state bits of the socket to the log file.
Definition at line 652 of file Socket.cpp.
References bad(), DL, eof(), fail(), getOption(), good(), nonblocking, ASSA::SOCKTRACE, and trace_with_mask.
|
inline |
An earlier extraction operation has encountered the end of file of the input stream (peer closed its socket).
Definition at line 321 of file Socket.h.
References eofbit, and m_state.
Referenced by dumpState().
|
inline |
Indicates that earlier extraction opeartion has failed to match the required pattern of input.
Socket should be closed at this point by the owner.
Definition at line 328 of file Socket.h.
References badbit, failbit, and m_state.
Referenced by dumpState(), and operator!().
|
virtual |
This function simply calls the public "synchronizing" function rdbuf()->pubsync() (assuming the associated streambuf object is present).
Typically, such an operation flushes an output stream to the associated external pipe.
Definition at line 73 of file Socket.cpp.
References badbit, good(), rdbuf(), and setstate().
Referenced by ASSA::IPv4Socket::close().
Return number of bytes available in socket receive buffer.
There can be bytes already stored in the Streambuf buffer, in addition to bytes that arrived and are ready for reading in the socket buffer (kernel address space).
Sum of these two numbers is the true number of bytes available for immediate reading.
Definition at line 47 of file Socket.cpp.
References ASSA::ASSAERR, DL, EL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by ASSA::Socketbuf::showmanyc().
Get socket domain.
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
Get file descriptor.
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
Referenced by ASSA::Socketbuf::sys_read(), and ASSA::Socketbuf::sys_write().
Get current value of a socket option.
opt_ | option name |
Definition at line 247 of file Socket.cpp.
References ASSA::ASSAERR, EL, m_fd, nonblocking, rcvlowat, reuseaddr, sndlowat, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by dumpState().
|
inline |
Indicates no error on the socket.
Definition at line 315 of file Socket.h.
References m_state.
Referenced by ASSA::IPv4Socket::clone(), dumpState(), flush(), and ASSA::RemoteLogger::log_func().
Extracts bytes and discards them.
With no arguments, read and discard until eof is encountered.
Bytes are extracted in the following manner:
n_ | delim_ | Action |
---|---|---|
1 | EOF | Read and discard 1 byte |
k | EOF | Read and discard at most k bytes |
INT_MAX | EOF | Read and discard till eof is reached |
INT_MAX | 'c' | Read and discard till either 'c' or eof is found |
k | 'c' | Read and discard at most k bytes, but stop if 'c' is found |
n_ | number of bytes to ignore (default=INT_MAX) |
delim_ | delimiter to search for (default=EOF) |
Definition at line 307 of file Socket.cpp.
References eofbit, failbit, PGSIZE, read(), setstate(), ASSA::SOCKTRACE, and trace_with_mask.
Referenced by operator>>().
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.
It is certain that returned number of characters may be fetched without error, and without accessing any external device.
Implemented in ASSA::ConUDPSocket, ASSA::IPv4Socket, and ASSA::UnConUDPSocket.
|
static |
Determine the endianess of the platform we are on.
Definition at line 699 of file Socket.cpp.
Open socket.
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
|
inline |
|
inline |
Output of built-in char type. The value will be XDR-encoded.
Definition at line 506 of file Socket.cpp.
Output of STL string type. The value will be XDR-encoded.
XDR STRING representation: (from RFC 1832 - http://www.faqs.org/rfcs/rfc1832.html)
0 1 2 3 4 5 ... +--—+--—+--—+--—+--—+--—+...+--—+--—+...+--—+ | length n |byte0|byte1|...| n-1 | 0 |...| 0 | +--—+--—+--—+--—+--—+--—+...+--—+--—+...+--—+ |<----—4 bytes----—>|<---—n bytes---—>|<—r bytes—>| |<-—n+r (where (n+r) mod 4 = 0)-—>|
If n is not a multiple of four, then the n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of four. In other words, (4 - n % 4) == r.
We follow the format, but send data bytes as-is - no conversion is required. We also pad data with r bytes.
Definition at line 544 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
Output of built-in double type. The value will be XDR-encoded.
Definition at line 634 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
Output of built-in float type. The value will be XDR-encoded.
Definition at line 617 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
Output of built-in integer type. The value will be XDR-encoded.
Definition at line 612 of file Socket.cpp.
Output of built-in long type. The value will be XDR-encoded.
Definition at line 614 of file Socket.cpp.
Output of built-in short type. The value will be XDR-encoded.
Definition at line 564 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
Output of built-in u_int type. The value will be XDR-encoded.
Definition at line 613 of file Socket.cpp.
Output of built-in u_long type. The value will be XDR-encoded.
Definition at line 615 of file Socket.cpp.
Output of built-in u_short type. The value will be XDR-encoded.
Definition at line 576 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
Input of built-in char type. The value will be XDR-decoded.
From xdr_simple(3N) man page:
"xdr_char() translates between C characters and their external representations. Note: encoded characters are not packed, and occupy 4 bytes each."
From SunOS 5.3 Network Interfaces Programmer's Guide, "XDR Protocol Specification":
"The representation of all items requires a multipe of four bytes (or 32 bits) of data. ... The n bytes are followed by enough (0 to 3) residual zero bytes, r, to make the total byte count a multiple of four."
The implication if this is that even though we transerfer 1 byte, we still have to allocate and transfer 4 bytes to hold it.
Definition at line 358 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
Referenced by operator>>(), and operator>>().
Input of built-in double type. The value will be XDR-decoded.
Definition at line 486 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
Input of built-in float type. The value will be XDR-decoded.
Definition at line 468 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
Input of built-in integer type. The value will be XDR-decoded.
Definition at line 462 of file Socket.cpp.
Input of built-in long type. The value will be XDR-decoded.
Definition at line 464 of file Socket.cpp.
Input of built-in short type. The value will be XDR-decoded.
Definition at line 401 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
Input of built-in signed char type. The value will be XDR-decoded.
Definition at line 358 of file Socket.h.
References operator>>().
Socket & Socket::operator>> | ( | std::string & | s_ | ) |
Input of STL string type. The string content will be XDR-decoded.
For format explanation, see comment to operator<<(string&)
Definition at line 380 of file Socket.cpp.
Input of built-in u_char type. The value will be XDR-decoded.
Definition at line 352 of file Socket.h.
References operator>>().
Input of built-in u_int type. The value will be XDR-decoded.
Definition at line 463 of file Socket.cpp.
Input of built-in u_long type. The value will be XDR-decoded.
Definition at line 465 of file Socket.cpp.
Input of built-in u_short type. The value will be XDR-decoded.
Definition at line 415 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
Return a pointer to the Streambuf 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 Streambuf functionality directly, given a Socket object. Default behavior is to return NULL.
Reimplemented in ASSA::IPv4Socket.
Definition at line 240 of file Socket.h.
Referenced by flush().
Virtual function that sets new socket buffer and returns the old one.
Default behavior is to return NULL.
Reimplemented in ASSA::IPv4Socket.
Definition at line 247 of file Socket.h.
|
inline |
Read expected number of bytes from the socket.
buf_ | buffer to save received packet to |
size_ | size of the packet |
Reimplemented in ASSA::ConUDPSocket, and ASSA::IPv4Socket.
Definition at line 551 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
Referenced by ignore(), operator>>(), operator>>(), operator>>(), operator>>(), operator>>(), and operator>>().
Gateway method for setting file descriptor options.
This is a private function used only by class Socket.
The only value for flags_ is O_NONBLOCK.
Definition at line 103 of file Socket.cpp.
References decode_fcntl_flags(), DL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by setOption(), and turnOptionOn().
Gateway method of setting socket options.
Definition at line 85 of file Socket.cpp.
References failbit, m_fd, and setstate().
Referenced by setOption(), turnOptionOff(), and turnOptionOn().
Set socket option to value required.
opt_ | option name |
arg_ | value to set (for binary: 0 - disable, 1 - enable). |
Definition at line 222 of file Socket.cpp.
References ASSA::ASSAERR, clear_fd_options(), EL, nonblocking, rcvlowat, set_fd_options(), set_option(), sndlowat, ASSA::SOCKTRACE, and trace_with_mask.
Set socket state to flag_ by adding flag_ to the existing state.
flag_ | new state |
Definition at line 577 of file Socket.h.
References m_state.
Referenced by ASSA::IPv4Socket::bind(), ASSA::UDPSocket::bind(), ASSA::IPv4Socket::close(), ASSA::UDPSocket::close(), ASSA::ConUDPSocket::connect(), flush(), ignore(), ASSA::IPv4Socket::open(), ASSA::UDPSocket::open(), operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), operator>>(), operator>>(), operator>>(), operator>>(), operator>>(), ASSA::ConUDPSocket::read(), ASSA::IPv4Socket::read(), and set_option().
Disable socket option.
opt_ | option name |
Definition at line 203 of file Socket.cpp.
References ASSA::ASSAERR, clear_fd_options(), EL, nonblocking, reuseaddr, set_option(), ASSA::SOCKTRACE, and trace_with_mask.
Referenced by ASSA::RemoteLogger::log_open().
Enable socket option.
opt_ | option name |
Definition at line 184 of file Socket.cpp.
References ASSA::ASSAERR, EL, nonblocking, reuseaddr, set_fd_options(), set_option(), ASSA::SOCKTRACE, and trace_with_mask.
Referenced by ASSA::IPv4Socket::bind(), and ASSA::IPv4Socket::open().
Write specified number of bytes to the socket.
buf_ | packet to send |
size_ | size of the packet |
Reimplemented in ASSA::IPv4Socket, and ASSA::ConUDPSocket.
Definition at line 544 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
Referenced by operator<<(), operator<<(), operator<<(), operator<<(), operator<<(), and operator<<().
Give the true length of the XDR-encoded STL string.
Definition at line 343 of file Socket.h.
Referenced by ASSA::RemoteLogger::log_func(), ASSA::RemoteLogger::log_msg(), ASSA::RemoteLogger::log_open(), and ASSA::xdrIOBuffer::operator>>().
|
protected |
File descriptor.
Definition at line 485 of file Socket.h.
Referenced by ASSA::IPv4Socket::accept(), ASSA::IPv4Socket::bind(), ASSA::UDPSocket::bind(), clear(), clear_fd_options(), ASSA::IPv4Socket::clone(), ASSA::IPv4Socket::close(), ASSA::UDPSocket::close(), ASSA::IPv4Socket::connect(), getBytesAvail(), ASSA::IPv4Socket::getHandler(), ASSA::UDPSocket::getHandler(), getOption(), ASSA::IPv4Socket::IPv4Socket(), ASSA::IPv4Socket::open(), ASSA::UDPSocket::open(), ASSA::IPv4Socket::read(), set_fd_options(), set_option(), ASSA::UDPSocket::setHandler(), ASSA::UDPSocket::UDPSocket(), and ASSA::IPv4Socket::write().
|
protected |
|
protected |
Socket domain type.
Definition at line 488 of file Socket.h.
Referenced by ASSA::IPv4Socket::getDomain(), ASSA::UDPSocket::getDomain(), ASSA::IPv4Socket::open(), ASSA::UDPSocket::open(), and ASSA::UDPSocket::setDomain().