/indra/llmessage/llhost.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 151 lines · 91 code · 27 blank · 33 comment · 12 complexity · 9e3ee3ad05bbb7c5c451d44a79a9c483 MD5 · raw file

  1. /**
  2. * @file llhost.h
  3. * @brief a LLHost uniquely defines a host (Simulator, Proxy or other)
  4. * across the network
  5. *
  6. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_LLHOST_H
  28. #define LL_LLHOST_H
  29. #include <iostream>
  30. #include <string>
  31. #include "net.h"
  32. const U32 INVALID_PORT = 0;
  33. const U32 INVALID_HOST_IP_ADDRESS = 0x0;
  34. class LLHost {
  35. protected:
  36. U32 mPort;
  37. U32 mIP;
  38. public:
  39. static LLHost invalid;
  40. // CREATORS
  41. LLHost()
  42. : mPort(INVALID_PORT),
  43. mIP(INVALID_HOST_IP_ADDRESS)
  44. { } // STL's hash_map expect this T()
  45. LLHost( U32 ipv4_addr, U32 port )
  46. : mPort( port )
  47. {
  48. mIP = ipv4_addr;
  49. }
  50. LLHost( const std::string& ipv4_addr, U32 port )
  51. : mPort( port )
  52. {
  53. mIP = ip_string_to_u32(ipv4_addr.c_str());
  54. }
  55. explicit LLHost(const U64 ip_port)
  56. {
  57. U32 ip = (U32)(ip_port >> 32);
  58. U32 port = (U32)(ip_port & (U64)0xFFFFFFFF);
  59. mIP = ip;
  60. mPort = port;
  61. }
  62. explicit LLHost(const std::string& ip_and_port);
  63. ~LLHost()
  64. { }
  65. // MANIPULATORS
  66. void set( U32 ip, U32 port ) { mIP = ip; mPort = port; }
  67. void set( const std::string& ipstr, U32 port ) { mIP = ip_string_to_u32(ipstr.c_str()); mPort = port; }
  68. void setAddress( const std::string& ipstr ) { mIP = ip_string_to_u32(ipstr.c_str()); }
  69. void setAddress( U32 ip ) { mIP = ip; }
  70. void setPort( U32 port ) { mPort = port; }
  71. BOOL setHostByName(const std::string& hname);
  72. LLHost& operator=(const LLHost &rhs);
  73. void invalidate() { mIP = INVALID_HOST_IP_ADDRESS; mPort = INVALID_PORT;};
  74. // READERS
  75. U32 getAddress() const { return mIP; }
  76. U32 getPort() const { return mPort; }
  77. BOOL isOk() const { return (mIP != INVALID_HOST_IP_ADDRESS) && (mPort != INVALID_PORT); }
  78. size_t hash() const { return (mIP << 16) | (mPort & 0xffff); }
  79. std::string getString() const;
  80. std::string getIPString() const;
  81. std::string getHostName() const;
  82. std::string getIPandPort() const;
  83. friend std::ostream& operator<< (std::ostream& os, const LLHost &hh);
  84. // This operator is not well defined. does it expect a
  85. // "192.168.1.1:80" notation or "int int" format? Phoenix 2007-05-18
  86. //friend std::istream& operator>> (std::istream& is, LLHost &hh);
  87. friend bool operator==( const LLHost &lhs, const LLHost &rhs );
  88. friend bool operator!=( const LLHost &lhs, const LLHost &rhs );
  89. friend bool operator<(const LLHost &lhs, const LLHost &rhs);
  90. };
  91. // Function Object required for STL templates using LLHost as key
  92. class LLHostHash
  93. {
  94. public:
  95. size_t operator() (const LLHost &hh) const { return hh.hash(); }
  96. };
  97. inline bool operator==( const LLHost &lhs, const LLHost &rhs )
  98. {
  99. return (lhs.mIP == rhs.mIP) && (lhs.mPort == rhs.mPort);
  100. }
  101. inline bool operator!=( const LLHost &lhs, const LLHost &rhs )
  102. {
  103. return (lhs.mIP != rhs.mIP) || (lhs.mPort != rhs.mPort);
  104. }
  105. inline bool operator<(const LLHost &lhs, const LLHost &rhs)
  106. {
  107. if (lhs.mIP < rhs.mIP)
  108. {
  109. return true;
  110. }
  111. if (lhs.mIP > rhs.mIP)
  112. {
  113. return false;
  114. }
  115. if (lhs.mPort < rhs.mPort)
  116. {
  117. return true;
  118. }
  119. else
  120. {
  121. return false;
  122. }
  123. }
  124. #endif // LL_LLHOST_H