ASSA::Socket Class Reference

#include <Socket.h>

Inheritance diagram for ASSA::Socket:

Inheritance graph
[legend]
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, 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 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 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 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.
Socketoperator= (const Socket &)

Detailed Description

Definition at line 71 of file Socket.h.


Member Typedef Documentation

typedef unsigned char ASSA::Socket::IOState
 

Definition at line 93 of file Socket.h.

typedef int ASSA::Socket::iostate
 

Definition at line 92 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 80 of file Socket.h.

00080                     { 
00081         goodbit = 0,  
00082         eofbit  = 1,  
00084         failbit = 2,  
00087         badbit  = 4   
00090     };

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)

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.

00099     { 
00100         reuseaddr,   
00102         rcvlowat,    
00108         sndlowat,    
00115         nonblocking  
00143     };


Constructor & Destructor Documentation

ASSA::Socket::Socket  )  [inline]
 

Constructor.

Definition at line 518 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

00519     : 
00520     m_fd (BAD_HANDLER),
00521     m_type(0),
00522 #if defined (WIN32)
00523     m_nonblocking (false),
00524 #endif
00525     m_state(Socket::badbit)
00526 {
00527     trace_with_mask("Socket::Socket",SOCKTRACE);
00528 }

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

Destructor.

Definition at line 531 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

00532 { 
00533     trace_with_mask("Socket::~Socket",SOCKTRACE); 
00534 }

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

References badbit, and m_state.

00337 { 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 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().

00572 { 
00573     m_state = is_valid_handler (m_fd) ? state_ : state_ | Socket::badbit; 
00574 }

int Socket::clear_fd_options long  flags_  )  [protected]
 

Gateway method for clearing file descriptor options.

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

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

00148 {
00149     trace_with_mask("Socket::clear_fd_options",SOCKTRACE);
00150     long oldflags;
00151     long newflags;
00152     int ret;
00153 
00154 #if defined (WIN32)
00155 
00156     u_long set_block = 0;
00157     if ((ret = ioctlsocket (m_fd, FIONBIO, &set_block)) == 0) {
00158         m_nonblocking = false;
00159         return 0;
00160     }
00161     return -1;
00162 
00163 #else
00164 
00165     if ((oldflags = ::fcntl (m_fd, F_GETFL, 0)) < 0) {
00166         return -1;
00167     }
00168     newflags = oldflags & ~flags_;  // clear flags
00169 
00170     DL ((SOCKTRACE,"Set flags fcntl(%d, %s)\n", m_fd, 
00171          decode_fcntl_flags (newflags).c_str ()));
00172 
00173     ret = ::fcntl (m_fd, F_SETFL, newflags);
00174 
00175     newflags = ::fcntl (m_fd, F_GETFL, 0);
00176     DL ((SOCKTRACE,"Flags are set to %s via fcntl(%d)\n",
00177          decode_fcntl_flags (newflags).c_str ()));
00178 
00179     return (ret);
00180 #endif
00181 }

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

Close socket.

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

Referenced by close_handler().

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

00449     {
00450 #if defined (WIN32)
00451         closesocket (socket_);
00452 #else
00453 		::close (socket_);
00454 #endif
00455         disable_handler (socket_);
00456     }

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

References ASSA::SOCKTRACE, and trace_with_mask.

00538 {
00539     trace_with_mask("Socket::connect",SOCKTRACE);
00540     return false; 
00541 }

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

00714 {
00715     string answer;
00716 
00717     if (mask_ & O_RDONLY) {
00718         answer = "O_RDONLY|";   // 000000
00719     }
00720     if (mask_ & O_WRONLY) {
00721         answer += "O_WRONLY|";  // 000001
00722     }
00723     if (mask_ & O_RDWR) {
00724         answer += "O_RDWR|";    // 000002
00725     }
00726     if (mask_ & O_APPEND) {
00727         answer += "O_APPEND|";  // 001000
00728     }
00729     if (mask_ & O_NONBLOCK) {
00730         answer += "O_NONBLOCK|";// 004000
00731     }
00732     if (mask_ & O_SYNC) {
00733         answer += "O_SYNC|";    // 010000
00734     }
00735 
00736     if (mask_ & O_ASYNC) {      // 020000
00737         answer += "O_ASYNC|";
00738     }
00739 
00740     answer.erase (answer.end () - 1);
00741 
00742     return answer;
00743 }

void Socket::dumpState  )  const
 

Write state bits of the socket to the log file.

Definition at line 653 of file Socket.cpp.

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

00654 {
00655     trace_with_mask("Socket::dumpState",SOCKTRACE);
00656 
00657     char state_set[]    = "[    set]\n";
00658     char state_not_set[] = "[not set]\n";
00659 
00660     std::ostringstream msg;
00661 
00662     msg << "\n";
00663     msg << "\tTesting good() ....... ";
00664 
00665     if (this->good ()) msg << state_set;
00666     else msg << state_not_set;
00667 
00668     msg << "\tTesting eof() ........ ";
00669     if (this->eof ()) msg << state_set;
00670     else msg << state_not_set;
00671 
00672     msg << "\tTesting fail() ....... ";
00673     if (this->fail ()) msg << state_set;
00674     else msg << state_not_set;
00675 
00676     msg << "\tTesting bad() ........ ";
00677     if (this->bad ()) msg << state_set;
00678     else msg << state_not_set;
00679 
00680     msg << "\tTesting !() .......... ";
00681     if ( !(*this) ) msg << state_set;
00682     else msg << state_not_set;
00683 
00684     msg << "\tTesting void *() ..... ";
00685     if ( *this ) msg << state_set;
00686     else msg << state_not_set;
00687 
00688     msg << "\tTesting nonblocking... ";
00689     if (getOption (nonblocking) == 1) msg << state_set;
00690     else msg << state_not_set;
00691 
00692     /*--- Terminate stream buffer ---*/
00693     msg << std::ends;
00694 
00695     DL((SOCKTRACE,"%s\n", msg.str ().c_str ()));
00696 }

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

References eofbit, and m_state.

00321 { 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 328 of file Socket.h.

References badbit, failbit, and m_state.

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

00329     { 
00330         return m_state & (Socket::failbit | Socket::badbit);
00331     }

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

00075 {
00076     if (good () && rdbuf ()) {
00077         if (rdbuf ()->pubsync () == EOF) {
00078             setstate (badbit);
00079         }
00080     }
00081     return (*this);
00082 }

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

00049 {
00050     trace_with_mask("Socket::getBytesAvail",SOCKTRACE);
00051       
00052     Socket* This = (Socket*) this;
00053     u_long ba    = 0;
00054     int ret      = 0;
00055 
00056 #if defined(WIN32)
00057     ret = ioctlsocket (m_fd, FIONREAD, &ba);
00058 #else 
00059     ret = ioctl (m_fd, FIONREAD, &ba);
00060 #endif
00061 
00062     if (ret == -1) {
00063         EL((ASSAERR,"ioctl(2) failed with ret: %d\n", ret));
00064         return ret;
00065     }
00066     ba += This->rdbuf ()->in_avail ();
00067 
00068     DL((SOCKTRACE,"%ld bytes available for reading\n", ba));
00069     return (int(ba));
00070 }

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.

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

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

00249 {
00250     trace_with_mask("Socket::getOption",SOCKTRACE);
00251     int optval = 0;
00252 
00253     if (nonblocking == opt_) 
00254     {
00255 #if defined (WIN32)
00256         return (m_nonblocking ? 1 : 0);
00257 #else
00258         if ((optval = ::fcntl (m_fd, F_GETFL, 0)) < 0) {
00259             return -1;
00260         }
00261         return ((optval & O_NONBLOCK) == O_NONBLOCK ? 1 : 0);
00262 #endif
00263     }
00264 
00265     int optname;
00266     int level = SOL_SOCKET;
00267     bool bin = false;
00268 
00269     socklen_t len = sizeof (optval);
00270     int ret;
00271 
00272     if (rcvlowat == opt_) {
00273         optname = SO_RCVLOWAT;
00274     }
00275     else if (sndlowat == opt_) {
00276         optname = SO_SNDLOWAT;
00277     }
00278     else if (reuseaddr == opt_) {
00279         optname = SO_REUSEADDR;
00280         bin = true;
00281     }
00282     else {
00283         EL((ASSAERR,"Invalid socket option\n"));
00284         return (-1);
00285     }
00286 
00287 #if defined (__CYGWIN32__) || defined (WIN32)
00288 
00289     ret = getsockopt (m_fd, level, optname, (char*) &optval, (int*)&len);
00290 
00291 #else  // posix/unix
00292 
00293     ret = getsockopt (m_fd, level, optname, (char*) &optval, &len);
00294 
00295 #endif
00296 
00297     if (ret < 0) {
00298         return (-1);
00299     }
00300     if (bin) {
00301         return (ret == 0 ? 0 : 1);
00302     }
00303     return (ret);
00304 }

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

Indicates no error on the socket.

Returns:
true if goodbit is set, false otherwise

Definition at line 315 of file Socket.h.

References m_state.

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

00315 { 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 308 of file Socket.cpp.

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

Referenced by operator>>().

00309 {
00310     trace_with_mask("Socket::ignore",SOCKTRACE);
00311     register int b;
00312     register int count = 0;
00313     register char c;
00314 
00315     if (n_ == INT_MAX && delim_ == EOF) {
00316         char buf[PGSIZE];
00317         while ((b = read (buf, PGSIZE))) {
00318             count += b;
00319         }
00320         setstate (Socket::eofbit|Socket::failbit);
00321         return count;
00322     }
00323     for (; n_; n_--, count++) {
00324         if ( (b = read (&c, 1)) == 0 ) {
00325             setstate (Socket::eofbit|Socket::failbit);
00326             break;
00327         }
00328         if ( c == delim_ )
00329             break;
00330     }
00331     return count;
00332 }

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

00701 {
00702     union {
00703         char  c [sizeof (short)];
00704         short v;
00705     } endian_u;
00706 
00707     endian_u.v = 256;
00708     return (endian_u.c [0] == 0);
00709 }

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

References fail().

00559 {
00560     return fail() ? (void *)0 : (void *)(-1); 
00561 }

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

Alias to fail().

Definition at line 564 of file Socket.h.

References fail().

00565 { 
00566     return fail(); 
00567 }

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

Manipulators plug-in operator.

Definition at line 433 of file Socket.h.

00434     {
00435         return (f (*this));
00436     }

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

00637 { 
00638     double buf, f = n_; 
00639     XDR xdrs; 
00640     xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(double), XDR_ENCODE); 
00641     xdr_double (&xdrs, &f); 
00642 
00643     int ret = write ((const char*) &buf, sizeof(double)); 
00644     xdr_destroy (&xdrs); 
00645     if ( ret != sizeof(double) ) { 
00646         setstate (Socket::eofbit|Socket::failbit); 
00647     } 
00648     return *this; 
00649 }

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

00620 { 
00621     float buf, f = n_; 
00622     XDR xdrs; 
00623     xdrmem_create (&xdrs, (caddr_t) &buf, sizeof(float), XDR_ENCODE); 
00624     xdr_float (&xdrs, &f); 
00625 
00626     int ret = write ((const char*) &buf, sizeof(float)); 
00627     xdr_destroy (&xdrs); 
00628     if ( ret != sizeof(float) ) { 
00629         setstate (Socket::eofbit|Socket::failbit); 
00630     } 
00631     return *this; 
00632 }

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

00578 { 
00579     u_short val = (u_short) htons((u_short)n_);
00580 
00581     if (write ((const char*) &val, sizeof(u_short)) != sizeof(u_short)) 
00582     {
00583         setstate (Socket::eofbit|Socket::failbit);
00584     }
00585     return *this;
00586 }

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

00566 { 
00567     short val = (short) htons((short)n_);
00568 
00569     if (write ((const char*) &val, sizeof(short)) != sizeof(short)) 
00570     {
00571         setstate (Socket::eofbit|Socket::failbit);
00572     }
00573     return *this;
00574 }

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

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

00547 { 
00548     static const char pad [4] = { 0, 0, 0, 0 };
00549 
00550     (*this) << s_.length ();
00551     int ret = write (s_.c_str (), s_.length ());
00552     if ( ret != s_.length () ) { 
00553         setstate (Socket::eofbit|Socket::failbit); 
00554     } 
00555     size_t r = 4 - s_.length() % 4;
00556     if (r) {
00557         if (write (pad, r) != r) {
00558             setstate (Socket::eofbit|Socket::failbit); 
00559         }
00560     }
00561     return *this;
00562 }

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

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

Definition at line 400 of file Socket.h.

00401     { 
00402         return (*this) << (char) c_; 
00403     }

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

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

Definition at line 394 of file Socket.h.

00395     { 
00396         return (*this) << (char) c_; 
00397     }

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

00509 { 
00510     /* See comment to operator>>(char n_) */
00511 
00512     int buf = 0;
00513     int len = sizeof (int);
00514     XDR xdrs; 
00515 
00516     xdrmem_create (&xdrs, (caddr_t) &buf, len, XDR_ENCODE); 
00517     xdr_char (&xdrs, &n_); 
00518 
00519     if (write ((const char*) &buf, len) != len) {
00520         (Socket::eofbit|Socket::failbit); 
00521     }
00522     xdr_destroy (&xdrs); 
00523     return *this; 
00524 }

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

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

00488 {
00489     double val = 0; 
00490     XDR xdrs; 
00491     xdrmem_create (&xdrs, (caddr_t) &val, sizeof(double), XDR_DECODE); 
00492     if (read ((char*) &val, sizeof(double)) == sizeof(double)) { 
00493         xdr_double (&xdrs, &n_); 
00494     } 
00495     else { 
00496         setstate (Socket::eofbit|Socket::failbit); 
00497     } 
00498     xdr_destroy (&xdrs); 
00499     return *this; 
00500 }

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

00470 {
00471     float val; 
00472     XDR xdrs; 
00473     xdrmem_create (&xdrs, (caddr_t) &val, sizeof(float), XDR_DECODE); 
00474 
00475     if (read ((char*) &val, sizeof(float)) == sizeof(float)) { 
00476         xdr_float (&xdrs, &n_); 
00477     } 
00478     else { 
00479         setstate (Socket::eofbit|Socket::failbit); 
00480     } 
00481     xdr_destroy (&xdrs); 
00482     return *this; 
00483 }

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

00417 {
00418     u_short val;
00419     if (read ((char*) &val, sizeof(u_short)) == sizeof(u_short)) {
00420         n_ = (u_short) ntohs ((u_short)val);
00421     }
00422     else {
00423         setstate (Socket::eofbit|Socket::failbit);
00424     }
00425     return *this;
00426 }

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

00403 {
00404     short val;
00405     if (read ((char*) &val, sizeof(short)) == sizeof(short)) {
00406         n_ = (short) ntohs ((short)val);
00407     }
00408     else {
00409         setstate (Socket::eofbit|Socket::failbit);
00410     }
00411     return *this;
00412 }

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

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

Definition at line 381 of file Socket.cpp.

References ignore(), and read().

00382 {
00383     char c = 0;
00384     size_t n = 0;
00385     s_ = "";
00386 
00387     (*this) >> n;
00388 
00389     if (n == 0) {
00390         return *this;
00391     }
00392     size_t len = n;
00393     while (len-- && read (&c, 1) == 1) {
00394         s_ += c;
00395     }
00396     ignore (4 - n % 4);
00397     return *this; 
00398 }

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

00359     { 
00360         return operator>>((char&) c_); 
00361     }

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

00353     { 
00354         return operator>>((char&) c_); 
00355     }

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

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

Referenced by operator>>().

00360 {
00361 
00362     int c = 0;
00363     int len = sizeof (int);
00364     XDR xdrs; 
00365     xdrmem_create (&xdrs, (caddr_t) &c, len, XDR_DECODE); 
00366 
00367     if (read ((char* ) &c, len) == len) { 
00368         xdr_char (&xdrs, &n_); 
00369     } 
00370     else { 
00371         setstate (Socket::eofbit|Socket::failbit); 
00372     } 
00373     xdr_destroy(&xdrs); 
00374     return *this; 
00375 }

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

00247 { 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 240 of file Socket.h.

Referenced by flush(), and getBytesAvail().

00240 { return 0; }

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

Retrieve state of the socket.

Returns:
control state of the socket

Definition at line 302 of file Socket.h.

References m_state.

00302 { return m_state; }

int ASSA::Socket::read char *  buf_,
const u_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 551 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

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

00552 { 
00553     trace_with_mask("Socket::read()",SOCKTRACE);
00554     return -1; 
00555 }

int Socket::set_fd_options long  flags_  )  [protected]
 

Gateway method for setting file descriptor options.

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

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

00105 {
00106     trace_with_mask("Socket::set_fd_options",SOCKTRACE);
00107     int val;
00108     int ret;
00109 
00110 #if defined (WIN32)
00111 
00112     u_long set_nonblock = 1;
00113     if ((val = ioctlsocket (m_fd, FIONBIO, &set_nonblock)) == 0) {
00114         m_nonblocking = true;
00115         return 0;
00116     }
00117     return -1;
00118 
00119 #else  // POSIX/UNIX
00120 
00121     if ((val = ::fcntl (m_fd, F_GETFL, 0)) < 0) {
00122         return -1;
00123     }
00124     val |= flags_;      // turn flags on
00125 
00126     DL ((SOCKTRACE,"Set flags fcntl(%d, %s)\n", m_fd, 
00127          decode_fcntl_flags (val).c_str ()));
00128 
00129     ret = ::fcntl (m_fd, F_SETFL, val);
00130 
00131     val = ::fcntl (m_fd, F_GETFL, 0);
00132     DL ((SOCKTRACE,"Flags are set to %s via fcntl(25)\n",
00133          decode_fcntl_flags (val).c_str ()));
00134 
00135     return (ret);
00136 
00137 #endif
00138 }

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

References failbit, m_fd, and setstate().

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

00087 {
00088     int ret = setsockopt (m_fd, 
00089                           level_, 
00090                           optname_, 
00091                           (const char*) &val_, 
00092                           sizeof (val_));
00093     if (ret < 0) {
00094         setstate (Socket::failbit);
00095     }
00096     return ret;
00097 }

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

00224 {
00225     trace_with_mask("Socket::setOption(,)",SOCKTRACE);
00226     int optname;
00227 
00228     if (nonblocking == opt_) {
00229         return (arg_ == 1) ? set_fd_options (O_NONBLOCK)
00230             : clear_fd_options (O_NONBLOCK);
00231     }
00232 
00233     if (rcvlowat == opt_) {
00234         optname = SO_RCVLOWAT;
00235     }
00236     else if (sndlowat == opt_) {
00237         optname = SO_SNDLOWAT;
00238     }
00239     else {
00240         EL((ASSAERR,"Invalid socket option\n"));
00241         return false;
00242     }
00243     return set_option (SOL_SOCKET, optname, arg_) == 0;
00244 }

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

00578 { 
00579     m_state |= flag_; 
00580 }

bool Socket::turnOptionOff opt_t  opt_  ) 
 

Disable socket option.

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

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

00205 {
00206     trace_with_mask("Socket::turnOptionOff",SOCKTRACE);
00207 
00208     if (nonblocking == opt_) 
00209         return clear_fd_options (O_NONBLOCK);
00210 
00211     int optname;
00212     if (reuseaddr == opt_) 
00213         optname = SO_REUSEADDR;
00214     else {
00215         EL((ASSAERR,"Invalid socket option\n"));
00216         return false;
00217     }
00218     return set_option (SOL_SOCKET, optname, 0) == 0;
00219 }

bool Socket::turnOptionOn opt_t  opt_  ) 
 

Enable socket option.

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

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

00186 {
00187     trace_with_mask("Socket::turnOptionOn",SOCKTRACE);
00188 
00189     if (nonblocking == opt_) 
00190         return set_fd_options (O_NONBLOCK);
00191 
00192     int optname;
00193     if (reuseaddr == opt_) 
00194         optname = SO_REUSEADDR;
00195     else {
00196         EL((ASSAERR,"Invalid socket option\n"));
00197         return false;
00198     }
00199     return set_option (SOL_SOCKET, optname, 1) == 0;
00200 }

int ASSA::Socket::write const char *  buf_,
const u_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 544 of file Socket.h.

References ASSA::SOCKTRACE, and trace_with_mask.

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

00545 { 
00546     trace_with_mask("Socket::write",SOCKTRACE); 
00547     return -1; 
00548 }

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

00344     { 
00345         return (4 + s_.length () + s_.length () % 4);
00346     }


Member Data Documentation

handler_t ASSA::Socket::m_fd [protected]
 

File descriptor.

Definition at line 485 of file Socket.h.

Referenced by ASSA::UDPSocket::bind(), clear(), clear_fd_options(), ASSA::UDPSocket::close(), ASSA::IPv4Socket::close(), getBytesAvail(), ASSA::UDPSocket::getHandler(), ASSA::IPv4Socket::getHandler(), getOption(), ASSA::IPv4Socket::IPv4Socket(), ASSA::UDPSocket::open(), ASSA::IPv4Socket::open(), set_fd_options(), set_option(), ASSA::UDPSocket::setHandler(), and ASSA::UDPSocket::UDPSocket().

IOState ASSA::Socket::m_state [protected]
 

Control state of the socket.

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

Size of bytes of a kernel page.

Definition at line 74 of file Socket.h.

Referenced by ignore().


The documentation for this class was generated from the following files:
Generated on Sun Aug 13 15:08:21 2006 for libassa by  doxygen 1.4.6