osg::DgramSocket Class Reference
[Network]

Datagramm socket handler. More...

#include <OSGDgramSocket.h>

Inheritance diagram for osg::DgramSocket:

osg::Socket

List of all members.

Public Member Functions

Constructors


 DgramSocket ()
 DgramSocket (const DgramSocket &source)
open/close


virtual void open (void)
virtual void close (void)
read, write


int recvFrom (void *buf, int size, SocketAddress &from)
int peekFrom (void *buf, int size, SocketAddress &from)
int recvFrom (NetworkMessage &msg, SocketAddress &from)
int sendTo (const void *buf, int size, const SocketAddress &to)
int sendTo (NetworkMessage &msg, const SocketAddress &to)
multicast


void join (const SocketAddress &group, const SocketAddress &interf=SocketAddress(SocketAddress::ANY))
void leave (const SocketAddress &group, const SocketAddress &interf=SocketAddress(SocketAddress::ANY))
void setTTL (unsigned char ttl)
void setMCastInterface (const SocketAddress &interf)
assignment


const DgramSocketoperator= (const DgramSocket &source)
open, close, connect


void bind (const SocketAddress &address=SocketAddress(SocketAddress::ANY))
void listen (int maxPending=10)
void connect (const SocketAddress &address)
read, write


int recv (void *buf, int size)
int recv (NetworkMessage &msg)
int recvAvailable (void *buf, int size)
int peek (void *buf, int size)
int send (const void *buf, int size)
int send (NetworkMessage &msg)
state access


void setReusePort (bool value)
void setBlocking (bool value)
SocketAddress getAddress (void)
void setReadBufferSize (int size)
void setWriteBufferSize (int size)
int getReadBufferSize (void)
int getWriteBufferSize (void)
int getAvailable (void)
bool waitReadable (double duration)
bool waitWritable (double duration)

Static Public Member Functions

Error information


static int getError (void)
static int getHostError (void)
static std::string getErrorStr (void)
static std::string getHostErrorStr (void)

Protected Types

typedef void SocketOptT
typedef socklen_t SocketLenT

Protected Attributes

member


int _sd

Private Types

typedef Socket Inherited

Static Private Attributes

static char cvsid []


Detailed Description

This class is a handler to packet oriented socket. open will assing a udp socket and close releases the socket. Messages send with DgramSockets can be lost or the order in which they arrive can be changed.

 char buffer[100];
 DgramSocket s;
 SocketAddress from;
 s.open();
 s.sendTo(buffer,10,SocketAddress("serverhost.com",4567));
 s.recvFrom(buffer,10,from);
 s.close();
 

Definition at line 56 of file OSGDgramSocket.h.


Member Typedef Documentation

Definition at line 124 of file OSGDgramSocket.h.

typedef void osg::Socket::SocketOptT [protected, inherited]

Socket option type. Used to hide the different interface implementations

Definition at line 139 of file OSGSocket.h.

typedef socklen_t osg::Socket::SocketLenT [protected, inherited]

Socket length type. Used to hide the different interface implementations

Definition at line 146 of file OSGSocket.h.


Constructor & Destructor Documentation

DgramSocket::DgramSocket (  ) 

Constructor. Use open to assign a system socket. No system socket is assigned by the constructor.

See also:
DgramSocket::open

Definition at line 104 of file OSGDgramSocket.cpp.

00104                         :
00105     Socket()
00106 {
00107 }

DgramSocket::DgramSocket ( const DgramSocket source  ) 

Copy constructor

Definition at line 111 of file OSGDgramSocket.cpp.

00111                                                  :
00112     Socket(source)
00113 {
00114 }


Member Function Documentation

void DgramSocket::open ( void   )  [virtual]

Assign a socket. Open assignes a system socket to the DgramSocket.

See also:
Socket::close

Implements osg::Socket.

Definition at line 123 of file OSGDgramSocket.cpp.

References osg::Socket::_sd, and setTTL().

Referenced by osg::ClusterServer::acceptClient(), osg::GroupMCastConnection::GroupMCastConnection(), osg::ClusterWindow::init(), and osg::PointMCastConnection::initialize().

00124 {
00125     _sd = socket(AF_INET, SOCK_DGRAM, 0);
00126     if(_sd<0)
00127     {
00128         throw SocketError("socket()");
00129     }
00130     // all dgram sockets are allowed to send broadcast
00131     int on = 1;
00132     if(::setsockopt(_sd, 
00133                     SOL_SOCKET, SO_BROADCAST, 
00134                     (SocketOptT*)&on, sizeof(on)) < 0)
00135     {
00136         throw SocketError("setsockopt(,SOL_SOCKET,SO_BROADCAST)");
00137     }
00138     // by default, multicast only in local network
00139     setTTL(2);
00140 }

void DgramSocket::close ( void   )  [virtual]

close socket

Implements osg::Socket.

Definition at line 144 of file OSGDgramSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::ClusterServer::acceptClient(), osg::ClusterWindow::init(), osg::GroupMCastConnection::~GroupMCastConnection(), and osg::PointMCastConnection::~PointMCastConnection().

00145 {
00146 #ifdef WIN32
00147     ::closesocket(_sd);
00148 #else
00149     ::close(_sd);
00150 #endif
00151 }

int DgramSocket::recvFrom ( void *  buf,
int  size,
SocketAddress from 
)

Read size bytes into the buffer. Wait until size Bytes are available data maight be lossed, if size is smaller then the incomming package. This situation will not be treated as an error.

See also:
Socket::recvAvailable Socket::recv

Definition at line 161 of file OSGDgramSocket.cpp.

References osg::Socket::_sd, osg::Socket::getError(), osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize().

Referenced by osg::ClusterServer::acceptClient(), osg::ClusterWindow::init(), recvFrom(), osg::PointMCastConnection::recvNextDgram(), and osg::GroupMCastConnection::sendQueue().

00162 {
00163     int len;
00164     SocketLenT addrLen=from.getSockAddrSize();
00165 
00166 #ifndef WIN32
00167     do
00168     {
00169 #endif
00170         len=recvfrom(_sd,
00171                      (char*)buf,
00172                      size,
00173                      0,
00174                      from.getSockAddr(),
00175                      &addrLen);
00176 #ifndef WIN32
00177     } 
00178     while(len < 0 && errno == EAGAIN);
00179 #endif
00180 
00181     if(len < 0)
00182     {
00183 #if defined WIN32
00184         if(getError() == WSAECONNRESET)
00185         {
00186             throw SocketConnReset("recvfrom");
00187         }
00188         if(getError() == WSAEMSGSIZE)
00189         {
00190             len=size;
00191         }
00192         else
00193 #endif
00194         throw SocketError("recvfrom()");
00195     }
00196     return len;
00197 }

int DgramSocket::peekFrom ( void *  buf,
int  size,
SocketAddress from 
)

Read size bytes into the buffer. Wait until size Bytes are available On Dgram sockets data maight be lossed, if size is smaller then the incomming package. This situation will not be treated as an error. The read bytes will not be removed from the in buffer. A call to recv after peek will result in the same data.

See also:
recv Socket::recv

Definition at line 206 of file OSGDgramSocket.cpp.

References osg::Socket::_sd, osg::Socket::getError(), osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize().

00207 {
00208     int len;
00209     SocketLenT addrLen=from.getSockAddrSize();
00210 
00211     len=recvfrom(_sd,
00212                  (char*)buf,
00213                  size,
00214                  MSG_PEEK,
00215                  from.getSockAddr(),
00216                  &addrLen);
00217     if(len == -1)
00218     {
00219 #if defined WIN32
00220         if(getError() == WSAECONNRESET)
00221         {
00222             throw SocketConnReset("recvfrom");
00223         }
00224         if(getError() == WSAEMSGSIZE)
00225         {
00226             len=size;
00227         }
00228         else
00229 #endif
00230         throw SocketError("recvfrom()");
00231     }
00232     return len;
00233 }

int DgramSocket::recvFrom ( NetworkMessage msg,
SocketAddress from 
)

Receive a NetworkMessage. Workes like recv, but buffer and size is taken from the NetworkMessage

See also:
Socket::recv

Definition at line 239 of file OSGDgramSocket.cpp.

References osg::NetworkMessage::getBuffer(), osg::NetworkMessage::getSize(), osg::osgntohl(), osg::Socket::peek(), recvFrom(), osg::NetworkMessage::setSize(), and osg::NetworkMessage::Header::size.

00240 {
00241     NetworkMessage::Header hdr;
00242     peek(&hdr,sizeof(hdr));
00243     msg.setSize(osgntohl(hdr.size));
00244     return recvFrom(msg.getBuffer(),msg.getSize(),from);
00245 }

int DgramSocket::sendTo ( const void *  buf,
int  size,
const SocketAddress to 
)

Write size bytes to the socket. This method maight block, if the output buffer is full.

See also:
recv Socket::send

Definition at line 251 of file OSGDgramSocket.cpp.

References osg::Socket::_sd, osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize().

Referenced by osg::ClusterServer::acceptClient(), osg::PointMCastConnection::combineAck(), osg::ClusterWindow::init(), osg::PointMCastConnection::recvNextDgram(), osg::PointMCastConnection::recvQueue(), osg::GroupMCastConnection::sendQueue(), sendTo(), and osg::GroupMCastConnection::write().

00252 {
00253     int len;
00254 
00255     // send Request
00256     len=sendto(_sd,
00257                (const char*)buf,size,
00258 #if defined(WIN32) && defined(MSG_NOSIGNAL)
00259                MSG_NOSIGNAL,
00260 #else
00261                0,
00262 #endif
00263                to.getSockAddr(),
00264                to.getSockAddrSize());
00265 #ifdef _sgi
00266     /* Irix simetimes returns ENOBUFS on blocking write.
00267        Retry until buffer is available */
00268     while(len == -1 && errno == ENOBUFS)
00269     {
00270         usleep(100);
00271         len=sendto(_sd,
00272                    (const char*)buf,size,
00273                    0,
00274                    to.getSockAddr(),
00275                    to.getSockAddrSize());
00276     }
00277 #endif
00278 
00279     if(len == -1)
00280     {
00281         throw SocketError("sendto()");
00282     }
00283     return len;
00284 }

int DgramSocket::sendTo ( NetworkMessage msg,
const SocketAddress to 
)

Send a NetworkMessage to an address. Workes like send, but buffer and size is taken from the NetworkMessage.

See also:
Socket::send

Definition at line 290 of file OSGDgramSocket.cpp.

References osg::NetworkMessage::getBuffer(), osg::NetworkMessage::getHeader(), osg::NetworkMessage::getSize(), osg::osghtonl(), sendTo(), and osg::NetworkMessage::Header::size.

00291 {
00292     NetworkMessage::Header &hdr=msg.getHeader();
00293     hdr.size=osghtonl(msg.getSize());
00294     return sendTo(msg.getBuffer(),msg.getSize(),to);
00295 }

void DgramSocket::join ( const SocketAddress group,
const SocketAddress interf = SocketAddress(SocketAddress::ANY) 
)

The socket will receive all messages from the given multicast address It is possible to join more then on goup.

Definition at line 303 of file OSGDgramSocket.cpp.

References osg::Socket::_sd, and osg::SocketAddress::getSockAddr().

Referenced by osg::ClusterServer::acceptClient(), and osg::PointMCastConnection::initialize().

00304 {
00305     struct ip_mreq joinAddr;
00306     int rc;
00307 
00308     // group to join
00309     joinAddr.imr_multiaddr.s_addr =
00310         ((sockaddr_in*)group.getSockAddr())->sin_addr.s_addr;
00311 
00312     // interface that joins. (equal to bind address)
00313     joinAddr.imr_interface =
00314         ((struct sockaddr_in*)interf.getSockAddr())->sin_addr;
00315     rc=setsockopt(_sd,
00316                   IPPROTO_IP,
00317                   IP_ADD_MEMBERSHIP,
00318                   (SocketOptT*)&joinAddr,
00319                   sizeof(joinAddr));
00320     if(rc < 0)
00321     {
00322         throw SocketError("setsockopt(IPPROTO_IP,IP_ADD_MEMBERSHIP)");
00323     }
00324 }

void DgramSocket::leave ( const SocketAddress group,
const SocketAddress interf = SocketAddress(SocketAddress::ANY) 
)

Leave a multicast group. Don't receive messages from the given group.

Definition at line 328 of file OSGDgramSocket.cpp.

References osg::Socket::_sd, and osg::SocketAddress::getSockAddr().

00329 {
00330     struct ip_mreq joinAddr;
00331     int rc;
00332 
00333     // group to join
00334     joinAddr.imr_multiaddr.s_addr =
00335         ((sockaddr_in*)group.getSockAddr())->sin_addr.s_addr;
00336     // interface that joins. (equal to bind address)
00337     joinAddr.imr_interface =
00338         ((sockaddr_in*)interf.getSockAddr())->sin_addr;
00339     rc=setsockopt(_sd,
00340                   IPPROTO_IP,
00341                   IP_DROP_MEMBERSHIP,
00342                   (SocketOptT*)&joinAddr,
00343                   sizeof(joinAddr));
00344     if(rc < 0)
00345     {
00346         throw SocketError("setsockopt(IPPROTO_IP,IP_DROP_MEMBERSHIP)");
00347     }
00348 }

void DgramSocket::setTTL ( unsigned char  ttl  ) 

Set TTL for broadcast and multicast. Defines how many routers a package will pass until it is deleted. 0 = local host, 1 = local network, ...

Definition at line 354 of file OSGDgramSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::ClusterWindow::init(), open(), and osg::GroupMCastConnection::setParams().

00355 {
00356     int rc=setsockopt(_sd, IPPROTO_IP,IP_MULTICAST_TTL,
00357                       (SocketOptT*)&ttl,sizeof(ttl));
00358     if(rc < 0)
00359     {
00360         throw SocketError("setsockopt(IPPROTO_IP,IP_MULTICAST_TTL)");
00361     }
00362 }

void DgramSocket::setMCastInterface ( const SocketAddress interf  ) 

Spcify the network interface for outgoing multicast packages

Definition at line 367 of file OSGDgramSocket.cpp.

References osg::Socket::_sd, osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize().

Referenced by osg::ClusterWindow::init(), osg::PointMCastConnection::initialize(), and osg::GroupMCastConnection::initialize().

00368 {
00369     int rc=setsockopt(_sd,
00370                       IPPROTO_IP,
00371                       IP_MULTICAST_IF,
00372                       (SocketOptT*)interf.getSockAddr(),
00373                       interf.getSockAddrSize());
00374     if(rc < 0)
00375     {
00376         throw SocketError("setsockopt(IPPROTO_IP,IP_MULTICAST_IF)");
00377     }
00378 
00379 }

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

assignment

Definition at line 386 of file OSGDgramSocket.cpp.

References osg::Socket::_sd.

00387 {
00388     _sd=source._sd;
00389     return *this;
00390 }

void Socket::bind ( const SocketAddress address = SocketAddress(SocketAddress::ANY)  )  [inherited]

Bind a socket to a given SocketAddress. It is possible to bind a Socket to a special network interface ore to all availabel interfaces.

    sock.bind(AnySocketAddress(23344));           Bind Socket to port 23344 
    sock.bind(Address("123.223.112.33",0);  Bind to the given adapter
    sock.bind(AnySocketAddress(0));               Bind to a free port      
    port = sock.getAddress().getPort();     Get bound port
    

Definition at line 153 of file OSGSocket.cpp.

References osg::Socket::_sd, osg::Socket::getError(), osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize().

Referenced by osg::ClusterServer::acceptClient(), osg::PointSockConnection::bind(), osg::GroupSockConnection::bind(), osg::GroupMCastConnection::GroupMCastConnection(), osg::PointSockPipeline::initialize(), and osg::PointMCastConnection::initialize().

00154 {
00155     SocketAddress result=address;
00156     
00157     if( ::bind(_sd,
00158                result.getSockAddr(),
00159                result.getSockAddrSize()) < 0)
00160     {
00161         if(getError() ==
00162 #if defined WIN32
00163             WSAEADDRINUSE 
00164 #else
00165             EADDRINUSE
00166 #endif
00167         )
00168         {
00169             throw SocketInUse("bind()");
00170         }
00171         else
00172         {
00173             throw SocketError("bind()");
00174         }
00175     }
00176 }

void Socket::listen ( int  maxPending = 10  )  [inherited]

Set queue length for incomming connection requests

Definition at line 180 of file OSGSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::PointSockConnection::bind(), osg::GroupSockConnection::bind(), and osg::PointSockPipeline::initialize().

00181 {
00182     if(::listen(_sd,maxPending)<0)
00183     {
00184         throw SocketError("listen()");
00185     }
00186 }

void Socket::connect ( const SocketAddress address  )  [inherited]

Connect to the given address. After connect, all send data will be transfered to the address.

Definition at line 191 of file OSGSocket.cpp.

References osg::Socket::_sd, osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize().

Referenced by osg::GroupSockConnection::connectSocket(), osg::PointSockPipeline::initialize(), and osg::GroupSockPipeline::initialize().

00192 {
00193     if( ::connect(_sd,
00194                   address.getSockAddr(),
00195                   address.getSockAddrSize()) )
00196     {
00197         throw SocketError("connect()");
00198     }
00199 }

int Socket::recv ( void *  buf,
int  size 
) [inherited]

Read size bytes into the buffer. Wait until size Bytes are available On Dgram sockets data maight be lossed, if size is smaller then the incomming package. This situation will not be treated as an error.

See also:
recvAvailable recvFrom

Definition at line 209 of file OSGSocket.cpp.

References osg::Socket::_sd, and osg::Socket::getError().

Referenced by osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), osg::Socket::peek(), osg::PointSockPipeline::read(), osg::PointSockConnection::read(), osg::PointSockPipeline::readBuffer(), osg::PointSockConnection::readBuffer(), osg::Socket::recv(), osg::Socket::recvAvailable(), osg::PointMCastConnection::recvQueue(), and osg::PointSockConnection::wait().

00210 {
00211     int readSize;
00212     int pos=0;
00213 
00214     while(size)
00215     {
00216         readSize=::recv(_sd,((char*)buf) + pos,size,0);
00217         if(readSize < 0)
00218         {
00219 #if defined WIN32
00220             if(getError() == WSAECONNRESET)
00221             {
00222                 throw SocketConnReset("recv");
00223             }
00224             if(getError() == WSAEMSGSIZE)
00225             {
00226                 readSize=size;
00227             }
00228             else
00229 #endif
00230             throw SocketError("recv()");
00231         }
00232         if(readSize == 0)
00233         {
00234             return 0;
00235         }
00236         size-=readSize;
00237         pos +=readSize;
00238     }
00239     return pos;
00240 }

int Socket::recv ( NetworkMessage msg  )  [inherited]

Like recv, but buffer and size is taken from the NetworkMessage

See also:
recv

Definition at line 283 of file OSGSocket.cpp.

References osg::NetworkMessage::getBuffer(), osg::NetworkMessage::getSize(), osg::osgntohl(), osg::Socket::peek(), osg::Socket::recv(), osg::NetworkMessage::setSize(), and osg::NetworkMessage::Header::size.

00284 {
00285     NetworkMessage::Header hdr;
00286     peek(&hdr,sizeof(hdr));
00287     msg.setSize(osgntohl(hdr.size));
00288     return recv(msg.getBuffer(),msg.getSize());
00289 }

int Socket::recvAvailable ( void *  buf,
int  size 
) [inherited]

Read the data from the in buffer to a maximun length of size. don't wait until size bytes are available.

See also:
recv

Definition at line 246 of file OSGSocket.cpp.

References osg::Socket::_sd, osg::Socket::getError(), and osg::Socket::recv().

00247 {
00248     int len;
00249 
00250 #ifndef WIN32
00251     do
00252     {
00253 #endif
00254         len=::recv(_sd,(char*)buf,size,0);
00255 #ifndef WIN32
00256     } 
00257     while(len < 0 && errno == EAGAIN);
00258 #endif
00259     if(len==-1)
00260     {
00261 #if defined WIN32
00262         switch(getError())
00263         {
00264         case WSAECONNRESET:
00265             throw SocketConnReset("recvAvailable()");
00266             break;
00267         case WSAEMSGSIZE:
00268             len=size;
00269             break;
00270         default:
00271             throw SocketError("recv()");
00272         }
00273 #else
00274         throw SocketError("recv()");
00275 #endif
00276     }
00277     return len;
00278 }

int Socket::peek ( void *  buf,
int  size 
) [inherited]

Read size bytes into the buffer. Wait until size Bytes are available On Dgram sockets data maight be lossed, if size is smaller then the incomming package. This situation will not be treated as an error. The read bytes will not be removed from the in buffer. A call to recv after peek will result in the same data.

See also:
recv recvAvailable

Definition at line 298 of file OSGSocket.cpp.

References osg::Socket::_sd, osg::Socket::getError(), and osg::Socket::recv().

Referenced by osg::Socket::recv(), and recvFrom().

00299 {
00300     int readSize;
00301     int pos=0;
00302 
00303     do
00304     {
00305         readSize=::recv(_sd,((char*)buf)+pos,size,MSG_PEEK);
00306         if(readSize<0)
00307         {
00308 #if defined WIN32
00309             if(getError() == WSAECONNRESET)
00310             {
00311                 throw SocketConnReset("peek");
00312             }
00313             if(getError() == WSAEMSGSIZE)
00314             {
00315                 readSize=size;
00316             }
00317             else
00318 #endif
00319                 throw SocketError("peek");
00320         }
00321         if(readSize == 0)
00322         {
00323             return 0;
00324         }
00325     }
00326     while(readSize != size);
00327     return readSize;
00328 }

int Socket::send ( const void *  buf,
int  size 
) [inherited]

Write size bytes to the socket. This method maight block, if the output buffer is full.

Definition at line 333 of file OSGSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), osg::PointSockPipeline::read(), osg::PointSockPipeline::readBuffer(), osg::Socket::send(), osg::PointSockConnection::signal(), osg::PointSockConnection::write(), osg::GroupSockPipeline::write(), osg::PointSockConnection::writeBuffer(), and osg::GroupSockPipeline::writeBuffer().

00334 {
00335     int writeSize;
00336     int pos=0;
00337     while(size)
00338     {
00339 #if defined(WIN32) && defined(MSG_NOSIGNAL)
00340         writeSize=::send(_sd,((const char*)buf)+pos,size,MSG_NOSIGNAL);
00341 #else
00342         writeSize=::send(_sd,((const char*)buf)+pos,size,0);
00343 #endif
00344         if(writeSize == -1)
00345         {
00346             throw SocketError("send()");
00347         }
00348         if(writeSize == 0)
00349         {
00350             return 0;
00351         }
00352         size-=writeSize;
00353         pos+=writeSize;
00354     }
00355     return pos;
00356 }

int Socket::send ( NetworkMessage msg  )  [inherited]

Like send, but buffer and size is taken from the NetworkMessage

See also:
send

Definition at line 361 of file OSGSocket.cpp.

References osg::NetworkMessage::getBuffer(), osg::NetworkMessage::getHeader(), osg::NetworkMessage::getSize(), osg::osghtonl(), osg::Socket::send(), and osg::NetworkMessage::Header::size.

00362 {
00363     NetworkMessage::Header &hdr=msg.getHeader();
00364     hdr.size=osghtonl(msg.getSize());
00365     return send(msg.getBuffer(),msg.getSize());
00366 }

void Socket::setReusePort ( bool  value  )  [inherited]

Enable, disable reuse port behavior If reuse port is true, then more then on process or thread is able to bind to the same port. This makes sense for multicast or braodcast sockets. For StreamSockets this feature can be used to avoid the Socket in use message on not propperly closed ports.

Definition at line 377 of file OSGSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::ClusterServer::acceptClient(), osg::PointSockConnection::bind(), osg::GroupSockConnection::bind(), osg::GroupSockConnection::GroupSockConnection(), osg::PointMCastConnection::initialize(), osg::PointMCastConnection::PointMCastConnection(), and osg::PointSockConnection::PointSockConnection().

00378 {
00379     int v=(int)value;
00380 #ifdef SO_REUSEPORT
00381     ::setsockopt(_sd,SOL_SOCKET,SO_REUSEPORT,(SocketOptT*)&v,sizeof(v));
00382 #endif
00383     ::setsockopt(_sd,SOL_SOCKET,SO_REUSEADDR,(SocketOptT*)&v,sizeof(v));
00384 }

void Socket::setBlocking ( bool  value  )  [inherited]

By default all recv, send, accept calls will block until the executeion is finished. This behavior can be swithed off bei setting blocking to false. This will lead to a more difficult programming. An easier way to get non blocking behavior is to use SocketSelections or waitReadable, waitWritable. These methods provide a timeout for waiting.

See also:
Socket::waitReadable Socket::waitWritable SocketSelection

Definition at line 394 of file OSGSocket.cpp.

References osg::Socket::_sd.

00395 {
00396 #ifndef WIN32
00397     int val=0;
00398     
00399     if(value==false)
00400         val=O_NDELAY;
00401     if (fcntl(_sd, F_GETFL, &val) < 0) 
00402     {
00403         throw SocketError("fcntl()");
00404     }    
00405     val|=O_NDELAY;
00406     if(value)
00407     {
00408         val^=O_NDELAY;
00409     }
00410     if (fcntl(_sd, F_SETFL, val) < 0) 
00411     {
00412         throw SocketError("fcntl()");
00413     }    
00414 #else
00415     u_long ulVal = !value;
00416     if( (ioctlsocket(_sd, FIONBIO, &ulVal)) != 0) 
00417     {
00418         throw SocketError("ioctlsocket()");
00419     }    
00420 #endif
00421 }

SocketAddress Socket::getAddress ( void   )  [inherited]

Get bound SocketAddress

See also:
SocketAddress

Definition at line 426 of file OSGSocket.cpp.

References osg::Socket::_sd, osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize().

Referenced by osg::PointSockConnection::bind(), osg::GroupSockConnection::bind(), osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), and osg::GroupMCastConnection::initialize().

00427 {
00428     SocketAddress result;
00429     SocketLenT len;
00430 
00431     len=result.getSockAddrSize();
00432     if( ::getsockname(_sd,result.getSockAddr(),&len) < 0)
00433     {
00434         throw SocketError("getsockname()");
00435     }
00436     return result;
00437 }

void Socket::setReadBufferSize ( int  size  )  [inherited]

Set the internal read buffer size

See also:
Socket::getReadBufferSize

Definition at line 442 of file OSGSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::GroupSockConnection::acceptSocket(), osg::GroupSockConnection::connectSocket(), osg::GroupMCastConnection::GroupMCastConnection(), and osg::PointMCastConnection::initialize().

00443 {
00444     int v=(int)size;
00445     ::setsockopt(_sd,SOL_SOCKET,SO_RCVBUF,(SocketOptT*)&v,sizeof(v));
00446 }

void Socket::setWriteBufferSize ( int  size  )  [inherited]

Set the internal write buffer size

See also:
Socket::getWriteBufferSize

Definition at line 451 of file OSGSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::GroupSockConnection::acceptSocket(), and osg::GroupSockConnection::connectSocket().

00452 {
00453     int v=(int)size;
00454     ::setsockopt(_sd,SOL_SOCKET,SO_SNDBUF,(SocketOptT*)&v,sizeof(v));
00455 }

int Socket::getReadBufferSize ( void   )  [inherited]

Get internal read buffer size

See also:
Socket::setReadBufferSize

Definition at line 460 of file OSGSocket.cpp.

References osg::Socket::_sd.

Referenced by osg::GroupMCastConnection::GroupMCastConnection().

00461 {
00462     int v;
00463     SocketLenT len=sizeof(v);
00464     ::getsockopt(_sd,SOL_SOCKET,SO_RCVBUF,(SocketOptT*)&v,&len);
00465     return v;
00466 }

int Socket::getWriteBufferSize ( void   )  [inherited]

Get internal write buffer size

See also:
Socket::setWriteBufferSize

Definition at line 471 of file OSGSocket.cpp.

References osg::Socket::_sd.

00472 {
00473     int v;
00474     SocketLenT len=sizeof(v);
00475     ::getsockopt(_sd,SOL_SOCKET,SO_SNDBUF,(SocketOptT*)&v,&len);
00476     return v;
00477 }

int Socket::getAvailable ( void   )  [inherited]

Get number of bytes in the internal read buffer

Definition at line 481 of file OSGSocket.cpp.

References osg::Socket::_sd.

00482 {
00483 #ifndef WIN32
00484     int value;
00485     if(::ioctl(_sd, FIONREAD, &value)<0)
00486     {    
00487         throw SocketError("ioctl()");
00488     }
00489     return value;
00490 #else
00491     u_long ulVal;
00492     if( (ioctlsocket(_sd, FIONREAD, &ulVal)) != 0) 
00493     {    
00494         throw SocketError("ioctlsocket()");
00495     }
00496     return (int)ulVal;
00497 #endif
00498 }

bool Socket::waitReadable ( double  duration  )  [inherited]

Wait until recv or accept will not block. True is returned if data is available.

Definition at line 503 of file OSGSocket.cpp.

References osg::SocketSelection::select(), and osg::SocketSelection::setRead().

Referenced by osg::ClusterServer::acceptClient(), osg::GroupSockConnection::acceptSocket(), osg::ClusterWindow::init(), osg::PointMCastConnection::recvQueue(), osg::PointSockPipeline::selectChannel(), osg::PointSockConnection::selectChannel(), osg::PointMCastConnection::selectChannel(), osg::GroupMCastConnection::sendQueue(), and osg::PointSockConnection::wait().

00504 {
00505     SocketSelection selection;
00506     selection.setRead(*this);
00507     if(selection.select(duration)==1)
00508         return true;
00509     else
00510         return false;
00511 }

bool Socket::waitWritable ( double  duration  )  [inherited]

Wait until send will not block for the given duration. True is returned if the next send will not block.

Definition at line 516 of file OSGSocket.cpp.

References osg::SocketSelection::select(), and osg::SocketSelection::setWrite().

00517 {
00518     SocketSelection selection;
00519     selection.setWrite(*this);
00520     if(selection.select(duration)==1)
00521         return true;
00522     else
00523         return false;
00524 }

int Socket::getError ( void   )  [static, inherited]

Get last occured error

Definition at line 539 of file OSGSocket.cpp.

Referenced by osg::Socket::bind(), osg::Socket::getErrorStr(), osg::Socket::peek(), peekFrom(), osg::Socket::recv(), osg::Socket::recvAvailable(), recvFrom(), and osg::SocketError::SocketError().

00540 {
00541 #ifdef WIN32
00542     return WSAGetLastError();
00543 #else
00544     return errno;
00545 #endif
00546 }

int Socket::getHostError ( void   )  [static, inherited]

Get last host error

Definition at line 550 of file OSGSocket.cpp.

Referenced by osg::Socket::getHostErrorStr(), and osg::SocketHostError::SocketHostError().

00551 {
00552 #ifdef WIN32
00553     return WSAGetLastError();
00554 #else
00555     return h_errno;
00556 #endif
00557 }

std::string Socket::getErrorStr ( void   )  [static, inherited]

Get last occured error as string

Definition at line 561 of file OSGSocket.cpp.

References osg::Socket::getError().

Referenced by osg::SocketError::SocketError().

00562 {
00563     const char *err=NULL;
00564 
00565 #ifdef WIN32
00566     switch(getError())
00567     {
00568         case WSAEINTR: err= "WSAEINTR"; break;
00569         case WSAEBADF: err= "WSAEBADF"; break;
00570         case WSAEFAULT: err= "WSAEFAULT"; break; 
00571         case WSAEINVAL: err= "WSAEINVAL"; break; 
00572         case WSAEMFILE: err= "WSAEMFILE"; break; 
00573         case WSAEWOULDBLOCK: err= "WSAEWOULDBLOCK"; break; 
00574         case WSAEINPROGRESS: err= "WSAEINPROGRESS"; break; 
00575         case WSAEALREADY: err= "WSAEALREADY"; break; 
00576         case WSAENOTSOCK: err= "WSAENOTSOCK"; break; 
00577         case WSAEDESTADDRREQ: err= "WSAEDESTADDRREQ"; break; 
00578         case WSAEMSGSIZE: err= "WSAEMSGSIZE"; break; 
00579         case WSAEPROTOTYPE: err= "WSAEPROTOTYPE"; break; 
00580         case WSAENOPROTOOPT: err= "WSAENOPROTOOPT"; break; 
00581         case WSAEPROTONOSUPPORT: err= "WSAEPROTONOSUPPORT"; break; 
00582         case WSAESOCKTNOSUPPORT: err= "WSAESOCKTNOSUPPORT"; break; 
00583         case WSAEOPNOTSUPP: err= "WSAEOPNOTSUPP"; break; 
00584         case WSAEPFNOSUPPORT: err= "WSAEPFNOSUPPORT"; break; 
00585         case WSAEAFNOSUPPORT: err= "WSAEAFNOSUPPORT"; break; 
00586         case WSAEADDRINUSE: err= "WSAEADDRINUSE"; break; 
00587         case WSAEADDRNOTAVAIL: err= "WSAEADDRNOTAVAIL"; break; 
00588         case WSAENETDOWN: err= "WSAENETDOWN"; break; 
00589         case WSAENETUNREACH: err= "WSAENETUNREACH"; break; 
00590         case WSAENETRESET: err= "WSAENETRESET"; break; 
00591         case WSAECONNABORTED: err= "WSAECONNABORTED"; break; 
00592         case WSAECONNRESET: err= "WSAECONNRESET"; break; 
00593         case WSAENOBUFS: err= "WSAENOBUFS"; break; 
00594         case WSAEISCONN: err= "WSAEISCONN"; break; 
00595         case WSAENOTCONN: err= "WSAENOTCONN"; break; 
00596         case WSAESHUTDOWN: err= "WSAESHUTDOWN"; break; 
00597         case WSAETOOMANYREFS: err= "WSAETOOMANYREFS"; break; 
00598         case WSAETIMEDOUT: err= "WSAETIMEDOUT"; break; 
00599         case WSAECONNREFUSED: err= "WSAECONNREFUSED"; break; 
00600         case WSAELOOP: err= "WSAELOOP"; break; 
00601         case WSAENAMETOOLONG: err= "WSAENAMETOOLONG"; break; 
00602         case WSAEHOSTDOWN: err= "WSAEHOSTDOWN"; break; 
00603         case WSAEHOSTUNREACH: err= "WSAEHOSTUNREACH"; break; 
00604         case WSASYSNOTREADY: err= "WSASYSNOTREADY"; break; 
00605         case WSAVERNOTSUPPORTED: err= "WSAVERNOTSUPPORTED"; break; 
00606         case WSANOTINITIALISED: err= "WSANOTINITIALISED"; break; 
00607         case WSAHOST_NOT_FOUND: err= "WSAHOST_NOT_FOUND"; break; 
00608         case WSATRY_AGAIN: err= "WSATRY_AGAIN"; break; 
00609         case WSANO_RECOVERY: err= "WSANO_RECOVERY"; break; 
00610         case WSANO_DATA: err= "WSANO_DATA"; break; 
00611     }
00612 #else
00613     err=strerror(getError());
00614 #endif
00615     if(err)
00616         return std::string(err);
00617     else
00618         return std::string("Unknown error");
00619 }

std::string Socket::getHostErrorStr ( void   )  [static, inherited]

Get last occured host error as string

Definition at line 623 of file OSGSocket.cpp.

References osg::Socket::getHostError().

Referenced by osg::SocketHostError::SocketHostError().

00624 {
00625     const char *err;
00626 #if defined(WIN32) || defined(__hpux)
00627     err = strerror(getHostError());
00628 #else
00629     err = hstrerror(getHostError());
00630 #endif
00631     if(err)
00632         return std::string(err);
00633     else
00634         return std::string("Unknown error");
00635 }


Member Data Documentation

char osg::DgramSocket::cvsid[] [static, private]

Definition at line 126 of file OSGDgramSocket.h.

int osg::Socket::_sd [protected, inherited]


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

Generated on Mon Mar 17 12:03:50 2008 for OpenSG by  doxygen 1.5.5