udpTransport.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:11k
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- static const char* const udpTransport_cxx_Version =
- "$Id: udpTransport.cxx,v 1.1 2001/03/30 02:49:23 icahoon Exp $";
- #include "StdAfx.h"
- #include <sys/types.h>
- #ifndef WIN32
- #include <sys/socket.h>
- #endif
- #include <string.h> // for memset
- #ifndef WIN32
- #include <netdb.h>
- #include <sys/errno.h>
- #endif
- #include "vovida-endian.h"
- #ifdef __sgi
- #include <errno.h>
- #define socklen_t int
- #endif
- #include <assert.h>
- #include <iostream>
- #include "MgcpUdpStack.hxx"
- #include "MgcpDebugManager.h"
- MgcpUdpPacket::MgcpUdpPacket(int payloadSize)
- {
- data = NULL;
- size = 0;
- allocatedSize = 0;
- // assert( payloadSize > 0 );
- // assert( payloadSize < 8192 ); // some udp stacks will have problems with large data
- // assert( numSource >= 0 );
- // assert( numSource <= 15 );
- // assert( padBytes >= 0 );
- // assert( padBytes < 8192 );
- // assert( sizeof( MgcpUdpHeader ) == 12 );
- size = payloadSize;
- //sizeof( MgcpUdpHeader ) +
- // numSource*sizeof(u_int32_t) + payloadSize + padBytes;
- data = new char[size];
- assert( data );
- allocatedSize = size;
- // memset( data, 0 , sizeof( MgcpUdpHeader ) );
- // MgcpUdpHeader* header = reinterpret_cast<MgcpUdpHeader*>(data);
- // header->version = 2;
- // header->paddingFlag = (padBytes>0)?1:0;
- //header->extetionFlag = 0;
- // header->numSource = numSource;
- //header->markerFlag = 0;
- //header->payloadType = MgcpUdpPayloadUndefined;
- //header->seqenceNumber =0;
- //header->timeStamp = 0;
- //header->ssrc = 0;
- }
- MgcpUdpPacket::~MgcpUdpPacket()
- {
- delete [] data; data = NULL;
- size = 0;
- allocatedSize = 0;
- }
- char* MgcpUdpPacket::getMessageLocation()
- {
- return data;
- }
- ostream& MgcpUdpPacket::print(ostream& s) const
- {
- s << data;
- return s;
- }
- int MgcpUdpPacket::getMessageSize()
- {
- assert( size > 0 );
- return size;
- }
- void
- MgcpUdpPacket::setMessageSize(int s)
- {
- assert( s > 0 );
- assert( s <= allocatedSize );
- size = s;
- }
- MgcpUdpStack::MgcpUdpStack(const char* serverName /* null if this is the server */ ,
- int aPort )
- {
- assert(0);
- assert( aPort > 1024 );
- assert( aPort < 65000 );
- port = aPort;
- //socketFd = socket(AF_INET,SOCK_DGRAM,0);
- socketFd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if ( socketFd < 0 )
- {
- // error
- cerr << "Unable to bind to socket";
- assert(0);
- }
- memset((char*) &rxAddress, 0, sizeof(rxAddress));
- assert( port >= 1024 ); // don't use privliged ports
- rxAddress.sin_family = AF_INET;
- rxAddress.sin_port = htons(port);
- rxAddress.sin_addr.s_addr = htonl(INADDR_ANY);
- // rxAddress.sin_len = sizeof(rxAddress);
- if ( serverName )
- {
- // this is a client
- // struct hostent* host = gethostbyname (serverName);
- assert(0); // this code shouldn't be in use anymore
- struct hostent host;
- gethostbyname (serverName);
- if (host == NULL)
- {
- cerr << "Can't lookup hostname "
- << serverName
- << endl;
- throw MgcpUdpException(MgcpUdpErrorUnkownHost);
- assert(0);
- }
- else
- {
- assert( host->h_addr_list[0] );
- memcpy((char*)&rxAddress.sin_addr.s_addr,
- host->h_addr_list[0], // take the first entry
- host->h_length);
- }
- if (connect(socketFd,
- (struct sockaddr*) &rxAddress,
- sizeof(rxAddress)))
- {
- cerr << "Unable to connect to socket";
- assert(0);
- }
- }
- else
- {
- // this is a server
- if ( bind( socketFd,
- (struct sockaddr*) &rxAddress,
- sizeof(rxAddress))
- != 0 )
- {
- cerr << "Unable to bind to rx socket -- is another server running?n";
- assert(0);
- }
- }
- }
- // MgcpUdpReceiver::MgcpUdpReceiver( int aPort=5004 , int jitterBufferMs = 50 ) :
- MgcpUdpReceiver::MgcpUdpReceiver( int aPort, int jitterBufferMs) :
- MgcpUdpStack( NULL , aPort )
- {}
- int
- MgcpUdpReceiver::receive( char* buffer, int bufferSize)
- {
- // form a RTP packet, do normal receive, copy data to buffer, free packet
- return 0;
- }
- MgcpUdpPacket*
- MgcpUdpReceiver::receive()
- {
- /*
- This function needs to do much of the work for the stack.
- First all packets in the OS que for the port must be recieved
- Packets must be reordered.
- Duplicate packets must be chucked out.
-
- Next the jitter buffer control is implemented. If a packet is deamed to be
- still younger than the jitter buffer time, it is NOT returned to the user
- until it is old enough.
- Missing packets are inserted using the provided algorithm.
- Packets are translated from network format to api format usings CODECs
-
- Statistics are collected, and may result in transmitting some RTCP info
- Any RTCP info is received and used to update statitics
- Error information is passed up to the user of the stack
- */
- return getPacket();
- }
- MgcpUdpPacket*
- MgcpUdpReceiver::getPacket()
- {
- /// xxx
- MgcpUdpPacket* packet = new MgcpUdpPacket(512 /* buffer size */ ); // fix hard code here
- //unsigned int fromLen = sizeof(txAddress);
- // int fromLen = sizeof(txAddress);
- socklen_t fromLen = sizeof(txAddress);
- // int fromLen = sizeof(txAddress);
- int len = recvfrom( socketFd,
- packet->getMessageLocation(),
- packet->getMessageSize(),
- 0 /*flags */,
- (struct sockaddr*) & txAddress,
- &fromLen);
- if ( len <= 0 )
- {
- assert(0);
- delete packet;
- packet = NULL;
- return NULL;
- }
- packet->setMessageSize(len);
- assert( fromLen <= sizeof(txAddress) );
- MgcpDumpMessage("IN ", '*', packet);
- return packet;
- }
- void MgcpUdpReceiver::addToFdSet( fd_set* set )
- {
- FD_SET(socketFd, set);
- }
- bool MgcpUdpReceiver::checkIfSet( fd_set* set )
- {
- // return static_cast<bool> FD_ISSET(socketFd, set);
- return FD_ISSET(socketFd, set) != 0;
- }
- MgcpUdpTransmitter::MgcpUdpTransmitter( const char* destinationHostName,
- int aPort ):
- MgcpUdpStack(destinationHostName, aPort)
- {}
- void
- MgcpUdpTransmitter::transmit( char* data, int length )
- {
- MgcpUdpPacket packet(length);
- memcpy(packet.getMessageLocation() , data, length);
- transmit(&packet);
- }
- void
- MgcpUdpTransmitter::transmit( MgcpUdpPacket* packet )
- {
- /*
- Much work needs to be done in this function.
- First the packet is translated to network format.
- Any FEC information is computed and saved for later transmition.
- Previous FEC information is transmitted.
- This packet is transmitted.
- Statistics are updated and may send RTCP packets.
- RTCP info is received and statistics are update.
- Error are provided up to the stack user
- */
- // assert( packet->getPayloadType() == apiFormat );
- // if ( apiFormat != networkFormat )
- // {
- // need to translate packet using CODEC
- // assert(0);
- // }
- // packet->setSequenceNumber( nextSequenceNumber++ );
- // previousTimestamp = packet->getTimestamp();
- MgcpDumpMessage("OUT", '-', packet);
- int count = sendto( socketFd,
- packet->getMessageLocation(), packet->getMessageSize(),
- 0 /* flags */ ,
- (struct sockaddr*) 0, 0);
- #if 0
- int count = sendto( socketFd,
- packet->getMessageLocation(), packet->getMessageSize(),
- 0 /* flags */ ,
- (struct sockaddr*) & rxAddress, sizeof(rxAddress) );
- #endif
- if (count < 0)
- {
- // an error -- but what?
- cerr << "Udp send error: " << errno << "n";
- }
- }
- MgcpUdpPacket*
- MgcpUdpTransmitter::allocatePacket( int payloadLength )
- {
- MgcpUdpPacket* packet = new MgcpUdpPacket( payloadLength );
- assert( packet );
- // packet->setSSRC(SSRC);
- // packet->setPayloadType( apiFormat );
- return packet;
- }
- MgcpUdpException::MgcpUdpException( MgcpUdpErrorType theErrorType ):
- errorType(theErrorType)
- {}