EndpointInfo.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "servant/EndpointInfo.h"
  17. #include "servant/TarsLogger.h"
  18. #include "servant/NetworkUtil.h"
  19. #include "util/tc_socket.h"
  20. namespace tars
  21. {
  22. EndpointInfo::EndpointInfo()
  23. : _port(0)
  24. , _grid(0)
  25. , _qos(0)
  26. , _type(TCP)
  27. , _weight(-1)
  28. , _weighttype(0)
  29. , _authType(0)
  30. , _isIPv6(false)
  31. {
  32. _setDivision.clear();
  33. memset(&_addr,0,sizeof(_addr));
  34. }
  35. EndpointInfo::EndpointInfo(const string& host, uint16_t port, EndpointInfo::EType type, int32_t grid, const string & setDivision, int qos, int weight, unsigned int weighttype, int authType)
  36. : _host(host)
  37. , _port(port)
  38. , _grid(grid)
  39. , _qos(qos)
  40. , _type(type)
  41. , _setDivision(setDivision)
  42. , _weight(weight)
  43. , _weighttype(weighttype)
  44. , _authType(authType)
  45. {
  46. _isIPv6 = TC_Socket::addressIsIPv6(host);
  47. try
  48. {
  49. if(_weighttype == 0)
  50. {
  51. _weight = -1;
  52. }
  53. else
  54. {
  55. if(_weight == -1)
  56. {
  57. _weight = 100;
  58. }
  59. _weight = (_weight > 100 ? 100 : _weight);
  60. }
  61. if (_isIPv6)
  62. {
  63. NetworkUtil::getAddress(_host, _port, _addr.in6);
  64. }
  65. else
  66. {
  67. NetworkUtil::getAddress(_host, _port, _addr.in);
  68. }
  69. _cmpDesc = createCompareDesc();
  70. _desc = createDesc();
  71. }
  72. catch (...)
  73. {
  74. TLOGERROR("[ERROR:getAddress fail:" << _host << ":" << _port << "]" << endl);
  75. }
  76. }
  77. string EndpointInfo::createCompareDesc()
  78. {
  79. ostringstream os;
  80. if (_type == EndpointInfo::UDP) { os << "udp:"; }
  81. if (_type == EndpointInfo::TCP) { os << "tcp:"; }
  82. if (_type == EndpointInfo::SSL) { os << "ssl:"; }
  83. os << _grid << ":" << _host << ":" << _port
  84. << ":" << _setDivision << ":" << _qos << ":" << _weight << ":" << _weighttype << ":" << _authType;
  85. return os.str();
  86. }
  87. string EndpointInfo::createDesc() const
  88. {
  89. ostringstream os;
  90. if (_type == EndpointInfo::TCP)
  91. os << "tcp";
  92. else if (_type == EndpointInfo::UDP)
  93. os << "udp";
  94. else
  95. os << "ssl";
  96. os << " -h " << host();
  97. os << " -p " << port();
  98. if(0 != _grid)
  99. os << " -g " << _grid;
  100. if (!_setDivision.empty())
  101. {
  102. os << " -s " << _setDivision;
  103. }
  104. if(0 != _qos)
  105. os << " -q " << _qos;
  106. if(0 != _authType)
  107. os << " -e " << _authType;
  108. return os.str();
  109. }
  110. bool EndpointInfo::operator == (const EndpointInfo& r) const
  111. {
  112. return (_cmpDesc == r._cmpDesc);
  113. }
  114. bool EndpointInfo::operator < (const EndpointInfo& r) const
  115. {
  116. return (_cmpDesc < r._cmpDesc);
  117. }
  118. string EndpointInfo::host() const
  119. {
  120. return string(_host);
  121. }
  122. int32_t EndpointInfo::grid() const
  123. {
  124. return _grid;
  125. }
  126. uint16_t EndpointInfo::port() const
  127. {
  128. return _port;
  129. }
  130. const struct sockaddr_in& EndpointInfo::addr() const
  131. {
  132. return _addr.in;
  133. }
  134. const struct sockaddr * EndpointInfo::addrPtr() const
  135. {
  136. return _isIPv6 ? (struct sockaddr *)&_addr.in6 : (struct sockaddr *)&_addr.in;
  137. }
  138. EndpointInfo::EType EndpointInfo::type() const
  139. {
  140. return _type;
  141. }
  142. const string& EndpointInfo::setDivision() const
  143. {
  144. return _setDivision;
  145. }
  146. ///////////////////////////////////////////////////////////
  147. }