blob: 43c564552bff6ae45c8771160f617a2c767f9d3a [file] [log] [blame]
murgatroid9930b7d4e2015-04-24 10:36:43 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
murgatroid9930b7d4e2015-04-24 10:36:43 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
murgatroid9930b7d4e2015-04-24 10:36:43 -070034#import "GRPCWrappedCall.h"
Jorge Canizales3a5253e2015-07-31 23:48:56 -070035
murgatroid9969927d62015-04-24 13:32:48 -070036#import <Foundation/Foundation.h>
Muxi Yanbd19fc72016-10-25 13:28:12 -070037#include <grpc/grpc.h>
Muxi Yan2c88b462016-10-28 10:47:25 -070038#include <grpc/byte_buffer.h>
murgatroid9969927d62015-04-24 13:32:48 -070039#include <grpc/support/alloc.h>
Jorge Canizales3a5253e2015-07-31 23:48:56 -070040
Jorge Canizalese8543b02015-08-01 17:37:40 -070041#import "GRPCCompletionQueue.h"
42#import "GRPCHost.h"
Muxi Yanbd19fc72016-10-25 13:28:12 -070043#import "NSDictionary+GRPC.h"
Muxi Yan2c88b462016-10-28 10:47:25 -070044#import "NSData+GRPC.h"
murgatroid9930b7d4e2015-04-24 10:36:43 -070045#import "NSError+GRPC.h"
murgatroid9930b7d4e2015-04-24 10:36:43 -070046
Jorge Canizales8d997752015-06-21 21:43:26 -070047@implementation GRPCOperation {
Muxi Yan2c88b462016-10-28 10:47:25 -070048@protected
49 // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being
50 // initialized to zero.
Jorge Canizales8d997752015-06-21 21:43:26 -070051 grpc_op _op;
Muxi Yan2c88b462016-10-28 10:47:25 -070052 void(^_handler)();
murgatroid9954e93d42015-04-27 09:29:49 -070053}
54
Jorge Canizales8d997752015-06-21 21:43:26 -070055- (void)finish {
56 if (_handler) {
Muxi Yan2c88b462016-10-28 10:47:25 -070057 void(^handler)() = _handler;
Nicolas "Pixel" Noble70224362016-03-21 22:19:43 +010058 _handler = nil;
59 handler();
Jorge Canizales8d997752015-06-21 21:43:26 -070060 }
61}
62@end
63
64@implementation GRPCOpSendMetadata
65
murgatroid9954e93d42015-04-27 09:29:49 -070066- (instancetype)init {
Muxi Yane97f7c02016-09-28 11:25:57 -070067 return [self initWithMetadata:nil flags:0 handler:nil];
murgatroid9954e93d42015-04-27 09:29:49 -070068}
69
Muxi Yanfdea83d2016-09-27 16:11:18 -070070- (instancetype)initWithMetadata:(NSDictionary *)metadata
71 handler:(void (^)())handler {
Muxi Yane97f7c02016-09-28 11:25:57 -070072 return [self initWithMetadata:metadata flags:0 handler:handler];
Muxi Yanfdea83d2016-09-27 16:11:18 -070073}
74
75- (instancetype)initWithMetadata:(NSDictionary *)metadata
Muxi Yane97f7c02016-09-28 11:25:57 -070076 flags:(uint32_t)flags
Muxi Yanfdea83d2016-09-27 16:11:18 -070077 handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070078 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070079 _op.op = GRPC_OP_SEND_INITIAL_METADATA;
80 _op.data.send_initial_metadata.count = metadata.count;
81 _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray;
David Garcia Quintase825df72016-05-17 22:11:18 -070082 _op.data.send_initial_metadata.maybe_compression_level.is_set = false;
David Garcia Quintas8ba42be2016-06-07 17:30:20 -070083 _op.data.send_initial_metadata.maybe_compression_level.level = 0;
Muxi Yane97f7c02016-09-28 11:25:57 -070084 _op.flags = flags;
murgatroid9954e93d42015-04-27 09:29:49 -070085 _handler = handler;
86 }
87 return self;
88}
89
murgatroid9933655f92015-04-29 11:15:35 -070090- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -070091 gpr_free(_op.data.send_initial_metadata.metadata);
murgatroid9933655f92015-04-29 11:15:35 -070092}
93
murgatroid9954e93d42015-04-27 09:29:49 -070094@end
95
Jorge Canizales8d997752015-06-21 21:43:26 -070096@implementation GRPCOpSendMessage
murgatroid9954e93d42015-04-27 09:29:49 -070097
98- (instancetype)init {
99 return [self initWithMessage:nil handler:nil];
100}
101
Jorge Canizales8d997752015-06-21 21:43:26 -0700102- (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700103 if (!message) {
Muxi Yan2c88b462016-10-28 10:47:25 -0700104 [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"];
murgatroid9954e93d42015-04-27 09:29:49 -0700105 }
106 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700107 _op.op = GRPC_OP_SEND_MESSAGE;
108 _op.data.send_message = message.grpc_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -0700109 _handler = handler;
110 }
111 return self;
112}
113
murgatroid9933655f92015-04-29 11:15:35 -0700114- (void)dealloc {
Paul Quernad9073702016-11-17 23:16:41 -0800115 grpc_byte_buffer_destroy(_op.data.send_message);
murgatroid9933655f92015-04-29 11:15:35 -0700116}
117
murgatroid9954e93d42015-04-27 09:29:49 -0700118@end
119
Jorge Canizales8d997752015-06-21 21:43:26 -0700120@implementation GRPCOpSendClose
murgatroid9954e93d42015-04-27 09:29:49 -0700121
122- (instancetype)init {
123 return [self initWithHandler:nil];
124}
125
Jorge Canizales8d997752015-06-21 21:43:26 -0700126- (instancetype)initWithHandler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700127 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700128 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
murgatroid9954e93d42015-04-27 09:29:49 -0700129 _handler = handler;
130 }
131 return self;
132}
133
murgatroid9954e93d42015-04-27 09:29:49 -0700134@end
135
Jorge Canizales8d997752015-06-21 21:43:26 -0700136@implementation GRPCOpRecvMetadata {
137 grpc_metadata_array _headers;
murgatroid9954e93d42015-04-27 09:29:49 -0700138}
139
Muxi Yan2c88b462016-10-28 10:47:25 -0700140- (instancetype) init {
murgatroid9954e93d42015-04-27 09:29:49 -0700141 return [self initWithHandler:nil];
142}
143
Muxi Yan2c88b462016-10-28 10:47:25 -0700144- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700145 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700146 _op.op = GRPC_OP_RECV_INITIAL_METADATA;
147 grpc_metadata_array_init(&_headers);
148 _op.data.recv_initial_metadata = &_headers;
149 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700150 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700151 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700152 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700153 __strong typeof(self) strongSelf = weakSelf;
154 NSDictionary *metadata = [NSDictionary
Muxi Yan2c88b462016-10-28 10:47:25 -0700155 grpc_dictionaryFromMetadataArray:strongSelf->_headers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700156 handler(metadata);
157 };
158 }
murgatroid9954e93d42015-04-27 09:29:49 -0700159 }
160 return self;
161}
162
murgatroid9933655f92015-04-29 11:15:35 -0700163- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700164 grpc_metadata_array_destroy(&_headers);
murgatroid9933655f92015-04-29 11:15:35 -0700165}
166
murgatroid9954e93d42015-04-27 09:29:49 -0700167@end
168
Muxi Yan2c88b462016-10-28 10:47:25 -0700169@implementation GRPCOpRecvMessage{
Jorge Canizales8d997752015-06-21 21:43:26 -0700170 grpc_byte_buffer *_receivedMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700171}
172
173- (instancetype)init {
174 return [self initWithHandler:nil];
175}
176
murgatroid996cc46802015-04-28 09:35:48 -0700177- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700178 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700179 _op.op = GRPC_OP_RECV_MESSAGE;
180 _op.data.recv_message = &_receivedMessage;
181 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700182 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700183 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700184 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700185 __strong typeof(self) strongSelf = weakSelf;
186 handler(strongSelf->_receivedMessage);
Jorge Canizales8d997752015-06-21 21:43:26 -0700187 };
188 }
murgatroid9954e93d42015-04-27 09:29:49 -0700189 }
190 return self;
191}
192
murgatroid9954e93d42015-04-27 09:29:49 -0700193@end
194
Muxi Yan2c88b462016-10-28 10:47:25 -0700195@implementation GRPCOpRecvStatus{
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700196 grpc_status_code _statusCode;
Muxi Yan180b3232017-01-13 17:42:53 -0800197 grpc_slice _details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700198 size_t _detailsCapacity;
Jorge Canizales8d997752015-06-21 21:43:26 -0700199 grpc_metadata_array _trailers;
murgatroid9954e93d42015-04-27 09:29:49 -0700200}
201
Muxi Yan2c88b462016-10-28 10:47:25 -0700202- (instancetype) init {
murgatroid9954e93d42015-04-27 09:29:49 -0700203 return [self initWithHandler:nil];
204}
205
Muxi Yan2c88b462016-10-28 10:47:25 -0700206- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700207 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700208 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
209 _op.data.recv_status_on_client.status = &_statusCode;
210 _op.data.recv_status_on_client.status_details = &_details;
Jorge Canizales8d997752015-06-21 21:43:26 -0700211 grpc_metadata_array_init(&_trailers);
212 _op.data.recv_status_on_client.trailing_metadata = &_trailers;
213 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700214 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700215 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700216 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700217 __strong typeof(self) strongSelf = weakSelf;
Muxi Yand3c25942017-01-16 21:57:21 -0800218 if (strongSelf) {
219 char *details = grpc_slice_to_c_string(strongSelf->_details);
220 NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
221 details:details];
222 NSDictionary *trailers = [NSDictionary
223 grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
224 handler(error, trailers);
225 gpr_free(details);
226 }
Jorge Canizales8d997752015-06-21 21:43:26 -0700227 };
228 }
murgatroid9954e93d42015-04-27 09:29:49 -0700229 }
230 return self;
231}
232
murgatroid9933655f92015-04-29 11:15:35 -0700233- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700234 grpc_metadata_array_destroy(&_trailers);
Muxi Yan180b3232017-01-13 17:42:53 -0800235 grpc_slice_unref(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700236}
237
murgatroid9954e93d42015-04-27 09:29:49 -0700238@end
239
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700240#pragma mark GRPCWrappedCall
241
Jorge Canizalese8543b02015-08-01 17:37:40 -0700242@implementation GRPCWrappedCall {
243 GRPCCompletionQueue *_queue;
murgatroid9969927d62015-04-24 13:32:48 -0700244 grpc_call *_call;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700245}
246
247- (instancetype)init {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700248 return [self initWithHost:nil path:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700249}
250
Muxi Yan2c88b462016-10-28 10:47:25 -0700251- (instancetype)initWithHost:(NSString *)host
252 path:(NSString *)path {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700253 if (!path || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700254 [NSException raise:NSInvalidArgumentException
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700255 format:@"path and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700256 }
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700257
murgatroid9930b7d4e2015-04-24 10:36:43 -0700258 if (self = [super init]) {
Muxi Yan2c88b462016-10-28 10:47:25 -0700259 // Each completion queue consumes one thread. There's a trade to be made between creating and
260 // consuming too many threads and having contention of multiple calls in a single completion
261 // queue. Currently we use a singleton queue.
Jorge Canizalese8543b02015-08-01 17:37:40 -0700262 _queue = [GRPCCompletionQueue completionQueue];
263
Muxi Yan2c88b462016-10-28 10:47:25 -0700264 _call = [[GRPCHost hostWithAddress:host] unmanagedCallWithPath:path completionQueue:_queue];
murgatroid9969927d62015-04-24 13:32:48 -0700265 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700266 return nil;
267 }
268 }
269 return self;
270}
271
murgatroid9954e93d42015-04-27 09:29:49 -0700272- (void)startBatchWithOperations:(NSArray *)operations {
273 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700274}
275
Muxi Yan2c88b462016-10-28 10:47:25 -0700276- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
murgatroid9969927d62015-04-24 13:32:48 -0700277 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700278 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700279 size_t i = 0;
Jorge Canizales8d997752015-06-21 21:43:26 -0700280 for (GRPCOperation *operation in operations) {
281 ops_array[i++] = operation.op;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700282 }
Muxi Yan2c88b462016-10-28 10:47:25 -0700283 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
284 (__bridge_retained void *)(^(bool success){
285 if (!success) {
286 if (errorHandler) {
287 errorHandler();
288 } else {
289 return;
290 }
291 }
292 for (GRPCOperation *operation in operations) {
293 [operation finish];
294 }
295 }), NULL);
Jorge Canizales8d997752015-06-21 21:43:26 -0700296 gpr_free(ops_array);
297
murgatroid9930b7d4e2015-04-24 10:36:43 -0700298 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700299 [NSException raise:NSInternalInconsistencyException
Muxi Yan2c88b462016-10-28 10:47:25 -0700300 format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i",
301 error];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700302 }
303}
304
305- (void)cancel {
Jorge Canizalesb3584aa2015-08-08 15:01:24 -0700306 grpc_call_cancel(_call, NULL);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700307}
308
309- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700310 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700311}
312
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700313@end