libassa 3.5.0
|
#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 &) |
typedef int ASSA::Socket::iostate |
typedef unsigned char ASSA::Socket::IOState |
State bits: goodbit, eofbit, failbit, badbit.
Meaning to these is explained in class' documentation.
enum ASSA::Socket::opt_t |
Socket options.
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.
{ reuseaddr, rcvlowat, sndlowat, nonblocking };
ASSA::Socket::Socket | ( | ) | [inline] |
Constructor.
Definition at line 518 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
: m_fd (BAD_HANDLER), m_type(0), #if defined (WIN32) m_nonblocking (false), #endif m_state(Socket::badbit) { trace_with_mask("Socket::Socket",SOCKTRACE); }
ASSA::Socket::~Socket | ( | ) | [inline, virtual] |
Destructor.
Definition at line 531 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
{ trace_with_mask("Socket::~Socket",SOCKTRACE); }
ASSA::Socket::Socket | ( | const Socket & | ) | [private] |
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.
bool ASSA::Socket::bad | ( | ) | const [inline] |
Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer.
Definition at line 337 of file Socket.h.
References badbit, and m_state.
Referenced by dumpState().
{ return m_state & Socket::badbit; }
virtual bool ASSA::Socket::bind | ( | const Address & | my_address_ | ) | [pure virtual] |
Server binds listening socket to its local well-known port.
my_address_ | address to bind to |
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
void ASSA::Socket::clear | ( | iostate | state_ = Socket::goodbit | ) | [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::accept(), ASSA::IPv4Socket::clone(), ASSA::IPv4Socket::connect(), ASSA::UDPSocket::open(), and ASSA::IPv4Socket::open().
{ m_state = is_valid_handler (m_fd) ? state_ : state_ | Socket::badbit; }
int Socket::clear_fd_options | ( | long | flags_ | ) | [protected] |
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 147 of file Socket.cpp.
References decode_fcntl_flags(), DL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by setOption(), and turnOptionOff().
{ trace_with_mask("Socket::clear_fd_options",SOCKTRACE); long oldflags; long newflags; int ret; #if defined (WIN32) u_long set_block = 0; if ((ret = ioctlsocket (m_fd, FIONBIO, &set_block)) == 0) { m_nonblocking = false; return 0; } return -1; #else if ((oldflags = ::fcntl (m_fd, F_GETFL, 0)) < 0) { return -1; } newflags = oldflags & ~flags_; // clear flags DL ((SOCKTRACE,"Set flags fcntl(%d, %s)\n", m_fd, decode_fcntl_flags (newflags).c_str ())); ret = ::fcntl (m_fd, F_SETFL, newflags); newflags = ::fcntl (m_fd, F_GETFL, 0); DL ((SOCKTRACE,"Flags are set to %s via fcntl(%d)\n", decode_fcntl_flags (newflags).c_str ())); return (ret); #endif }
virtual bool ASSA::Socket::close | ( | ) | [pure virtual] |
static void ASSA::Socket::close_handler | ( | handler_t & | socket_ | ) | [inline, static] |
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().
{ #if defined (WIN32) closesocket (socket_); #else ::close (socket_); #endif disable_handler (socket_); }
bool ASSA::Socket::connect | ( | const Address & | address_ | ) | [inline, virtual] |
Make a connection.
address_ | address of the server to connect to |
Reimplemented in ASSA::ConUDPSocket, and ASSA::IPv4Socket.
Definition at line 537 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
{ trace_with_mask("Socket::connect",SOCKTRACE); return false; }
string Socket::decode_fcntl_flags | ( | long | mask_ | ) | [static] |
Decipher flags packed into mask_ used in fcntl() call.
Definition at line 713 of file Socket.cpp.
Referenced by clear_fd_options(), and set_fd_options().
{ string answer; #if !defined (WIN32) if (mask_ & O_RDONLY) { answer = "O_RDONLY|"; // 000000 } if (mask_ & O_WRONLY) { answer += "O_WRONLY|"; // 000001 } if (mask_ & O_RDWR) { answer += "O_RDWR|"; // 000002 } if (mask_ & O_APPEND) { answer += "O_APPEND|"; // 001000 } if (mask_ & O_NONBLOCK) { answer += "O_NONBLOCK|";// 004000 } if (mask_ & O_SYNC) { answer += "O_SYNC|"; // 010000 } if (mask_ & O_ASYNC) { // 020000 answer += "O_ASYNC|"; } answer.erase (answer.end () - 1); #endif return answer; }
void Socket::dumpState | ( | void | ) | const |
Write state bits of the socket to the log file.
Definition at line 653 of file Socket.cpp.
References bad(), DL, ASSA::ends(), eof(), fail(), getOption(), good(), nonblocking, ASSA::SOCKTRACE, and trace_with_mask.
{ trace_with_mask("Socket::dumpState",SOCKTRACE); char state_set[] = "[ set]\n"; char state_not_set[] = "[not set]\n"; std::ostringstream msg; msg << "\n"; msg << "\tTesting good() ....... "; if (this->good ()) msg << state_set; else msg << state_not_set; msg << "\tTesting eof() ........ "; if (this->eof ()) msg << state_set; else msg << state_not_set; msg << "\tTesting fail() ....... "; if (this->fail ()) msg << state_set; else msg << state_not_set; msg << "\tTesting bad() ........ "; if (this->bad ()) msg << state_set; else msg << state_not_set; msg << "\tTesting !() .......... "; if ( !(*this) ) msg << state_set; else msg << state_not_set; msg << "\tTesting void *() ..... "; if ( *this ) msg << state_set; else msg << state_not_set; msg << "\tTesting nonblocking... "; if (getOption (nonblocking) == 1) msg << state_set; else msg << state_not_set; /*--- Terminate stream buffer ---*/ msg << std::ends; DL((SOCKTRACE,"%s\n", msg.str ().c_str ())); }
bool ASSA::Socket::eof | ( | ) | const [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().
{ return m_state & Socket::eofbit; }
bool ASSA::Socket::fail | ( | ) | const [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(), operator void *(), and operator!().
{ return m_state & (Socket::failbit | Socket::badbit); }
Socket & Socket::flush | ( | ) | [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 74 of file Socket.cpp.
References badbit, good(), rdbuf(), and setstate().
Referenced by ASSA::IPv4Socket::close(), ASSA::endl(), and ASSA::flush().
int Socket::getBytesAvail | ( | void | ) | const |
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 48 of file Socket.cpp.
References ASSA::ASSAERR, DL, EL, ASSA::Streambuf::in_avail(), m_fd, rdbuf(), ASSA::SOCKTRACE, and trace_with_mask.
Referenced by ASSA::operator>>(), and ASSA::Socketbuf::showmanyc().
{ trace_with_mask("Socket::getBytesAvail",SOCKTRACE); Socket* This = (Socket*) this; u_long ba = 0; int ret = 0; #if defined(WIN32) ret = ioctlsocket (m_fd, FIONREAD, &ba); #else ret = ioctl (m_fd, FIONREAD, &ba); #endif if (ret == -1) { EL((ASSAERR,"ioctl(2) failed with ret: %d\n", ret)); return ret; } ba += This->rdbuf ()->in_avail (); DL((SOCKTRACE,"%ld bytes available for reading\n", ba)); return (int(ba)); }
virtual const int ASSA::Socket::getDomain | ( | ) | const [pure virtual] |
Get socket domain.
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
virtual handler_t ASSA::Socket::getHandler | ( | ) | const [pure virtual] |
Get file descriptor.
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
Referenced by ASSA::Socketbuf::sys_read(), and ASSA::Socketbuf::sys_write().
int Socket::getOption | ( | opt_t | opt_ | ) | const |
Get current value of a socket option.
opt_ | option name |
Definition at line 248 of file Socket.cpp.
References ASSA::ASSAERR, EL, m_fd, nonblocking, rcvlowat, reuseaddr, sndlowat, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by dumpState().
{ trace_with_mask("Socket::getOption",SOCKTRACE); int optval = 0; if (nonblocking == opt_) { #if defined (WIN32) return (m_nonblocking ? 1 : 0); #else if ((optval = ::fcntl (m_fd, F_GETFL, 0)) < 0) { return -1; } return ((optval & O_NONBLOCK) == O_NONBLOCK ? 1 : 0); #endif } int optname; int level = SOL_SOCKET; bool bin = false; socklen_t len = sizeof (optval); int ret; if (rcvlowat == opt_) { optname = SO_RCVLOWAT; } else if (sndlowat == opt_) { optname = SO_SNDLOWAT; } else if (reuseaddr == opt_) { optname = SO_REUSEADDR; bin = true; } else { EL((ASSAERR,"Invalid socket option\n")); return (-1); } #if defined (__CYGWIN32__) || defined (WIN32) ret = getsockopt (m_fd, level, optname, (char*) &optval, (int*)&len); #else // posix/unix ret = getsockopt (m_fd, level, optname, (char*) &optval, &len); #endif if (ret < 0) { return (-1); } if (bin) { return (ret == 0 ? 0 : 1); } return (ret); }
bool ASSA::Socket::good | ( | ) | const [inline] |
Indicates no error on the socket.
Definition at line 315 of file Socket.h.
References m_state.
Referenced by ASSA::IPv4Socket::clone(), dumpState(), and flush().
{ return m_state == 0; }
int Socket::ignore | ( | int | n_ = INT_MAX , |
int | delim_ = EOF |
||
) |
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 308 of file Socket.cpp.
References eofbit, failbit, PGSIZE, read(), setstate(), ASSA::SOCKTRACE, and trace_with_mask.
Referenced by operator>>().
{ trace_with_mask("Socket::ignore",SOCKTRACE); register int b; register int count = 0; register char c; if (n_ == INT_MAX && delim_ == EOF) { char buf[PGSIZE]; while ((b = read (buf, PGSIZE))) { count += b; } setstate (Socket::eofbit|Socket::failbit); return count; } for (; n_; n_--, count++) { if ( (b = read (&c, 1)) == 0 ) { setstate (Socket::eofbit|Socket::failbit); break; } if ( c == delim_ ) break; } return count; }
virtual int ASSA::Socket::in_avail | ( | ) | const [pure virtual] |
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.
bool Socket::is_little_endian | ( | ) | [static] |
Determine the endianess of the platform we are on.
Definition at line 700 of file Socket.cpp.
{ union { char c [sizeof (short)]; short v; } endian_u; endian_u.v = 256; return (endian_u.c [0] == 0); }
virtual bool ASSA::Socket::open | ( | const int | domain_ | ) | [pure virtual] |
Open socket.
Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.
ASSA::Socket::operator void * | ( | ) | const [inline] |
bool ASSA::Socket::operator! | ( | ) | const [inline] |
Socket & Socket::operator<< | ( | unsigned short | n_ | ) |
Output of built-in u_short type. The value will be XDR-encoded.
Definition at line 577 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
{ u_short val = (u_short) htons((u_short)n_); if (write ((const char*) &val, sizeof(u_short)) != sizeof(u_short)) { setstate (Socket::eofbit|Socket::failbit); } return *this; }
Socket & Socket::operator<< | ( | unsigned int | n_ | ) |
Output of built-in u_int type. The value will be XDR-encoded.
Definition at line 613 of file Socket.cpp.
Socket & Socket::operator<< | ( | char | c | ) |
Output of built-in char type. The value will be XDR-encoded.
Definition at line 508 of file Socket.cpp.
References eofbit, failbit, and write().
{ /* See comment to operator>>(char n_) */ int buf = 0; int len = sizeof (int); XDR xdrs; xdrmem_create (&xdrs, (caddr_t) &buf, len, XDR_ENCODE); xdr_char (&xdrs, &n_); if (write ((const char*) &buf, len) != len) { (Socket::eofbit|Socket::failbit); } xdr_destroy (&xdrs); return *this; }
Socket& ASSA::Socket::operator<< | ( | unsigned char | c_ | ) | [inline] |
Socket& ASSA::Socket::operator<< | ( | signed char | c_ | ) | [inline] |
Socket & Socket::operator<< | ( | const std::string & | s_ | ) |
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 546 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
{ static const char pad [4] = { 0, 0, 0, 0 }; (*this) << s_.length (); int ret = write (s_.c_str (), s_.length ()); if ( ret != s_.length () ) { setstate (Socket::eofbit|Socket::failbit); } size_t r = 4 - s_.length() % 4; if (r) { if (write (pad, r) != r) { setstate (Socket::eofbit|Socket::failbit); } } return *this; }
Socket & Socket::operator<< | ( | short | n_ | ) |
Output of built-in short type. The value will be XDR-encoded.
Definition at line 565 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
{ short val = (short) htons((short)n_); if (write ((const char*) &val, sizeof(short)) != sizeof(short)) { setstate (Socket::eofbit|Socket::failbit); } return *this; }
Socket & Socket::operator<< | ( | unsigned long | n_ | ) |
Output of built-in u_long type. The value will be XDR-encoded.
Definition at line 615 of file Socket.cpp.
Socket & Socket::operator<< | ( | int | n_ | ) |
Output of built-in integer type. The value will be XDR-encoded.
Definition at line 612 of file Socket.cpp.
Socket & Socket::operator<< | ( | double | n_ | ) |
Output of built-in double type. The value will be XDR-encoded.
Definition at line 636 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
{ double buf, f = n_; XDR xdrs; xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(double), XDR_ENCODE); xdr_double (&xdrs, &f); int ret = write ((const char*) &buf, sizeof(double)); xdr_destroy (&xdrs); if ( ret != sizeof(double) ) { setstate (Socket::eofbit|Socket::failbit); } return *this; }
Socket & Socket::operator<< | ( | long | n_ | ) |
Output of built-in long type. The value will be XDR-encoded.
Definition at line 614 of file Socket.cpp.
Socket & Socket::operator<< | ( | float | n_ | ) |
Output of built-in float type. The value will be XDR-encoded.
Definition at line 619 of file Socket.cpp.
References eofbit, failbit, setstate(), and write().
{ float buf, f = n_; XDR xdrs; xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(float), XDR_ENCODE); xdr_float (&xdrs, &f); int ret = write ((const char*) &buf, sizeof(float)); xdr_destroy (&xdrs); if ( ret != sizeof(float) ) { setstate (Socket::eofbit|Socket::failbit); } return *this; }
Socket & Socket::operator>> | ( | unsigned long & | n_ | ) |
Input of built-in u_long type. The value will be XDR-decoded.
Definition at line 465 of file Socket.cpp.
Socket& ASSA::Socket::operator>> | ( | signed char & | c_ | ) | [inline] |
Input of built-in signed char type. The value will be XDR-decoded.
Definition at line 358 of file Socket.h.
References operator>>().
{ return operator>>((char&) c_); }
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 381 of file Socket.cpp.
Socket & Socket::operator>> | ( | short & | n_ | ) |
Input of built-in short type. The value will be XDR-decoded.
Definition at line 402 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
{ short val; if (read ((char*) &val, sizeof(short)) == sizeof(short)) { n_ = (short) ntohs ((short)val); } else { setstate (Socket::eofbit|Socket::failbit); } return *this; }
Socket & Socket::operator>> | ( | int & | n_ | ) |
Input of built-in integer type. The value will be XDR-decoded.
Definition at line 462 of file Socket.cpp.
Socket & Socket::operator>> | ( | unsigned int & | n_ | ) |
Input of built-in u_int type. The value will be XDR-decoded.
Definition at line 463 of file Socket.cpp.
Socket & Socket::operator>> | ( | long & | n_ | ) |
Input of built-in long type. The value will be XDR-decoded.
Definition at line 464 of file Socket.cpp.
Socket & Socket::operator>> | ( | float & | n_ | ) |
Input of built-in float type. The value will be XDR-decoded.
Definition at line 469 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
{ float val; XDR xdrs; xdrmem_create (&xdrs, (caddr_t) &val, sizeof(float), XDR_DECODE); if (read ((char*) &val, sizeof(float)) == sizeof(float)) { xdr_float (&xdrs, &n_); } else { setstate (Socket::eofbit|Socket::failbit); } xdr_destroy (&xdrs); return *this; }
Socket & Socket::operator>> | ( | double & | n_ | ) |
Input of built-in double type. The value will be XDR-decoded.
Definition at line 487 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
{ double val = 0; XDR xdrs; xdrmem_create (&xdrs, (caddr_t) &val, sizeof(double), XDR_DECODE); if (read ((char*) &val, sizeof(double)) == sizeof(double)) { xdr_double (&xdrs, &n_); } else { setstate (Socket::eofbit|Socket::failbit); } xdr_destroy (&xdrs); return *this; }
Socket & Socket::operator>> | ( | unsigned short & | n_ | ) |
Input of built-in u_short type. The value will be XDR-decoded.
Definition at line 416 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
{ u_short val; if (read ((char*) &val, sizeof(u_short)) == sizeof(u_short)) { n_ = (u_short) ntohs ((u_short)val); } else { setstate (Socket::eofbit|Socket::failbit); } return *this; }
Socket & Socket::operator>> | ( | char & | n_ | ) |
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 359 of file Socket.cpp.
References eofbit, failbit, read(), and setstate().
Referenced by operator>>().
{ int c = 0; int len = sizeof (int); XDR xdrs; xdrmem_create (&xdrs, (caddr_t) &c, len, XDR_DECODE); if (read ((char* ) &c, len) == len) { xdr_char (&xdrs, &n_); } else { setstate (Socket::eofbit|Socket::failbit); } xdr_destroy(&xdrs); return *this; }
Socket& ASSA::Socket::operator>> | ( | unsigned char & | c_ | ) | [inline] |
Input of built-in u_char type. The value will be XDR-decoded.
Definition at line 352 of file Socket.h.
References operator>>().
{ return operator>>((char&) c_); }
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.
{ return 0; }
virtual Streambuf* ASSA::Socket::rdbuf | ( | ) | [inline, virtual] |
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(), and getBytesAvail().
{ return 0; }
iostate ASSA::Socket::rdstate | ( | ) | const [inline] |
int ASSA::Socket::read | ( | char * | buf_, |
const u_int | size_ | ||
) | [inline, virtual] |
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>>(), and ASSA::operator>>().
{ trace_with_mask("Socket::read()",SOCKTRACE); return -1; }
int Socket::set_fd_options | ( | long | flags_ | ) | [protected] |
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 104 of file Socket.cpp.
References decode_fcntl_flags(), DL, m_fd, ASSA::SOCKTRACE, and trace_with_mask.
Referenced by setOption(), and turnOptionOn().
{ trace_with_mask("Socket::set_fd_options",SOCKTRACE); int val; int ret; #if defined (WIN32) u_long set_nonblock = 1; if ((val = ioctlsocket (m_fd, FIONBIO, &set_nonblock)) == 0) { m_nonblocking = true; return 0; } return -1; #else // POSIX/UNIX if ((val = ::fcntl (m_fd, F_GETFL, 0)) < 0) { return -1; } val |= flags_; // turn flags on DL ((SOCKTRACE,"Set flags fcntl(%d, %s)\n", m_fd, decode_fcntl_flags (val).c_str ())); ret = ::fcntl (m_fd, F_SETFL, val); val = ::fcntl (m_fd, F_GETFL, 0); DL ((SOCKTRACE,"Flags are set to %s via fcntl(25)\n", decode_fcntl_flags (val).c_str ())); return (ret); #endif }
int Socket::set_option | ( | int | level_, |
int | optname_, | ||
int | val_ | ||
) | [protected] |
Gateway method of setting socket options.
Definition at line 86 of file Socket.cpp.
References failbit, m_fd, and setstate().
Referenced by setOption(), turnOptionOff(), and turnOptionOn().
{ int ret = setsockopt (m_fd, level_, optname_, (const char*) &val_, sizeof (val_)); if (ret < 0) { setstate (Socket::failbit); } return ret; }
bool Socket::setOption | ( | opt_t | opt_, |
int | arg_ | ||
) |
Set socket option to value required.
opt_ | option name |
arg_ | value to set (for binary: 0 - disable, 1 - enable). |
Definition at line 223 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.
{ trace_with_mask("Socket::setOption(,)",SOCKTRACE); int optname; if (nonblocking == opt_) { return (arg_ == 1) ? set_fd_options (O_NONBLOCK) : clear_fd_options (O_NONBLOCK); } if (rcvlowat == opt_) { optname = SO_RCVLOWAT; } else if (sndlowat == opt_) { optname = SO_SNDLOWAT; } else { EL((ASSAERR,"Invalid socket option\n")); return false; } return set_option (SOL_SOCKET, optname, arg_) == 0; }
void ASSA::Socket::setstate | ( | iostate | flag_ | ) | [inline] |
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::UDPSocket::bind(), ASSA::IPv4Socket::bind(), ASSA::IPv4Socket::clone(), ASSA::UDPSocket::close(), ASSA::IPv4Socket::close(), ASSA::ConUDPSocket::connect(), flush(), ignore(), ASSA::UDPSocket::open(), ASSA::IPv4Socket::open(), operator<<(), operator>>(), ASSA::IPv4Socket::read(), ASSA::ConUDPSocket::read(), and set_option().
{ m_state |= flag_; }
bool Socket::turnOptionOff | ( | opt_t | opt_ | ) |
Disable socket option.
opt_ | option name |
Definition at line 204 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().
{ trace_with_mask("Socket::turnOptionOff",SOCKTRACE); if (nonblocking == opt_) return clear_fd_options (O_NONBLOCK); int optname; if (reuseaddr == opt_) optname = SO_REUSEADDR; else { EL((ASSAERR,"Invalid socket option\n")); return false; } return set_option (SOL_SOCKET, optname, 0) == 0; }
bool Socket::turnOptionOn | ( | opt_t | opt_ | ) |
Enable socket option.
opt_ | option name |
Definition at line 185 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::accept(), ASSA::IPv4Socket::bind(), and ASSA::IPv4Socket::open().
{ trace_with_mask("Socket::turnOptionOn",SOCKTRACE); if (nonblocking == opt_) return set_fd_options (O_NONBLOCK); int optname; if (reuseaddr == opt_) optname = SO_REUSEADDR; else { EL((ASSAERR,"Invalid socket option\n")); return false; } return set_option (SOL_SOCKET, optname, 1) == 0; }
int ASSA::Socket::write | ( | const char * | buf_, |
const u_int | size_ | ||
) | [inline, virtual] |
Write specified number of bytes to the socket.
buf_ | packet to send |
size_ | size of the packet |
Reimplemented in ASSA::ConUDPSocket, and ASSA::IPv4Socket.
Definition at line 544 of file Socket.h.
References ASSA::SOCKTRACE, and trace_with_mask.
Referenced by ASSA::endl(), ASSA::ends(), and operator<<().
{ trace_with_mask("Socket::write",SOCKTRACE); return -1; }
static size_t ASSA::Socket::xdr_length | ( | const std::string & | s_ | ) | [inline, static] |
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>>().
{
return (4 + s_.length () + s_.length () % 4);
}
handler_t ASSA::Socket::m_fd [protected] |
File descriptor.
Definition at line 485 of file Socket.h.
Referenced by ASSA::IPv4Socket::accept(), ASSA::UDPSocket::bind(), ASSA::IPv4Socket::bind(), clear(), clear_fd_options(), ASSA::IPv4Socket::clone(), ASSA::UDPSocket::close(), ASSA::IPv4Socket::close(), ASSA::IPv4Socket::connect(), getBytesAvail(), ASSA::UDPSocket::getHandler(), ASSA::IPv4Socket::getHandler(), getOption(), ASSA::IPv4Socket::IPv4Socket(), ASSA::UDPSocket::open(), ASSA::IPv4Socket::open(), ASSA::IPv4Socket::read(), set_fd_options(), set_option(), ASSA::UDPSocket::setHandler(), ASSA::UDPSocket::UDPSocket(), and ASSA::IPv4Socket::write().
IOState ASSA::Socket::m_state [protected] |
int ASSA::Socket::m_type [protected] |
Socket domain type.
Definition at line 488 of file Socket.h.
Referenced by ASSA::UDPSocket::getDomain(), ASSA::IPv4Socket::getDomain(), ASSA::UDPSocket::open(), ASSA::IPv4Socket::open(), and ASSA::UDPSocket::setDomain().
const int ASSA::Socket::PGSIZE = 4096 [static] |