osg::SocketSelection Class Reference
[Network]

Wait or check one or more sockets for read/write blocking. More...

#include <OSGSocketSelection.h>

List of all members.

Public Member Functions

Constructors


 SocketSelection ()
 Constructor.
 SocketSelection (const SocketSelection &source)
 Copy constructor.
Destructor


 ~SocketSelection ()
 Destructor.
SocketSelection functions


void clear (void)
 Clear all settings.
void clearRead (const Socket &soc)
 Clear read settings for the given socket.
void clearWrite (const Socket &soc)
 Clear write settings for the given socket.
void setRead (const Socket &soc)
 Set read flag for the given socket.
void setWrite (const Socket &soc)
 Set write flag for the given socket.
bool isSetRead (const Socket &soc)
 Check if read flag is set for a socket.
bool isSetWrite (const Socket &soc)
 Check if write flag is set for a socket.
int select (double time)
 Start selection.
int select (double time, SocketSelection &result) const
 Start selection.
assignment


const SocketSelectionoperator= (const SocketSelection &source)
 assignment

Protected Attributes

fd_set * _fdSetRead
fd_set * _fdSetWrite


Detailed Description

Author:
Marcus Roth
You can use a SocketSelection to wait for data on one ore more sockets. It is possible to use a timeout as a maximum wait time.

Example:

 SocketSelection sel;
 Socket s1,s2;
 ...
 sel.setRead(s1);
 sel.setRead(s2);
 if(sel.select(2))
 {
   if(sel.isSetRead(s1)) cout << "Data on s1" << endl;
   if(sel.isSetRead(s2)) cout << "Data on s2" << endl;
 }
 else
 {
   cout << "No data after 2 seconds" << endl;
 }
 

Definition at line 63 of file OSGSocketSelection.h.


Constructor & Destructor Documentation

SocketSelection::SocketSelection (  ) 

Definition at line 119 of file OSGSocketSelection.cpp.

References _fdSetRead, _fdSetWrite, and clear().

00119                                  :
00120     _fdSetRead (NULL),
00121     _fdSetWrite(NULL)
00122 {
00123     _fdSetRead  = new fd_set;
00124     _fdSetWrite = new fd_set;
00125 
00126     clear();
00127 }

SocketSelection::SocketSelection ( const SocketSelection source  ) 

Definition at line 132 of file OSGSocketSelection.cpp.

References _fdSetRead, and _fdSetWrite.

00132                                                               :
00133     _fdSetRead (NULL),
00134     _fdSetWrite(NULL)
00135 {
00136     _fdSetRead  = new fd_set;
00137     _fdSetWrite = new fd_set;
00138 
00139     *_fdSetRead  = *(source._fdSetRead);
00140     *_fdSetWrite = *(source._fdSetWrite);
00141 }

SocketSelection::~SocketSelection (  ) 

Definition at line 146 of file OSGSocketSelection.cpp.

References _fdSetRead, and _fdSetWrite.

00147 {
00148     delete _fdSetRead;
00149     delete _fdSetWrite;
00150 }


Member Function Documentation

void SocketSelection::clear ( void   ) 

Definition at line 155 of file OSGSocketSelection.cpp.

References _fdSetRead, and _fdSetWrite.

Referenced by SocketSelection().

00156 {
00157     FD_ZERO(_fdSetRead);
00158     FD_ZERO(_fdSetWrite);
00159 }

void SocketSelection::clearRead ( const Socket sock  ) 

Parameters:
sock For this socket the read flag is cleared

Definition at line 165 of file OSGSocketSelection.cpp.

References _fdSetRead, and osg::Socket::_sd.

Referenced by osg::GroupSockConnection::wait().

00166 {
00167     FD_CLR(sock._sd,_fdSetRead);
00168 }

void SocketSelection::clearWrite ( const Socket sock  ) 

Parameters:
sock For this socket the write flag is cleared

Definition at line 174 of file OSGSocketSelection.cpp.

References _fdSetWrite, and osg::Socket::_sd.

00175 {
00176     FD_CLR(sock._sd,_fdSetWrite);
00177 }

void SocketSelection::setRead ( const Socket sock  ) 

Parameters:
sock For this socket the read flag is set

Definition at line 183 of file OSGSocketSelection.cpp.

References _fdSetRead, and osg::Socket::_sd.

Referenced by osg::GroupMCastConnection::checkChannels(), osg::PointMCastConnection::recvNextDgram(), osg::GroupSockConnection::selectChannel(), osg::GroupSockConnection::wait(), and osg::Socket::waitReadable().

00184 {
00185     FD_SET(sock._sd,_fdSetRead);
00186 }

void SocketSelection::setWrite ( const Socket sock  ) 

Parameters:
sock For this socket the write flag is set

Definition at line 192 of file OSGSocketSelection.cpp.

References _fdSetWrite, and osg::Socket::_sd.

Referenced by osg::Socket::waitWritable().

00193 {
00194     FD_SET(sock._sd,_fdSetWrite);
00195 }

bool SocketSelection::isSetRead ( const Socket sock  ) 

Parameters:
sock For this socket the read flag is tested

Definition at line 264 of file OSGSocketSelection.cpp.

References _fdSetRead, and osg::Socket::_sd.

Referenced by osg::GroupMCastConnection::checkChannels(), osg::PointMCastConnection::recvNextDgram(), osg::GroupSockConnection::selectChannel(), and osg::GroupSockConnection::wait().

00265 {
00266     if(FD_ISSET(sock._sd, _fdSetRead))
00267         return true;
00268     else
00269         return false;
00270 }

bool SocketSelection::isSetWrite ( const Socket sock  ) 

Parameters:
sock For this socket the write flag is tested

Definition at line 276 of file OSGSocketSelection.cpp.

References _fdSetWrite, and osg::Socket::_sd.

00277 {
00278     if(FD_ISSET(sock._sd, _fdSetWrite))
00279         return true;
00280     else
00281         return false;
00282 }

int SocketSelection::select ( double  duration  ) 

Wait for the first read or write flag to be true. All other flags are cleared.

Parameters:
duration Maximum wait time in seconds
Returns:
Number of set flags

Definition at line 206 of file OSGSocketSelection.cpp.

References _fdSetRead, and _fdSetWrite.

Referenced by osg::GroupMCastConnection::checkChannels(), osg::PointMCastConnection::recvNextDgram(), select(), osg::GroupSockConnection::selectChannel(), osg::GroupSockConnection::wait(), osg::Socket::waitReadable(), and osg::Socket::waitWritable().

00207 {
00208     timeval tVal,*tValP;
00209     int count;
00210     
00211     if(duration<0)
00212     {
00213         tValP=NULL;
00214     }
00215     else
00216     {       
00217         tVal.tv_sec  = int( duration );
00218         tVal.tv_usec = int( (duration-tVal.tv_sec)*1000000 );
00219         tValP=&tVal;
00220     }
00221     do
00222     {
00223         count=::select(FD_SETSIZE, 
00224                        _fdSetRead, 
00225                        _fdSetWrite,
00226                        NULL,
00227                        tValP);
00228         if(count < 0)
00229         {
00230 #ifndef WIN32
00231             // select was interrupted by a signal. Ignore this
00232             // and retry to select
00233             if(errno != EINTR)
00234                 throw SocketError("select()");
00235 #else
00236             throw SocketError("select()");
00237 #endif
00238         }
00239     }
00240     while(count < 0);
00241     return count;
00242 }

int SocketSelection::select ( double  duration,
SocketSelection result 
) const

Wait for the first read or write flag to be true. The resulting flags are set in result.

Parameters:
duration Maximum wait time in seconds
result Result selection
Returns:
Number of set flags

Definition at line 254 of file OSGSocketSelection.cpp.

References select().

00255 {
00256     result=*this;
00257     return result.select(duration);
00258 }

const SocketSelection & SocketSelection::operator= ( const SocketSelection source  ) 

Definition at line 289 of file OSGSocketSelection.cpp.

References _fdSetRead, and _fdSetWrite.

00290 {
00291     *_fdSetRead  = *(source._fdSetRead);
00292     *_fdSetWrite = *(source._fdSetWrite);
00293 
00294     return *this;
00295 }


Member Data Documentation

fd_set* osg::SocketSelection::_fdSetRead [protected]

fd_set* osg::SocketSelection::_fdSetWrite [protected]


The documentation for this class was generated from the following files:

Generated on Mon Mar 17 11:11:10 2008 for OpenSG by  doxygen 1.5.5