Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

OSGSocketSelection.cpp

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002  *                                OpenSG                                     *
00003  *                                                                           *
00004  *                                                                           *
00005  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
00006  *                                                                           *
00007  *                            www.opensg.org                                 *
00008  *                                                                           *
00009  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
00010  *                                                                           *
00011 \*---------------------------------------------------------------------------*/
00012 /*---------------------------------------------------------------------------*\
00013  *                                License                                    *
00014  *                                                                           *
00015  * This library is free software; you can redistribute it and/or modify it   *
00016  * under the terms of the GNU Library General Public License as published    *
00017  * by the Free Software Foundation, version 2.                               *
00018  *                                                                           *
00019  * This library is distributed in the hope that it will be useful, but       *
00020  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
00022  * Library General Public License for more details.                          *
00023  *                                                                           *
00024  * You should have received a copy of the GNU Library General Public         *
00025  * License along with this library; if not, write to the Free Software       *
00026  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
00027  *                                                                           *
00028 \*---------------------------------------------------------------------------*/
00029 /*---------------------------------------------------------------------------*\
00030  *                                Changes                                    *
00031  *                                                                           *
00032  *                                                                           *
00033  *                                                                           *
00034  *                                                                           *
00035  *                                                                           *
00036  *                                                                           *
00037 \*---------------------------------------------------------------------------*/
00038 
00039 //---------------------------------------------------------------------------
00040 //  Includes
00041 //---------------------------------------------------------------------------
00042 
00043 #include <string.h>
00044 #ifdef WIN32
00045 #include <windows.h>
00046 #include <io.h>
00047 #else
00048 #include <sys/types.h>
00049 #include <sys/socket.h>
00050 #include <netinet/in.h>
00051 #include <netinet/tcp.h>
00052 #include <arpa/inet.h>
00053 #include <netdb.h>
00054 #include <unistd.h>
00055 #include <fcntl.h>
00056 #include <sys/time.h>
00057 #endif
00058 #include <errno.h>
00059 #include <stdio.h>
00060 
00061 #include <OSGBase.h>
00062 #include <OSGSocketException.h>
00063 #include <OSGSocket.h>
00064 #include <OSGSocketSelection.h>
00065 
00066 OSG_USING_NAMESPACE
00067 
00095 /***************************************************************************\
00096  *                               Types                                     *
00097 \***************************************************************************/
00098 
00099 /***************************************************************************\
00100  *                           Class variables                               *
00101 \***************************************************************************/
00102 
00103 /***************************************************************************\
00104  *                           Instance methods                              *
00105 \***************************************************************************/
00106 
00107 /*-------------------------------------------------------------------------*\
00108  -  public                                                                 -
00109 \*-------------------------------------------------------------------------*/
00110 
00111 /*------------- constructors & destructors --------------------------------*/
00112 
00116 SocketSelection::SocketSelection()
00117 {
00118     clear();
00119 }
00120 
00124 SocketSelection::SocketSelection(const SocketSelection &source):
00125     _fdSetRead (source._fdSetRead),
00126     _fdSetWrite(source._fdSetWrite)
00127 {
00128 }
00129 
00133 SocketSelection::~SocketSelection()
00134 {
00135 }
00136 
00140 void SocketSelection::clear()
00141 {
00142     FD_ZERO(&_fdSetRead);
00143     FD_ZERO(&_fdSetWrite);
00144 }
00145 
00150 void SocketSelection::clearRead(const Socket &sock)
00151 {
00152     FD_CLR(sock._sd,&_fdSetRead);
00153 }
00154 
00159 void SocketSelection::clearWrite(const Socket &sock)
00160 {
00161     FD_CLR(sock._sd,&_fdSetWrite);
00162 }
00163 
00168 void SocketSelection::setRead(const Socket &sock)
00169 {
00170     FD_SET(sock._sd,&_fdSetRead);
00171 }
00172 
00177 void SocketSelection::setWrite(const Socket &sock)
00178 {
00179     FD_SET(sock._sd,&_fdSetWrite);
00180 }
00181 
00191 int SocketSelection::select(double duration)
00192 {
00193     timeval tVal,*tValP;
00194     int count;
00195     
00196     if(duration<0)
00197     {
00198         tValP=NULL;
00199     }
00200     else
00201     {       
00202         tVal.tv_sec  = int( duration );
00203         tVal.tv_usec = int( (duration-tVal.tv_sec)*1000000 );
00204         tValP=&tVal;
00205     }
00206     do
00207     {
00208         count=::select(FD_SETSIZE, 
00209                        &_fdSetRead, 
00210                        &_fdSetWrite,
00211                        NULL,
00212                        tValP);
00213         if(count < 0)
00214         {
00215 #ifndef WIN32
00216             // select was interrupted by a signal. Ignore this
00217             // and retry to select
00218             if(errno != EINTR)
00219                 throw SocketError("select()");
00220 #else
00221             throw SocketError("select()");
00222 #endif
00223         }
00224     }
00225     while(count < 0);
00226     return count;
00227 }
00228 
00239 int SocketSelection::select(double duration,SocketSelection &result) const
00240 {
00241     result=*this;
00242     return result.select(duration);
00243 }
00244 
00249 bool SocketSelection::isSetRead(const Socket &sock)
00250 {
00251     if(FD_ISSET(sock._sd, &_fdSetRead))
00252         return true;
00253     else
00254         return false;
00255 }
00256 
00261 bool SocketSelection::isSetWrite(const Socket &sock)
00262 {
00263     if(FD_ISSET(sock._sd, &_fdSetWrite))
00264         return true;
00265     else
00266         return false;
00267 }
00268 
00269 /*-------------------------- assignment -----------------------------------*/
00270 
00274 const SocketSelection & SocketSelection::operator =(const SocketSelection &source)
00275 {
00276     _fdSetRead =source._fdSetRead;
00277     _fdSetWrite=source._fdSetWrite;
00278     return *this;
00279 }
00280 
00281 /*-------------------------------------------------------------------------*/
00282 /*                              cvs id's                                   */
00283 
00284 #ifdef __sgi
00285 #pragma set woff 1174
00286 #endif
00287 
00288 #ifdef OSG_LINUX_ICC
00289 #pragma warning( disable : 177 )
00290 #endif
00291 
00292 namespace
00293 {
00294     static Char8 cvsid_cpp       [] = "@(#)$Id: $";
00295     static Char8 cvsid_hpp       [] = OSG_SOCKET_SELECTION_HEADER_CVSID;
00296 }
00297 

Generated on Thu Aug 25 04:10:26 2005 for OpenSG by  doxygen 1.4.3