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

vpr::SocketStreamImplBSD Class Reference

Implementation of the stream socket wrapper using BSD sockets. More...

#include <SocketStreamImplBSD.h>

Inheritance diagram for vpr::SocketStreamImplBSD:

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

Collaboration graph
[legend]
List of all members.

Public Methods

 SocketStreamImplBSD ()
 Constructor. More...

 SocketStreamImplBSD (const InetAddr &local_addr, const InetAddr &remote_addr)
 Constructs a stream socket using the given addresses as defaults for communication channels. More...

 SocketStreamImplBSD (const SocketStreamImplBSD &sock)
 Copy constructor. More...

vpr::ReturnStatus listen (const int backlog=5)
 Puts this socket into the listening state where it listens for incoming connection requests. More...

vpr::ReturnStatus accept (SocketStreamImplBSD &sock, vpr::Interval timeout=vpr::Interval::NoTimeout)
 Accepts an incoming connection request and return the connected socket to the caller in the given socket object reference. More...


Detailed Description

Implementation of the stream socket wrapper using BSD sockets.

Definition at line 58 of file SocketStreamImplBSD.h.


Constructor & Destructor Documentation

vpr::SocketStreamImplBSD::SocketStreamImplBSD  
 

Constructor.

Precondition:
None.
Postcondition:
The member variables are initialized with the mType variable in particular set to vpr::SocketTypes::SOCK_STREAM.

Definition at line 62 of file SocketStreamImplBSD.cpp.

References vpr::SocketTypes::STREAM.

00063    : SocketImplBSD(vpr::SocketTypes::STREAM)
00064 {
00065    /* Do nothing. */ ;
00066 }

vpr::SocketStreamImplBSD::SocketStreamImplBSD const InetAddr   local_addr,
const InetAddr   remote_addr
 

Constructs a stream socket using the given addresses as defaults for communication channels.

This takes the address (either hostname or IP address) of a remote site and a port and stores the values for later use in the member variables of the object.

Postcondition:
The member variables are initialized with the type in particular set to vpr::SocketTypes::STREAM.
Parameters:
local_addr  The local address for this socket. This is used for binding the socket.
remote_addr  The remote address for this socket. This is used to specify the connection addres for this socket.

Definition at line 68 of file SocketStreamImplBSD.cpp.

References vpr::InetAddr, and vpr::SocketTypes::STREAM.

00070    : SocketImplBSD(local_addr, remote_addr, SocketTypes::STREAM)
00071 {
00072    /* Do nothing. */ ;
00073 }

vpr::SocketStreamImplBSD::SocketStreamImplBSD const SocketStreamImplBSD &    sock
 

Copy constructor.

Postcondition:
This socket is a copy of the given socket.

Definition at line 75 of file SocketStreamImplBSD.cpp.

References vpr::FileHandleImplUNIX::mFdesc, vpr::SocketImplBSD::mHandle, and vpr::SocketTypes::STREAM.

00076    : SocketImplBSD(sock.mLocalAddr, sock.mRemoteAddr, SocketTypes::STREAM)
00077 {
00078    mHandle         = new FileHandleImplUNIX(sock.mHandle->getName());
00079    mHandle->mFdesc = sock.mHandle->mFdesc;
00080 }


Member Function Documentation

vpr::ReturnStatus vpr::SocketStreamImplBSD::listen const int    backlog = 5
 

Puts this socket into the listening state where it listens for incoming connection requests.

Precondition:
The socket has been opened and bound to the address in mLocalAddr.
Postcondition:
The socket is in a listening state waiting for incoming connection requests.
Parameters:
backlog  The maximum length of th queue of pending connections.
Returns:
vpr::ReturnStatus::Succeed is returned if this socket is now in a listening state.
vpr::ReturnStatus::Fail is returned otherwise.

Definition at line 83 of file SocketStreamImplBSD.cpp.

References errno, vpr::FileHandleImplUNIX::mFdesc, vpr::SocketImplBSD::mHandle, and vpr::ReturnStatus::setCode.

00084 {
00085    vpr::ReturnStatus retval;
00086 
00087    // Put the socket into listning mode.  If that fails, print an error and
00088    // return error status.
00089    if ( ::listen(mHandle->mFdesc, backlog) == -1 )
00090    {
00091       fprintf(stderr,
00092               "[vpr::SocketStreamImplBSD] Cannot listen on socket: %s\n",
00093               strerror(errno));
00094       retval.setCode(ReturnStatus::Fail);
00095    }
00096 
00097    return retval;
00098 }

vpr::ReturnStatus vpr::SocketStreamImplBSD::accept SocketStreamImplBSD &    sock,
vpr::Interval    timeout = vpr::Interval::NoTimeout
 

Accepts an incoming connection request and return the connected socket to the caller in the given socket object reference.

Precondition:
The socket is open and is in a listening state.
Postcondition:
When a connection is established, the given vpr::SocketStream object is assigned the newly connected socket.
Parameters:
sock  A reference to a vpr::SocketStream object that will be used to return the connected socket created.
timeout  The length of time to wait for the accept call to return.
Returns:
vpr::ReturnStatus::Succeed is returned if the incoming request has been handled, and the given SocketStream object is a valid, connected socket.
vpr::ReturnStatus::WouldBlock is returned if this is a non-blocking socket, and there are no waiting connection requests.
vpr::ReturnStatus::Timeout is returned when no connections requests arrived within the given timeout period.
vpr::ReturnStatus::Fail is returned if the accept failed. The given vpr::SocketStream object is not modified in this case.

Definition at line 101 of file SocketStreamImplBSD.cpp.

References errno, vpr::InetAddr, vpr::SocketImplBSD::isBlocking, vpr::FileHandleImplUNIX::isReadable, vpr::SocketImplBSD::mBlockingFixed, vpr::FileHandleImplUNIX::mFdesc, vpr::SocketImplBSD::mHandle, vpr::ReturnStatus::setCode, and vpr::ReturnStatus::success.

00102 {
00103    int accept_sock;
00104    vpr::ReturnStatus retval;
00105    InetAddr addr;
00106 #if defined(VPR_OS_IRIX) || defined(VPR_OS_HPUX)
00107    int addrlen;
00108 #else
00109    socklen_t addrlen;
00110 #endif
00111 
00112    retval = mHandle->isReadable(timeout);
00113 
00114    if ( retval.success() )
00115    {
00116       mBlockingFixed = true;
00117 
00118       // Accept an incoming connection request.
00119       addrlen = addr.size();
00120       accept_sock = ::accept(mHandle->mFdesc,
00121                              (struct sockaddr*) &addr.mAddr, &addrlen);
00122 
00123       // If accept(2) failed, print an error message and return error stauts.
00124       if ( accept_sock == -1 )
00125       {
00126          if ( errno == EWOULDBLOCK && ! isBlocking() )
00127          {
00128             retval.setCode(ReturnStatus::WouldBlock);
00129          }
00130          else
00131          {
00132             fprintf(stderr,
00133                     "[vpr::SocketStreamImplBSD] Error while accepting "
00134                     "incoming connection: %s\n", strerror(errno));
00135             retval.setCode(ReturnStatus::Fail);
00136          }
00137       }
00138       // Otherwise, put the new socket in the passed socket object.
00139       else
00140       {
00141          sock.setRemoteAddr(addr);
00142          sock.mHandle         = new FileHandleImplUNIX(addr.getAddressString());
00143          sock.mHandle->mFdesc = accept_sock;
00144          sock.mHandle->mOpen  = true;
00145 
00146          sock.setBlocking(this->isBlocking());
00147 
00148          sock.mBound         = true;
00149          sock.mConnected     = true;
00150          sock.mBlockingFixed = true;
00151       }
00152    }
00153 
00154    return retval;
00155 }


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:47:04 2004 for VR Juggler Portable Runtime by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002