ASSA::Socket Class Reference

#include <Socket.h>

Inheritance diagram for ASSA::Socket:

ASSA::IPv4Socket ASSA::UDPSocket ASSA::ConUDPSocket ASSA::UnConUDPSocket List of all members.

Public Types

typedef int iostate
typedef unsigned char IOState
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, blocking,
  nonblocking
}
 Socket options. More...

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 unsigned 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 unsigned 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 const int getHandler () const =0
 Get file descriptor.
virtual const int getDomain () const =0
 Get socket domain.
virtual Streambufrdbuf ()
 Return a pointer to the Streambuf associated with the stream.
virtual Streambufrdbuf (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 Socketflush ()
 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.
Socketoperator>> (char &c)
 Input of built-in char type. The value will be XDR-decoded.
Socketoperator>> (unsigned char &c_)
 Input of built-in u_char type. The value will be XDR-decoded.
Socketoperator>> (signed char &c_)
 Input of built-in signed char type. The value will be XDR-decoded.
Socketoperator>> (std::string &s_)
 Input of STL string type. The string content will be XDR-decoded.
Socketoperator>> (short &n_)
 Input of built-in short type. The value will be XDR-decoded.
Socketoperator>> (unsigned short &n_)
 Input of built-in u_short type. The value will be XDR-decoded.
Socketoperator>> (int &n_)
 Input of built-in integer type. The value will be XDR-decoded.
Socketoperator>> (unsigned int &n_)
 Input of built-in u_int type. The value will be XDR-decoded.
Socketoperator>> (long &n_)
 Input of built-in long type. The value will be XDR-decoded.
Socketoperator>> (unsigned long &n_)
 Input of built-in u_long type. The value will be XDR-decoded.
Socketoperator>> (float &n_)
 Input of built-in float type. The value will be XDR-decoded.
Socketoperator>> (double &n_)
 Input of built-in double type. The value will be XDR-decoded.
Socketoperator<< (char c)
 Output of built-in char type. The value will be XDR-encoded.
Socketoperator<< (unsigned char c_)
 Output of built-in u_char type. The value will be XDR-encoded.
Socketoperator<< (signed char c_)
 Output of built-in signed char type. The value will be XDR-encoded.
Socketoperator<< (const std::string &s_)
 Output of STL string type. The value will be XDR-encoded.
Socketoperator<< (short n_)
 Output of built-in short type. The value will be XDR-encoded.
Socketoperator<< (unsigned short n_)
 Output of built-in u_short type. The value will be XDR-encoded.
Socketoperator<< (int n_)
 Output of built-in integer type. The value will be XDR-encoded.
Socketoperator<< (unsigned int n_)
 Output of built-in u_int type. The value will be XDR-encoded.
Socketoperator<< (long n_)
 Output of built-in long type. The value will be XDR-encoded.
Socketoperator<< (unsigned long n_)
 Output of built-in u_long type. The value will be XDR-encoded.
Socketoperator<< (float n_)
 Output of built-in float type. The value will be XDR-encoded.
Socketoperator<< (double n_)
 Output of built-in double type. The value will be XDR-encoded.
Socketoperator<< (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 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 (int flags_)
 Gateway method for setting file descriptor options.
int clear_fd_options (int flags_)
 Gateway method for clearing file descriptor options.

Protected Attributes

int 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.
Socketoperator= (const Socket &)

Detailed Description

Definition at line 70 of file Socket.h.


Member Typedef Documentation

typedef unsigned char ASSA::Socket::IOState
 

Definition at line 92 of file Socket.h.

typedef int ASSA::Socket::iostate
 

Definition at line 91 of file Socket.h.


Member Enumeration Documentation

enum ASSA::Socket::io_state_t
 

State bits: goodbit, eofbit, failbit, badbit.

Meaning to these is explained in class' documentation.

Enumerator:
goodbit  indicates that socket is ready for use
eofbit  indicates that an input operation reached the end of an input sequence
failbit  indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters.
badbit  indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file)

Definition at line 79 of file Socket.h.

00079                     { 
00080         goodbit = 0,  
00081         eofbit  = 1,  
00083         failbit = 2,  
00086         badbit  = 4   
00089     };

enum ASSA::Socket::opt_t
 

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)

blocking  Default behaviour - all read/write operations on socket are blocking.

For input operations, if there is no data available in the socket receiver buffer, we are put to sleep until some data arrives.

For output operations, if there is no room in the socket send buffer for a blocking socket, the process is put to sleep until there is room.

nonblocking  Set Socket to a 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.

Definition at line 97 of file Socket.h.

00097                { 
00098         reuseaddr,   
00099         rcvlowat,    
00104         sndlowat,    
00110         blocking,    
00120         nonblocking  
00134     };


Constructor & Destructor Documentation

ASSA::Socket::Socket  )  [inline]
 

Constructor.

Definition at line 481 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

00482     : m_fd(-1), m_type(0), m_state(Socket::badbit)
00483 {
00484     trace_with_mask("Socket::Socket",SOCKTRACE);
00485 }

ASSA::Socket::~Socket  )  [inline, virtual]
 

Destructor.

Definition at line 488 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

00489 { 
00490     trace_with_mask("Socket::~Socket",SOCKTRACE); 
00491 }

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.


Member Function Documentation

bool ASSA::Socket::bad  )  const [inline]
 

Socket fd == -1 or read/write error occured or some loss of integrity on assosiated stream buffer.

Returns:
true if badbit is set, false otherwise

Definition at line 328 of file Socket.h.

References badbit, and m_state.

00328 { 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.

Parameters:
my_address_ address to bind to
Returns:
true if success, false otherwise

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 528 of file Socket.h.

References badbit, m_fd, and m_state.

Referenced by ASSA::IPv4Socket::accept(), and ASSA::IPv4Socket::clone().

00529 { 
00530     m_state = m_fd >=0 ? state_ : state_ | Socket::badbit; 
00531 }

int Socket::clear_fd_options int  flags_  )  [protected]
 

Gateway method for clearing file descriptor options.

Returns:
0 on success, -1 on error (fcntl(2) failed)

Definition at line 94 of file Socket.cpp.

References m_fd.

Referenced by setOption(), turnOptionOff(), and turnOptionOn().

00095 {
00096     int val;
00097     if ((val = fcntl (m_fd, F_GETFL, 0)) < 0) {
00098         return -1;
00099     }
00100     val &= ~flags_; // turn flags off
00101 
00102     return (fcntl (m_fd, F_SETFL, val) < 0) ? -1 : 0;
00103 }

virtual bool ASSA::Socket::close  )  [pure virtual]
 

Close socket.

Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.

bool ASSA::Socket::connect const Address address_  )  [inline, virtual]
 

Make a connection.

Parameters:
address_ address of the server to connect to

Reimplemented in ASSA::ConUDPSocket, and ASSA::IPv4Socket.

Definition at line 494 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

00495 {
00496     trace_with_mask("Socket::connect",SOCKTRACE);
00497     return false; 
00498 }

void Socket::dumpState  )  const
 

Write state bits of the socket to the log file.

Definition at line 578 of file Socket.cpp.

References DL, ASSA::ends(), getOption(), nonblocking, ASSA::SOCKTRACE, and trace_with_mask.

00579 {
00580     trace_with_mask("Socket::dumpState",SOCKTRACE);
00581 
00582     char state_set[]    = "[    set]\n";
00583     char state_not_set[] = "[not set]\n";
00584 
00585     std::ostringstream msg;
00586 
00587     msg << "\n";
00588     msg << "\tTesting good() ....... ";
00589 
00590     if (this->good ()) msg << state_set;
00591     else msg << state_not_set;
00592 
00593     msg << "\tTesting eof() ........ ";
00594     if (this->eof ()) msg << state_set;
00595     else msg << state_not_set;
00596 
00597     msg << "\tTesting fail() ....... ";
00598     if (this->fail ()) msg << state_set;
00599     else msg << state_not_set;
00600 
00601     msg << "\tTesting bad() ........ ";
00602     if (this->bad ()) msg << state_set;
00603     else msg << state_not_set;
00604 
00605     msg << "\tTesting !() .......... ";
00606     if ( !(*this) ) msg << state_set;
00607     else msg << state_not_set;
00608 
00609     msg << "\tTesting void *() ..... ";
00610     if ( *this ) msg << state_set;
00611     else msg << state_not_set;
00612 
00613     msg << "\tTesting nonblocking... ";
00614     if (getOption (nonblocking) == 1) msg << state_set;
00615     else msg << state_not_set;
00616 
00617     /*--- Terminate stream buffer ---*/
00618     msg << std::ends;
00619 
00620     DL((SOCKTRACE,"%s\n", msg.str ().c_str ()));
00621 }

bool ASSA::Socket::eof  )  const [inline]
 

An earlier extraction operation has encountered the end of file of the input stream (peer closed its socket).

Returns:
true if peer closed the socket; false otherwise

Definition at line 312 of file Socket.h.

References eofbit, and m_state.

00312 { 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.

Returns:
true if failbit or badbit is set, false otherwise

Definition at line 319 of file Socket.h.

References badbit, failbit, and m_state.

Referenced by operator void *(), and operator!().

00320         { 
00321             return m_state & (Socket::failbit | Socket::badbit);
00322         }

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 58 of file Socket.cpp.

References badbit, good(), rdbuf(), and setstate().

Referenced by ASSA::IPv4Socket::close(), ASSA::endl(), and ASSA::flush().

00059 {
00060     if (good () && rdbuf ()) {
00061         if (rdbuf ()->pubsync () == EOF) {
00062             setstate (badbit);
00063         }
00064     }
00065     return (*this);
00066 }

int Socket::getBytesAvail void   )  const
 

Return number of bytes available in socket receive buffer.

Sum of these two numbers is the true number of bytes available for immediate reading.

Definition at line 38 of file Socket.cpp.

References DL, EL, ASSA::ERROR, ASSA::Streambuf::in_avail(), m_fd, rdbuf(), ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::operator>>(), and ASSA::Socketbuf::showmanyc().

00039 {
00040     trace_with_mask("Socket::getBytesAvail",SOCKTRACE);
00041       
00042     Socket* This = (Socket*) this;
00043     int ba = 0;
00044     int ret = ioctl (m_fd, FIONREAD, &ba);
00045 
00046     if (ret == -1) {
00047         EL((ERROR,"ioctl(2) failed with ret: %d\n", ret));
00048         return ret;
00049     }
00050     ba += This->rdbuf ()->in_avail ();
00051 
00052     DL((SOCKTRACE,"%d bytes available for reading\n", ba));
00053     return ba;
00054 }

virtual const int ASSA::Socket::getDomain  )  const [pure virtual]
 

Get socket domain.

Implemented in ASSA::IPv4Socket, and ASSA::UDPSocket.

virtual const int 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.

Parameters:
opt_ option name
Returns:
option value on success (for binary: 0 - disable, 1 - enabled); -1 if error

Definition at line 175 of file Socket.cpp.

References blocking, EL, ASSA::ERROR, m_fd, nonblocking, rcvlowat, reuseaddr, sndlowat, ASSA::SOCKTRACE, and trace_with_mask.

Referenced by dumpState().

00176 {
00177     trace_with_mask("Socket::getOption",SOCKTRACE);
00178     int optval = 0;
00179 
00180     if (nonblocking == opt_) {
00181         if ((optval = fcntl (m_fd, F_GETFL, 0)) < 0) {
00182             return -1;
00183         }
00184         return ((optval & O_NONBLOCK) == O_NONBLOCK ? 1 : 0);
00185     }
00186 
00187     if (blocking == opt_) {
00188         if ((optval = fcntl (m_fd, F_GETFL, 0)) < 0) {
00189             return -1;
00190         }
00191         return ((optval & O_NONBLOCK) == O_NONBLOCK ? 0 : 1);
00192     }
00193     
00194     int optname;
00195     int level = SOL_SOCKET;
00196     bool bin = false;
00197 
00198     socklen_t len = sizeof (optval);
00199     int ret;
00200 
00201     if (rcvlowat == opt_) {
00202         optname = SO_RCVLOWAT;
00203     }
00204     else if (sndlowat == opt_) {
00205         optname = SO_SNDLOWAT;
00206     }
00207     else if (reuseaddr == opt_) {
00208         optname = SO_REUSEADDR;
00209         bin = true;
00210     }
00211     else {
00212         EL((ERROR,"Invalid socket option\n"));
00213         return (-1);
00214     }
00215 
00216 #ifndef __CYGWIN32__
00217     ret = getsockopt (m_fd, level, optname, (char*) &optval, &len);
00218 #else
00219     ret = getsockopt (m_fd, level, optname, (char*) &optval, (int*)&len);
00220 #endif
00221 
00222     if (ret < 0) {
00223         return (-1);
00224     }
00225     if (bin) {
00226         return (ret == 0 ? 0 : 1);
00227     }
00228     return (ret);
00229 }

bool ASSA::Socket::good  )  const [inline]
 

Indicates no error on the socket.

Returns:
true if goodbit is set, false otherwise

Definition at line 306 of file Socket.h.

References m_state.

Referenced by ASSA::IPv4Socket::clone(), and flush().

00306 { 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

Parameters:
n_ number of bytes to ignore (default=INT_MAX)
delim_ delimiter to search for (default=EOF)
Returns:
number of bytes discarded

Definition at line 233 of file Socket.cpp.

References eofbit, failbit, PGSIZE, read(), setstate(), ASSA::SOCKTRACE, and trace_with_mask.

Referenced by operator>>().

00234 {
00235     trace_with_mask("Socket::ignore",SOCKTRACE);
00236     register int b;
00237     register int count = 0;
00238     register char c;
00239 
00240     if (n_ == INT_MAX && delim_ == EOF) {
00241         char buf[PGSIZE];
00242         while ((b = read (buf, PGSIZE))) {
00243             count += b;
00244         }
00245         setstate (Socket::eofbit|Socket::failbit);
00246         return count;
00247     }
00248     for (; n_; n_--, count++) {
00249         if ( (b = read (&c, 1)) == 0 ) {
00250             setstate (Socket::eofbit|Socket::failbit);
00251             break;
00252         }
00253         if ( c == delim_ )
00254             break;
00255     }
00256     return count;
00257 }

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.

Returns:
true if it is a little-endian host; false if a big-endian host.

Definition at line 625 of file Socket.cpp.

00626 {
00627     union {
00628         char  c [sizeof (short)];
00629         short v;
00630     } endian_u;
00631 
00632     endian_u.v = 256;
00633     return (endian_u.c [0] == 0);
00634 }

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]
 

Convertion to void* (for testing where bool is required).

Definition at line 515 of file Socket.h.

References fail().

00516 {
00517     return fail() ? (void *)0 : (void *)(-1); 
00518 }

bool ASSA::Socket::operator!  )  const [inline]
 

Alias to fail().

Definition at line 521 of file Socket.h.

References fail().

00522 { 
00523     return fail(); 
00524 }

Socket& ASSA::Socket::operator<< Socket &(*)(Socket &)  f  )  [inline]
 

Manipulators plug-in operator.

Definition at line 424 of file Socket.h.

00425     {
00426         return (f (*this));
00427     }

Socket & Socket::operator<< double  n_  ) 
 

Output of built-in double type. The value will be XDR-encoded.

Definition at line 561 of file Socket.cpp.

References eofbit, failbit, setstate(), and write().

00562 { 
00563     double buf, f = n_; 
00564     XDR xdrs; 
00565     xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(double), XDR_ENCODE); 
00566     xdr_double (&xdrs, &f); 
00567 
00568     int ret = write ((const char*) &buf, sizeof(double)); 
00569     xdr_destroy (&xdrs); 
00570     if ( ret != sizeof(double) ) { 
00571         setstate (Socket::eofbit|Socket::failbit); 
00572     } 
00573     return *this; 
00574 }

Socket & Socket::operator<< float  n_  ) 
 

Output of built-in float type. The value will be XDR-encoded.

Definition at line 544 of file Socket.cpp.

References eofbit, failbit, setstate(), and write().

00545 { 
00546     float buf, f = n_; 
00547     XDR xdrs; 
00548     xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(float), XDR_ENCODE); 
00549     xdr_float (&xdrs, &f); 
00550 
00551     int ret = write ((const char*) &buf, sizeof(float)); 
00552     xdr_destroy (&xdrs); 
00553     if ( ret != sizeof(float) ) { 
00554         setstate (Socket::eofbit|Socket::failbit); 
00555     } 
00556     return *this; 
00557 }

Socket & Socket::operator<< unsigned long  n_  ) 
 

Output of built-in u_long type. The value will be XDR-encoded.

Definition at line 540 of file Socket.cpp.

Socket & Socket::operator<< long  n_  ) 
 

Output of built-in long type. The value will be XDR-encoded.

Definition at line 539 of file Socket.cpp.

Socket & Socket::operator<< unsigned int  n_  ) 
 

Output of built-in u_int type. The value will be XDR-encoded.

Definition at line 538 of file Socket.cpp.

Socket & Socket::operator<< int  n_  ) 
 

Output of built-in integer type. The value will be XDR-encoded.

Definition at line 537 of file Socket.cpp.

Socket & Socket::operator<< unsigned short  n_  ) 
 

Output of built-in u_short type. The value will be XDR-encoded.

Definition at line 502 of file Socket.cpp.

References eofbit, failbit, setstate(), and write().

00503 { 
00504     u_short val = (u_short) htons((u_short)n_);
00505 
00506     if (write ((const char*) &val, sizeof(u_short)) != sizeof(u_short)) 
00507     {
00508         setstate (Socket::eofbit|Socket::failbit);
00509     }
00510     return *this;
00511 }

Socket & Socket::operator<< short  n_  ) 
 

Output of built-in short type. The value will be XDR-encoded.

Definition at line 490 of file Socket.cpp.

References eofbit, failbit, setstate(), and write().

00491 { 
00492     short val = (short) htons((short)n_);
00493 
00494     if (write ((const char*) &val, sizeof(short)) != sizeof(short)) 
00495     {
00496         setstate (Socket::eofbit|Socket::failbit);
00497     }
00498     return *this;
00499 }

Socket & Socket::operator<< const std::string &  s_  ) 
 

Output of STL string type. The value will be XDR-encoded.

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 471 of file Socket.cpp.

References eofbit, failbit, setstate(), and write().

00472 { 
00473     static const char pad [4] = { 0, 0, 0, 0 };
00474 
00475     (*this) << s_.length ();
00476     int ret = write (s_.c_str (), s_.length ());
00477     if ( ret != s_.length () ) { 
00478         setstate (Socket::eofbit|Socket::failbit); 
00479     } 
00480     size_t r = 4 - s_.length() % 4;
00481     if (r) {
00482         if (write (pad, r) != r) {
00483             setstate (Socket::eofbit|Socket::failbit); 
00484         }
00485     }
00486     return *this;
00487 }

Socket& ASSA::Socket::operator<< signed char  c_  )  [inline]
 

Output of built-in signed char type. The value will be XDR-encoded.

Definition at line 391 of file Socket.h.

References operator<<().

00392     { 
00393         return (*this) << (char) c_; 
00394     }

Socket& ASSA::Socket::operator<< unsigned char  c_  )  [inline]
 

Output of built-in u_char type. The value will be XDR-encoded.

Definition at line 385 of file Socket.h.

00386     { 
00387         return (*this) << (char) c_; 
00388     }

Socket & Socket::operator<< char  c  ) 
 

Output of built-in char type. The value will be XDR-encoded.

Definition at line 433 of file Socket.cpp.

References eofbit, failbit, and write().

Referenced by operator<<().

00434 { 
00435     /* See comment to operator>>(char n_) */
00436 
00437     int buf = 0;
00438     int len = sizeof (int);
00439     XDR xdrs; 
00440 
00441     xdrmem_create (&xdrs, (caddr_t) &buf, len, XDR_ENCODE); 
00442     xdr_char (&xdrs, &n_); 
00443 
00444     if (write ((const char*) &buf, len) != len) {
00445         (Socket::eofbit|Socket::failbit); 
00446     }
00447     xdr_destroy (&xdrs); 
00448     return *this; 
00449 }

Socket& ASSA::Socket::operator= const Socket  )  [private]
 

Socket & Socket::operator>> double &  n_  ) 
 

Input of built-in double type. The value will be XDR-decoded.

Definition at line 412 of file Socket.cpp.

References eofbit, failbit, read(), and setstate().

00413 {
00414     double val = 0; 
00415     XDR xdrs; 
00416     xdrmem_create (&xdrs, (caddr_t) &val, sizeof(double), XDR_DECODE); 
00417     if (read ((char*) &val, sizeof(double)) == sizeof(double)) { 
00418         xdr_double (&xdrs, &n_); 
00419     } 
00420     else { 
00421         setstate (Socket::eofbit|Socket::failbit); 
00422     } 
00423     xdr_destroy (&xdrs); 
00424     return *this; 
00425 }

Socket & Socket::operator>> float &  n_  ) 
 

Input of built-in float type. The value will be XDR-decoded.

Definition at line 394 of file Socket.cpp.

References eofbit, failbit, read(), and setstate().

00395 {
00396     float val; 
00397     XDR xdrs; 
00398     xdrmem_create (&xdrs, (caddr_t) &val, sizeof(float), XDR_DECODE); 
00399 
00400     if (read ((char*) &val, sizeof(float)) == sizeof(float)) { 
00401         xdr_float (&xdrs, &n_); 
00402     } 
00403     else { 
00404         setstate (Socket::eofbit|Socket::failbit); 
00405     } 
00406     xdr_destroy (&xdrs); 
00407     return *this; 
00408 }

Socket & Socket::operator>> unsigned long &  n_  ) 
 

Input of built-in u_long type. The value will be XDR-decoded.

Definition at line 390 of file Socket.cpp.

Socket & Socket::operator>> long &  n_  ) 
 

Input of built-in long type. The value will be XDR-decoded.

Definition at line 389 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 388 of file Socket.cpp.

Socket & Socket::operator>> int &  n_  ) 
 

Input of built-in integer type. The value will be XDR-decoded.

Definition at line 387 of file Socket.cpp.

Socket & Socket::operator>> unsigned short &  n_  ) 
 

Input of built-in u_short type. The value will be XDR-decoded.

Definition at line 341 of file Socket.cpp.

References eofbit, failbit, read(), and setstate().

00342 {
00343     u_short val;
00344     if (read ((char*) &val, sizeof(u_short)) == sizeof(u_short)) {
00345         n_ = (u_short) ntohs ((u_short)val);
00346     }
00347     else {
00348         setstate (Socket::eofbit|Socket::failbit);
00349     }
00350     return *this;
00351 }

Socket & Socket::operator>> short &  n_  ) 
 

Input of built-in short type. The value will be XDR-decoded.

Definition at line 327 of file Socket.cpp.

References eofbit, failbit, read(), and setstate().

00328 {
00329     short val;
00330     if (read ((char*) &val, sizeof(short)) == sizeof(short)) {
00331         n_ = (short) ntohs ((short)val);
00332     }
00333     else {
00334         setstate (Socket::eofbit|Socket::failbit);
00335     }
00336     return *this;
00337 }

Socket & Socket::operator>> std::string &  s_  ) 
 

Input of STL string type. The string content will be XDR-decoded.

Definition at line 306 of file Socket.cpp.

References ignore(), and read().

00307 {
00308     char c = 0;
00309     size_t n = 0;
00310     s_ = "";
00311 
00312     (*this) >> n;
00313 
00314     if (n == 0) {
00315         return *this;
00316     }
00317     size_t len = n;
00318     while (len-- && read (&c, 1) == 1) {
00319         s_ += c;
00320     }
00321     ignore (4 - n % 4);
00322     return *this; 
00323 }

Socket& ASSA::Socket::operator>> signed char &  c_  )  [inline]
 

Input of built-in signed char type. The value will be XDR-decoded.

Definition at line 349 of file Socket.h.

References operator>>().

00350     { 
00351         return operator>>((char&) c_); 
00352     }

Socket& ASSA::Socket::operator>> unsigned char &  c_  )  [inline]
 

Input of built-in u_char type. The value will be XDR-decoded.

Definition at line 343 of file Socket.h.

References operator>>().

00344     { 
00345         return operator>>((char&) c_); 
00346     }

Socket & Socket::operator>> char &  n_  ) 
 

Input of built-in char type. The value will be XDR-decoded.

"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 284 of file Socket.cpp.

References eofbit, failbit, read(), and setstate().

Referenced by operator>>().

00285 {
00286 
00287     int c = 0;
00288     int len = sizeof (int);
00289     XDR xdrs; 
00290     xdrmem_create (&xdrs, (caddr_t) &c, len, XDR_DECODE); 
00291 
00292     if (read ((char* ) &c, len) == len) { 
00293         xdr_char (&xdrs, &n_); 
00294     } 
00295     else { 
00296         setstate (Socket::eofbit|Socket::failbit); 
00297     } 
00298     xdr_destroy(&xdrs); 
00299     return *this; 
00300 }

virtual Streambuf* ASSA::Socket::rdbuf Streambuf  )  [inline, virtual]
 

Virtual function that sets new socket buffer and returns the old one.

Default behavior is to return NULL.

Returns:
Old Socketbuf object.

Reimplemented in ASSA::IPv4Socket.

Definition at line 238 of file Socket.h.

00238 { 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.

Returns:
NULL

Reimplemented in ASSA::IPv4Socket.

Definition at line 231 of file Socket.h.

Referenced by flush(), and getBytesAvail().

00231 { return 0; }

iostate ASSA::Socket::rdstate  )  const [inline]
 

Retrieve state of the socket.

Returns:
control state of the socket

Definition at line 293 of file Socket.h.

References m_state.

00293 { return m_state; }

int ASSA::Socket::read char *  buf_,
const unsigned int  size_
[inline, virtual]
 

Read expected number of bytes from the socket.

Parameters:
buf_ buffer to save received packet to
size_ size of the packet

Reimplemented in ASSA::ConUDPSocket, and ASSA::IPv4Socket.

Definition at line 508 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ignore(), operator>>(), and ASSA::operator>>().

00509 { 
00510     trace_with_mask("Socket::read()",SOCKTRACE);
00511     return -1; 
00512 }

int Socket::set_fd_options int  flags_  )  [protected]
 

Gateway method for setting file descriptor options.

Returns:
0 on success, -1 on error (fcntl(2) failed)

Definition at line 81 of file Socket.cpp.

References m_fd.

Referenced by setOption(), turnOptionOff(), and turnOptionOn().

00082 {
00083     int val;
00084     if ((val = fcntl (m_fd, F_GETFL, 0)) < 0) {
00085         return -1;
00086     }
00087     val |= flags_;      // turn flags on
00088 
00089     return (fcntl (m_fd, F_SETFL, val) < 0) ? -1 : 0;
00090 }

int Socket::set_option int  level_,
int  optname_,
int  val_
[protected]
 

Gateway method of setting socket options.

Returns:
0 on success, -1 on error (setsockopt(2) failed)

Definition at line 70 of file Socket.cpp.

References failbit, m_fd, and setstate().

Referenced by setOption(), turnOptionOff(), and turnOptionOn().

00071 {
00072     int ret = setsockopt (m_fd, level_, optname_, (const char*) &val_, 
00073                   sizeof (val_));
00074     if (ret < 0)
00075         setstate (Socket::failbit);
00076     return ret;
00077 }

bool Socket::setOption opt_t  opt_,
int  arg_
 

Set socket option to value required.

Parameters:
opt_ option name
arg_ value to set (for binary: 0 - disable, 1 - enable).
Returns:
true on success_; false if error

Definition at line 151 of file Socket.cpp.

References blocking, clear_fd_options(), EL, ASSA::ERROR, nonblocking, rcvlowat, set_fd_options(), set_option(), sndlowat, ASSA::SOCKTRACE, and trace_with_mask.

00152 {
00153     trace_with_mask("Socket::setOption(,)",SOCKTRACE);
00154     int optname;
00155 
00156     if (nonblocking == opt_) 
00157         return (arg_ == 1) ? set_fd_options (O_NONBLOCK)
00158             : clear_fd_options (O_NONBLOCK);
00159 
00160     if (blocking == opt_) 
00161         return (arg_ == 1) ? clear_fd_options (O_NONBLOCK)
00162             : set_fd_options (O_NONBLOCK);
00163 
00164     if (rcvlowat == opt_)      optname = SO_RCVLOWAT;
00165     else if (sndlowat == opt_) optname = SO_SNDLOWAT;
00166     else {
00167         EL((ERROR,"Invalid socket option\n"));
00168         return false;
00169     }
00170     return set_option (SOL_SOCKET, optname, arg_) == 0;
00171 }

void ASSA::Socket::setstate iostate  flag_  )  [inline]
 

Set socket state to flag_ by adding flag_ to the existing state.

Parameters:
flag_ new state

Definition at line 534 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::ConUDPSocket::read(), and set_option().

00535 { 
00536     m_state |= flag_; 
00537 }

bool Socket::turnOptionOff opt_t  opt_  ) 
 

Disable socket option.

Parameters:
opt_ option name
Returns:
true on success; false if error

Definition at line 129 of file Socket.cpp.

References blocking, clear_fd_options(), EL, ASSA::ERROR, nonblocking, reuseaddr, set_fd_options(), set_option(), ASSA::SOCKTRACE, and trace_with_mask.

00130 {
00131     trace_with_mask("Socket::turnOptionOff",SOCKTRACE);
00132 
00133     if (blocking == opt_) 
00134         return set_fd_options (O_NONBLOCK);
00135 
00136     if (nonblocking == opt_) 
00137         return clear_fd_options (O_NONBLOCK);
00138 
00139     int optname;
00140     if (reuseaddr == opt_) 
00141         optname = SO_REUSEADDR;
00142     else {
00143         EL((ERROR,"Invalid socket option\n"));
00144         return false;
00145     }
00146     return set_option (SOL_SOCKET, optname, 0) == 0;
00147 }

bool Socket::turnOptionOn opt_t  opt_  ) 
 

Enable socket option.

Parameters:
opt_ option name
Returns:
true on success; false if error

Definition at line 107 of file Socket.cpp.

References blocking, clear_fd_options(), EL, ASSA::ERROR, nonblocking, reuseaddr, set_fd_options(), set_option(), ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::IPv4Socket::accept(), ASSA::IPv4Socket::bind(), and ASSA::RemoteLogger::log_open().

00108 {
00109     trace_with_mask("Socket::turnOptionOn",SOCKTRACE);
00110 
00111     if (blocking == opt_) 
00112         return clear_fd_options (O_NONBLOCK);
00113 
00114     if (nonblocking == opt_) 
00115         return set_fd_options (O_NONBLOCK);
00116 
00117     int optname;
00118     if (reuseaddr == opt_) 
00119         optname = SO_REUSEADDR;
00120     else {
00121         EL((ERROR,"Invalid socket option\n"));
00122         return false;
00123     }
00124     return set_option (SOL_SOCKET, optname, 1) == 0;
00125 }

int ASSA::Socket::write const char *  buf_,
const unsigned int  size_
[inline, virtual]
 

Write specified number of bytes to the socket.

Parameters:
buf_ packet to send
size_ size of the packet

Reimplemented in ASSA::ConUDPSocket, and ASSA::IPv4Socket.

Definition at line 501 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

Referenced by ASSA::endl(), ASSA::ends(), and operator<<().

00502 { 
00503     trace_with_mask("Socket::write",SOCKTRACE); 
00504     return -1; 
00505 }

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 334 of file Socket.h.

Referenced by ASSA::RemoteLogger::log_func(), ASSA::RemoteLogger::log_msg(), ASSA::RemoteLogger::log_open(), and ASSA::xdrIOBuffer::operator>>().

00335     { 
00336         return (4 + s_.length () + s_.length () % 4);
00337     }


Member Data Documentation

int ASSA::Socket::m_fd [protected]
 

File descriptor.

Definition at line 453 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]
 

Control state of the socket.

Definition at line 459 of file Socket.h.

Referenced by bad(), clear(), eof(), fail(), good(), rdstate(), and setstate().

int ASSA::Socket::m_type [protected]
 

Socket domain type.

Definition at line 456 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]
 

Size of bytes of a kernel page.

Definition at line 73 of file Socket.h.

Referenced by ignore().


The documentation for this class was generated from the following files:
Generated on Sat Dec 31 19:52:17 2005 for libassa by  doxygen 1.4.5