ViewController.mm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #import "ViewController.h"
  19. #import <grpcpp/grpcpp.h>
  20. #include <grpcpp/generic/async_generic_service.h>
  21. #include <grpcpp/generic/generic_stub.h>
  22. static void* tag(int i) { return (void*)(intptr_t)i; }
  23. // Serialized Proto bytes of Hello World example
  24. const uint8_t kMessage[] = {0x0A, 0x0B, 0x4F, 0x62, 0x6A, 0x65, 0x63,
  25. 0x74, 0x69, 0x76, 0x65, 0x2D, 0x43};
  26. @interface ViewController ()
  27. @end
  28. @implementation ViewController {
  29. grpc::CompletionQueue cq_;
  30. std::unique_ptr<grpc::GenericStub> generic_stub_;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Setup call stub
  35. std::shared_ptr<grpc::Channel> channel =
  36. CreateChannel("localhost:50051", grpc::InsecureChannelCredentials());
  37. generic_stub_.reset(new grpc::GenericStub(channel));
  38. const std::string kMethodName("/helloworld.Greeter/SayHello");
  39. void* got_tag;
  40. bool ok;
  41. grpc::ClientContext cli_ctx;
  42. std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call =
  43. generic_stub_->Call(&cli_ctx, kMethodName, &cq_, tag(1));
  44. cq_.Next(&got_tag, &ok);
  45. if (!ok || got_tag != tag(1)) {
  46. NSLog(@"Failed to create call.");
  47. abort();
  48. }
  49. grpc::Slice send_slice = grpc::Slice(kMessage, sizeof(kMessage) / sizeof(kMessage[0]));
  50. std::unique_ptr<grpc::ByteBuffer> send_buffer(new grpc::ByteBuffer(&send_slice, 1));
  51. call->Write(*send_buffer, tag(2));
  52. cq_.Next(&got_tag, &ok);
  53. if (!ok || got_tag != tag(2)) {
  54. NSLog(@"Failed to send message.");
  55. abort();
  56. }
  57. grpc::ByteBuffer recv_buffer;
  58. call->Read(&recv_buffer, tag(3));
  59. cq_.Next(&got_tag, &ok);
  60. if (!ok || got_tag != tag(3)) {
  61. NSLog(@"Failed to receive message.");
  62. abort();
  63. }
  64. grpc::Status status;
  65. call->Finish(&status, tag(4));
  66. cq_.Next(&got_tag, &ok);
  67. if (!ok || got_tag != tag(4)) {
  68. NSLog(@"Failed to finish call.");
  69. abort();
  70. }
  71. if (!status.ok()) {
  72. NSLog(@"Received unsuccessful status code: %d", status.error_code());
  73. abort();
  74. }
  75. std::vector<grpc::Slice> slices;
  76. recv_buffer.Dump(&slices);
  77. NSString* recvBytes = [[NSString alloc] init];
  78. for (auto slice : slices) {
  79. auto p = slice.begin();
  80. while (p != slice.end()) {
  81. recvBytes = [recvBytes stringByAppendingString:[NSString stringWithFormat:@"%02x ", *p]];
  82. p++;
  83. }
  84. }
  85. NSLog(@"Hello World succeeded.\nReceived bytes: %@\n"
  86. "Expected bytes: 0a 11 48 65 6c 6c 6f 20 4f 62 6a 65 63 74 69 76 65 2d 43",
  87. recvBytes);
  88. }
  89. @end