rpc_global.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef __RPC_GLOBAL_H__
  14. #define __RPC_GLOBAL_H__
  15. #include <random>
  16. #include <workflow/URIParser.h>
  17. #include "rpc_options.h"
  18. #include "rpc_module.h"
  19. namespace srpc
  20. {
  21. class SRPCGlobal
  22. {
  23. public:
  24. static SRPCGlobal *get_instance()
  25. {
  26. static SRPCGlobal kInstance;
  27. return &kInstance;
  28. }
  29. public:
  30. const char *get_srpc_version() const;
  31. bool task_init(RPCClientParams& params, ParsedURI& uri,
  32. struct sockaddr_storage *ss, socklen_t *ss_len) const;
  33. unsigned long long get_random();
  34. void set_group_id(unsigned short id) { this->group_id = id; }
  35. void set_machine_id(unsigned short id) { this->machine_id = id; }
  36. private:
  37. SRPCGlobal();
  38. SnowFlake snowflake;
  39. std::random_device rd;
  40. std::mt19937 gen;
  41. unsigned short group_id;
  42. unsigned short machine_id;
  43. };
  44. } // namespace srpc
  45. #endif