tutorial-14-trpc_http_client.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright (c) 2021 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 "helloworld.srpc.h"
  16. #include "srpc/rpc_types.h"
  17. using namespace srpc;
  18. using namespace trpc::test::helloworld;
  19. static WFFacilities::WaitGroup wait_group(1);
  20. int main()
  21. {
  22. Greeter::TRPCHttpClient client("127.0.0.1", 1412);
  23. //async
  24. HelloRequest req;
  25. req.set_msg("Hello, trpc-http server. This is srpc framework trpc-http client!");
  26. client.SayHello(&req, [](HelloReply *resp, RPCContext *ctx) {
  27. if (ctx->success())
  28. printf("%s\n", resp->DebugString().c_str());
  29. else
  30. printf("status[%d] error[%d] errmsg:%s\n",
  31. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  32. });
  33. //sync
  34. HelloRequest sync_req;
  35. HelloReply sync_resp;
  36. RPCSyncContext sync_ctx;
  37. sync_req.set_msg("Hi, trpc-http server. This is srpc framework trpc-http client!");
  38. client.SayHi(&sync_req, &sync_resp, &sync_ctx);
  39. if (sync_ctx.success)
  40. printf("%s\n", sync_resp.DebugString().c_str());
  41. else
  42. printf("status[%d] error[%d] errmsg:%s\n",
  43. sync_ctx.status_code, sync_ctx.error, sync_ctx.errmsg.c_str());
  44. wait_group.wait();
  45. return 0;
  46. }