udpTransport.hxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
- #ifndef UDPTRANSPORT_HXX_
- #define UDPTRANSPORT_HXX_
- /* ====================================================================
- * 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 udpTransportHeaderVersion =
- "$Id: udpTransport.hxx,v 1.1 2001/03/30 02:49:23 icahoon Exp $";
- #include "global.h"
- #include <sys/types.h>
- #include <sys/select.h>
- #ifndef WIN32
- #include <sys/socket.h>
- #include <netinet/in.h> // for struct sockaddr_in
- #endif
- #include <vector>
- #include <iostream>
- ///
- typedef enum
- {
- MgcpUdpErrorNone = 0,
- MgcpUdpErrorUnkownHost = 100,
- MgcpUdpErrorConnectionLost,
- MgcpUdpErrorUndefined = -1
- } ///
- MgcpUdpErrorType;
- ///
- typedef void* MgcpUdpPacketPtr;
- ///
- typedef void* MgcpUdpReceiverPtr;
- ///
- typedef void* MgcpUdpTransmitterPtr;
- /// class to hold a single RTP packet
- class MgcpUdpPacket
- {
- // Curently header extentions are not supported
- public:
- /// Create a packet and allocate data space on heap
- MgcpUdpPacket(int payloadSize);
- /// Free space allocated on heap
- ~MgcpUdpPacket();
- /// Get UDP messsage location
- char* getMessageLocation();
- /// Get UDP messsage length in bytes
- int getMessageSize();
- /// Set UDP messsage lenght in bytes
- void setMessageSize(int size);
- /// Print minimal info about packet, type, destination, timestamp
- // friend ostream& operator<<(ostream& s, const MgcpUdpPacket& x) {
- friend ostream& operator<<(ostream& s, MgcpUdpPacket& x)
- {
- x.print(s);
- return s;
- }
- /// Print minimal info about packet, type, destination, timestamp
- ostream& print( ostream& stream ) const;
- /// Print all info about a packet including hex version of payload data
- void printVerbose( ostream& stream );
- private:
- /// pointer to the the raw data for the packet (including header)
- char* data;
- /// size of all the raw data for the packet (including header)
- int size;
- /// allocated size of data buffer
- int allocatedSize;
- };
- ///
- class MgcpUdpStack
- {
- public:
- ///
- MgcpUdpStack(const char* serverName, int aPort );
- protected:
- ///
- int port;
- /// file descriptor of socket
- int socketFd;
- /// address of computer that will receive the packets
- struct sockaddr_in rxAddress;
- /// address of computer that is sending the packets
- struct sockaddr_in txAddress;
- };
- ///
- class MgcpUdpReceiver: public MgcpUdpStack
- {
- public:
- /// Create an input stack and start listening on aproperate port
- MgcpUdpReceiver( int port = 5004 , int jitterBufferMs = 50 );
- /// Get the next chunk of data from the stream. Returns bytes received.
- int receive( char* buffer, int bufferSize);
- /// return a new UDP packet, non-blocking, NULL if none available, user must free this packet
- MgcpUdpPacket* receive();
- /// Set the stack so it generates an error if it received data from other hosts
- void onlyAcceptPacketsFrom( char* hostName );
- /// add this file descriptor to a FD_SET for a select call to use
- void addToFdSet( fd_set* set );
- /// Check if this file descriptor is set in an FD_SET for checking after a select call
- bool checkIfSet( fd_set* set );
- int getFd()
- {
- return socketFd;
- }
- private:
- /// read the next packet off the network
- MgcpUdpPacket* getPacket();
- private:
- };
- ///
- class MgcpUdpTransmitter: public MgcpUdpStack
- {
- public:
- ///
- MgcpUdpTransmitter( const char* destinationName,
- int port = 5004 );
- /// transmit the data
- void transmit( char* data, int length );
- /// Stack will free this packet when it is done with it so it must be alloacted on the heap
- void transmit( MgcpUdpPacket* packet );
- /// will allocate a packet and fill in UDP header fields
- MgcpUdpPacket* allocatePacket( int payloadLength );
- ///
- MgcpUdpTransmitter& operator=(const MgcpUdpTransmitter& x)
- {
- return * this;
- }
- public: // statistics
- ///
- int bytesTransmitted;
- ///
- int packetsTransmitted;
- private:
- };
- /// Class to hold all types of exceptions that occur in the UDP stack
- class MgcpUdpException
- {
- public:
- ///
- MgcpUdpException( MgcpUdpErrorType theErrorType );
- ///
- const MgcpUdpErrorType errorType;
- };
- #endif