client_protobuf.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. #include "workflow/WFFacilities.h"
  3. #include "srpc/rpc_types.h"
  4. #include "%s.srpc.h"
  5. #include "config/config.h"
  6. using namespace srpc;
  7. static WFFacilities::WaitGroup wait_group(1);
  8. static srpc::RPCConfig config;
  9. void init()
  10. {
  11. if (config.load("./client.conf") == false)
  12. {
  13. perror("Load config failed");
  14. exit(1);
  15. }
  16. }
  17. int main()
  18. {
  19. // 1. load config
  20. init();
  21. // 2. start client
  22. RPCClientParams params = RPC_CLIENT_PARAMS_DEFAULT;
  23. params.host = config.client_host();
  24. params.port = config.client_port();
  25. %s
  26. %s::%sClient client(&params);
  27. config.load_filter(client);
  28. // 3. request with sync api
  29. EchoRequest req;
  30. EchoResponse resp;
  31. RPCSyncContext ctx;
  32. req.set_message("Hello, this is sync request!");
  33. client.Echo(&req, &resp, &ctx);
  34. if (ctx.success)
  35. fprintf(stderr, "sync resp. %%s\n", resp.DebugString().c_str());
  36. else
  37. fprintf(stderr, "sync status[%%d] error[%%d] errmsg:%%s\n",
  38. ctx.status_code, ctx.error, ctx.errmsg.c_str());
  39. // 4. request with async api
  40. req.set_message("Hello, this is async request!");
  41. client.Echo(&req, [](EchoResponse *resp, RPCContext *ctx) {
  42. if (ctx->success())
  43. fprintf(stderr, "async resp. %%s\n", resp->DebugString().c_str());
  44. else
  45. fprintf(stderr, "async status[%%d] error[%%d] errmsg:%%s\n",
  46. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  47. wait_group.done();
  48. });
  49. wait_group.wait();
  50. return 0;
  51. }