| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // A simple UDP source, where every UDP payload is a complete frame |
| 19 | // Implementation |
| 20 | |
| 21 | #include "BasicUDPSource.hh" |
| 22 | #include <GroupsockHelper.hh> |
| 23 | |
| 24 | BasicUDPSource* BasicUDPSource::createNew(UsageEnvironment& env, |
| 25 | Groupsock* inputGS) { |
| 26 | return new BasicUDPSource(env, inputGS); |
| 27 | } |
| 28 | |
| 29 | BasicUDPSource::BasicUDPSource(UsageEnvironment& env, Groupsock* inputGS) |
| 30 | : FramedSource(env), fInputGS(inputGS), fHaveStartedReading(False) { |
| 31 | // Try to use a large receive buffer (in the OS): |
| 32 | increaseReceiveBufferTo(env, inputGS->socketNum(), 50*1024); |
| 33 | |
| 34 | // Make the socket non-blocking, even though it will be read from only asynchronously, when packets arrive. |
| 35 | // The reason for this is that, in some OSs, reads on a blocking socket can (allegedly) sometimes block, |
| 36 | // even if the socket was previously reported (e.g., by "select()") as having data available. |
| 37 | // (This can supposedly happen if the UDP checksum fails, for example.) |
| 38 | makeSocketNonBlocking(fInputGS->socketNum()); |
| 39 | } |
| 40 | |
| 41 | BasicUDPSource::~BasicUDPSource(){ |
| 42 | envir().taskScheduler().turnOffBackgroundReadHandling(fInputGS->socketNum()); |
| 43 | } |
| 44 | |
| 45 | void BasicUDPSource::doGetNextFrame() { |
| 46 | if (!fHaveStartedReading) { |
| 47 | // Await incoming packets: |
| 48 | envir().taskScheduler().turnOnBackgroundReadHandling(fInputGS->socketNum(), |
| 49 | (TaskScheduler::BackgroundHandlerProc*)&incomingPacketHandler, this); |
| 50 | fHaveStartedReading = True; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void BasicUDPSource::doStopGettingFrames() { |
| 55 | envir().taskScheduler().turnOffBackgroundReadHandling(fInputGS->socketNum()); |
| 56 | fHaveStartedReading = False; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void BasicUDPSource::incomingPacketHandler(BasicUDPSource* source, int /*mask*/){ |
| 61 | source->incomingPacketHandler1(); |
| 62 | } |
| 63 | |
| 64 | void BasicUDPSource::incomingPacketHandler1() { |
| 65 | if (!isCurrentlyAwaitingData()) return; // we're not ready for the data yet |
| 66 | |
| 67 | // Read the packet into our desired destination: |
| 68 | struct sockaddr_in fromAddress; |
| 69 | if (!fInputGS->handleRead(fTo, fMaxSize, fFrameSize, fromAddress)) return; |
| 70 | |
| 71 | // Tell our client that we have new data: |
| 72 | afterGetting(this); // we're preceded by a net read; no infinite recursion |
| 73 | } |
| 74 | |