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 "OSGConfig.h"
00044
00045 #include <sys/types.h>
00046 #ifdef WIN32
00047 #ifdef WIN32_LEAN_AND_MEAN
00048 #include <winsock2.h>
00049 #endif
00050 #ifndef IP_ADD_MEMBERSHIP // VS.Net defines this within winsock2.h
00051 #include <WS2TCPIP.h>
00052 #endif
00053 #include <io.h>
00054 #else
00055 #include <sys/socket.h>
00056 #include <netinet/in.h>
00057 #include <netinet/tcp.h>
00058 #include <arpa/inet.h>
00059 #include <netdb.h>
00060 #include <unistd.h>
00061 #include <fcntl.h>
00062 #include <sys/time.h>
00063 #endif
00064 #include <errno.h>
00065 #include <stdio.h>
00066 #include <math.h>
00067 #include <map>
00068 #include <OSGBase.h>
00069 #include <OSGBaseFunctions.h>
00070 #include <OSGSocketAddress.h>
00071 #include <OSGDgramSocket.h>
00072 #include <OSGNetworkMessage.h>
00073
00074 OSG_USING_NAMESPACE
00075
00097
00098
00099
00104 DgramSocket::DgramSocket():
00105 Socket()
00106 {
00107 }
00108
00111 DgramSocket::DgramSocket(const DgramSocket &source):
00112 Socket(source)
00113 {
00114 }
00115
00116
00117
00118
00123 void DgramSocket::open()
00124 {
00125 _sd = socket(AF_INET, SOCK_DGRAM, 0);
00126 if(_sd<0)
00127 {
00128 throw SocketError("socket()");
00129 }
00130
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
00139 setTTL(2);
00140 }
00141
00144 void DgramSocket::close(void)
00145 {
00146 #ifdef WIN32
00147 ::closesocket(_sd);
00148 #else
00149 ::close(_sd);
00150 #endif
00151 }
00152
00153
00154
00155
00161 int DgramSocket::recvFrom(void *buf,int size,SocketAddress &from)
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 }
00198
00206 int DgramSocket::peekFrom(void *buf,int size,SocketAddress &from)
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 }
00234
00239 int DgramSocket::recvFrom(NetworkMessage &msg,SocketAddress &from)
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 }
00246
00251 int DgramSocket::sendTo(const void *buf,int size,const SocketAddress &to)
00252 {
00253 int len;
00254
00255
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
00267
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 }
00285
00290 int DgramSocket::sendTo(NetworkMessage &msg,const SocketAddress &to)
00291 {
00292 NetworkMessage::Header &hdr=msg.getHeader();
00293 hdr.size=osghtonl(msg.getSize());
00294 return sendTo(msg.getBuffer(),msg.getSize(),to);
00295 }
00296
00297
00298
00299
00303 void DgramSocket::join(const SocketAddress &group,const SocketAddress &interf)
00304 {
00305 struct ip_mreq joinAddr;
00306 int rc;
00307
00308
00309 joinAddr.imr_multiaddr.s_addr =
00310 ((sockaddr_in*)group.getSockAddr())->sin_addr.s_addr;
00311
00312
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 }
00325
00328 void DgramSocket::leave(const SocketAddress &group,const SocketAddress &interf)
00329 {
00330 struct ip_mreq joinAddr;
00331 int rc;
00332
00333
00334 joinAddr.imr_multiaddr.s_addr =
00335 ((sockaddr_in*)group.getSockAddr())->sin_addr.s_addr;
00336
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 }
00349
00354 void DgramSocket::setTTL(unsigned char ttl)
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 }
00363
00367 void DgramSocket::setMCastInterface(const SocketAddress &interf)
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 }
00380
00381
00382
00383
00386 const DgramSocket & DgramSocket::operator =(const DgramSocket &source)
00387 {
00388 _sd=source._sd;
00389 return *this;
00390 }
00391
00392
00393
00394
00395 #ifdef __sgi
00396 #pragma set woff 1174
00397 #endif
00398
00399 #ifdef OSG_LINUX_ICC
00400 #pragma warning( disable : 177 )
00401 #endif
00402
00403 namespace
00404 {
00405 static Char8 cvsid_cpp [] = "@(#)$Id: $";
00406 static Char8 cvsid_hpp [] = OSG_DGRAMSOCKET_HEADER_CVSID;
00407 }