Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Examples  

vpr::SocketImplBSD Class Reference

Basic socket wrapper implementation for BSD sockets. More...

#include <SocketImplBSD.h>

Inheritance diagram for vpr::SocketImplBSD:

Inheritance graph
[legend]
Collaboration diagram for vpr::SocketImplBSD:

Collaboration graph
[legend]
List of all members.

Public Methods

const std::string & getName () const
 Gets the "name" of this socket. More...

vpr::ReturnStatus open ()
 Opens the socket. More...

vpr::ReturnStatus close ()
 Close the socket. More...

bool isOpen () const
 Gets the open state of this socket. More...

bool isBound () const
vpr::ReturnStatus bind ()
 Binds this socket to the address in the host address member variable. More...

vpr::IOSys::Handle getHandle () const
 Returns the contained handle. More...

bool isBlockingFixed () const
 Queries if the blocking state for this socket is fixed and can no longer be changed. More...

vpr::ReturnStatus setBlocking (bool blocking)
 Reconfigures the socket so that it is in blocking or non-blocking mode. More...

bool isBlocking () const
 Gets the current blocking state for the socket. More...

vpr::ReturnStatus connect (vpr::Interval timeout=vpr::Interval::NoTimeout)
 Connects the socket on the client side to the server side. More...

bool isConnected () const
 Gets the status of a possibly connected socket. More...

const vpr::SocketTypes::TypegetType () const
 Gets the type of this socket (e.g., vpr::SocketTypes::STREAM). More...

const vpr::InetAddrgetLocalAddr () const
 Gets the type of this socket (e.g., vpr::SocketTypes::STREAM). More...

vpr::ReturnStatus setLocalAddr (const vpr::InetAddr &addr)
 Changes the local address for this socket. More...

const vpr::InetAddrgetRemoteAddr () const
 Returns the remote address for this socket. More...

vpr::ReturnStatus setRemoteAddr (const vpr::InetAddr &addr)
 Changes the remote address for this socket. More...

vpr::ReturnStatus read_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytes_read, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the read template method. More...

vpr::ReturnStatus readn_i (void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytes_read, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the readn template method. More...

vpr::ReturnStatus write_i (const void *buffer, const vpr::Uint32 length, vpr::Uint32 &bytes_written, const vpr::Interval timeout=vpr::Interval::NoTimeout)
 Implementation of the write template method. More...

vpr::Uint32 availableBytes () const
vpr::ReturnStatus getOption (const vpr::SocketOptions::Types option, struct vpr::SocketOptions::Data &data) const
 Retrieves the value for the given option as set on the socket. More...

vpr::ReturnStatus setOption (const vpr::SocketOptions::Types option, const struct vpr::SocketOptions::Data &data)
 Do nothing. More...

 ~SocketImplBSD ()
 Destructor. More...


Protected Methods

 SocketImplBSD (const vpr::SocketTypes::Type sock_type)
 Default constructor. More...

 SocketImplBSD (const vpr::InetAddr &local_addr, const vpr::InetAddr &remote_addr, const vpr::SocketTypes::Type sock_type)
 Standard constructor. More...


Protected Attributes

bool mOpenBlocking
 Used for working around socket(2) semantics. More...

bool mBound
bool mConnected
bool mBlockingFixed
vpr::FileHandleImplUNIXmHandle
 The OS handle for this socket. More...

vpr::InetAddr mLocalAddr
 The local site's address structure. More...

vpr::InetAddr mRemoteAddr
 The remote site's address structure. More...

vpr::SocketTypes::Type mType
 The type for this socket. More...


Detailed Description

Basic socket wrapper implementation for BSD sockets.

Definition at line 63 of file SocketImplBSD.h.


Constructor & Destructor Documentation

vpr::SocketImplBSD::~SocketImplBSD  
 

Destructor.

This releases the memory allocated for mHandle (if it is non-NULL).

Precondition:
If mHandle is NULL, its memory has already been deleted.
Postcondition:
The memory for mHandle is deleted.

Definition at line 767 of file SocketImplBSD.cpp.

References mHandle.

00768 {
00769    if ( mHandle != NULL )
00770    {
00771       delete mHandle;
00772       mHandle = NULL;
00773    }
00774 }

vpr::SocketImplBSD::SocketImplBSD const vpr::SocketTypes::Type    sock_type [protected]
 

Default constructor.

This just initializes member variables to reasonable defaults.

Postcondition:
The member variables are initialized accordingly to reasonable defaults.

Definition at line 780 of file SocketImplBSD.cpp.

References mHandle.

00781    : mOpenBlocking(true)
00782    , mBound(false)
00783    , mConnected(false)
00784    , mBlockingFixed(false)
00785    , mHandle(NULL)
00786    , mType(sock_type)
00787 {
00788    mHandle = new FileHandleImplUNIX();
00789 }

vpr::SocketImplBSD::SocketImplBSD const vpr::InetAddr   local_addr,
const vpr::InetAddr   remote_addr,
const vpr::SocketTypes::Type    sock_type
[protected]
 

Standard constructor.

This takes two vpr::InetAddr objects, a local address and a remote address.

Postcondition:
The member variables are initialized with the given values.
Parameters:
local_addr  The local address for the socket.
remote_addr  The remote address for the socket.
sock_type  The type for this socket (stream, datagram, etc.).

Definition at line 791 of file SocketImplBSD.cpp.

References vpr::InetAddrBSD::getAddressString, and mHandle.

00794    : mOpenBlocking(true)
00795    , mBound(false)
00796    , mConnected(false)
00797    , mBlockingFixed(false)
00798    , mHandle(NULL)
00799    , mLocalAddr(local_addr)
00800    , mRemoteAddr(remote_addr)
00801    , mType(sock_type)
00802 {
00803    mHandle = new FileHandleImplUNIX(remote_addr.getAddressString());
00804 }


Member Function Documentation

const std::string & vpr::SocketImplBSD::getName   const
 

Gets the "name" of this socket.

It is typically the address of the peer host.

Precondition:
None.
Returns:
An object containing the "name" of this socket.

Definition at line 66 of file SocketImplBSD.cpp.

References vpr::FileHandleImplUNIX::getName, and mHandle.

Referenced by open.

00067 {
00068    return mHandle->getName();
00069 }

vpr::ReturnStatus vpr::SocketImplBSD::open  
 

Opens the socket.

This creates a new socket using the domain and type options set through member variables.

Precondition:
mDomain and mType have been set to values recognized by the socket(2) system call.
Postcondition:
A new socket is created with its file handle stored in the mFdesc member variable.
Returns:
vpr::ReturnStatus::Succeed is returned if the socket was opened successfully.
vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 73 of file SocketImplBSD.cpp.

References errno, vpr::ReturnStatus::Fail, vpr::InetAddrBSD::getAddressString, vpr::InetAddrBSD::getFamily, getName, vpr::FileHandleImplUNIX::mFdesc, mHandle, mLocalAddr, vpr::FileHandleImplUNIX::mOpen, mOpenBlocking, mRemoteAddr, vpr::FileHandleImplUNIX::setBlocking, and vpr::ReturnStatus::setCode.

00074 {
00075    int domain, type, sock;
00076    vpr::ReturnStatus retval;
00077 
00078    switch ( mLocalAddr.getFamily() )
00079    {
00080       case SocketTypes::LOCAL:
00081 #ifdef PF_LOCAL
00082          domain = PF_LOCAL;
00083 #else
00084          domain = PF_UNIX;
00085 #endif
00086          break;
00087       case SocketTypes::INET:
00088          domain = PF_INET;
00089          break;
00090       case SocketTypes::INET6:
00091 #ifdef PF_INET6
00092          domain = PF_INET6;
00093 #else
00094          fprintf(stderr,
00095                  "[vpr::SocketImplBSD] WARNING: IPv6 not supported on this host!\n");
00096 #endif
00097          break;
00098       case SocketTypes::LINK:
00099 #if defined(PF_LINK)
00100          domain = PF_LINK;
00101 #elif defined(PF_RAW)
00102          domain = PF_RAW;
00103 #else
00104          fprintf(stderr,
00105                  "[vpr::SocketImplBSD] WARNING: Cannot use LINK sockets. domain %d\n",
00106                  mLocalAddr.getFamily());
00107 #endif
00108          break;
00109       default:
00110          domain = -1;
00111          fprintf(stderr,
00112                  "[vpr::SocketImplBSD] ERROR: Unknown socket domain value %d\n",
00113                  mLocalAddr.getFamily());
00114          break;
00115    }
00116 
00117    switch ( mType )
00118    {
00119       case SocketTypes::STREAM:
00120          type = SOCK_STREAM;
00121          break;
00122       case SocketTypes::DATAGRAM:
00123          type = SOCK_DGRAM;
00124          break;
00125       case SocketTypes::RAW:
00126          type = SOCK_RAW;
00127          break;
00128       default:
00129          type = -1;
00130          fprintf(stderr,
00131                  "[vpr::SocketImplBSD] ERROR: Unknown socket type value %d\n",
00132                  mLocalAddr.getFamily());
00133          break;
00134    }
00135 
00136    // Attempt to create a new socket using the values in mLocalAddr and
00137    // mType.
00138    sock = socket(domain, type, 0);
00139 
00140    // If socket(2) failed, print an error message and return error status.
00141    if ( sock == -1 )
00142    {
00143       fprintf(stderr,
00144               "[vpr::SocketImplBSD] Could not create socket (%s): %s\n",
00145               getName().c_str(), strerror(errno));
00146       retval.setCode(vpr::ReturnStatus::Fail);
00147    }
00148    // Otherwise, return success.
00149    else
00150    {
00151       if ( mHandle == NULL )
00152       {
00153          mHandle = new FileHandleImplUNIX(mRemoteAddr.getAddressString());
00154       }
00155 
00156       mHandle->mFdesc = sock;
00157       mHandle->mOpen  = true;
00158 
00159       // Since socket(2) cannot open a socket in non-blocking mode, we call
00160       // vpr::FileHandleUNIX::setBlocking() directly.
00161       retval = mHandle->setBlocking(mOpenBlocking);
00162    }
00163 
00164    return retval;
00165 }

vpr::ReturnStatus vpr::SocketImplBSD::close  
 

Close the socket.

Precondition:
The socket is open.
Postcondition:
An attempt is made to close the socket. The resulting status is returned to the caller.
Returns:
vpr::ReturnStatus::Succeed is returned if the socket was closed successfully; vpr::ReturnStatus::Fail otherwise.

Definition at line 167 of file SocketImplBSD.cpp.

References vpr::FileHandleImplUNIX::close, and mHandle.

00168 {
00169    return mHandle->close();
00170 }

bool vpr::SocketImplBSD::isOpen   const [inline]
 

Gets the open state of this socket.

Precondition:
None.
Postcondition:
The boolean value giving the open state is returned to the caller.
Returns:
true is returned if this socket is open; false otherwise.

Definition at line 116 of file SocketImplBSD.h.

References vpr::FileHandleImplUNIX::isOpen, and mHandle.

Referenced by isConnected, and setBlocking.

00117    {
00118       return mHandle->isOpen();
00119    }

bool vpr::SocketImplBSD::isBound   const [inline]
 

Definition at line 121 of file SocketImplBSD.h.

References mBound.

00122    {
00123       return mBound;
00124    }

vpr::ReturnStatus vpr::SocketImplBSD::bind  
 

Binds this socket to the address in the host address member variable.

Precondition:
The socket is open, and mLocalAddr has been initialized properly.
Postcondition:
The socket is bound to the address in mLocalAddr.
Returns:
vpr::ReturnStatus::Sucess is returned if the socket was bound to the address successfully. vpr::ReturnStatus::Fail is returned if the socket could not be bound to the address in mLocalAddr.

Definition at line 198 of file SocketImplBSD.cpp.

References errno, vpr::ReturnStatus::Fail, mBound, vpr::FileHandleImplUNIX::mFdesc, mHandle, mLocalAddr, and vpr::ReturnStatus::setCode.

00199 {
00200    vpr::ReturnStatus retval;
00201    int status;
00202 
00203    // Bind the socket to the address in mLocalAddr.
00204    status = ::bind(mHandle->mFdesc, (struct sockaddr*) &mLocalAddr.mAddr,
00205                    mLocalAddr.size());
00206 
00207    // If that fails, print an error and return error status.
00208    if ( status == -1 )
00209    {
00210       fprintf(stderr,
00211               "[vpr::SocketImplBSD] Cannot bind socket to address: %s\n",
00212               strerror(errno));
00213       retval.setCode(vpr::ReturnStatus::Fail);
00214    }
00215    else
00216    {
00217       mBound = true;
00218    }
00219 
00220    return retval;
00221 }

vpr::IOSys::Handle vpr::SocketImplBSD::getHandle   const [inline]
 

Returns the contained handle.

Definition at line 140 of file SocketImplBSD.h.

References vpr::FileHandleImplUNIX::getHandle, and mHandle.

00141    {
00142       return mHandle->getHandle();
00143    }

bool vpr::SocketImplBSD::isBlockingFixed   const [inline]
 

Queries if the blocking state for this socket is fixed and can no longer be changed.

Definition at line 149 of file SocketImplBSD.h.

References mBlockingFixed.

00150    {
00151       return mBlockingFixed;
00152    }

vpr::ReturnStatus vpr::SocketImplBSD::setBlocking bool    blocking
 

Reconfigures the socket so that it is in blocking or non-blocking mode.

If this socket has not been opened yet, it will be opened in blocking or non-blocking mode appropriately when open() is called.

Postcondition:
Processes will block (or not) when accessing the socket.
Parameters:
blocking  A value of true indicates that the socket will be configured to use blocking I/O. A value of false indicates that it will use non-blocking I/O.
Returns:
vpr::ReturnStatus::Succeed is returned if the blocking mode was changed successfully; vpr::ReturnStatus::Fail otherwise.

Definition at line 173 of file SocketImplBSD.cpp.

References vpr::ReturnStatus::Fail, isOpen, mBlockingFixed, mHandle, mOpenBlocking, vpr::FileHandleImplUNIX::setBlocking, vpr::ReturnStatus::setCode, and vprASSERT.

00174 {
00175    vpr::ReturnStatus status;
00176 
00177    vprASSERT(! mBlockingFixed &&
00178              "Cannot change blocking state after a blocking call!");
00179 
00180    if ( ! mBlockingFixed )
00181    {
00182       status = mHandle->setBlocking(blocking);
00183 
00184       if ( ! isOpen() )
00185       {
00186          mOpenBlocking = blocking;
00187       }
00188    }
00189    else
00190    {
00191       status.setCode(vpr::ReturnStatus::Fail);
00192    }
00193 
00194    return status;
00195 }

bool vpr::SocketImplBSD::isBlocking   const [inline]
 

Gets the current blocking state for the socket.

Returns:
true is returned if the socket is in blocking mode. Otherwise, false is returned.

Definition at line 176 of file SocketImplBSD.h.

References vpr::FileHandleImplUNIX::isBlocking, and mHandle.

Referenced by vpr::SocketStreamImplBSD::accept, connect, vpr::SocketDatagramImplBSD::recvfrom, and vpr::SocketDatagramImplBSD::sendto.

00177    {
00178       return mHandle->isBlocking();
00179    }

vpr::ReturnStatus vpr::SocketImplBSD::connect vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Connects the socket on the client side to the server side.

For a datagram socket, this makes the address given to the constructor the default destination for all packets. For a stream socket, this has the effect of establishing a connection with the destination.

Precondition:
The socket is open.
Postcondition:
The socket is connected to the address in mLocalAddr. For a stream socket, this means that a connection for future communication has been established. For a datagram socket, the default destination for all packets is now mLocalAddr.
Returns:
vpr::ReturnStatus::Succeed is returned if the connection succeeded.
vpr::ReturnStatus::InProgress is returned if this is a non-blocking socket and the connection is still in progress. The connection will be completed "in the background".
vpr::ReturnStatus::Timeout is returned if the connection could not be made within the given timeout interval.
vpr::ReturnStatus::Fail is returned if the connection could not be made.

Definition at line 227 of file SocketImplBSD.cpp.

References errno, vpr::ReturnStatus::Fail, vpr::InetAddrBSD::getAddressString, vpr::ReturnStatus::InProgress, isBlocking, vpr::FileHandleImplUNIX::isWriteable, mBlockingFixed, mBound, mConnected, vpr::FileHandleImplUNIX::mFdesc, mHandle, mLocalAddr, mRemoteAddr, vpr::Interval::NoWait, vpr::ReturnStatus::setCode, vpr::InetAddrBSD::setSockaddr, vpr::ReturnStatus::success, vprDBG_ALL, vprDBG_STATE_LVL, vprDBG_WARNING_LVL, vprDEBUG, and vprDEBUG_FLUSH.

00228 {
00229    vpr::ReturnStatus retval;
00230    int status;
00231 
00232    // Attempt to connect to the address in mAddr.
00233    status = ::connect(mHandle->mFdesc,
00234                       (struct sockaddr*) &mRemoteAddr.mAddr,
00235                       mRemoteAddr.size());
00236 
00237    // If connect(2) failed, print an error message explaining why and return
00238    // error status.
00239    if ( status == -1 )
00240    {
00241       // If this is a non-blocking connection, return
00242       // vpr::ReturnStatus::InProgress to indicate that the connection will
00243       // complete later.  I'm not sure if it's safe to set mConnected and
00244       // mBlockingFixed at this point, but they have to be set sometime.
00245       if ( errno == EINPROGRESS && ! isBlocking() )
00246       {
00247          if ( vpr::Interval::NoWait == timeout )
00248          {
00249             retval.setCode(vpr::ReturnStatus::InProgress);
00250          }
00251          // If we have a timeout value, wait for at most the duration of
00252          // that interval.  If we time out, tell the caller that the
00253          // connection is still in progress.
00254          else if ( ! mHandle->isWriteable(timeout).success() )
00255          {
00256             retval.setCode(vpr::ReturnStatus::InProgress);
00257          }
00258 
00259          mBound          = true;
00260          mConnected      = true;
00261          mBlockingFixed = true;
00262       }
00263       else
00264       {
00265 //         fprintf(stderr, "[vpr::SocketImplBSD] Error connecting to %s: %s\n",
00266 //                 mRemoteAddr.getAddressString().c_str(), strerror(errno));
00267          vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL) << "[vpr::SocketImplBSD] Error connecting to "
00268             << mRemoteAddr.getAddressString() << ": " << strerror(errno) << std::endl << vprDEBUG_FLUSH;
00269          retval.setCode(vpr::ReturnStatus::Fail);
00270       }
00271    }
00272    // Otherwise, return success.
00273    else
00274    {
00275       mBound          = true;
00276       mConnected      = true;
00277       mBlockingFixed = true;
00278    }
00279 
00280    // Fill in the local address if has not already been assigned.
00281    if ( mConnected && vpr::InetAddr::AnyAddr == mLocalAddr )
00282    {
00283       int status;
00284 #if defined(VPR_OS_IRIX) || defined(VPR_OS_HPUX)
00285       int namelen;
00286 #else
00287       socklen_t namelen;
00288 #endif
00289       struct sockaddr temp_addr;
00290 
00291       namelen = sizeof(struct sockaddr);
00292       status  = getsockname(mHandle->mFdesc, &temp_addr, &namelen);
00293 
00294       if ( status == 0 )
00295       {
00296          mLocalAddr.setSockaddr(&temp_addr);
00297 /* XXX: This doesn't compile on IRIX, and I don't know why.
00298          vprDEBUG(vprDBG_ALL, vprDBG_STATE_LVL) << "Connected, local address is "
00299                                           << mLocalAddr << std::endl
00300                                           << vprDEBUG_FLUSH;
00301 */
00302       }
00303       else
00304       {
00305          vprDEBUG(vprDBG_ALL, vprDBG_WARNING_LVL) << "Failed to get sock name: "
00306                                                   << strerror(errno)
00307                                                   << std::endl
00308                                                   << vprDEBUG_FLUSH;
00309       }
00310    }
00311 
00312    return retval;
00313 }

bool vpr::SocketImplBSD::isConnected   const
 

Gets the status of a possibly connected socket.

Precondition:
This socket is opened and has been connected to a peer.
Returns:
true is returned if this socket is still connected.
false is returned if this socket is not currently connected.

Definition at line 315 of file SocketImplBSD.cpp.

References vpr::FileHandleImplUNIX::getReadBufferSize, isOpen, vpr::FileHandleImplUNIX::isReadable, mConnected, mHandle, vpr::Interval::NoWait, and vpr::ReturnStatus::success.

00316 {
00317    bool connected(false);
00318 
00319    if ( isOpen() && mConnected )
00320    {
00321       vpr::ReturnStatus status;
00322       vpr::Int32 bytes;
00323 
00324       status = mHandle->getReadBufferSize(bytes);
00325 
00326       if ( status.success() )
00327       {
00328          if ( bytes == 0 )
00329          {
00330             // If there are no bytes to read but the OS tells us that the
00331             // socket is readable, then something is wrong.
00332             if ( ! mHandle->isReadable(vpr::Interval::NoWait).success() )
00333             {
00334                connected = true;
00335             }
00336          }
00337          else
00338          {
00339             connected = true;
00340          }
00341       }
00342    }
00343 
00344    return connected;
00345 }

const vpr::SocketTypes::Type& vpr::SocketImplBSD::getType   const [inline]
 

Gets the type of this socket (e.g., vpr::SocketTypes::STREAM).

Precondition:
The socket implementation pointer is valid.
Postcondition:
The socket type for this socket is returned to the caller.
Returns:
A vpr::SocketTypes::Type value giving the socket type for this socket.

Definition at line 228 of file SocketImplBSD.h.

References mType.

00229    {
00230       return mType;
00231    }

const vpr::InetAddr& vpr::SocketImplBSD::getLocalAddr   const [inline]
 

Gets the type of this socket (e.g., vpr::SocketTypes::STREAM).

Precondition:
The socket implementation pointer is valid.
Postcondition:
The socket type for this socket is returned to the caller.
Returns:
A vpr::SocketTypes::Type value giving the socket type for this socket.

Definition at line 242 of file SocketImplBSD.h.

References mLocalAddr.

00243    {
00244       return mLocalAddr;
00245    }

vpr::ReturnStatus vpr::SocketImplBSD::setLocalAddr const vpr::InetAddr   addr
 

Changes the local address for this socket.

Precondition:
This socket is not already bound.
Postcondition:
mLocalAddr is updated to use the given vpr::InetAddr object.

Definition at line 347 of file SocketImplBSD.cpp.

References vpr::ReturnStatus::Fail, vpr::InetAddr, mBound, mConnected, mLocalAddr, vpr::ReturnStatus::setCode, vprDBG_ALL, vprDBG_CRITICAL_LVL, vprDEBUG, and vprDEBUG_FLUSH.

00348 {
00349    vpr::ReturnStatus status;
00350 
00351    if ( mBound || mConnected )
00352    {
00353       vprDEBUG(vprDBG_ALL, vprDBG_CRITICAL_LVL)
00354          << "SocketImplBSD::setLocalAddr: Can't set address of a "
00355          << "bound or connected socket.\n" << vprDEBUG_FLUSH;
00356       status.setCode(vpr::ReturnStatus::Fail);
00357    }
00358    else
00359    {
00360       mLocalAddr = addr;
00361    }
00362 
00363    return status;
00364 }

const vpr::InetAddr& vpr::SocketImplBSD::getRemoteAddr   const [inline]
 

Returns the remote address for this socket.

This is typically the address to which this socket is connected.

Definition at line 259 of file SocketImplBSD.h.

References mRemoteAddr.

00260    {
00261       return mRemoteAddr;
00262    }

vpr::ReturnStatus vpr::SocketImplBSD::setRemoteAddr const vpr::InetAddr   addr
 

Changes the remote address for this socket.

Precondition:
This socket is not already connected.
Postcondition:
mLocalAddr is updated to use the given vpr::InetAddr object.

Definition at line 366 of file SocketImplBSD.cpp.

References vpr::ReturnStatus::Fail, vpr::InetAddr, mRemoteAddr, and vpr::ReturnStatus::setCode.

00367 {
00368    vpr::ReturnStatus status;
00369 
00370    if ( mConnected )
00371    {
00372       status.setCode(vpr::ReturnStatus::Fail);
00373    }
00374    else
00375    {
00376       mRemoteAddr = addr;
00377    }
00378 
00379    return status;
00380 }

vpr::ReturnStatus vpr::SocketImplBSD::read_i void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   bytes_read,
const vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Implementation of the read template method.

This reads at most the specified number of bytes from the socket into the given buffer.

Precondition:
The device is open for reading, and the buffer is at least length bytes long.
Postcondition:
The given buffer has length bytes copied into it from the device, and the number of bytes read successfully is returned to the caller via the bytes_read parameter.
Parameters:
buffer  A pointer to the buffer where the device's buffer contents are to be stored.
length  The number of bytes to be read.
bytes_read  The number of bytes read into the buffer.
timeout  The maximum amount of time to wait for data to be available for reading. This argument is optional and defaults to vpr::Interval::NoTimeout.
Returns:
vpr::ReturnStatus::Succeed is returned if the read operation completed successfully.
vpr::ReturnStatus::WouldBlock if the device is in non-blocking mode, and there is no data to read.
vpr::ReturnStatus::Timeout is returned if the read could not begin within the timeout interval.
vpr::ReturnStatus::Fail is returned if the read operation failed.

Definition at line 382 of file SocketImplBSD.cpp.

References mBlockingFixed, mHandle, vpr::ReturnStatus::NotConnected, vpr::FileHandleImplUNIX::read_i, and vpr::ReturnStatus::setCode.

00386 {
00387    vpr::ReturnStatus status;
00388    mBlockingFixed = true;
00389    status = mHandle->read_i(buffer, length, bytes_read, timeout);
00390 
00391    if ( bytes_read == 0 )
00392    {
00393       status.setCode(vpr::ReturnStatus::NotConnected);
00394    }
00395 
00396    return status;
00397 }

vpr::ReturnStatus vpr::SocketImplBSD::readn_i void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   bytes_read,
const vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Implementation of the readn template method.

This reads exactly the specified number of bytes from the socket into the given buffer.

Precondition:
The device is open for reading, and the buffer is at least length bytes long.
Postcondition:
The given buffer has length bytes copied into it from the device, and the number of bytes read successfully is returned to the caller via the bytes_read parameter.
Parameters:
buffer  A pointer to the buffer where the device's buffer contents are to be stored.
length  The number of bytes to be read.
bytes_read  The number of bytes read into the buffer.
timeout  The maximum amount of time to wait for data to be available for reading. This argument is optional and defaults to vpr::Interval::NoTimeout
Returns:
vpr::ReturnStatus::Succeed is returned if the read operation completed successfully.
vpr::ReturnStatus::WouldBlock if the device is in non-blocking mode, and there is no data to read.
vpr::ReturnStatus::Timeout is returned if the read could not begin within the timeout interval.
vpr::ReturnStatus::Fail is returned if the read operation failed.

Definition at line 399 of file SocketImplBSD.cpp.

References mBlockingFixed, mHandle, vpr::ReturnStatus::NotConnected, vpr::FileHandleImplUNIX::readn_i, and vpr::ReturnStatus::setCode.

00403 {
00404    vpr::ReturnStatus status;
00405    mBlockingFixed = true;
00406    status = mHandle->readn_i(buffer, length, bytes_read, timeout);
00407 
00408    if ( bytes_read == 0 )
00409    {
00410       status.setCode(vpr::ReturnStatus::NotConnected);
00411    }
00412 
00413    return status;
00414 }

vpr::ReturnStatus vpr::SocketImplBSD::write_i const void *    buffer,
const vpr::Uint32    length,
vpr::Uint32   bytes_written,
const vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Implementation of the write template method.

This writes the buffer to the socket.

Precondition:
The device is open for writing.
Postcondition:
The given buffer is written to the I/O device, and the number of bytes written successfully is returned to the caller via the bytes_written parameter.
Parameters:
buffer  A pointer to the buffer to be written.
length  The length of the buffer.
bytes_written  The number of bytes written to the device.
timeout  The maximum amount of time to wait for data to be available for writing. This argument is optional and defaults to vpr::Interval::NoTimeout.
Returns:
vpr::ReturnStatus::Succeed is returned if the write operation completed successfully.
vpr::ReturnStatus::WouldBlock is returned if the handle is in non-blocking mode, and the write operation could not be completed.
vpr::ReturnStatus::Timeout is returned if the write could not begin within the timeout interval.
vpr::ReturnStatus::Fail is returned if the write operation failed.

Definition at line 416 of file SocketImplBSD.cpp.

References errno, vpr::ReturnStatus::failure, mBlockingFixed, mHandle, vpr::ReturnStatus::NotConnected, vpr::ReturnStatus::setCode, and vpr::FileHandleImplUNIX::write_i.

00420 {
00421    vpr::ReturnStatus status;
00422    mBlockingFixed = true;
00423    status = mHandle->write_i(buffer, length, bytes_written, timeout);
00424 
00425    if ( status.failure() )
00426    {
00427       if ( ECONNRESET == errno || EHOSTUNREACH || EHOSTDOWN || ENETDOWN )
00428       {
00429          status.setCode(vpr::ReturnStatus::NotConnected);
00430       }
00431    }
00432 
00433    return status;
00434 }

vpr::Uint32 vpr::SocketImplBSD::availableBytes   const [inline]
 

Definition at line 366 of file SocketImplBSD.h.

References vpr::FileHandleImplUNIX::availableBytes, and mHandle.

00367    {
00368       return mHandle->availableBytes();
00369    }

vpr::ReturnStatus vpr::SocketImplBSD::getOption const vpr::SocketOptions::Types    option,
struct vpr::SocketOptions::Data   data
const
 

Retrieves the value for the given option as set on the socket.

Parameters:
option  The option to be queried.
data  A data buffer that will be used to store the value of the given option.
Returns:
vpr::ReturnStatus::Succeed is returned if the value for the given option was retrieved successfully.
vpr::ReturnStatus;:Fail is returned otherwise.

Definition at line 450 of file SocketImplBSD.cpp.

References errno, vpr::ReturnStatus::Fail, vpr::FileHandleImplUNIX::getName, vpr::InetAddr, vpr::SocketOptions::Data::ip_ttl, vpr::SocketOptions::Data::keep_alive, vpr::SocketOptions::Data::linger, vpr::SocketOptions::Data::max_segment, vpr::SocketOptions::Data::mcast_if, vpr::SocketOptions::Data::mcast_loopback, vpr::SocketOptions::Data::mcast_ttl, vpr::FileHandleImplUNIX::mFdesc, mHandle, vpr::SocketOptions::Data::no_delay, vpr::SocketOptions::Data::recv_buffer_size, vpr::SocketOptions::Data::reuse_addr, vpr::SocketOptions::Data::send_buffer_size, vpr::ReturnStatus::setCode, and vpr::SocketOptions::Data::type_of_service.

00452 {
00453    int opt_name, opt_level, status;
00454    vpr::ReturnStatus retval;
00455 #if defined(VPR_OS_IRIX) || defined(VPR_OS_HPUX)
00456    int opt_size;
00457 #else
00458    socklen_t opt_size;
00459 #endif
00460    union sockopt_data opt_data;
00461 
00462    opt_name = opt_level = -1;
00463 
00464    switch ( option )
00465    {
00466       // Socket-level options.
00467       case vpr::SocketOptions::Linger:
00468          opt_level = SOL_SOCKET;
00469          opt_name  = SO_LINGER;
00470          opt_size  = sizeof(opt_data.linger_val);
00471          break;
00472       case vpr::SocketOptions::ReuseAddr:
00473          opt_level = SOL_SOCKET;
00474          opt_name  = SO_REUSEADDR;
00475          opt_size  = sizeof(opt_data.enabled);
00476          break;
00477       case vpr::SocketOptions::KeepAlive:
00478          opt_level = SOL_SOCKET;
00479          opt_name  = SO_KEEPALIVE;
00480          opt_size  = sizeof(opt_data.enabled);
00481          break;
00482       case vpr::SocketOptions::RecvBufferSize:
00483          opt_level = SOL_SOCKET;
00484          opt_name  = SO_RCVBUF;
00485          opt_size  = sizeof(opt_data.size);
00486          break;
00487       case vpr::SocketOptions::SendBufferSize:
00488          opt_level = SOL_SOCKET;
00489          opt_name  = SO_SNDBUF;
00490          opt_size  = sizeof(opt_data.size);
00491          break;
00492 
00493       // IP-level options.
00494       case vpr::SocketOptions::IpTimeToLive:
00495          opt_level = IPPROTO_IP;
00496          opt_name  = IP_TTL;
00497          opt_size  = sizeof(opt_data.size);
00498          break;
00499       case vpr::SocketOptions::IpTypeOfService:
00500          opt_level = IPPROTO_IP;
00501          opt_name  = IP_TOS;
00502          opt_size  = sizeof(opt_data.size);
00503          break;
00504       case vpr::SocketOptions::AddMember:
00505          opt_level = IPPROTO_IP;
00506          opt_name  = IP_ADD_MEMBERSHIP;
00507          opt_size  = sizeof(opt_data.mcast_req);
00508          break;
00509       case vpr::SocketOptions::DropMember:
00510          opt_level = IPPROTO_IP;
00511          opt_name  = IP_DROP_MEMBERSHIP;
00512          opt_size  = sizeof(opt_data.mcast_req);
00513          break;
00514       case vpr::SocketOptions::McastInterface:
00515          opt_level = IPPROTO_IP;
00516          opt_name  = IP_MULTICAST_IF;
00517          opt_size  = sizeof(opt_data.mcast_if);
00518          break;
00519       case vpr::SocketOptions::McastTimeToLive:
00520          opt_level = IPPROTO_IP;
00521          opt_name  = IP_MULTICAST_TTL;
00522          opt_size  = sizeof(opt_data.mcast_ttl);
00523          break;
00524       case vpr::SocketOptions::McastLoopback:
00525          opt_level = IPPROTO_IP;
00526          opt_name  = IP_MULTICAST_LOOP;
00527          opt_size  = sizeof(opt_data.mcast_loop);
00528          break;
00529 
00530       // TCP-level options.
00531       case vpr::SocketOptions::NoDelay:
00532          opt_level = IPPROTO_TCP;
00533          opt_name  = TCP_NODELAY;
00534          opt_size  = sizeof(opt_data.enabled);
00535          break;
00536       case vpr::SocketOptions::MaxSegment:
00537          opt_level = IPPROTO_TCP;
00538          opt_name  = TCP_MAXSEG;
00539          opt_size  = sizeof(opt_data.size);
00540          break;
00541    }
00542 
00543    status = getsockopt(mHandle->mFdesc, opt_level, opt_name,
00544                        (void*) &opt_data, &opt_size);
00545 
00546    if ( status == 0 )
00547    {
00548       // This extracts the information from the union passed to getsockopt(2)
00549       // and puts it in our friendly vpr::SocketOptions::Data object.  This
00550       // code depends on the type of that object being a union!
00551       switch ( option )
00552       {
00553          case vpr::SocketOptions::Linger:
00554             data.linger.enabled = (opt_data.linger_val.l_onoff != 0 ? true
00555                                                                     : false);
00556             data.linger.seconds = opt_data.linger_val.l_linger;
00557             break;
00558          case vpr::SocketOptions::ReuseAddr:
00559             data.reuse_addr = (opt_data.enabled != 0 ? true : false);
00560             break;
00561          case vpr::SocketOptions::KeepAlive:
00562             data.keep_alive = (opt_data.enabled != 0 ? true : false);
00563             break;
00564          case vpr::SocketOptions::RecvBufferSize:
00565             data.recv_buffer_size = opt_data.size;
00566             break;
00567          case vpr::SocketOptions::SendBufferSize:
00568             data.send_buffer_size = opt_data.size;
00569             break;
00570          case vpr::SocketOptions::IpTimeToLive:
00571             data.ip_ttl = opt_data.size;
00572             break;
00573          case vpr::SocketOptions::IpTypeOfService:
00574             switch ( opt_data.size )
00575             {
00576                case IPTOS_LOWDELAY:
00577                   data.type_of_service = vpr::SocketOptions::LowDelay;
00578                   break;
00579                case IPTOS_THROUGHPUT:
00580                   data.type_of_service = vpr::SocketOptions::Throughput;
00581                   break;
00582                case IPTOS_RELIABILITY:
00583                   data.type_of_service = vpr::SocketOptions::Reliability;
00584                   break;
00585 #ifdef IPTOS_LOWCOST
00586                case IPTOS_LOWCOST:
00587                   data.type_of_service = vpr::SocketOptions::LowCost;
00588                   break;
00589 #endif
00590             }
00591 
00592             break;
00593          case vpr::SocketOptions::McastInterface:
00594             data.mcast_if = InetAddr();
00595             data.mcast_if.setAddress(opt_data.mcast_if.s_addr, 0);
00596             break;
00597          case vpr::SocketOptions::McastTimeToLive:
00598             data.mcast_ttl = opt_data.mcast_ttl;
00599             break;
00600          case vpr::SocketOptions::McastLoopback:
00601             data.mcast_loopback = opt_data.mcast_loop;
00602             break;
00603          case vpr::SocketOptions::NoDelay:
00604             data.no_delay = (opt_data.enabled != 0 ? true : false);
00605             break;
00606          case vpr::SocketOptions::MaxSegment:
00607             data.max_segment = opt_data.size;
00608             break;
00609          case vpr::SocketOptions::AddMember:
00610          case vpr::SocketOptions::DropMember:
00612             break;
00613       }
00614    }
00615    else
00616    {
00617       retval.setCode(vpr::ReturnStatus::Fail);
00618       fprintf(stderr,
00619               "[vpr::SocketImplBSD] ERROR: Could not get socket option for socket %s: %s\n",
00620               mHandle->getName().c_str(), strerror(errno));
00621    }
00622 
00623    return retval;
00624 }

vpr::ReturnStatus vpr::SocketImplBSD::setOption const vpr::SocketOptions::Types    option,
const struct vpr::SocketOptions::Data   data
 

Do nothing.

Parameters:
option  The option whose value will be set.
data  A data buffer containing the value to be used in setting the socket option.

Definition at line 626 of file SocketImplBSD.cpp.

References vpr::ReturnStatus::Fail, vpr::SocketOptions::Data::ip_ttl, vpr::SocketOptions::Data::keep_alive, vpr::SocketOptions::Data::linger, vpr::SocketOptions::Data::max_segment, vpr::SocketOptions::Data::mcast_add_member, vpr::SocketOptions::Data::mcast_drop_member, vpr::SocketOptions::Data::mcast_if, vpr::SocketOptions::Data::mcast_loopback, vpr::SocketOptions::Data::mcast_ttl, vpr::FileHandleImplUNIX::mFdesc, mHandle, vpr::SocketOptions::Data::no_delay, vpr::SocketOptions::Data::recv_buffer_size, vpr::SocketOptions::Data::reuse_addr, vpr::SocketOptions::Data::send_buffer_size, and vpr::SocketOptions::Data::type_of_service.

00628 {
00629    int opt_name, opt_level;
00630 #if defined(VPR_OS_IRIX) || defined(VPR_OS_HPUX)
00631    int opt_size;
00632 #else
00633    socklen_t opt_size;
00634 #endif
00635    union sockopt_data opt_data;
00636    vpr::ReturnStatus retval;
00637 
00638    opt_name = opt_level = -1;
00639    opt_size = 0;
00640 
00641    switch ( option )
00642    {
00643       // Socket-level options.
00644       case vpr::SocketOptions::Linger:
00645          opt_level                    = SOL_SOCKET;
00646          opt_name                     = SO_LINGER;
00647          opt_data.linger_val.l_onoff  = (data.linger.enabled ? 1 : 0);
00648          opt_data.linger_val.l_linger = data.linger.seconds;
00649          opt_size                     = sizeof(struct linger);
00650          break;
00651       case vpr::SocketOptions::ReuseAddr:
00652          opt_level        = SOL_SOCKET;
00653          opt_name         = SO_REUSEADDR;
00654          opt_data.enabled = (data.reuse_addr ? 1 : 0);
00655          opt_size         = sizeof(int);
00656          break;
00657       case vpr::SocketOptions::KeepAlive:
00658          opt_level        = SOL_SOCKET;
00659          opt_name         = SO_KEEPALIVE;
00660          opt_data.enabled = (data.keep_alive ? 1 : 0);
00661          opt_size         = sizeof(int);
00662          break;
00663       case vpr::SocketOptions::RecvBufferSize:
00664          opt_name      = SO_RCVBUF;
00665          opt_level     = SOL_SOCKET;
00666          opt_data.size = data.recv_buffer_size;
00667          opt_size      = sizeof(size_t);
00668          break;
00669       case vpr::SocketOptions::SendBufferSize:
00670          opt_level     = SOL_SOCKET;
00671          opt_name      = SO_SNDBUF;
00672          opt_data.size = data.send_buffer_size;
00673          opt_size      = sizeof(size_t);
00674          break;
00675 
00676       // IP-level options.
00677       case vpr::SocketOptions::IpTimeToLive:
00678          opt_level     = IPPROTO_IP;
00679          opt_name      = IP_TTL;
00680          opt_data.size = data.ip_ttl;
00681          opt_size      = sizeof(size_t);
00682          break;
00683       case vpr::SocketOptions::IpTypeOfService:
00684          opt_level     = IPPROTO_IP;
00685          opt_name      = IP_TOS;
00686 
00687          switch ( data.type_of_service )
00688          {
00689             case vpr::SocketOptions::LowDelay:
00690                opt_data.size = IPTOS_LOWDELAY;
00691                break;
00692             case vpr::SocketOptions::Throughput:
00693                opt_data.size = IPTOS_THROUGHPUT;
00694                break;
00695             case vpr::SocketOptions::Reliability:
00696                opt_data.size = IPTOS_RELIABILITY;
00697                break;
00698             case vpr::SocketOptions::LowCost:
00699 #ifdef IPTOS_LOWCOST
00700                opt_data.size = IPTOS_LOWCOST;
00701 #else
00702                fprintf(stderr,
00703                        "[vpr::SocketImplBSD] WARNING: This platform does not "
00704                        "support LowCost type of service!\n");
00705 #endif
00706                break;
00707          }
00708 
00709          opt_size      = sizeof(size_t);
00710          break;
00711       case vpr::SocketOptions::AddMember:
00712          opt_level = IPPROTO_IP;
00713          opt_name  = IP_ADD_MEMBERSHIP;
00714          opt_data.mcast_req.imr_multiaddr.s_addr = data.mcast_add_member.getMulticastAddr().getAddressValue();
00715          opt_data.mcast_req.imr_interface.s_addr = data.mcast_add_member.getInterfaceAddr().getAddressValue();
00716          opt_size  = sizeof(struct ip_mreq);
00717          break;
00718       case vpr::SocketOptions::DropMember:
00719          opt_level          = IPPROTO_IP;
00720          opt_name           = IP_DROP_MEMBERSHIP;
00721          opt_data.mcast_req.imr_multiaddr.s_addr = data.mcast_drop_member.getMulticastAddr().getAddressValue();
00722          opt_data.mcast_req.imr_interface.s_addr = data.mcast_drop_member.getInterfaceAddr().getAddressValue();
00723          opt_size           = sizeof(struct ip_mreq);
00724          break;
00725       case vpr::SocketOptions::McastInterface:
00726          opt_level                = IPPROTO_IP;
00727          opt_name                 = IP_MULTICAST_IF;
00728          opt_data.mcast_if.s_addr = data.mcast_if.getAddressValue();
00729          opt_size                 = sizeof(struct in_addr);
00730          break;
00731       case vpr::SocketOptions::McastTimeToLive:
00732          opt_level          = IPPROTO_IP;
00733          opt_name           = IP_MULTICAST_TTL;
00734          opt_data.mcast_ttl = data.mcast_ttl;
00735          opt_size           = sizeof(Uint8);
00736          break;
00737       case vpr::SocketOptions::McastLoopback:
00738          opt_level           = IPPROTO_IP;
00739          opt_name            = IP_MULTICAST_LOOP;
00740          opt_data.mcast_loop = data.mcast_loopback;
00741          opt_size            = sizeof(Uint8);
00742          break;
00743 
00744       // TCP-level options.
00745       case vpr::SocketOptions::NoDelay:
00746          opt_level        = IPPROTO_TCP;
00747          opt_name         = TCP_NODELAY;
00748          opt_data.enabled = (data.no_delay ? 1 : 0);
00749          opt_size         = sizeof(int);
00750          break;
00751       case vpr::SocketOptions::MaxSegment:
00752          opt_level     = IPPROTO_TCP;
00753          opt_name      = TCP_MAXSEG;
00754          opt_data.size = data.max_segment;
00755          opt_size      = sizeof(size_t);
00756          break;
00757    }
00758 
00759    if ( ::setsockopt(mHandle->mFdesc, opt_level, opt_name, &opt_data, opt_size) != 0 )
00760    {
00761       retval.setCode(vpr::ReturnStatus::Fail);
00762    }
00763 
00764    return retval;
00765 }


Member Data Documentation

bool vpr::SocketImplBSD::mOpenBlocking [protected]
 

Used for working around socket(2) semantics.

Definition at line 430 of file SocketImplBSD.h.

Referenced by open, and setBlocking.

bool vpr::SocketImplBSD::mBound [protected]
 

Definition at line 431 of file SocketImplBSD.h.

Referenced by bind, connect, isBound, and setLocalAddr.

bool vpr::SocketImplBSD::mConnected [protected]
 

Definition at line 432 of file SocketImplBSD.h.

Referenced by connect, isConnected, and setLocalAddr.

bool vpr::SocketImplBSD::mBlockingFixed [protected]
 

Definition at line 433 of file SocketImplBSD.h.

Referenced by vpr::SocketStreamImplBSD::accept, connect, isBlockingFixed, read_i, readn_i, vpr::SocketDatagramImplBSD::recvfrom, vpr::SocketDatagramImplBSD::sendto, setBlocking, and write_i.

vpr::FileHandleImplUNIX* vpr::SocketImplBSD::mHandle [protected]
 

The OS handle for this socket.

Definition at line 435 of file SocketImplBSD.h.

Referenced by vpr::SocketStreamImplBSD::accept, availableBytes, bind, close, connect, getHandle, getName, getOption, isBlocking, isConnected, isOpen, vpr::SocketStreamImplBSD::listen, open, read_i, readn_i, vpr::SocketDatagramImplBSD::recvfrom, vpr::SocketDatagramImplBSD::sendto, setBlocking, setOption, vpr::SocketDatagramImplBSD::SocketDatagramImplBSD, SocketImplBSD, vpr::SocketStreamImplBSD::SocketStreamImplBSD, write_i, and ~SocketImplBSD.