EndpointInfo.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifndef __TARS_ENDPOINT_INFO_H_
  17. #define __TARS_ENDPOINT_INFO_H_
  18. //#include "servant/Global.h"
  19. #include "util/tc_socket.h"
  20. #include "util/tc_clientsocket.h"
  21. #include "servant/EndpointF.h"
  22. //#include "AuthF.h"
  23. #if TARGET_PLATFORM_WINDOWS
  24. #include <WS2tcpip.h>
  25. #endif
  26. using namespace std;
  27. namespace tars
  28. {
  29. /**
  30. * 地址信息IP:Port
  31. */
  32. class EndpointInfo
  33. {
  34. public:
  35. /**
  36. * 构造函数
  37. */
  38. EndpointInfo();
  39. /**
  40. *
  41. * @param ep
  42. */
  43. EndpointInfo(const TC_Endpoint &ep, const string &setDivision="");
  44. /**
  45. * 构造函数
  46. * @param host
  47. * @param port
  48. * @param type
  49. */
  50. EndpointInfo(const EndpointF &ep);
  51. /**
  52. * get endpoint
  53. * @return
  54. */
  55. inline const TC_Endpoint &getEndpoint() const { return _ep; }
  56. /**
  57. *
  58. * @return
  59. */
  60. inline TC_Endpoint &getEndpoint() { return _ep; }
  61. /**
  62. * 地址的字符串描述(用于比较)
  63. *
  64. * @return string
  65. */
  66. inline const string & cmpDesc() const { return _cmpDesc; }
  67. /**
  68. * 地址的字符串描述
  69. *
  70. * @return string
  71. */
  72. inline const string & desc() const { return _desc; }
  73. /**
  74. *
  75. * @return
  76. */
  77. inline bool isTcp() const { return _ep.isTcp(); }
  78. /**
  79. *
  80. * @return
  81. */
  82. inline bool isSsl() const { return _ep.isSSL(); }
  83. /**
  84. *
  85. *
  86. */
  87. inline bool isUdp() const { return _ep.isUdp(); }
  88. /**
  89. * 获取主机名
  90. *
  91. * @return const string&
  92. */
  93. const string &host() const { return _ep.getHost(); }
  94. /**
  95. * 获取端口号
  96. *
  97. * @return uint16_t
  98. */
  99. uint16_t port() const { return _ep.getPort(); }
  100. /**
  101. * 获取路由状态(不再使用)
  102. * @return int32_t
  103. */
  104. inline int32_t grid() const { return _ep.getGrid(); }
  105. /*
  106. * 获取qos的descp值
  107. */
  108. inline int32_t qos() const {return _ep.getQos();}
  109. /*
  110. * 获取节点的静态权重值
  111. */
  112. inline int weight() const {return _ep.getWeight();}
  113. /**
  114. * @brief 获取节点的权重使用方式
  115. */
  116. inline unsigned int getWeightType() const { return _ep.getWeightType(); }
  117. /**
  118. * 返回端口类型
  119. *
  120. * @return EndpointInfo::EType
  121. */
  122. inline TC_Endpoint::EType type() const { return _ep.getType(); }
  123. /**
  124. *返回set分组信息
  125. *
  126. *@return string
  127. */
  128. inline const string& setDivision() const { return _setDivision; }
  129. /*
  130. * 获取认证类型
  131. */
  132. inline TC_Endpoint::AUTH_TYPE authType() const { return _ep.getAuthType(); }
  133. /**
  134. * @brief is ipv6 socket or not
  135. * @return true if is ipv6
  136. */
  137. inline bool isIPv6() const { return _ep.isIPv6(); }
  138. /**
  139. * 等于
  140. * @param r
  141. *
  142. * @return bool
  143. */
  144. inline bool operator == (const EndpointInfo& r) const { return (_cmpDesc == r._cmpDesc); }
  145. /**
  146. * 小于
  147. * @param r
  148. *
  149. * @return bool
  150. */
  151. inline bool operator < (const EndpointInfo& r) const { return (_cmpDesc < r._cmpDesc); }
  152. protected:
  153. /**
  154. * 地址的字符串描述
  155. * @param bWithSetInfo,标识
  156. * @return string
  157. */
  158. string createDesc() const { return _ep.toString(); }
  159. /**
  160. * 详细地址字符串描述
  161. *
  162. * @return string
  163. */
  164. string createCompareDesc();
  165. private:
  166. /**
  167. * 服务地址
  168. */
  169. TC_Endpoint _ep;
  170. /**
  171. *set分组信息
  172. */
  173. string _setDivision;
  174. /**
  175. * 比较的地址字符串描述
  176. */
  177. string _cmpDesc;
  178. /**
  179. * 地址的字符串描述
  180. */
  181. string _desc;
  182. };
  183. /////////////////////////////////////////////////////////////////////////////
  184. }
  185. #endif