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(), ASSA::IPv4Socket::clone(), ASSA::IPv4Socket::connect(), ASSA::UDPSocket::open(), and ASSA::IPv4Socket::open().

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

References m_fd.

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

00100 {
00101     int val;
00102     if ((val = fcntl (m_fd, F_GETFL, 0)) < 0) {
00103         return -1;
00104     }
00105     val &= ~flags_; // turn flags off
00106 
00107     return (fcntl (m_fd, F_SETFL, val) < 0) ? -1 : 0;
00108 }

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

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

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

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

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

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

00064 {
00065     if (good () && rdbuf ()) {
00066         if (rdbuf ()->pubsync () == EOF) {
00067             setstate (badbit);
00068         }
00069     }
00070     return (*this);
00071 }

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 43 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().

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

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

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

Referenced by dumpState().

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

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

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

Referenced by operator>>().

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

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

00631 {
00632     union {
00633         char  c [sizeof (short)];
00634         short v;
00635     } endian_u;
00636 
00637     endian_u.v = 256;
00638     return (endian_u.c [0] == 0);
00639 }

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

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

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

Socket & Socket::operator<< float  n_  ) 
 

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

Definition at line 549 of file Socket.cpp.

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

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

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

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

Definition at line 545 of file Socket.cpp.

Socket & Socket::operator<< long  n_  ) 
 

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

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

Socket & Socket::operator<< int  n_  ) 
 

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

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

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

00508 { 
00509     u_short val = (u_short) htons((u_short)n_);
00510 
00511     if (write ((const char*) &val, sizeof(u_short)) != sizeof(u_short)) 
00512     {
00513         setstate (Socket::eofbit|Socket::failbit);
00514     }
00515     return *this;
00516 }

Socket & Socket::operator<< short  n_  ) 
 

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

Definition at line 495 of file Socket.cpp.

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

00496 { 
00497     short val = (short) htons((short)n_);
00498 
00499     if (write ((const char*) &val, sizeof(short)) != sizeof(short)) 
00500     {
00501         setstate (Socket::eofbit|Socket::failbit);
00502     }
00503     return *this;
00504 }

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

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

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

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.

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

References eofbit, failbit, and write().

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

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

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

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

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

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

Definition at line 399 of file Socket.cpp.

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

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

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

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

Definition at line 395 of file Socket.cpp.

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

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

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

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

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

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

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

00347 {
00348     u_short val;
00349     if (read ((char*) &val, sizeof(u_short)) == sizeof(u_short)) {
00350         n_ = (u_short) ntohs ((u_short)val);
00351     }
00352     else {
00353         setstate (Socket::eofbit|Socket::failbit);
00354     }
00355     return *this;
00356 }

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

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

Definition at line 332 of file Socket.cpp.

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

00333 {
00334     short val;
00335     if (read ((char*) &val, sizeof(short)) == sizeof(short)) {
00336         n_ = (short) ntohs ((short)val);
00337     }
00338     else {
00339         setstate (Socket::eofbit|Socket::failbit);
00340     }
00341     return *this;
00342 }

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

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

Definition at line 311 of file Socket.cpp.

References ignore(), and read().

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

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

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

Referenced by operator>>().

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

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(), ASSA::operator>>(), and 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 86 of file Socket.cpp.

References m_fd.

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

00087 {
00088     int val;
00089     if ((val = fcntl (m_fd, F_GETFL, 0)) < 0) {
00090         return -1;
00091     }
00092     val |= flags_;      // turn flags on
00093 
00094     return (fcntl (m_fd, F_SETFL, val) < 0) ? -1 : 0;
00095 }

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

References failbit, m_fd, and setstate().

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

00076 {
00077     int ret = setsockopt (m_fd, level_, optname_, (const char*) &val_, 
00078                   sizeof (val_));
00079     if (ret < 0)
00080         setstate (Socket::failbit);
00081     return ret;
00082 }

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 156 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.

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

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 134 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.

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

bool Socket::turnOptionOn opt_t  opt_  ) 
 

Enable socket option.

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

Definition at line 112 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(), ASSA::RemoteLogger::log_open(), and ASSA::IPv4Socket::open().

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

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 Wed Jun 21 01:42:53 2006 for libassa by  doxygen 1.4.6