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 19 #import "ViewController.h" 20 #import <grpcpp/grpcpp.h> 21 #include <grpcpp/generic/generic_stub.h> 22 #include <grpcpp/generic/async_generic_service.h> 23 24 static void* tag(int i) { return (void*)(intptr_t)i; } 25 26 // Serialized Proto bytes of Hello World example 27 const uint8_t kMessage[] = 28 {0x0A, 0x0B, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x69, 0x76, 0x65, 0x2D, 0x43}; 29 30 @interface ViewController () 31 32 @end 33 34 @implementation ViewController { 35 grpc::CompletionQueue cq_; 36 std::unique_ptr<grpc::GenericStub> generic_stub_; 37 } 38 39 - (void)viewDidLoad { 40 [super viewDidLoad]; 41 42 // Setup call stub 43 std::shared_ptr<grpc::Channel> channel = 44 CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()); 45 generic_stub_.reset(new grpc::GenericStub(channel)); 46 47 const grpc::string kMethodName("/helloworld.Greeter/SayHello"); 48 void* got_tag; 49 bool ok; 50 51 grpc::ClientContext cli_ctx; 52 std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call = 53 generic_stub_->Call(&cli_ctx, kMethodName, &cq_, tag(1)); 54 cq_.Next(&got_tag, &ok); 55 if (!ok || got_tag != tag(1)) { 56 NSLog(@"Failed to create call."); 57 abort(); 58 } 59 grpc::Slice send_slice = grpc::Slice(kMessage, sizeof(kMessage) / sizeof(kMessage[0])); 60 std::unique_ptr<grpc::ByteBuffer> send_buffer(new grpc::ByteBuffer(&send_slice, 1)); 61 call->Write(*send_buffer, tag(2)); 62 cq_.Next(&got_tag, &ok); 63 if (!ok || got_tag != tag(2)) { 64 NSLog(@"Failed to send message."); 65 abort(); 66 } 67 grpc::ByteBuffer recv_buffer; 68 call->Read(&recv_buffer, tag(3)); 69 cq_.Next(&got_tag, &ok); 70 if (!ok || got_tag != tag(3)) { 71 NSLog(@"Failed to receive message."); 72 abort(); 73 } 74 75 grpc::Status status; 76 call->Finish(&status, tag(4)); 77 cq_.Next(&got_tag, &ok); 78 if (!ok || got_tag != tag(4)) { 79 NSLog(@"Failed to finish call."); 80 abort(); 81 } 82 if (!status.ok()) { 83 NSLog(@"Received unsuccessful status code: %d", status.error_code()); 84 abort(); 85 } 86 std::vector<grpc::Slice> slices; 87 recv_buffer.Dump(&slices); 88 NSString *recvBytes = [[NSString alloc] init]; 89 for (auto slice : slices) { 90 auto p = slice.begin(); 91 while (p != slice.end()) { 92 recvBytes = 93 [recvBytes stringByAppendingString:[NSString stringWithFormat:@"%02x ", *p]]; 94 p++; 95 } 96 } 97 NSLog(@"Hello World succeeded.\nReceived bytes: %@\n" 98 "Expected bytes: 0a 11 48 65 6c 6c 6f 20 4f 62 6a 65 63 74 69 76 65 2d 43", recvBytes); 99 } 100 101 @end 102