tutorial-06-brpc_pb_client.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "echo_pb.srpc.h"
  15. using namespace srpc;
  16. int main()
  17. {
  18. Example::BRPCClient client("127.0.0.1", 1412);
  19. //async
  20. EchoRequest req;
  21. req.set_message("Hello, sogou rpc!");
  22. req.set_name("1412");
  23. client.Echo(&req, [](EchoResponse *response, RPCContext *ctx) {
  24. if (ctx->success())
  25. printf("%s\n", response->DebugString().c_str());
  26. else
  27. printf("status[%d] error[%d] errmsg:%s\n",
  28. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  29. });
  30. //sync
  31. EchoRequest sync_req;
  32. EchoResponse sync_resp;
  33. RPCSyncContext sync_ctx;
  34. sync_req.set_message("Hello, sogou rpc!");
  35. sync_req.set_name("Sync");
  36. client.Echo(&sync_req, &sync_resp, &sync_ctx);
  37. if (sync_ctx.success)
  38. printf("%s\n", sync_resp.DebugString().c_str());
  39. else
  40. printf("status[%d] error[%d] errmsg:%s\n",
  41. sync_ctx.status_code, sync_ctx.error, sync_ctx.errmsg.c_str());
  42. return 0;
  43. }