liveMedia/BasicUDPSource.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2008 Live Networks, Inc.  All rights reserved.
00018 // A simple UDP source, where every UDP payload is a complete frame
00019 // Implementation
00020 
00021 #include "BasicUDPSource.hh"
00022 #include <GroupsockHelper.hh>
00023 
00024 BasicUDPSource* BasicUDPSource::createNew(UsageEnvironment& env,
00025                                         Groupsock* inputGS) {
00026   return new BasicUDPSource(env, inputGS);
00027 }
00028 
00029 BasicUDPSource::BasicUDPSource(UsageEnvironment& env, Groupsock* inputGS)
00030   : FramedSource(env), fInputGS(inputGS), fHaveStartedReading(False) {
00031   // Try to use a large receive buffer (in the OS):
00032   increaseReceiveBufferTo(env, inputGS->socketNum(), 50*1024);
00033 
00034   // Make the socket non-blocking, even though it will be read from only asynchronously, when packets arrive.
00035   // The reason for this is that, in some OSs, reads on a blocking socket can (allegedly) sometimes block,
00036   // even if the socket was previously reported (e.g., by "select()") as having data available.
00037   // (This can supposedly happen if the UDP checksum fails, for example.)
00038   makeSocketNonBlocking(fInputGS->socketNum());
00039 }
00040 
00041 BasicUDPSource::~BasicUDPSource(){
00042   envir().taskScheduler().turnOffBackgroundReadHandling(fInputGS->socketNum());
00043 }
00044 
00045 void BasicUDPSource::doGetNextFrame() {
00046   if (!fHaveStartedReading) {
00047     // Await incoming packets:
00048     envir().taskScheduler().turnOnBackgroundReadHandling(fInputGS->socketNum(), 
00049          (TaskScheduler::BackgroundHandlerProc*)&incomingPacketHandler, this);
00050     fHaveStartedReading = True;
00051   }
00052 }
00053 
00054 void BasicUDPSource::doStopGettingFrames() {
00055   envir().taskScheduler().turnOffBackgroundReadHandling(fInputGS->socketNum());
00056   fHaveStartedReading = False;
00057 }
00058 
00059 
00060 void BasicUDPSource::incomingPacketHandler(BasicUDPSource* source, int /*mask*/){
00061   source->incomingPacketHandler1();
00062 }
00063 
00064 void BasicUDPSource::incomingPacketHandler1() {
00065   if (!isCurrentlyAwaitingData()) return; // we're not ready for the data yet
00066 
00067   // Read the packet into our desired destination:
00068   struct sockaddr_in fromAddress;
00069   if (!fInputGS->handleRead(fTo, fMaxSize, fFrameSize, fromAddress)) return;
00070     
00071   // Tell our client that we have new data:
00072   afterGetting(this); // we're preceded by a net read; no infinite recursion
00073 }

Generated on Tue Jul 22 06:39:05 2008 for live by  doxygen 1.5.2