NetInterface.hh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "mTunnel" multicast access service
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // Network Interfaces
  17. // C++ header
  18. #ifndef _NET_INTERFACE_HH
  19. #define _NET_INTERFACE_HH
  20. #ifndef _NET_ADDRESS_HH
  21. #include "NetAddress.hh"
  22. #endif
  23. class NetInterface {
  24. public:
  25. virtual ~NetInterface();
  26. static UsageEnvironment* DefaultUsageEnvironment;
  27. // if non-NULL, used for each new interfaces
  28. protected:
  29. NetInterface(); // virtual base class
  30. };
  31. class DirectedNetInterface: public NetInterface {
  32. public:
  33. virtual ~DirectedNetInterface();
  34. virtual Boolean write(unsigned char* data, unsigned numBytes) = 0;
  35. virtual Boolean SourceAddrOKForRelaying(UsageEnvironment& env,
  36. unsigned addr) = 0;
  37. protected:
  38. DirectedNetInterface(); // virtual base class
  39. };
  40. class DirectedNetInterfaceSet {
  41. public:
  42. DirectedNetInterfaceSet();
  43. virtual ~DirectedNetInterfaceSet();
  44. DirectedNetInterface* Add(DirectedNetInterface const* interf);
  45. // Returns the old value if different, otherwise 0
  46. Boolean Remove(DirectedNetInterface const* interf);
  47. Boolean IsEmpty() { return fTable->IsEmpty(); }
  48. // Used to iterate through the interfaces in the set
  49. class Iterator {
  50. public:
  51. Iterator(DirectedNetInterfaceSet& interfaces);
  52. virtual ~Iterator();
  53. DirectedNetInterface* next(); // NULL iff none
  54. private:
  55. HashTable::Iterator* fIter;
  56. };
  57. private:
  58. friend class Iterator;
  59. HashTable* fTable;
  60. };
  61. class Socket: public NetInterface {
  62. public:
  63. virtual ~Socket();
  64. void reset(); // closes the socket, and sets "fSocketNum" to -1
  65. virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
  66. unsigned& bytesRead,
  67. struct sockaddr_in& fromAddress) = 0;
  68. // Returns False on error; resultData == NULL if data ignored
  69. int socketNum() const { return fSocketNum; }
  70. Port port() const {
  71. return fPort;
  72. }
  73. UsageEnvironment& env() const { return fEnv; }
  74. static int DebugLevel;
  75. protected:
  76. Socket(UsageEnvironment& env, Port port); // virtual base class
  77. Boolean changePort(Port newPort); // will also cause socketNum() to change
  78. private:
  79. int fSocketNum;
  80. UsageEnvironment& fEnv;
  81. Port fPort;
  82. };
  83. UsageEnvironment& operator<<(UsageEnvironment& s, const Socket& sock);
  84. // A data structure for looking up a Socket by port:
  85. class SocketLookupTable {
  86. public:
  87. virtual ~SocketLookupTable();
  88. Socket* Fetch(UsageEnvironment& env, Port port, Boolean& isNew);
  89. // Creates a new Socket if none already exists
  90. Boolean Remove(Socket const* sock);
  91. protected:
  92. SocketLookupTable(); // abstract base class
  93. virtual Socket* CreateNew(UsageEnvironment& env, Port port) = 0;
  94. private:
  95. HashTable* fTable;
  96. };
  97. // A data structure for counting traffic:
  98. class NetInterfaceTrafficStats {
  99. public:
  100. NetInterfaceTrafficStats();
  101. void countPacket(unsigned packetSize);
  102. float totNumPackets() const {return fTotNumPackets;}
  103. float totNumBytes() const {return fTotNumBytes;}
  104. Boolean haveSeenTraffic() const;
  105. private:
  106. float fTotNumPackets;
  107. float fTotNumBytes;
  108. };
  109. #endif