blob: 1faba3e20b9fd031e2de0f6549f61d62afcac528 [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
Muxi Yan5bd16b72017-02-09 16:54:30 -080047#import "GRPCOpBatchLog.h"
Muxi Yan40d7c622017-02-08 14:41:45 -080048
Jorge Canizales8d997752015-06-21 21:43:26 -070049@implementation GRPCOperation {
Muxi Yan2c88b462016-10-28 10:47:25 -070050@protected
51 // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being
52 // initialized to zero.
Jorge Canizales8d997752015-06-21 21:43:26 -070053 grpc_op _op;
Muxi Yan2c88b462016-10-28 10:47:25 -070054 void(^_handler)();
murgatroid9954e93d42015-04-27 09:29:49 -070055}
56
Jorge Canizales8d997752015-06-21 21:43:26 -070057- (void)finish {
58 if (_handler) {
Muxi Yan2c88b462016-10-28 10:47:25 -070059 void(^handler)() = _handler;
Nicolas "Pixel" Noble70224362016-03-21 22:19:43 +010060 _handler = nil;
61 handler();
Jorge Canizales8d997752015-06-21 21:43:26 -070062 }
63}
64@end
65
66@implementation GRPCOpSendMetadata
67
murgatroid9954e93d42015-04-27 09:29:49 -070068- (instancetype)init {
Muxi Yane97f7c02016-09-28 11:25:57 -070069 return [self initWithMetadata:nil flags:0 handler:nil];
murgatroid9954e93d42015-04-27 09:29:49 -070070}
71
Muxi Yanfdea83d2016-09-27 16:11:18 -070072- (instancetype)initWithMetadata:(NSDictionary *)metadata
73 handler:(void (^)())handler {
Muxi Yane97f7c02016-09-28 11:25:57 -070074 return [self initWithMetadata:metadata flags:0 handler:handler];
Muxi Yanfdea83d2016-09-27 16:11:18 -070075}
76
77- (instancetype)initWithMetadata:(NSDictionary *)metadata
Muxi Yane97f7c02016-09-28 11:25:57 -070078 flags:(uint32_t)flags
Muxi Yanfdea83d2016-09-27 16:11:18 -070079 handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070080 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070081 _op.op = GRPC_OP_SEND_INITIAL_METADATA;
82 _op.data.send_initial_metadata.count = metadata.count;
83 _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray;
David Garcia Quintase825df72016-05-17 22:11:18 -070084 _op.data.send_initial_metadata.maybe_compression_level.is_set = false;
David Garcia Quintas8ba42be2016-06-07 17:30:20 -070085 _op.data.send_initial_metadata.maybe_compression_level.level = 0;
Muxi Yane97f7c02016-09-28 11:25:57 -070086 _op.flags = flags;
murgatroid9954e93d42015-04-27 09:29:49 -070087 _handler = handler;
88 }
89 return self;
90}
91
murgatroid9933655f92015-04-29 11:15:35 -070092- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -070093 gpr_free(_op.data.send_initial_metadata.metadata);
murgatroid9933655f92015-04-29 11:15:35 -070094}
95
murgatroid9954e93d42015-04-27 09:29:49 -070096@end
97
Jorge Canizales8d997752015-06-21 21:43:26 -070098@implementation GRPCOpSendMessage
murgatroid9954e93d42015-04-27 09:29:49 -070099
100- (instancetype)init {
101 return [self initWithMessage:nil handler:nil];
102}
103
Jorge Canizales8d997752015-06-21 21:43:26 -0700104- (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700105 if (!message) {
Muxi Yan2c88b462016-10-28 10:47:25 -0700106 [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"];
murgatroid9954e93d42015-04-27 09:29:49 -0700107 }
108 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700109 _op.op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth448c1f02017-01-25 10:44:30 -0800110 _op.data.send_message.send_message = message.grpc_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -0700111 _handler = handler;
112 }
113 return self;
114}
115
murgatroid9933655f92015-04-29 11:15:35 -0700116- (void)dealloc {
Mark D. Roth448c1f02017-01-25 10:44:30 -0800117 grpc_byte_buffer_destroy(_op.data.send_message.send_message);
murgatroid9933655f92015-04-29 11:15:35 -0700118}
119
murgatroid9954e93d42015-04-27 09:29:49 -0700120@end
121
Jorge Canizales8d997752015-06-21 21:43:26 -0700122@implementation GRPCOpSendClose
murgatroid9954e93d42015-04-27 09:29:49 -0700123
124- (instancetype)init {
125 return [self initWithHandler:nil];
126}
127
Jorge Canizales8d997752015-06-21 21:43:26 -0700128- (instancetype)initWithHandler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700129 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700130 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
murgatroid9954e93d42015-04-27 09:29:49 -0700131 _handler = handler;
132 }
133 return self;
134}
135
murgatroid9954e93d42015-04-27 09:29:49 -0700136@end
137
Jorge Canizales8d997752015-06-21 21:43:26 -0700138@implementation GRPCOpRecvMetadata {
139 grpc_metadata_array _headers;
murgatroid9954e93d42015-04-27 09:29:49 -0700140}
141
Muxi Yan2c88b462016-10-28 10:47:25 -0700142- (instancetype) init {
murgatroid9954e93d42015-04-27 09:29:49 -0700143 return [self initWithHandler:nil];
144}
145
Muxi Yan2c88b462016-10-28 10:47:25 -0700146- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700147 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700148 _op.op = GRPC_OP_RECV_INITIAL_METADATA;
149 grpc_metadata_array_init(&_headers);
Mark D. Roth448c1f02017-01-25 10:44:30 -0800150 _op.data.recv_initial_metadata.recv_initial_metadata = &_headers;
Jorge Canizales8d997752015-06-21 21:43:26 -0700151 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700152 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700153 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700154 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700155 __strong typeof(self) strongSelf = weakSelf;
156 NSDictionary *metadata = [NSDictionary
Muxi Yan2c88b462016-10-28 10:47:25 -0700157 grpc_dictionaryFromMetadataArray:strongSelf->_headers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700158 handler(metadata);
159 };
160 }
murgatroid9954e93d42015-04-27 09:29:49 -0700161 }
162 return self;
163}
164
murgatroid9933655f92015-04-29 11:15:35 -0700165- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700166 grpc_metadata_array_destroy(&_headers);
murgatroid9933655f92015-04-29 11:15:35 -0700167}
168
murgatroid9954e93d42015-04-27 09:29:49 -0700169@end
170
Muxi Yan2c88b462016-10-28 10:47:25 -0700171@implementation GRPCOpRecvMessage{
Jorge Canizales8d997752015-06-21 21:43:26 -0700172 grpc_byte_buffer *_receivedMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700173}
174
175- (instancetype)init {
176 return [self initWithHandler:nil];
177}
178
murgatroid996cc46802015-04-28 09:35:48 -0700179- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700180 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700181 _op.op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth448c1f02017-01-25 10:44:30 -0800182 _op.data.recv_message.recv_message = &_receivedMessage;
Jorge Canizales8d997752015-06-21 21:43:26 -0700183 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700184 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700185 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700186 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700187 __strong typeof(self) strongSelf = weakSelf;
188 handler(strongSelf->_receivedMessage);
Jorge Canizales8d997752015-06-21 21:43:26 -0700189 };
190 }
murgatroid9954e93d42015-04-27 09:29:49 -0700191 }
192 return self;
193}
194
murgatroid9954e93d42015-04-27 09:29:49 -0700195@end
196
Muxi Yan2c88b462016-10-28 10:47:25 -0700197@implementation GRPCOpRecvStatus{
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700198 grpc_status_code _statusCode;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800199 grpc_slice _details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700200 size_t _detailsCapacity;
Jorge Canizales8d997752015-06-21 21:43:26 -0700201 grpc_metadata_array _trailers;
murgatroid9954e93d42015-04-27 09:29:49 -0700202}
203
Muxi Yan2c88b462016-10-28 10:47:25 -0700204- (instancetype) init {
murgatroid9954e93d42015-04-27 09:29:49 -0700205 return [self initWithHandler:nil];
206}
207
Muxi Yan2c88b462016-10-28 10:47:25 -0700208- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700209 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700210 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
211 _op.data.recv_status_on_client.status = &_statusCode;
212 _op.data.recv_status_on_client.status_details = &_details;
Jorge Canizales8d997752015-06-21 21:43:26 -0700213 grpc_metadata_array_init(&_trailers);
214 _op.data.recv_status_on_client.trailing_metadata = &_trailers;
215 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700216 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700217 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700218 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700219 __strong typeof(self) strongSelf = weakSelf;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800220 if (strongSelf) {
221 char *details = grpc_slice_to_c_string(strongSelf->_details);
222 NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
223 details:details];
224 NSDictionary *trailers = [NSDictionary
225 grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
226 handler(error, trailers);
227 gpr_free(details);
228 }
Jorge Canizales8d997752015-06-21 21:43:26 -0700229 };
230 }
murgatroid9954e93d42015-04-27 09:29:49 -0700231 }
232 return self;
233}
234
murgatroid9933655f92015-04-29 11:15:35 -0700235- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700236 grpc_metadata_array_destroy(&_trailers);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800237 grpc_slice_unref(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700238}
239
murgatroid9954e93d42015-04-27 09:29:49 -0700240@end
241
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700242#pragma mark GRPCWrappedCall
243
Jorge Canizalese8543b02015-08-01 17:37:40 -0700244@implementation GRPCWrappedCall {
245 GRPCCompletionQueue *_queue;
murgatroid9969927d62015-04-24 13:32:48 -0700246 grpc_call *_call;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700247}
248
249- (instancetype)init {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700250 return [self initWithHost:nil path:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700251}
252
Muxi Yan2c88b462016-10-28 10:47:25 -0700253- (instancetype)initWithHost:(NSString *)host
254 path:(NSString *)path {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700255 if (!path || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700256 [NSException raise:NSInvalidArgumentException
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700257 format:@"path and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700258 }
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700259
murgatroid9930b7d4e2015-04-24 10:36:43 -0700260 if (self = [super init]) {
Muxi Yan2c88b462016-10-28 10:47:25 -0700261 // Each completion queue consumes one thread. There's a trade to be made between creating and
262 // consuming too many threads and having contention of multiple calls in a single completion
263 // queue. Currently we use a singleton queue.
Jorge Canizalese8543b02015-08-01 17:37:40 -0700264 _queue = [GRPCCompletionQueue completionQueue];
265
Muxi Yan2c88b462016-10-28 10:47:25 -0700266 _call = [[GRPCHost hostWithAddress:host] unmanagedCallWithPath:path completionQueue:_queue];
murgatroid9969927d62015-04-24 13:32:48 -0700267 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700268 return nil;
269 }
270 }
271 return self;
272}
273
murgatroid9954e93d42015-04-27 09:29:49 -0700274- (void)startBatchWithOperations:(NSArray *)operations {
275 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700276}
277
Muxi Yan2c88b462016-10-28 10:47:25 -0700278- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
Muxi Yan43730582017-03-07 17:49:13 -0800279 // Keep logs of op batches when we are running tests. Disabled when in production for improved
280 // performance.
Muxi Yan5bd16b72017-02-09 16:54:30 -0800281#ifdef GRPC_TEST_OBJC
282 [GRPCOpBatchLog addOpBatchToLog:operations];
283#endif
Muxi Yan40d7c622017-02-08 14:41:45 -0800284
murgatroid9969927d62015-04-24 13:32:48 -0700285 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700286 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700287 size_t i = 0;
Jorge Canizales8d997752015-06-21 21:43:26 -0700288 for (GRPCOperation *operation in operations) {
289 ops_array[i++] = operation.op;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700290 }
Muxi Yan2c88b462016-10-28 10:47:25 -0700291 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
292 (__bridge_retained void *)(^(bool success){
293 if (!success) {
294 if (errorHandler) {
295 errorHandler();
296 } else {
297 return;
298 }
299 }
300 for (GRPCOperation *operation in operations) {
301 [operation finish];
302 }
303 }), NULL);
Jorge Canizales8d997752015-06-21 21:43:26 -0700304 gpr_free(ops_array);
305
murgatroid9930b7d4e2015-04-24 10:36:43 -0700306 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700307 [NSException raise:NSInternalInconsistencyException
Muxi Yan2c88b462016-10-28 10:47:25 -0700308 format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i",
309 error];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700310 }
311}
312
313- (void)cancel {
Jorge Canizalesb3584aa2015-08-08 15:01:24 -0700314 grpc_call_cancel(_call, NULL);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700315}
316
317- (void)dealloc {
Craig Tillerdd36b152017-03-31 08:27:28 -0700318 grpc_call_unref(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700319}
320
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700321@end