rpc_global.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifdef _WIN32
  14. #include <workflow/PlatformSocket.h>
  15. #else
  16. #include <sys/socket.h>
  17. #include <arpa/inet.h>
  18. #include <netdb.h>
  19. #endif
  20. #include <workflow/WFGlobal.h>
  21. #include <workflow/URIParser.h>
  22. #include "rpc_basic.h"
  23. #include "rpc_global.h"
  24. namespace srpc
  25. {
  26. SRPCGlobal::SRPCGlobal() :
  27. gen(rd())
  28. {
  29. WFGlobal::register_scheme_port(SRPC_SCHEME, SRPC_DEFAULT_PORT);
  30. WFGlobal::register_scheme_port(SRPC_SSL_SCHEME, SRPC_SSL_DEFAULT_PORT);
  31. this->group_id = this->gen() % (1 << SRPC_GROUP_BITS);
  32. this->machine_id = this->gen() % (1 << SRPC_MACHINE_BITS);
  33. }
  34. static int __get_addr_info(const std::string& host, unsigned short port,
  35. struct sockaddr_storage *ss, socklen_t *ss_len)
  36. {
  37. if (!host.empty())
  38. {
  39. char front = host.front();
  40. char back = host.back();
  41. if (host.find(':') != std::string::npos)
  42. {
  43. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ss;
  44. memset(sin6, 0, sizeof (struct sockaddr_in6));
  45. if (inet_pton(AF_INET6, host.c_str(), &sin6->sin6_addr) == 1)
  46. {
  47. sin6->sin6_family = AF_INET6;
  48. sin6->sin6_port = htons(port);
  49. *ss_len = sizeof (struct sockaddr_in6);
  50. return 0;
  51. }
  52. }
  53. else if (isdigit(back) && isdigit(front))
  54. {
  55. struct sockaddr_in *sin = (struct sockaddr_in *)ss;
  56. memset(sin, 0, sizeof (struct sockaddr_in));
  57. if (inet_pton(AF_INET, host.c_str(), &sin->sin_addr) == 1)
  58. {
  59. sin->sin_family = AF_INET;
  60. sin->sin_port = htons(port);
  61. *ss_len = sizeof (struct sockaddr_in);
  62. return 0;
  63. }
  64. }
  65. }
  66. return -1;
  67. }
  68. bool SRPCGlobal::task_init(RPCClientParams& params, ParsedURI& uri,
  69. struct sockaddr_storage *ss, socklen_t *ss_len) const
  70. {
  71. if (!params.host.empty())
  72. {
  73. if (__get_addr_info(params.host, params.port, ss, ss_len) == 0)
  74. return true;
  75. if (params.is_ssl)
  76. uri.scheme = strdup(SRPC_SSL_SCHEME);
  77. else
  78. uri.scheme = strdup(SRPC_SCHEME);
  79. uri.host = strdup(params.host.c_str());
  80. uri.port = strdup(std::to_string(params.port).c_str());
  81. if (uri.scheme && uri.host && uri.port)
  82. uri.state = URI_STATE_SUCCESS;
  83. else
  84. {
  85. uri.state = URI_STATE_ERROR;
  86. uri.error = errno;
  87. }
  88. }
  89. else
  90. {
  91. URIParser::parse(params.url, uri);
  92. params.is_ssl = (uri.scheme &&
  93. (strcasecmp(uri.scheme, "https") == 0 ||
  94. strcasecmp(uri.scheme, "srpcs") == 0));
  95. }
  96. return false;
  97. }
  98. unsigned long long SRPCGlobal::get_random()
  99. {
  100. unsigned long long id = 0;
  101. this->snowflake.get_id(this->group_id, this->machine_id, &id);
  102. return id;
  103. }
  104. static const SRPCGlobal *srpc_global = SRPCGlobal::get_instance();
  105. } // namespace srpc