blob: 97f6b893407a42420d1524ab0029ce1386bfebfc [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>
37#include <grpc/grpc.h>
38#include <grpc/byte_buffer.h>
39#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"
murgatroid9930b7d4e2015-04-24 10:36:43 -070043#import "NSDictionary+GRPC.h"
44#import "NSData+GRPC.h"
45#import "NSError+GRPC.h"
murgatroid9930b7d4e2015-04-24 10:36:43 -070046
Jorge Canizales8d997752015-06-21 21:43:26 -070047@implementation GRPCOperation {
48@protected
Jorge Canizales88487042015-06-21 21:59:37 -070049 // 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;
52 void(^_handler)();
murgatroid9954e93d42015-04-27 09:29:49 -070053}
54
Jorge Canizales8d997752015-06-21 21:43:26 -070055- (void)finish {
56 if (_handler) {
Nicolas "Pixel" Noble70224362016-03-21 22:19:43 +010057 void(^handler)() = _handler;
58 _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 {
67 return [self initWithMetadata:nil handler:nil];
68}
69
Jorge Canizalesf4f150f2015-11-01 22:31:12 -080070- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070071 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070072 _op.op = GRPC_OP_SEND_INITIAL_METADATA;
73 _op.data.send_initial_metadata.count = metadata.count;
74 _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray;
David Garcia Quintase825df72016-05-17 22:11:18 -070075 _op.data.send_initial_metadata.maybe_compression_level.is_set = false;
David Garcia Quintas8ba42be2016-06-07 17:30:20 -070076 _op.data.send_initial_metadata.maybe_compression_level.level = 0;
murgatroid9954e93d42015-04-27 09:29:49 -070077 _handler = handler;
78 }
79 return self;
80}
81
murgatroid9933655f92015-04-29 11:15:35 -070082- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -070083 gpr_free(_op.data.send_initial_metadata.metadata);
murgatroid9933655f92015-04-29 11:15:35 -070084}
85
murgatroid9954e93d42015-04-27 09:29:49 -070086@end
87
Jorge Canizales8d997752015-06-21 21:43:26 -070088@implementation GRPCOpSendMessage
murgatroid9954e93d42015-04-27 09:29:49 -070089
90- (instancetype)init {
91 return [self initWithMessage:nil handler:nil];
92}
93
Jorge Canizales8d997752015-06-21 21:43:26 -070094- (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070095 if (!message) {
murgatroid992101a482015-04-29 11:42:26 -070096 [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"];
murgatroid9954e93d42015-04-27 09:29:49 -070097 }
98 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070099 _op.op = GRPC_OP_SEND_MESSAGE;
100 _op.data.send_message = message.grpc_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -0700101 _handler = handler;
102 }
103 return self;
104}
105
murgatroid9933655f92015-04-29 11:15:35 -0700106- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700107 gpr_free(_op.data.send_message);
murgatroid9933655f92015-04-29 11:15:35 -0700108}
109
murgatroid9954e93d42015-04-27 09:29:49 -0700110@end
111
Jorge Canizales8d997752015-06-21 21:43:26 -0700112@implementation GRPCOpSendClose
murgatroid9954e93d42015-04-27 09:29:49 -0700113
114- (instancetype)init {
115 return [self initWithHandler:nil];
116}
117
Jorge Canizales8d997752015-06-21 21:43:26 -0700118- (instancetype)initWithHandler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700119 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700120 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
murgatroid9954e93d42015-04-27 09:29:49 -0700121 _handler = handler;
122 }
123 return self;
124}
125
murgatroid9954e93d42015-04-27 09:29:49 -0700126@end
127
Jorge Canizales8d997752015-06-21 21:43:26 -0700128@implementation GRPCOpRecvMetadata {
129 grpc_metadata_array _headers;
murgatroid9954e93d42015-04-27 09:29:49 -0700130}
131
132- (instancetype) init {
133 return [self initWithHandler:nil];
134}
135
136- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
137 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700138 _op.op = GRPC_OP_RECV_INITIAL_METADATA;
139 grpc_metadata_array_init(&_headers);
140 _op.data.recv_initial_metadata = &_headers;
141 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700142 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700143 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700144 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700145 __strong typeof(self) strongSelf = weakSelf;
146 NSDictionary *metadata = [NSDictionary
147 grpc_dictionaryFromMetadataArray:strongSelf->_headers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700148 handler(metadata);
149 };
150 }
murgatroid9954e93d42015-04-27 09:29:49 -0700151 }
152 return self;
153}
154
murgatroid9933655f92015-04-29 11:15:35 -0700155- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700156 grpc_metadata_array_destroy(&_headers);
murgatroid9933655f92015-04-29 11:15:35 -0700157}
158
murgatroid9954e93d42015-04-27 09:29:49 -0700159@end
160
161@implementation GRPCOpRecvMessage{
Jorge Canizales8d997752015-06-21 21:43:26 -0700162 grpc_byte_buffer *_receivedMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700163}
164
165- (instancetype)init {
166 return [self initWithHandler:nil];
167}
168
murgatroid996cc46802015-04-28 09:35:48 -0700169- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700170 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700171 _op.op = GRPC_OP_RECV_MESSAGE;
172 _op.data.recv_message = &_receivedMessage;
173 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700174 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700175 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700176 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700177 __strong typeof(self) strongSelf = weakSelf;
178 handler(strongSelf->_receivedMessage);
Jorge Canizales8d997752015-06-21 21:43:26 -0700179 };
180 }
murgatroid9954e93d42015-04-27 09:29:49 -0700181 }
182 return self;
183}
184
murgatroid9954e93d42015-04-27 09:29:49 -0700185@end
186
187@implementation GRPCOpRecvStatus{
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700188 grpc_status_code _statusCode;
189 char *_details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700190 size_t _detailsCapacity;
Jorge Canizales8d997752015-06-21 21:43:26 -0700191 grpc_metadata_array _trailers;
murgatroid9954e93d42015-04-27 09:29:49 -0700192}
193
194- (instancetype) init {
195 return [self initWithHandler:nil];
196}
197
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700198- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700199 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700200 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
201 _op.data.recv_status_on_client.status = &_statusCode;
202 _op.data.recv_status_on_client.status_details = &_details;
203 _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity;
204 grpc_metadata_array_init(&_trailers);
205 _op.data.recv_status_on_client.trailing_metadata = &_trailers;
206 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700207 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700208 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700209 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700210 __strong typeof(self) strongSelf = weakSelf;
211 NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
212 details:strongSelf->_details];
213 NSDictionary *trailers = [NSDictionary
214 grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700215 handler(error, trailers);
216 };
217 }
murgatroid9954e93d42015-04-27 09:29:49 -0700218 }
219 return self;
220}
221
murgatroid9933655f92015-04-29 11:15:35 -0700222- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700223 grpc_metadata_array_destroy(&_trailers);
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700224 gpr_free(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700225}
226
murgatroid9954e93d42015-04-27 09:29:49 -0700227@end
228
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700229#pragma mark GRPCWrappedCall
230
Jorge Canizalese8543b02015-08-01 17:37:40 -0700231@implementation GRPCWrappedCall {
232 GRPCCompletionQueue *_queue;
murgatroid9969927d62015-04-24 13:32:48 -0700233 grpc_call *_call;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700234}
235
236- (instancetype)init {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700237 return [self initWithHost:nil path:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700238}
239
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700240- (instancetype)initWithHost:(NSString *)host
241 path:(NSString *)path {
242 if (!path || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700243 [NSException raise:NSInvalidArgumentException
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700244 format:@"path and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700245 }
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700246
murgatroid9930b7d4e2015-04-24 10:36:43 -0700247 if (self = [super init]) {
248 static dispatch_once_t initialization;
249 dispatch_once(&initialization, ^{
250 grpc_init();
251 });
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700252
Jorge Canizalese8543b02015-08-01 17:37:40 -0700253 // Each completion queue consumes one thread. There's a trade to be made between creating and
254 // consuming too many threads and having contention of multiple calls in a single completion
Jorge Canizales5e824fa2016-06-24 11:32:51 -0700255 // queue. Currently we use a singleton queue.
Jorge Canizalese8543b02015-08-01 17:37:40 -0700256 _queue = [GRPCCompletionQueue completionQueue];
257
258 _call = [[GRPCHost hostWithAddress:host] unmanagedCallWithPath:path completionQueue:_queue];
murgatroid9969927d62015-04-24 13:32:48 -0700259 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700260 return nil;
261 }
262 }
263 return self;
264}
265
murgatroid9954e93d42015-04-27 09:29:49 -0700266- (void)startBatchWithOperations:(NSArray *)operations {
267 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700268}
269
murgatroid9954e93d42015-04-27 09:29:49 -0700270- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
murgatroid9969927d62015-04-24 13:32:48 -0700271 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700272 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700273 size_t i = 0;
Jorge Canizales8d997752015-06-21 21:43:26 -0700274 for (GRPCOperation *operation in operations) {
275 ops_array[i++] = operation.op;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700276 }
murgatroid996cc46802015-04-28 09:35:48 -0700277 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
murgatroid996b542442015-05-08 10:40:01 -0700278 (__bridge_retained void *)(^(bool success){
279 if (!success) {
murgatroid9969927d62015-04-24 13:32:48 -0700280 if (errorHandler) {
281 errorHandler();
282 } else {
murgatroid99def47aa2015-04-29 10:28:26 -0700283 return;
murgatroid9969927d62015-04-24 13:32:48 -0700284 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700285 }
Jorge Canizales8d997752015-06-21 21:43:26 -0700286 for (GRPCOperation *operation in operations) {
murgatroid992101a482015-04-29 11:42:26 -0700287 [operation finish];
murgatroid9969927d62015-04-24 13:32:48 -0700288 }
Jorge Canizalesb3584aa2015-08-08 15:01:24 -0700289 }), NULL);
Jorge Canizales8d997752015-06-21 21:43:26 -0700290 gpr_free(ops_array);
291
murgatroid9930b7d4e2015-04-24 10:36:43 -0700292 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700293 [NSException raise:NSInternalInconsistencyException
Jorge Canizalesbae38d92015-06-20 20:21:25 -0700294 format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i",
295 error];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700296 }
297}
298
299- (void)cancel {
Jorge Canizalesb3584aa2015-08-08 15:01:24 -0700300 grpc_call_cancel(_call, NULL);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700301}
302
303- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700304 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700305}
306
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700307@end