tutorial-06-brpc_pb_client.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <stdio.h>
  14. #include "workflow/WFFacilities.h"
  15. #include "echo_pb.srpc.h"
  16. using namespace srpc;
  17. static WFFacilities::WaitGroup wait_group(1);
  18. int main()
  19. {
  20. Example::BRPCClient client("127.0.0.1", 1412);
  21. //async
  22. EchoRequest req;
  23. req.set_message("Hello, srpc!");
  24. req.set_name("1412");
  25. client.Echo(&req, [](EchoResponse *resp, RPCContext *ctx) {
  26. if (ctx->success())
  27. printf("%s\n", resp->DebugString().c_str());
  28. else
  29. printf("status[%d] error[%d] errmsg:%s\n",
  30. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  31. const char *attachment;
  32. size_t len;
  33. while (ctx->get_attachment(&attachment, &len))
  34. printf("get attachment [%.*s] len=%zu\n", (int)len, attachment, len);
  35. wait_group.done();
  36. });
  37. //sync
  38. EchoRequest sync_req;
  39. EchoResponse sync_resp;
  40. RPCSyncContext sync_ctx;
  41. sync_req.set_message("Hello, srpc!");
  42. sync_req.set_name("Sync");
  43. client.Echo(&sync_req, &sync_resp, &sync_ctx);
  44. if (sync_ctx.success)
  45. printf("%s\n", sync_resp.DebugString().c_str());
  46. else
  47. printf("status[%d] error[%d] errmsg:%s\n",
  48. sync_ctx.status_code, sync_ctx.error, sync_ctx.errmsg.c_str());
  49. wait_group.wait();
  50. return 0;
  51. }