00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
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
00217
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
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
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