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 #include <stdlib.h>
00043 #include <stdio.h>
00044 #include <assert.h>
00045
00046 #include "OSGConfig.h"
00047 #include "OSGLog.h"
00048 #include "OSGBaseThread.h"
00049 #include "OSGSocketSelection.h"
00050 #include "OSGBinaryMessage.h"
00051 #include "OSGPointMCastConnection.h"
00052 #include "OSGGroupMCastConnection.h"
00053 #include "OSGConnectionType.h"
00054
00055 OSG_USING_NAMESPACE
00056
00061
00062
00063
00067 PointMCastConnection::PointMCastConnection():
00068 Inherited(),
00069 _lastDgram(NULL),
00070 _initialized(false)
00071 {
00072 char lockName[256];
00073 sprintf(lockName,"PointMCastConnection%p",this);
00074
00075
00076 _lock = Lock::get(lockName);
00077
00078
00079 for(UInt32 dI = 0 ; dI < OSG_DGRAM_QUEUE_LEN ; ++dI)
00080 _free.put(new Dgram());
00081
00082 _acceptSocket.open();
00083 _acceptSocket.setReusePort(true);
00084
00085
00086
00087
00088
00089
00090
00091 }
00092
00095 PointMCastConnection::~PointMCastConnection(void)
00096 {
00097
00098 _recvQueueThreadStop = true;
00099
00100 BaseThread::join(_recvQueueThread);
00101
00102 _mcastSocket.close();
00103
00104 _lock->aquire();
00105 while(!_free.empty())
00106 delete _free.get(_lock);
00107 while(!_queue.empty())
00108 delete _queue.get(_lock);
00109 _lock->release();
00110
00111 _acceptSocket.close();
00112 }
00113
00116 const ConnectionType *PointMCastConnection::getType()
00117 {
00118 return &_type;
00119 }
00120
00121
00122
00123
00127 Connection::Channel PointMCastConnection::connectGroup(
00128 const std::string &address,
00129 Time timeout)
00130 {
00131 Channel channel = Inherited::connectGroup(address,timeout);
00132 return channel;
00133 }
00134
00137 void PointMCastConnection::disconnect(void)
00138 {
00139 _socket.close();
00140 }
00141
00145 Connection::Channel PointMCastConnection::acceptGroup(Time timeout)
00146 {
00147 Channel channel = Inherited::acceptGroup(timeout);
00148 return channel;
00149 }
00150
00151
00152
00153
00157 Connection::Channel PointMCastConnection::selectChannel(Time timeout)
00158 throw (ReadError)
00159 {
00160 if(_pointToPoint)
00161 return Inherited::selectChannel(timeout);
00162 try
00163 {
00164 if(!_initialized)
00165 initialize();
00166
00167 if(isReadBufferEmpty() &&
00168 !_lastDgram &&
00169 _queue.empty())
00170 {
00171 if(timeout == -1)
00172 {
00173
00174 _lock->aquire();
00175 _queue.wait(_lock);
00176 _lock->release();
00177 return 0;
00178 }
00179 if(timeout == 0)
00180 return -1;
00181 while(_queue.empty() && timeout > 0)
00182 {
00183 _mcastSocket.waitReadable(.1);
00184 timeout-=.1;
00185 }
00186 if(_queue.empty())
00187 return -1;
00188 }
00189 }
00190 catch(SocketException &e)
00191 {
00192 throw ReadError(e.what());
00193 }
00194 return 0;
00195 }
00196
00197
00198
00199
00202 bool PointMCastConnection::wait(Time timeout) throw (ReadError)
00203 {
00204 UInt32 tag;
00205
00206 if(_pointToPoint)
00207 return Inherited::wait(timeout);
00208 try
00209 {
00210 if(selectChannel(timeout) < 0)
00211 return false;
00212 getValue(tag);
00213 if(tag != 314156)
00214 {
00215 FFATAL(("Stream out of sync in PointMCastConnection\n"));
00216 throw ReadError("Stream out of sync");
00217 }
00218 }
00219 catch(SocketError &e)
00220 {
00221 throw ReadError(e.what());
00222 }
00223 return true;
00224 }
00225
00226
00227
00231 PointConnection *PointMCastConnection::create(void)
00232 {
00233 return new PointMCastConnection();
00234 }
00235
00236
00237
00238
00246 void PointMCastConnection::read(MemoryHandle mem,UInt32 size)
00247 {
00248 if(_pointToPoint)
00249 {
00250 Inherited::read(mem,size);
00251 return;
00252 }
00253 Dgram *dgram = NULL;
00254 char *buffer = (char*)mem;
00255 UInt32 len;
00256 UInt32 dgramPos;
00257
00258 if(!_initialized)
00259 initialize();
00260 while(size)
00261 {
00262 if(_lastDgram)
00263 {
00264 dgramPos = _lastDgramPos;
00265 dgram = _lastDgram;
00266 }
00267 else
00268 {
00269
00270 _lock->aquire();
00271 dgram = _queue.get(_lock);
00272 _lock->release();
00273 dgramPos = 0;
00274 if(dgram->getSize() == 0)
00275 throw ReadError("Channel closed\n");
00276 }
00277
00278 len = osgMin(size,dgram->getSize()-dgramPos);
00279 memcpy(buffer,dgram->getData()+dgramPos,len);
00280 buffer += len;
00281 size -= len;
00282 dgramPos += len;
00283 if(dgramPos == dgram->getSize())
00284 {
00285
00286 _lock->aquire();
00287 _free.put(dgram);
00288 _lock->release();
00289 _lastDgram = NULL;
00290 }
00291 else
00292 {
00293 _lastDgram = dgram;
00294 _lastDgramPos = dgramPos;
00295 }
00296 }
00297 }
00298
00306 void PointMCastConnection::readBuffer()
00307 {
00308 if(_pointToPoint)
00309 {
00310 Inherited::readBuffer();
00311 return;
00312 }
00313
00314 static int sumSize=0;
00315 Dgram *dgram = NULL;
00316 UInt32 size = readBufBegin()->getSize();
00317 MemoryHandle buffer = readBufBegin()->getMem();
00318 UInt32 len;
00319 UInt32 dgramPos;
00320
00321 if(!_initialized)
00322 initialize();
00323
00324 do
00325 {
00326 if(_lastDgram)
00327 {
00328 dgramPos = _lastDgramPos;
00329 dgram = _lastDgram;
00330 }
00331 else
00332 {
00333
00334 _lock->aquire();
00335 dgram = _queue.get(_lock);
00336 _lock->release();
00337 dgramPos = 0;
00338 if(dgram->getSize() == 0)
00339 throw ReadError("Channel closed");
00340 }
00341
00342 len = osgMin(size,dgram->getSize()-dgramPos);
00343 memcpy(buffer,dgram->getData()+dgramPos,len);
00344 buffer += len;
00345 size -= len;
00346 dgramPos += len;
00347 if(dgramPos == dgram->getSize())
00348 {
00349
00350 _lock->aquire();
00351 _free.put(dgram);
00352 _lock->release();
00353 _lastDgram = NULL;
00354 }
00355 else
00356 {
00357 _lastDgram = dgram;
00358 _lastDgramPos = dgramPos;
00359 }
00360 }
00361 while(size && !_queue.empty());
00362
00363 readBufBegin()->setDataSize(readBufBegin()->getSize()-size);
00364 sumSize += readBufBegin()->getDataSize();
00365 }
00366
00367
00368
00369
00372 bool PointMCastConnection::recvNextDgram(Dgram *dgram)
00373 {
00374 SocketSelection selection;
00375 SocketAddress from;
00376 UInt32 length;
00377
00378 selection.setRead(_mcastSocket);
00379 selection.setRead(_responseSocket);
00380 if(!selection.select(0.5))
00381 return false;
00382 if(selection.isSetRead(_responseSocket))
00383 {
00384 length = _responseSocket.recvFrom(dgram->getBuffer(),
00385 dgram->getBufferCapacity(),
00386 from);
00387 dgram->setBufferSize(length);
00388 #if 0
00389
00390
00391 if(from == _sender && !_combineAck.empty())
00392 {
00393 exit(0);
00394
00395
00396 if(_maxAck == dgram->getId())
00397 {
00398
00399 dgram->setId(_maxAck);
00400 dgram->setResponseSize();
00401 dgram->setResponseAck(true);
00402 #ifdef TEST_LOST_DGRAM_RATE
00403 if(drand48()>TEST_LOST_DGRAM_RATE)
00404 #endif
00405 _responseSocket.sendTo(
00406 dgram->getBuffer(),
00407 dgram->getBufferSize(),
00408 _ackDestination);
00409 return false;
00410 }
00411 else
00412 {
00413 return true;
00414 }
00415 }
00416 #endif
00417 combineAck(dgram,from);
00418 }
00419 if(selection.isSetRead(_mcastSocket))
00420 {
00421 length = _mcastSocket.recvFrom(dgram->getBuffer(),
00422 dgram->getBufferCapacity(),
00423 from);
00424 dgram->setBufferSize(length);
00425
00426 if(from != _sender)
00427 return false;
00428 else
00429 return true;
00430 }
00431 else
00432 {
00433 return false;
00434 }
00435 }
00436
00439 void PointMCastConnection::combineAck(Dgram *dgram,SocketAddress from)
00440 {
00441 UInt16 maxAck;
00442
00443 if(dgram)
00444 {
00445
00446 if(_combineAck.count(from)==0)
00447 {
00448 FFATAL(("no ack from other expected\n"));
00449 return;
00450 }
00451
00452 if( Dgram::less(dgram->getId(),_combineAck[from] ) )
00453 {
00454
00455 return;
00456 }
00457 _combineAck[from] = dgram->getId();
00458 }
00459
00460 maxAck = _seqNumber-1;
00461 for(std::map<SocketAddress,UInt16>::iterator aI
00462 = _combineAck.begin() ;
00463 aI != _combineAck.end() ; ++aI)
00464 {
00465 if( Dgram::less(aI->second,maxAck) )
00466 maxAck = aI->second;
00467 }
00468
00469
00470 if( Dgram::less(_maxAck,maxAck))
00471 {
00472 Dgram response;
00473
00474 _maxAck = maxAck;
00475 response.setResponseSize();
00476 response.setId(_maxAck);
00477 response.setResponseAck(true);
00478
00479 _responseSocket.sendTo(
00480 response.getBuffer(),
00481 response.getBufferSize(),
00482 _ackDestination);
00483 }
00484 }
00485
00488 bool PointMCastConnection::recvQueue(void)
00489 {
00490 SocketAddress from;
00491 Dgram *dgram;
00492 Dgram response;
00493 UInt32 readCount=0;
00494 UInt32 length;
00495 bool missing=false;
00496 Time ignoreT=getSystemTime();
00497 UInt16 id;
00498
00499 #ifdef TEST_LOST_DGRAM_RATE
00500 srand48((long int)(10000*getSystemTime()));
00501 #endif
00502
00503 for(;;)
00504 {
00505
00506 _lock->aquire();
00507 dgram =_free.get(_lock);
00508 _lock->release();
00509 do
00510 {
00511
00512 if(missing)
00513 ignoreT = getSystemTime();
00514 do
00515 {
00516 while(!recvNextDgram(dgram))
00517 {
00518 if(_recvQueueThreadStop)
00519 return true;
00520 try
00521 {
00522 while(_socket.waitReadable(0))
00523 {
00524 char buffer;
00525 if(_socket.recv(&buffer,1) <= 0)
00526 {
00527
00528 dgram->setSize(0);
00529 _lock->aquire();
00530 _queue.put(dgram);
00531 _lock->release();
00532 FLOG(("Connection lost\n"));
00533 return false;
00534 }
00535 }
00536 }
00537 catch(SocketException &e)
00538 {
00539
00540 dgram->setSize(0);
00541 _lock->aquire();
00542 _queue.put(dgram);
00543 _lock->release();
00544 FLOG(("Connection lost\n"));
00545 return false;
00546 }
00547 }
00548 id = dgram->getId();
00549 }
00550 while( missing &&
00551 id != _seqNumber &&
00552 (getSystemTime() - ignoreT) < 0.01);
00553
00554 missing = false;
00555 response.setId(id);
00556
00557
00558 if(dgram->getSize() == 0)
00559 {
00560 if( !Dgram::less(id,_seqNumber ) )
00561 {
00562 missing = true;
00563 response.setId(_seqNumber);
00564 }
00565
00566 }
00567 else
00568 {
00569
00570 if( dgram->getId() == _seqNumber)
00571 {
00572
00573
00574 _lock->aquire();
00575 _queue.put(dgram);
00576 _lock->release();
00577 }
00578 else
00579 {
00580
00581 if( Dgram::less(id,_seqNumber ) )
00582 {
00583 continue;
00584 }
00585 else
00586 {
00587 missing = true;
00588 response.setId(_seqNumber);
00589 }
00590 }
00591 }
00592
00593
00594
00595 response.setResponseAck(!missing);
00596 response.setResponseSize();
00597
00598
00599 if(!response.getResponseAck() ||
00600 !_mcastSocket.waitReadable(0))
00601 {
00602 #ifdef TEST_LOST_DGRAM_RATE
00603 if(drand48()>TEST_LOST_DGRAM_RATE)
00604 #endif
00605 if(response.getResponseAck())
00606 {
00607
00608
00609 if(_combineAck.empty() || id == _maxAck)
00610 _responseSocket.sendTo(response.getBuffer(),
00611 response.getBufferSize(),
00612 _ackDestination);
00613 else
00614 combineAck(NULL,_sender);
00615 }
00616 else
00617 {
00618
00619 _responseSocket.sendTo(response.getBuffer(),
00620 response.getBufferSize(),
00621 _sender);
00622 }
00623 }
00624 }
00625 while(id != _seqNumber || missing);
00626 _seqNumber++;
00627 }
00628
00629 }
00630
00631 void PointMCastConnection::recvQueueThread(void *arg)
00632 {
00633 PointMCastConnection *the=(PointMCastConnection*)arg;
00634 try
00635 {
00636 the->recvQueue();
00637 }
00638 catch(SocketException &e)
00639 {
00640 SFATAL << "Error in dgram reader thread:" << e.what() << std::endl;
00641 }
00642 the->_recvQueueThreadRunning = false;
00643 }
00644
00647 void PointMCastConnection::initialize()
00648 {
00649 std::string group;
00650 Channel channel;
00651 BinaryMessage message;
00652 std::string fromHost;
00653 UInt32 fromPort;
00654 UInt32 combineCount;
00655 std::string host;
00656 UInt32 port;
00657 char threadName[256];
00658
00659 sprintf(threadName,"PointMCastConnection%p",this);
00660
00661
00662 _socket.recv(message);
00663
00664 group = message.getString();
00665 port = message.getUInt32();
00666
00667 _seqNumber = message.getUInt32();
00668 _maxAck = _seqNumber - 1;
00669
00670 fromPort = message.getUInt32();
00671
00672 _sender = SocketAddress(_remoteAddress.getHost().c_str(),fromPort);
00673
00674 std::cout << _remoteAddress.getHost() << " " << fromPort << std::endl;
00675
00676
00677 _mcastSocket.open();
00678 _mcastSocket.setReusePort(true);
00679 _mcastSocket.setReadBufferSize(524288);
00680 _mcastSocket.bind(SocketAddress(SocketAddress::ANY,port));
00681 try
00682 {
00683 _mcastSocket.join(SocketAddress(group.c_str()));
00684 }
00685 catch(...) {}
00686
00687 if(!getInterface().empty())
00688 {
00689 _mcastSocket.setMCastInterface(SocketAddress(getInterface().c_str()));
00690 }
00691
00692 _responseSocket.open();
00693 _responseSocket.bind(SocketAddress(SocketAddress::ANY,0));
00694
00695
00696 message.clear();
00697 message.putUInt32(_responseSocket.getAddress().getPort());
00698 _socket.send(message);
00699
00700
00701 _socket.recv(message);
00702
00703 message.getUInt32(combineCount);
00704
00705 while(combineCount--)
00706 {
00707 host=message.getString();
00708 port=message.getUInt32();
00709 _combineAck[SocketAddress(host.c_str(),port)]=_seqNumber-1;
00710 }
00711
00712 host=message.getString();
00713 port=message.getUInt32();
00714 if(host.empty())
00715 host = _remoteAddress.getHost();
00716 _ackDestination = SocketAddress(host.c_str(),port);
00717
00718
00719 _recvQueueThread=BaseThread::get(threadName);
00720 _recvQueueThreadRunning = true;
00721 _recvQueueThreadStop = false;
00722 _recvQueueThread->runFunction( recvQueueThread, (void *) (this) );
00723
00724 _initialized = true;
00725
00726 }
00727
00728
00729
00730
00731 ConnectionType PointMCastConnection::_type(
00732 &PointMCastConnection::create,
00733 "Multicast");
00734
00735
00736
00737
00738 #ifdef __sgi
00739 #pragma set woff 1174
00740 #endif
00741
00742 #ifdef OSG_LINUX_ICC
00743 #pragma warning( disable : 177 )
00744 #endif
00745
00746 namespace
00747 {
00748 static Char8 cvsid_cpp [] = "@(#)$Id: $";
00749 static Char8 cvsid_hpp [] = OSG_GROUPMCASTCONNECTION_HEADER_CVSID;
00750 }
00751