UdpStack.hxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:12k
- #ifndef UDP_HXX
- #define UDP_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 UdpStackHeaderVersion =
- "$Id: UdpStack.hxx,v 1.2 2001/06/27 01:43:47 bko Exp $";
- #include "global.h"
- #include <fstream>
- #include <string>
- #include <assert.h>
- #include "NetworkAddress.h"
- #ifdef __vxworks
- #include <selectLib.h>
- #else
- #include <sys/select.h>
- #endif
- typedef enum
- {
- inactive,
- sendonly,
- recvonly,
- sendrecv
- } UdpMode;
- // this class hold stuff that is likely to be a problem to port
- class UdpStackPrivateData;
- /// classs implements a UDP transmitter AND a reciever
- class UdpStack
- {
- /* Issues: Current no support for saying which interface a packet
- arrived on. I'm not sure why that woudl be usefull but I think
- someone requested it once. */
- public:
- /** no ports can be specified and the stack will pick one, OR a minPort
- can be specified and it will be used, OR a min and max can be
- specified and the stack will use the first free port in that
- range. The destiantion hostname and port may be set in the
- destinationHost paramter **/
- UdpStack ( const NetworkAddress* destinationHost = NULL,
- int localMinPort = -1,
- int localMaxPort = -1,
- UdpMode mode = sendrecv,
- bool log_flag = false,
- bool isMulticast = false);
- /// connectPorts() has to be called before using send()
- /// It is associate the local port to remote port
- /// so that receive and send only from and to that remote port
- void connectPorts();
- /// disconnectPorts() dissolve the port association
- void disconnectPorts();
- /// set the local ports
- void setLocal (const int minPort = -1, int maxPort = -1 );
- /// set the default destination
- void setDestination ( const NetworkAddress* host );
- /// set the default destination
- void setDestination ( const char* host, int port );
- /// Get the udp stack mode
- UdpMode getMode ()
- {
- return mode;
- };
- /// Get the udp stack logFlag
- bool getLogFlag ()
- {
- return logFlag;
- };
- /// Get the name for local
- string getLclName () const
- {
- return lclName;
- };
- /// Get the name for remote
- string getRmtName () const
- {
- return rmtName;
- };
- /// Get the port being used for recieving
- int getRxPort ()
- {
- return localPort;
- };
- /// Get the port being used for transmitting
- int getTxPort ()
- {
- return localPort;
- };
- /// Get the destination port
- int getDestinationPort ()
- {
- return remotePort;
- };
- ///
- NetworkAddress* getDestinationHost () const;
- /// Set the mode
- void setMode ( const UdpMode theMode )
- {
- mode = theMode;
- };
- /// Set the logFlag
- void setLogFlag ( const bool theLogFlag )
- {
- logFlag = theLogFlag;
- };
- /// Set the name to something logical - only used for errors
- void setLclName ( const string& theName )
- {
- lclName = theName;
- };
- void setRmtName ( const string& theName )
- {
- rmtName = theName;
- };
- /// Get the file descriptor for the socket - use with care or not at all
- int getSocketFD ();
- /// Add this stacks file descriptors to the the fdSet
- void addToFdSet ( fd_set* set );
- /// Find the max of any file descripts in this stack and the passed value
- int getMaxFD ( int prevMax = 0 );
- /// Check and see if this stacks file descriptor is set in fd_set
- bool checkIfSet ( fd_set* set );
- /// Recive a datagram into the specifed buffer - returns size of mesg.
- /// Once connectPorts() is called, can't receive from other ports
- int receive ( const char* buffer,
- const int bufSize ); // returns bytes read
- /// Recive a datagram into the specifed buffer - returns size of mesg.
- /// Use receive if you do not need to be able to tell who this packet
- /// came from
- int receiveFrom ( const char* buffer,
- const int bufSize,
- NetworkAddress* sender ); // returns bytes read
- /// Recive a datagram into the specifed buffer - returns size of mesg.
- /// if successfull, -1 for error and 0 for timeout.
- int receiveTimeout ( const char* buffer,
- const int bufSize,
- NetworkAddress* sender,
- int sec = 0,
- int usec = 0);
- /// Transmit a datagram
- /// ConnectPorts() should be called before it is called
- void transmit ( const char* buffer,
- const int length );
- /// Transmit a datagram - use transmit if you always sent to the same person
- int transmitTo ( const char* buffer,
- const int length,
- const NetworkAddress* dest );
- ///
- void joinMulticastGroup ( NetworkAddress group,
- NetworkAddress* iface = NULL,
- int ifaceInexe = 0 );
- ///
- void leaveMulticastGroup( NetworkAddress group,
- NetworkAddress* iface = NULL,
- int ifaceInexe = 0 );
- ///
- virtual ~UdpStack ();
- /// Get the number of bytes the stack has processed
- int getBytesTransmitted ();
- /// Get the number of datagrams the stack has processed
- int getPacketsTransmitted ();
- /// Get the number of bytes the stack has processed
- int getBytesReceived ();
- /// Get the number of datagrams the stack has processed
- int getPacketsReceived ();
- /** This can be used to eulate network packet loss by throwing
- out a certain percentage of the tranmitted packets */
- void emulatePacketLoss ( float probabilityOfLoss )
- {
- packetLossProbability = probabilityOfLoss;
- };
- private:
- /// Do not use
- UdpStack& operator= ( const UdpStack& x )
- {
- assert ( 0 );
- return *this;
- };
- /// Do not use
- UdpStack( const UdpStack& )
- {
- assert ( 0 );
- };
- void doServer ( int minPort,
- int maxPort );
- void doClient ( const NetworkAddress* desName,
- int iPort );
- private:
- /// port the stack is currently using
- int localPort;
- /// port the stack is currently using
- int remotePort;
- /// name of receiver
- string lclName;
- /// name of transmitter
- string rmtName;
- /// probability of a given packet being thrown out by the stack
- float packetLossProbability;
- /// number of bytes the stack has received
- int numBytesReceived;
- /// number of datagrams the stack has received
- int numPacketsReceived;
- /// number of bytes the stack has transimitted
- int numBytesTransmitted;
- /// number of datagrams the stack has transimitted
- int numPacketsTransmitted;
- /// Mode
- UdpMode mode;
- /// flag for msg log
- bool logFlag;
- /// private data that will be a pain to port
- UdpStackPrivateData* data;
- ofstream* in_log;
- ofstream* out_log;
- int rcvCount ;
- int sndCount ;
- VMutex _lock;
- };
- /// Class to hold all types of exceptions that occur in the UDP stack
- class UdpStackException
- {
- public:
- ///
- UdpStackException ( const string& description )
- : desc(description)
- {}
- ;
- string getDescription() const
- {
- return desc;
- }
- private:
- string desc;
- friend std::ostream& operator<< ( std::ostream& strm , const UdpStackException& e );
- };
- inline std::ostream&
- operator<< ( std::ostream& strm , const UdpStackException& e )
- {
- strm << e.desc;
- return strm;
- }
- //e exception class for when a transmited packet is renused
- class UdpStackExceptionConectionRefused: public UdpStackException
- {
- public:
- ///
- UdpStackExceptionConectionRefused( const string& description )
- : UdpStackException(description)
- {}
- ;
- };
- /// exception class for when all ports all already in use
- class UdpStackExceptionPortsInUse: public UdpStackException
- {
- public:
- ///
- UdpStackExceptionPortsInUse ( const string& description )
- : UdpStackException(description)
- {}
- ;
- };
- /// exception class for when a bogus hostname is given
- class UdpStackExceptionBadHostname: public UdpStackException
- {
- public:
- ///
- UdpStackExceptionBadHostname( const string& description )
- : UdpStackException(description)
- {}
- ;
- };
- /// exception class for when a bogus hostname is given
- class UdpStackExceptionBadPort: public UdpStackException
- {
- public:
- ///
- UdpStackExceptionBadPort( const string& description )
- : UdpStackException(description)
- {}
- ;
- };
- #endif