#include <OSGSocket.h>
Inheritance diagram for osg::Socket:

Public Member Functions | |
Constructors | |
| * | Socket (void) |
| Socket (const Socket &source) | |
Destructor | |
| *virtual | ~Socket () |
open, close, connect | |
| *virtual void | open (void)=0 |
| virtual void | close (void)=0 |
| 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 | recvAvailable (void *buf, int size) |
| int | recv (NetworkMessage &msg) |
| 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) |
Assignment | |
| *const Socket & | operator= (const Socket &source) |
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 |
Static Private Attributes | |
static member | |
| *static int | initialized = 0 |
Friends | |
| class | SocketSelection |
Definition at line 53 of file OSGSocket.h.
|
|
Socket option type. Used to hide the different interface implementations Definition at line 139 of file OSGSocket.h. |
|
|
Socket length type. Used to hide the different interface implementations Definition at line 146 of file OSGSocket.h. |
|
|
Create a new Socket class. A valid socket descriptor will be assigned by calling open on a derived Socket class e.g. StreamSocket or DgramSocket. On Windows WSAStartup is called by the first socket createn. Definition at line 105 of file OSGSocket.cpp. References initialized. 00105 : 00106 _sd(-1) 00107 { 00108 if(!initialized) 00109 { 00110 initialized=1; 00111 #ifdef WIN32 00112 WSADATA wsaData; 00113 00114 WORD wVersionRequested = MAKEWORD( 2, 2 ); 00115 if(WSAStartup( wVersionRequested, &wsaData )!=0) 00116 { 00117 throw SocketError("WSAStartup()"); 00118 } 00119 #endif 00120 } 00121 }
|
|
|
Copy constructor Definition at line 125 of file OSGSocket.cpp.
|
|
|
Destructor. The Socket class is only a descriptor to the socket. By destruction of this class, the socket remains open. Definition at line 133 of file OSGSocket.cpp.
|
|
|
Implemented in osg::DgramSocket, and osg::StreamSocket. |
|
|
Implemented in osg::DgramSocket, and osg::StreamSocket. |
|
|
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 150 of file OSGSocket.cpp. References _sd, 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(). 00151 { 00152 SocketAddress result=address; 00153 00154 if( ::bind(_sd, 00155 result.getSockAddr(), 00156 result.getSockAddrSize()) < 0) 00157 { 00158 if(getError() == 00159 #if defined WIN32 00160 WSAEADDRINUSE 00161 #else 00162 EADDRINUSE 00163 #endif 00164 ) 00165 { 00166 throw SocketInUse("bind()"); 00167 } 00168 else 00169 { 00170 throw SocketError("bind()"); 00171 } 00172 } 00173 }
|
|
|
Set queue length for incomming connection requests Definition at line 177 of file OSGSocket.cpp. References _sd. Referenced by osg::PointSockConnection::bind(), osg::GroupSockConnection::bind(), and osg::PointSockPipeline::initialize(). 00178 { 00179 if(::listen(_sd,maxPending)<0) 00180 { 00181 throw SocketError("listen()"); 00182 } 00183 }
|
|
|
Connect to the given address. After connect, all send data will be transfered to the address. Definition at line 188 of file OSGSocket.cpp. References _sd, osg::SocketAddress::getSockAddr(), and osg::SocketAddress::getSockAddrSize(). Referenced by osg::GroupSockConnection::connectSocket(), osg::PointSockPipeline::initialize(), and osg::GroupSockPipeline::initialize(). 00189 { 00190 if( ::connect(_sd, 00191 address.getSockAddr(), 00192 address.getSockAddrSize()) ) 00193 { 00194 throw SocketError("connect()"); 00195 } 00196 }
|
|
||||||||||||
|
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.
Definition at line 206 of file OSGSocket.cpp. References _sd, and getError(). Referenced by osg::ClusterWindow::init(), osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), peek(), osg::PointSockPipeline::read(), osg::PointSockConnection::read(), osg::PointSockPipeline::readBuffer(), osg::PointSockConnection::readBuffer(), recv(), recvAvailable(), and osg::PointMCastConnection::recvQueue(). 00207 { 00208 int readSize; 00209 int pos=0; 00210 00211 while(size) 00212 { 00213 readSize=::recv(_sd,((char*)buf) + pos,size,0); 00214 if(readSize < 0) 00215 { 00216 #if defined WIN32 00217 if(getError() == WSAECONNRESET) 00218 { 00219 throw SocketConnReset("recv"); 00220 } 00221 if(getError() == WSAEMSGSIZE) 00222 { 00223 readSize=size; 00224 } 00225 else 00226 #endif 00227 throw SocketError("recv()"); 00228 } 00229 if(readSize == 0) 00230 { 00231 return 0; 00232 } 00233 size-=readSize; 00234 pos +=readSize; 00235 } 00236 return pos; 00237 }
|
|
||||||||||||
|
Read the data from the in buffer to a maximun length of size. don't wait until size bytes are available.
Definition at line 243 of file OSGSocket.cpp. References _sd, getError(), and recv(). 00244 { 00245 int len; 00246 00247 #ifndef WIN32 00248 do 00249 { 00250 #endif 00251 len=::recv(_sd,(char*)buf,size,0); 00252 #ifndef WIN32 00253 } 00254 while(len < 0 && errno == EAGAIN); 00255 #endif 00256 if(len==-1) 00257 { 00258 #if defined WIN32 00259 switch(getError()) 00260 { 00261 case WSAECONNRESET: 00262 throw SocketConnReset("recvAvailable()"); 00263 break; 00264 case WSAEMSGSIZE: 00265 len=size; 00266 break; 00267 default: 00268 throw SocketError("recv()"); 00269 } 00270 #else 00271 throw SocketError("recv()"); 00272 #endif 00273 } 00274 return len; 00275 }
|
|
|
Like recv, but buffer and size is taken from the NetworkMessage
Definition at line 280 of file OSGSocket.cpp. References osg::NetworkMessage::getBuffer(), osg::NetworkMessage::getSize(), osg::osgntohl(), peek(), recv(), and osg::NetworkMessage::setSize(). 00281 { 00282 NetworkMessage::Header hdr; 00283 peek(&hdr,sizeof(hdr)); 00284 msg.setSize(osgntohl(hdr.size)); 00285 return recv(msg.getBuffer(),msg.getSize()); 00286 }
|
|
||||||||||||
|
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.
Definition at line 295 of file OSGSocket.cpp. References _sd, getError(), and recv(). Referenced by recv(), and osg::DgramSocket::recvFrom(). 00296 { 00297 int readSize; 00298 int pos=0; 00299 00300 do 00301 { 00302 readSize=::recv(_sd,((char*)buf)+pos,size,MSG_PEEK); 00303 if(readSize<0) 00304 { 00305 #if defined WIN32 00306 if(getError() == WSAECONNRESET) 00307 { 00308 throw SocketConnReset("peek"); 00309 } 00310 if(getError() == WSAEMSGSIZE) 00311 { 00312 readSize=size; 00313 } 00314 else 00315 #endif 00316 throw SocketError("peek"); 00317 } 00318 if(readSize == 0) 00319 { 00320 return 0; 00321 } 00322 } 00323 while(readSize != size); 00324 return readSize; 00325 }
|
|
||||||||||||
|
Write size bytes to the socket. This method maight block, if the output buffer is full. Definition at line 330 of file OSGSocket.cpp. References _sd. Referenced by osg::PointSockPipeline::initialize(), osg::PointMCastConnection::initialize(), osg::PointSockPipeline::read(), osg::PointSockPipeline::readBuffer(), send(), osg::PointSockConnection::signal(), osg::PointSockConnection::write(), osg::GroupSockPipeline::write(), osg::PointSockConnection::writeBuffer(), and osg::GroupSockPipeline::writeBuffer(). 00331 { 00332 int writeSize; 00333 int pos=0; 00334 while(size) 00335 { 00336 #if defined(WIN32) && defined(MSG_NOSIGNAL) 00337 writeSize=::send(_sd,((const char*)buf)+pos,size,MSG_NOSIGNAL); 00338 #else 00339 writeSize=::send(_sd,((const char*)buf)+pos,size,0); 00340 #endif 00341 if(writeSize == -1) 00342 { 00343 throw SocketError("send()"); 00344 } 00345 if(writeSize == 0) 00346 { 00347 return 0; 00348 } 00349 size-=writeSize; 00350 pos+=writeSize; 00351 } 00352 return pos; 00353 }
|
|
|
Like send, but buffer and size is taken from the NetworkMessage
Definition at line 358 of file OSGSocket.cpp. References osg::NetworkMessage::getBuffer(), osg::NetworkMessage::getHeader(), osg::NetworkMessage::getSize(), osg::osghtonl(), send(), and osg::NetworkMessage::Header::size. 00359 { 00360 NetworkMessage::Header &hdr=msg.getHeader(); 00361 hdr.size=osghtonl(msg.getSize()); 00362 return send(msg.getBuffer(),msg.getSize()); 00363 }
|
|
|
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 374 of file OSGSocket.cpp. References _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(). 00375 { 00376 int v=(int)value; 00377 #ifdef SO_REUSEPORT 00378 ::setsockopt(_sd,SOL_SOCKET,SO_REUSEPORT,(SocketOptT*)&v,sizeof(v)); 00379 #endif 00380 ::setsockopt(_sd,SOL_SOCKET,SO_REUSEADDR,(SocketOptT*)&v,sizeof(v)); 00381 }
|
|
|
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. Definition at line 391 of file OSGSocket.cpp. References _sd. 00392 { 00393 #ifndef WIN32 00394 int val=0; 00395 00396 if(value==false) 00397 val=O_NDELAY; 00398 if (fcntl(_sd, F_GETFL, &val) < 0) 00399 { 00400 throw SocketError("fcntl()"); 00401 } 00402 val|=O_NDELAY; 00403 if(value) 00404 { 00405 val^=O_NDELAY; 00406 } 00407 if (fcntl(_sd, F_SETFL, val) < 0) 00408 { 00409 throw SocketError("fcntl()"); 00410 } 00411 #else 00412 u_long ulVal = !value; 00413 if( (ioctlsocket(_sd, FIONBIO, &ulVal)) != 0) 00414 { 00415 throw SocketError("ioctlsocket()"); 00416 } 00417 #endif 00418 }
|
|
|
Get bound SocketAddress
Definition at line 423 of file OSGSocket.cpp. References _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(). 00424 { 00425 SocketAddress result; 00426 SocketLenT len; 00427 00428 len=result.getSockAddrSize(); 00429 if( ::getsockname(_sd,result.getSockAddr(),&len) < 0) 00430 { 00431 throw SocketError("getsockname()"); 00432 } 00433 return result; 00434 }
|
|
|
Set the internal read buffer size
Definition at line 439 of file OSGSocket.cpp. References _sd. Referenced by osg::GroupSockConnection::acceptSocket(), osg::GroupSockConnection::connectSocket(), osg::GroupMCastConnection::GroupMCastConnection(), and osg::PointMCastConnection::initialize(). 00440 { 00441 int v=(int)size; 00442 ::setsockopt(_sd,SOL_SOCKET,SO_RCVBUF,(SocketOptT*)&v,sizeof(v)); 00443 }
|
|
|
Set the internal write buffer size
Definition at line 448 of file OSGSocket.cpp. References _sd. Referenced by osg::GroupSockConnection::acceptSocket(), and osg::GroupSockConnection::connectSocket(). 00449 { 00450 int v=(int)size; 00451 ::setsockopt(_sd,SOL_SOCKET,SO_SNDBUF,(SocketOptT*)&v,sizeof(v)); 00452 }
|
|
|
Get internal read buffer size
Definition at line 457 of file OSGSocket.cpp. References _sd. Referenced by osg::GroupMCastConnection::GroupMCastConnection(). 00458 { 00459 int v; 00460 SocketLenT len=sizeof(v); 00461 ::getsockopt(_sd,SOL_SOCKET,SO_RCVBUF,(SocketOptT*)&v,&len); 00462 return v; 00463 }
|
|
|
Get internal write buffer size
Definition at line 468 of file OSGSocket.cpp. References _sd. 00469 { 00470 int v; 00471 SocketLenT len=sizeof(v); 00472 ::getsockopt(_sd,SOL_SOCKET,SO_SNDBUF,(SocketOptT*)&v,&len); 00473 return v; 00474 }
|
|
|
Get number of bytes in the internal read buffer Definition at line 478 of file OSGSocket.cpp. References _sd. 00479 { 00480 #ifndef WIN32 00481 int value; 00482 if(::ioctl(_sd, FIONREAD, &value)<0) 00483 { 00484 throw SocketError("ioctl()"); 00485 } 00486 return value; 00487 #else 00488 u_long ulVal; 00489 if( (ioctlsocket(_sd, FIONREAD, &ulVal)) != 0) 00490 { 00491 throw SocketError("ioctlsocket()"); 00492 } 00493 return (int)ulVal; 00494 #endif 00495 }
|
|
|
Wait until recv or accept will not block. True is returned if data is available. Definition at line 500 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(), and osg::GroupMCastConnection::sendQueue(). 00501 { 00502 SocketSelection selection; 00503 selection.setRead(*this); 00504 if(selection.select(duration)==1) 00505 return true; 00506 else 00507 return false; 00508 }
|
|
|
Wait until send will not block for the given duration. True is returned if the next send will not block. Definition at line 513 of file OSGSocket.cpp. References osg::SocketSelection::select(), and osg::SocketSelection::setWrite(). 00514 { 00515 SocketSelection selection; 00516 selection.setWrite(*this); 00517 if(selection.select(duration)==1) 00518 return true; 00519 else 00520 return false; 00521 }
|
|
|
assignment Definition at line 525 of file OSGSocket.cpp. References _sd.
|
|
|
Get last occured error Definition at line 536 of file OSGSocket.cpp. Referenced by bind(), getErrorStr(), peek(), osg::DgramSocket::peekFrom(), recv(), recvAvailable(), osg::DgramSocket::recvFrom(), and osg::SocketError::SocketError(). 00537 { 00538 #ifdef WIN32 00539 return WSAGetLastError(); 00540 #else 00541 return errno; 00542 #endif 00543 }
|
|
|
Get last host error Definition at line 547 of file OSGSocket.cpp. Referenced by getHostErrorStr(), and osg::SocketHostError::SocketHostError(). 00548 { 00549 #ifdef WIN32 00550 return WSAGetLastError(); 00551 #else 00552 return h_errno; 00553 #endif 00554 }
|
|
|
Get last occured error as string Definition at line 558 of file OSGSocket.cpp. References getError(). Referenced by osg::SocketError::SocketError(). 00559 { 00560 const char *err=NULL; 00561 00562 #ifdef WIN32 00563 switch(getError()) 00564 { 00565 case WSAEINTR: err= "WSAEINTR"; break; 00566 case WSAEBADF: err= "WSAEBADF"; break; 00567 case WSAEFAULT: err= "WSAEFAULT"; break; 00568 case WSAEINVAL: err= "WSAEINVAL"; break; 00569 case WSAEMFILE: err= "WSAEMFILE"; break; 00570 case WSAEWOULDBLOCK: err= "WSAEWOULDBLOCK"; break; 00571 case WSAEINPROGRESS: err= "WSAEINPROGRESS"; break; 00572 case WSAEALREADY: err= "WSAEALREADY"; break; 00573 case WSAENOTSOCK: err= "WSAENOTSOCK"; break; 00574 case WSAEDESTADDRREQ: err= "WSAEDESTADDRREQ"; break; 00575 case WSAEMSGSIZE: err= "WSAEMSGSIZE"; break; 00576 case WSAEPROTOTYPE: err= "WSAEPROTOTYPE"; break; 00577 case WSAENOPROTOOPT: err= "WSAENOPROTOOPT"; break; 00578 case WSAEPROTONOSUPPORT: err= "WSAEPROTONOSUPPORT"; break; 00579 case WSAESOCKTNOSUPPORT: err= "WSAESOCKTNOSUPPORT"; break; 00580 case WSAEOPNOTSUPP: err= "WSAEOPNOTSUPP"; break; 00581 case WSAEPFNOSUPPORT: err= "WSAEPFNOSUPPORT"; break; 00582 case WSAEAFNOSUPPORT: err= "WSAEAFNOSUPPORT"; break; 00583 case WSAEADDRINUSE: err= "WSAEADDRINUSE"; break; 00584 case WSAEADDRNOTAVAIL: err= "WSAEADDRNOTAVAIL"; break; 00585 case WSAENETDOWN: err= "WSAENETDOWN"; break; 00586 case WSAENETUNREACH: err= "WSAENETUNREACH"; break; 00587 case WSAENETRESET: err= "WSAENETRESET"; break; 00588 case WSAECONNABORTED: err= "WSAECONNABORTED"; break; 00589 case WSAECONNRESET: err= "WSAECONNRESET"; break; 00590 case WSAENOBUFS: err= "WSAENOBUFS"; break; 00591 case WSAEISCONN: err= "WSAEISCONN"; break; 00592 case WSAENOTCONN: err= "WSAENOTCONN"; break; 00593 case WSAESHUTDOWN: err= "WSAESHUTDOWN"; break; 00594 case WSAETOOMANYREFS: err= "WSAETOOMANYREFS"; break; 00595 case WSAETIMEDOUT: err= "WSAETIMEDOUT"; break; 00596 case WSAECONNREFUSED: err= "WSAECONNREFUSED"; break; 00597 case WSAELOOP: err= "WSAELOOP"; break; 00598 case WSAENAMETOOLONG: err= "WSAENAMETOOLONG"; break; 00599 case WSAEHOSTDOWN: err= "WSAEHOSTDOWN"; break; 00600 case WSAEHOSTUNREACH: err= "WSAEHOSTUNREACH"; break; 00601 case WSASYSNOTREADY: err= "WSASYSNOTREADY"; break; 00602 case WSAVERNOTSUPPORTED: err= "WSAVERNOTSUPPORTED"; break; 00603 case WSANOTINITIALISED: err= "WSANOTINITIALISED"; break; 00604 case WSAHOST_NOT_FOUND: err= "WSAHOST_NOT_FOUND"; break; 00605 case WSATRY_AGAIN: err= "WSATRY_AGAIN"; break; 00606 case WSANO_RECOVERY: err= "WSANO_RECOVERY"; break; 00607 case WSANO_DATA: err= "WSANO_DATA"; break; 00608 } 00609 #else 00610 err=strerror(getError()); 00611 #endif 00612 if(err) 00613 return std::string(err); 00614 else 00615 return std::string("Unknown error"); 00616 }
|
|
|
Get last occured host error as string Definition at line 620 of file OSGSocket.cpp. References getHostError(). Referenced by osg::SocketHostError::SocketHostError(). 00621 { 00622 const char *err; 00623 #if defined(WIN32) || defined(__hpux) 00624 err = strerror(getHostError()); 00625 #else 00626 err = hstrerror(getHostError()); 00627 #endif 00628 if(err) 00629 return std::string(err); 00630 else 00631 return std::string("Unknown error"); 00632 }
|
|
|
Definition at line 161 of file OSGSocket.h. |
|
|
|
Definition at line 95 of file OSGSocket.cpp. Referenced by Socket(). |
1.4.3