blob: bbda9f2f6470e4090e27039db1af043aee74d08d [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>
murgatroid9969927d62015-04-24 13:32:48 -070037#include <grpc/byte_buffer.h>
Muxi Yanbd19fc72016-10-25 13:28:12 -070038#include <grpc/grpc.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"
murgatroid9930b7d4e2015-04-24 10:36:43 -070043#import "NSData+GRPC.h"
Muxi Yanbd19fc72016-10-25 13:28:12 -070044#import "NSDictionary+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 Yanbd19fc72016-10-25 13:28:12 -070048 @protected
49 // Most operation subclasses don't set any flags in the grpc_op, and rely on
50 // the flag member being initialized to zero.
Jorge Canizales8d997752015-06-21 21:43:26 -070051 grpc_op _op;
Muxi Yanbd19fc72016-10-25 13:28:12 -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 Yanbd19fc72016-10-25 13:28:12 -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 Yanbd19fc72016-10-25 13:28:12 -0700104 [NSException raise:NSInvalidArgumentException
105 format:@"message cannot be nil"];
murgatroid9954e93d42015-04-27 09:29:49 -0700106 }
107 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700108 _op.op = GRPC_OP_SEND_MESSAGE;
109 _op.data.send_message = message.grpc_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -0700110 _handler = handler;
111 }
112 return self;
113}
114
murgatroid9933655f92015-04-29 11:15:35 -0700115- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700116 gpr_free(_op.data.send_message);
murgatroid9933655f92015-04-29 11:15:35 -0700117}
118
murgatroid9954e93d42015-04-27 09:29:49 -0700119@end
120
Jorge Canizales8d997752015-06-21 21:43:26 -0700121@implementation GRPCOpSendClose
murgatroid9954e93d42015-04-27 09:29:49 -0700122
123- (instancetype)init {
124 return [self initWithHandler:nil];
125}
126
Jorge Canizales8d997752015-06-21 21:43:26 -0700127- (instancetype)initWithHandler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700128 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700129 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
murgatroid9954e93d42015-04-27 09:29:49 -0700130 _handler = handler;
131 }
132 return self;
133}
134
murgatroid9954e93d42015-04-27 09:29:49 -0700135@end
136
Jorge Canizales8d997752015-06-21 21:43:26 -0700137@implementation GRPCOpRecvMetadata {
138 grpc_metadata_array _headers;
murgatroid9954e93d42015-04-27 09:29:49 -0700139}
140
Muxi Yanbd19fc72016-10-25 13:28:12 -0700141- (instancetype)init {
murgatroid9954e93d42015-04-27 09:29:49 -0700142 return [self initWithHandler:nil];
143}
144
Muxi Yanbd19fc72016-10-25 13:28:12 -0700145- (instancetype)initWithHandler:(void (^)(NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700146 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700147 _op.op = GRPC_OP_RECV_INITIAL_METADATA;
148 grpc_metadata_array_init(&_headers);
149 _op.data.recv_initial_metadata = &_headers;
150 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700151 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700152 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700153 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700154 __strong typeof(self) strongSelf = weakSelf;
155 NSDictionary *metadata = [NSDictionary
Muxi Yanbd19fc72016-10-25 13:28:12 -0700156 grpc_dictionaryFromMetadataArray:strongSelf->_headers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700157 handler(metadata);
158 };
159 }
murgatroid9954e93d42015-04-27 09:29:49 -0700160 }
161 return self;
162}
163
murgatroid9933655f92015-04-29 11:15:35 -0700164- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700165 grpc_metadata_array_destroy(&_headers);
murgatroid9933655f92015-04-29 11:15:35 -0700166}
167
murgatroid9954e93d42015-04-27 09:29:49 -0700168@end
169
Muxi Yanbd19fc72016-10-25 13:28:12 -0700170@implementation GRPCOpRecvMessage {
Jorge Canizales8d997752015-06-21 21:43:26 -0700171 grpc_byte_buffer *_receivedMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700172}
173
174- (instancetype)init {
175 return [self initWithHandler:nil];
176}
177
murgatroid996cc46802015-04-28 09:35:48 -0700178- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700179 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700180 _op.op = GRPC_OP_RECV_MESSAGE;
181 _op.data.recv_message = &_receivedMessage;
182 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700183 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700184 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700185 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700186 __strong typeof(self) strongSelf = weakSelf;
187 handler(strongSelf->_receivedMessage);
Jorge Canizales8d997752015-06-21 21:43:26 -0700188 };
189 }
murgatroid9954e93d42015-04-27 09:29:49 -0700190 }
191 return self;
192}
193
murgatroid9954e93d42015-04-27 09:29:49 -0700194@end
195
Muxi Yanbd19fc72016-10-25 13:28:12 -0700196@implementation GRPCOpRecvStatus {
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700197 grpc_status_code _statusCode;
198 char *_details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700199 size_t _detailsCapacity;
Jorge Canizales8d997752015-06-21 21:43:26 -0700200 grpc_metadata_array _trailers;
murgatroid9954e93d42015-04-27 09:29:49 -0700201}
202
Muxi Yanbd19fc72016-10-25 13:28:12 -0700203- (instancetype)init {
murgatroid9954e93d42015-04-27 09:29:49 -0700204 return [self initWithHandler:nil];
205}
206
Muxi Yanbd19fc72016-10-25 13:28:12 -0700207- (instancetype)initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700208 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700209 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
210 _op.data.recv_status_on_client.status = &_statusCode;
211 _op.data.recv_status_on_client.status_details = &_details;
212 _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity;
213 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;
Muxi Yanbd19fc72016-10-25 13:28:12 -0700220 NSError *error =
221 [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
222 details:strongSelf->_details];
murgatroid99dbda9692015-06-24 16:55:55 -0700223 NSDictionary *trailers = [NSDictionary
Muxi Yanbd19fc72016-10-25 13:28:12 -0700224 grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700225 handler(error, trailers);
226 };
227 }
murgatroid9954e93d42015-04-27 09:29:49 -0700228 }
229 return self;
230}
231
murgatroid9933655f92015-04-29 11:15:35 -0700232- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700233 grpc_metadata_array_destroy(&_trailers);
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700234 gpr_free(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700235}
236
murgatroid9954e93d42015-04-27 09:29:49 -0700237@end
238
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700239#pragma mark GRPCWrappedCall
240
Jorge Canizalese8543b02015-08-01 17:37:40 -0700241@implementation GRPCWrappedCall {
242 GRPCCompletionQueue *_queue;
murgatroid9969927d62015-04-24 13:32:48 -0700243 grpc_call *_call;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700244}
245
246- (instancetype)init {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700247 return [self initWithHost:nil path:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700248}
249
Muxi Yanbd19fc72016-10-25 13:28:12 -0700250- (instancetype)initWithHost:(NSString *)host path:(NSString *)path {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700251 if (!path || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700252 [NSException raise:NSInvalidArgumentException
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700253 format:@"path and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700254 }
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700255
murgatroid9930b7d4e2015-04-24 10:36:43 -0700256 if (self = [super init]) {
Muxi Yanbd19fc72016-10-25 13:28:12 -0700257 // Each completion queue consumes one thread. There's a trade to be made
258 // between creating and consuming too many threads and having contention of
259 // multiple calls in a single completion queue. Currently we use a singleton
260 // queue.
Jorge Canizalese8543b02015-08-01 17:37:40 -0700261 _queue = [GRPCCompletionQueue completionQueue];
262
Muxi Yanbd19fc72016-10-25 13:28:12 -0700263 _call = [[GRPCHost hostWithAddress:host] unmanagedCallWithPath:path
264 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 Yanbd19fc72016-10-25 13:28:12 -0700276- (void)startBatchWithOperations:(NSArray *)operations
277 errorHandler:(void (^)())errorHandler {
murgatroid9969927d62015-04-24 13:32:48 -0700278 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700279 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700280 size_t i = 0;
Jorge Canizales8d997752015-06-21 21:43:26 -0700281 for (GRPCOperation *operation in operations) {
282 ops_array[i++] = operation.op;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700283 }
Muxi Yanbd19fc72016-10-25 13:28:12 -0700284 grpc_call_error error = grpc_call_start_batch(
285 _call, ops_array, nops, (__bridge_retained void *)(^(bool success) {
286 if (!success) {
287 if (errorHandler) {
288 errorHandler();
289 } else {
290 return;
291 }
292 }
293 for (GRPCOperation *operation in operations) {
294 [operation finish];
295 }
296 }),
297 NULL);
Jorge Canizales8d997752015-06-21 21:43:26 -0700298 gpr_free(ops_array);
299
murgatroid9930b7d4e2015-04-24 10:36:43 -0700300 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700301 [NSException raise:NSInternalInconsistencyException
Muxi Yanbd19fc72016-10-25 13:28:12 -0700302 format:@"A precondition for calling grpc_call_start_batch "
303 @"wasn't met. Error %i",
304 error];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700305 }
306}
307
308- (void)cancel {
Jorge Canizalesb3584aa2015-08-08 15:01:24 -0700309 grpc_call_cancel(_call, NULL);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700310}
311
312- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700313 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700314}
315
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700316@end