netbrowse.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // netbrowse.h
  19. // Project: RSPiX
  20. //
  21. // History:
  22. // 08/31/97 MJR Started.
  23. //
  24. ////////////////////////////////////////////////////////////////////////////////
  25. #ifndef NETBROWSE_H
  26. #define NETBROWSE_H
  27. #include "RSPiX.h"
  28. #include "socket.h"
  29. #include "net.h"
  30. ////////////////////////////////////////////////////////////////////////////////
  31. //
  32. // CNetBrowse makes it easy to browse for hosts
  33. //
  34. ////////////////////////////////////////////////////////////////////////////////
  35. class CNetBrowse
  36. {
  37. //------------------------------------------------------------------------------
  38. // Classes
  39. //------------------------------------------------------------------------------
  40. public:
  41. ////////////////////////////////////////////////////////////////////////////////
  42. // CHost contains basic info about a host
  43. ////////////////////////////////////////////////////////////////////////////////
  44. class CHost
  45. {
  46. public:
  47. char m_acName[Net::MaxHostNameSize]; // Name
  48. RSocket::Address m_address; // Address
  49. long m_lMagic; // Magic number
  50. long m_lLastHeardFrom; // Time we last heard from this host
  51. U32 m_u32User; // User-definable value
  52. public:
  53. // Constructor
  54. CHost()
  55. {
  56. m_acName[0] = 0;
  57. m_address.Reset();
  58. m_lMagic = 0;
  59. m_lLastHeardFrom = 0;
  60. m_u32User = 0;
  61. }
  62. // Destructor
  63. ~CHost()
  64. {
  65. }
  66. // operator=
  67. const CHost& operator=(const CHost& rhs)
  68. {
  69. strncpy(m_acName, rhs.m_acName, sizeof(m_acName));
  70. m_address = rhs.m_address;
  71. m_lMagic = rhs.m_lMagic;
  72. m_u32User = rhs.m_u32User;
  73. return *this;
  74. }
  75. // Compare two hosts
  76. bool IsSameHost(const CHost* rhs) const
  77. {
  78. // We ignore the user value in comparisons!!!!
  79. if ((strcmp(m_acName, rhs->m_acName) == 0) &&
  80. (m_address == rhs->m_address) &&
  81. (m_lMagic == rhs->m_lMagic))
  82. return true;
  83. return false;
  84. }
  85. };
  86. //------------------------------------------------------------------------------
  87. // Types, enums, etc.
  88. //------------------------------------------------------------------------------
  89. public:
  90. // List of hosts
  91. typedef RFList<CHost> Hosts;
  92. //------------------------------------------------------------------------------
  93. // Variables
  94. //------------------------------------------------------------------------------
  95. protected:
  96. RSocket m_socketBrowse; // Socket used to browse for hosts
  97. long m_lLastBroadcast; // Last broadcast time
  98. unsigned short m_usBasePort; // Base port
  99. //------------------------------------------------------------------------------
  100. // Functions
  101. //------------------------------------------------------------------------------
  102. public:
  103. ////////////////////////////////////////////////////////////////////////////////
  104. // Constructor
  105. ////////////////////////////////////////////////////////////////////////////////
  106. CNetBrowse();
  107. ////////////////////////////////////////////////////////////////////////////////
  108. // Destructor
  109. ////////////////////////////////////////////////////////////////////////////////
  110. ~CNetBrowse();
  111. ////////////////////////////////////////////////////////////////////////////////
  112. // Reset
  113. ////////////////////////////////////////////////////////////////////////////////
  114. void Reset(void);
  115. ////////////////////////////////////////////////////////////////////////////////
  116. // Startup
  117. ////////////////////////////////////////////////////////////////////////////////
  118. short Startup( // Returns 0 if sucessfull, non-zero otherwise
  119. USHORT usPort, // In: Server's base port number
  120. RSocket::BLOCK_CALLBACK callback); // In: Blocking callback
  121. ////////////////////////////////////////////////////////////////////////////////
  122. // Shutdown
  123. ////////////////////////////////////////////////////////////////////////////////
  124. void Shutdown(void);
  125. ////////////////////////////////////////////////////////////////////////////////
  126. // Update (must be called regularly!)
  127. //
  128. // The lists are updated, if necessary. Note that only the phostsAll is
  129. // important to this function, as it uses that list as the basis of its
  130. // decisions to add or remove hosts. This function will simply add to the
  131. // other two lists as needed -- it does not care what they contain. It is up
  132. // to the caller to decide whether and when to clear those lists.
  133. ////////////////////////////////////////////////////////////////////////////////
  134. void Update(
  135. Hosts* phostsAll, // I/O: List of all hosts
  136. Hosts* phostsAdded, // I/O: List of hosts that were added
  137. Hosts* phostsRemoved); // I/O: List of hosts that were removed
  138. ////////////////////////////////////////////////////////////////////////////////
  139. // Lookup host by name or hardwired address (like a TCP/IP dotted address).
  140. // The specified port must be the host's "base port".
  141. ////////////////////////////////////////////////////////////////////////////////
  142. static
  143. short LookupHost( // Returns 0 if successfull, non-zero otherwise
  144. char* pszName, // In: Server's name or dotted address (x.x.x.x)
  145. USHORT usPort, // In: Server's port number
  146. RSocket::Address* paddress); // Out: Addresss
  147. };
  148. #endif //NETBROWSE_H
  149. ////////////////////////////////////////////////////////////////////////////////
  150. // EOF
  151. ////////////////////////////////////////////////////////////////////////////////