Advertisement
Guest User

relevant code

a guest
Dec 11th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. // ~~~ means there is more but irrelevant code
  2. // api.proto
  3. syntax = "proto3";
  4.  
  5. package dev.api;
  6.  
  7. service ApiService {
  8.   rpc Call(CallRequest) returns (CallResponse) {}
  9.   rpc ServerStream(SStreamRequest) returns (stream SStreamResponse) {}
  10.   rpc ClientStream(stream CStreamRequest) returns (CStreamResponse) {}
  11. }
  12. ~~~
  13.  
  14. // service_config.json
  15. {
  16.     "loadBalancingConfig": [
  17.         {
  18.             "round_robin": {}
  19.         }
  20.     ],
  21.     "methodConfig": [
  22.         {
  23.             "name": [
  24.                 {
  25.                     "service": {}
  26.                 }
  27.             ],
  28.             "retryPolicy": {
  29.                 "maxAttempts": 2,
  30.                 "initialBackoff": "1s",
  31.                 "maxBackoff": "120s",
  32.                 "backoffMultiplier": 1.5,
  33.                 "retryableStatusCodes": [
  34.                     "UNAVAILABLE"
  35.                 ]
  36.             }
  37.         }
  38.     ]
  39. }
  40.  
  41. // global.h
  42. ~~~
  43. constexpr std::string_view Address = "localhost:5067";
  44.  
  45. // https://github.com/grpc/grpc/blob/master/doc/environment_variables.md
  46. inline void setDebugEnvs()
  47. {
  48.     setenv("GRPC_DNS_RESOLVER", "ares", 1); // native
  49.     setenv("GRPC_ABORT_ON_LEAKS", "1", 1);
  50.     setenv("GRPC_TRACE",
  51.         "cares_resolver,dns_resolver,"
  52.         "client_channel,round_robin,"
  53.     ,1);
  54.     setenv("GRPC_VERBOSITY", "DEBUG", 1);
  55. }
  56.  
  57. inline std::optional<std::string> readFile(std::string_view path)
  58. {
  59.     std::ifstream file(path.data());
  60.     if (!file.is_open())
  61.         return std::nullopt;
  62.     return std::string(
  63.         std::istreambuf_iterator<char>{file},
  64.         std::istreambuf_iterator<char>{}
  65.     );
  66. }
  67.  
  68. // main.cpp
  69. int main()
  70. {
  71.     setDebugEnvs();
  72.     Server server;
  73.     server.start(Address);
  74.  
  75.     while (!gSigShutdown) {
  76.         std::this_thread::sleep_for(std::chrono::milliseconds(4000));
  77.     }
  78.  
  79.     std::cout << "Shutting down server..." << std::endl;
  80.     server.stop();
  81.  
  82.     return 0;
  83. }
  84.  
  85. // server.cpp
  86. ~~~
  87. bool Server::start(std::string_view address)
  88. {
  89.     std::unique_lock lock(d->mtx);
  90.  
  91.     if (d->state != ServerPrivate::CREATED)
  92.         return false;
  93.  
  94.     const auto serviceConfig = readFile("service_config.json");
  95.     if (!serviceConfig)
  96.         return false;
  97.  
  98.     std::cout << "Configuring Config: \n" << *serviceConfig << '\n';
  99.     const auto valid = grpc::experimental::ValidateServiceConfigJSON(*serviceConfig);
  100.     std::cout << std::format(
  101.         "Valid: {}\n", valid.empty() ? "true" : valid
  102.     );
  103.  
  104.     {
  105.         grpc::ServerBuilder builder;
  106.         builder.RegisterService(&d->service);
  107.         builder.AddListeningPort(address.data(), d->creds, &d->port);
  108.         builder.AddChannelArgument(GRPC_ARG_SERVICE_CONFIG, *serviceConfig);
  109.         d->cq = builder.AddCompletionQueue();
  110.         d->server = builder.BuildAndStart();
  111.     }
  112.  
  113.     std::cout << "Server listening on " << address << ':' << d->port << '\n';
  114.     new CallTag(d->cq.get(), &d->service);
  115.     new SStreamTag(d->cq.get(), &d->service);
  116.     new CStreamTag(d->cq.get(), &d->service);
  117.  
  118.     d->cqThread = std::jthread(std::bind_front(&ServerPrivate::handleAsyncRpc, d.get()));
  119.     d->state = ServerPrivate::STARTED;
  120.  
  121.     return true;
  122. }
  123. ~~~
  124.  
  125. // client.cpp
  126. int main()
  127. {
  128.     setDebugEnvs();
  129.     const auto channel = grpc::CreateChannel(Address.data(), grpc::InsecureChannelCredentials());
  130.     std::cout << std::format("Server config: \n{}\n", channel->GetServiceConfigJSON());
  131.     Client client(channel);
  132.     client.sstream(10);
  133.     // client.cstream(3);
  134.     // client.call();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement