groupsock/include/NetInterface.hh

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 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "mTunnel" multicast access service
00017 // Copyright (c) 1996-2014 Live Networks, Inc.  All rights reserved.
00018 // Network Interfaces
00019 // C++ header
00020 
00021 #ifndef _NET_INTERFACE_HH
00022 #define _NET_INTERFACE_HH
00023 
00024 #ifndef _NET_ADDRESS_HH
00025 #include "NetAddress.hh"
00026 #endif
00027 
00028 class NetInterface {
00029 public:
00030   virtual ~NetInterface();
00031 
00032   static UsageEnvironment* DefaultUsageEnvironment;
00033       // if non-NULL, used for each new interfaces
00034 
00035 protected:
00036   NetInterface(); // virtual base class
00037 };
00038 
00039 class DirectedNetInterface: public NetInterface {
00040 public:
00041   virtual ~DirectedNetInterface();
00042 
00043   virtual Boolean write(unsigned char* data, unsigned numBytes) = 0;
00044 
00045   virtual Boolean SourceAddrOKForRelaying(UsageEnvironment& env,
00046                                           unsigned addr) = 0;
00047 
00048 protected:
00049   DirectedNetInterface(); // virtual base class
00050 };
00051 
00052 class DirectedNetInterfaceSet {
00053 public:
00054   DirectedNetInterfaceSet();
00055   virtual ~DirectedNetInterfaceSet();
00056 
00057   DirectedNetInterface* Add(DirectedNetInterface const* interf);
00058       // Returns the old value if different, otherwise 0
00059   Boolean Remove(DirectedNetInterface const* interf);
00060 
00061   Boolean IsEmpty() { return fTable->IsEmpty(); }
00062 
00063   // Used to iterate through the interfaces in the set
00064   class Iterator {
00065   public:
00066     Iterator(DirectedNetInterfaceSet& interfaces);
00067     virtual ~Iterator();
00068 
00069     DirectedNetInterface* next(); // NULL iff none
00070 
00071   private:
00072     HashTable::Iterator* fIter;
00073   };
00074 
00075 private:
00076   friend class Iterator;
00077   HashTable* fTable;
00078 };
00079 
00080 class Socket: public NetInterface {
00081 public:
00082   virtual ~Socket();
00083   void reset(); // closes the socket, and sets "fSocketNum" to -1
00084 
00085   virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
00086                              unsigned& bytesRead,
00087                              struct sockaddr_in& fromAddress) = 0;
00088       // Returns False on error; resultData == NULL if data ignored
00089 
00090   int socketNum() const { return fSocketNum; }
00091 
00092   Port port() const {
00093     return fPort;
00094   }
00095 
00096   UsageEnvironment& env() const { return fEnv; }
00097 
00098   static int DebugLevel;
00099 
00100 protected:
00101   Socket(UsageEnvironment& env, Port port); // virtual base class
00102 
00103   Boolean changePort(Port newPort); // will also cause socketNum() to change
00104 
00105 private:
00106   int fSocketNum;
00107   UsageEnvironment& fEnv;
00108   Port fPort;
00109 };
00110 
00111 UsageEnvironment& operator<<(UsageEnvironment& s, const Socket& sock);
00112 
00113 // A data structure for looking up a Socket by port:
00114 
00115 class SocketLookupTable {
00116 public:
00117   virtual ~SocketLookupTable();
00118 
00119   Socket* Fetch(UsageEnvironment& env, Port port, Boolean& isNew);
00120   // Creates a new Socket if none already exists
00121   Boolean Remove(Socket const* sock);
00122 
00123 protected:
00124   SocketLookupTable(); // abstract base class
00125   virtual Socket* CreateNew(UsageEnvironment& env, Port port) = 0;
00126 
00127 private:
00128   HashTable* fTable;
00129 };
00130 
00131 // A data structure for counting traffic:
00132 
00133 class NetInterfaceTrafficStats {
00134 public:
00135   NetInterfaceTrafficStats();
00136 
00137   void countPacket(unsigned packetSize);
00138 
00139   float totNumPackets() const {return fTotNumPackets;}
00140   float totNumBytes() const {return fTotNumBytes;}
00141 
00142   Boolean haveSeenTraffic() const;
00143 
00144 private:
00145   float fTotNumPackets;
00146   float fTotNumBytes;
00147 };
00148 
00149 #endif

Generated on Thu Oct 16 16:17:10 2014 for live by  doxygen 1.5.2