tutorial-08-thrift_thrift_client.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_thrift.srpc.h"
  15. using namespace srpc;
  16. int main()
  17. {
  18. Example::ThriftClient client("127.0.0.1", 1412);
  19. //sync
  20. EchoResult sync_res;
  21. client.Echo(sync_res, "Hello, srpc!", "1412");
  22. if (client.thrift_last_sync_success())
  23. printf("%s\n", sync_res.message.c_str());
  24. else
  25. {
  26. const auto& sync_ctx = client.thrift_last_sync_ctx();
  27. printf("status[%d] error[%d] errmsg:%s\n",
  28. sync_ctx.status_code, sync_ctx.error, sync_ctx.errmsg.c_str());
  29. }
  30. //send/recv
  31. client.send_Echo("Hello, srpc!", "1412");
  32. //do anything you want
  33. client.recv_Echo(sync_res);
  34. if (client.thrift_last_sync_success())
  35. printf("%s\n", sync_res.message.c_str());
  36. else
  37. {
  38. const auto& sync_ctx = client.thrift_last_sync_ctx();
  39. printf("status[%d] error[%d] errmsg:%s\n",
  40. sync_ctx.status_code, sync_ctx.error, sync_ctx.errmsg.c_str());
  41. }
  42. //async
  43. Example::EchoRequest req;
  44. req.message = "Hello, srpc!";
  45. req.name = "1412";
  46. client.Echo(&req, [](Example::EchoResponse *resp, RPCContext *ctx) {
  47. if (ctx->success())
  48. printf("%s\n", resp->result.message.c_str());
  49. else
  50. printf("status[%d] error[%d] errmsg:%s\n",
  51. ctx->get_status_code(), ctx->get_error(), ctx->get_errmsg());
  52. });
  53. //sync
  54. Example::EchoRequest sync_req;
  55. Example::EchoResponse sync_resp;
  56. RPCSyncContext sync_ctx;
  57. sync_req.message = "Hello, srpc!";
  58. sync_req.name = "Sync";
  59. client.Echo(&sync_req, &sync_resp, &sync_ctx);
  60. if (sync_ctx.success)
  61. printf("%s\n", sync_resp.result.message.c_str());
  62. else
  63. printf("status[%d] error[%d] errmsg:%s\n",
  64. sync_ctx.status_code, sync_ctx.error, sync_ctx.errmsg.c_str());
  65. return 0;
  66. }