blob: d94b25091e6a02615256c44e9dae2e9e0fc83e41 [file] [log] [blame]
murgatroid9930b7d4e2015-04-24 10:36:43 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * 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"
murgatroid9969927d62015-04-24 13:32:48 -070035#import <Foundation/Foundation.h>
36#include <grpc/grpc.h>
37#include <grpc/byte_buffer.h>
38#include <grpc/support/alloc.h>
murgatroid9930b7d4e2015-04-24 10:36:43 -070039#import "GRPCCompletionQueue.h"
40#import "NSDictionary+GRPC.h"
41#import "NSData+GRPC.h"
42#import "NSError+GRPC.h"
murgatroid9930b7d4e2015-04-24 10:36:43 -070043
Jorge Canizales8d997752015-06-21 21:43:26 -070044@implementation GRPCOperation {
45@protected
Jorge Canizales88487042015-06-21 21:59:37 -070046 // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being
47 // initialized to zero.
Jorge Canizales8d997752015-06-21 21:43:26 -070048 grpc_op _op;
49 void(^_handler)();
murgatroid9954e93d42015-04-27 09:29:49 -070050}
51
Jorge Canizales8d997752015-06-21 21:43:26 -070052- (void)finish {
53 if (_handler) {
54 _handler();
55 }
56}
57@end
58
59@implementation GRPCOpSendMetadata
60
murgatroid9954e93d42015-04-27 09:29:49 -070061- (instancetype)init {
62 return [self initWithMetadata:nil handler:nil];
63}
64
Jorge Canizales8d997752015-06-21 21:43:26 -070065- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070066 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070067 _op.op = GRPC_OP_SEND_INITIAL_METADATA;
68 _op.data.send_initial_metadata.count = metadata.count;
69 _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray;
murgatroid9954e93d42015-04-27 09:29:49 -070070 _handler = handler;
71 }
72 return self;
73}
74
murgatroid9933655f92015-04-29 11:15:35 -070075- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -070076 gpr_free(_op.data.send_initial_metadata.metadata);
murgatroid9933655f92015-04-29 11:15:35 -070077}
78
murgatroid9954e93d42015-04-27 09:29:49 -070079@end
80
Jorge Canizales8d997752015-06-21 21:43:26 -070081@implementation GRPCOpSendMessage
murgatroid9954e93d42015-04-27 09:29:49 -070082
83- (instancetype)init {
84 return [self initWithMessage:nil handler:nil];
85}
86
Jorge Canizales8d997752015-06-21 21:43:26 -070087- (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070088 if (!message) {
murgatroid992101a482015-04-29 11:42:26 -070089 [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"];
murgatroid9954e93d42015-04-27 09:29:49 -070090 }
91 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070092 _op.op = GRPC_OP_SEND_MESSAGE;
93 _op.data.send_message = message.grpc_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -070094 _handler = handler;
95 }
96 return self;
97}
98
murgatroid9933655f92015-04-29 11:15:35 -070099- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700100 gpr_free(_op.data.send_message);
murgatroid9933655f92015-04-29 11:15:35 -0700101}
102
murgatroid9954e93d42015-04-27 09:29:49 -0700103@end
104
Jorge Canizales8d997752015-06-21 21:43:26 -0700105@implementation GRPCOpSendClose
murgatroid9954e93d42015-04-27 09:29:49 -0700106
107- (instancetype)init {
108 return [self initWithHandler:nil];
109}
110
Jorge Canizales8d997752015-06-21 21:43:26 -0700111- (instancetype)initWithHandler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700112 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700113 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
murgatroid9954e93d42015-04-27 09:29:49 -0700114 _handler = handler;
115 }
116 return self;
117}
118
murgatroid9954e93d42015-04-27 09:29:49 -0700119@end
120
Jorge Canizales8d997752015-06-21 21:43:26 -0700121@implementation GRPCOpRecvMetadata {
122 grpc_metadata_array _headers;
murgatroid9954e93d42015-04-27 09:29:49 -0700123}
124
125- (instancetype) init {
126 return [self initWithHandler:nil];
127}
128
129- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
130 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700131 _op.op = GRPC_OP_RECV_INITIAL_METADATA;
132 grpc_metadata_array_init(&_headers);
133 _op.data.recv_initial_metadata = &_headers;
134 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700135 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700136 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700137 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700138 __strong typeof(self) strongSelf = weakSelf;
139 NSDictionary *metadata = [NSDictionary
140 grpc_dictionaryFromMetadataArray:strongSelf->_headers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700141 handler(metadata);
142 };
143 }
murgatroid9954e93d42015-04-27 09:29:49 -0700144 }
145 return self;
146}
147
murgatroid9933655f92015-04-29 11:15:35 -0700148- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700149 grpc_metadata_array_destroy(&_headers);
murgatroid9933655f92015-04-29 11:15:35 -0700150}
151
murgatroid9954e93d42015-04-27 09:29:49 -0700152@end
153
154@implementation GRPCOpRecvMessage{
Jorge Canizales8d997752015-06-21 21:43:26 -0700155 grpc_byte_buffer *_receivedMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700156}
157
158- (instancetype)init {
159 return [self initWithHandler:nil];
160}
161
murgatroid996cc46802015-04-28 09:35:48 -0700162- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700163 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700164 _op.op = GRPC_OP_RECV_MESSAGE;
165 _op.data.recv_message = &_receivedMessage;
166 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700167 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700168 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700169 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700170 __strong typeof(self) strongSelf = weakSelf;
171 handler(strongSelf->_receivedMessage);
Jorge Canizales8d997752015-06-21 21:43:26 -0700172 };
173 }
murgatroid9954e93d42015-04-27 09:29:49 -0700174 }
175 return self;
176}
177
murgatroid9954e93d42015-04-27 09:29:49 -0700178@end
179
180@implementation GRPCOpRecvStatus{
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700181 grpc_status_code _statusCode;
182 char *_details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700183 size_t _detailsCapacity;
Jorge Canizales8d997752015-06-21 21:43:26 -0700184 grpc_metadata_array _trailers;
murgatroid9954e93d42015-04-27 09:29:49 -0700185}
186
187- (instancetype) init {
188 return [self initWithHandler:nil];
189}
190
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700191- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700192 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700193 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
194 _op.data.recv_status_on_client.status = &_statusCode;
195 _op.data.recv_status_on_client.status_details = &_details;
196 _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity;
197 grpc_metadata_array_init(&_trailers);
198 _op.data.recv_status_on_client.trailing_metadata = &_trailers;
199 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700200 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700201 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700202 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700203 __strong typeof(self) strongSelf = weakSelf;
204 NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
205 details:strongSelf->_details];
206 NSDictionary *trailers = [NSDictionary
207 grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700208 handler(error, trailers);
209 };
210 }
murgatroid9954e93d42015-04-27 09:29:49 -0700211 }
212 return self;
213}
214
murgatroid9933655f92015-04-29 11:15:35 -0700215- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700216 grpc_metadata_array_destroy(&_trailers);
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700217 gpr_free(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700218}
219
murgatroid9954e93d42015-04-27 09:29:49 -0700220@end
221
murgatroid9930b7d4e2015-04-24 10:36:43 -0700222@implementation GRPCWrappedCall{
murgatroid9969927d62015-04-24 13:32:48 -0700223 grpc_call *_call;
224 GRPCCompletionQueue *_queue;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700225}
226
227- (instancetype)init {
murgatroid99463a7a82015-04-24 11:41:43 -0700228 return [self initWithChannel:nil method:nil host:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700229}
230
murgatroid996cc46802015-04-28 09:35:48 -0700231- (instancetype)initWithChannel:(GRPCChannel *)channel
232 method:(NSString *)method
233 host:(NSString *)host {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700234 if (!channel || !method || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700235 [NSException raise:NSInvalidArgumentException
236 format:@"channel, method, and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700237 }
238
239 if (self = [super init]) {
240 static dispatch_once_t initialization;
241 dispatch_once(&initialization, ^{
242 grpc_init();
243 });
244
murgatroid9969927d62015-04-24 13:32:48 -0700245 _queue = [GRPCCompletionQueue completionQueue];
murgatroid99fe2c0c62015-04-29 10:59:43 -0700246 if (!_queue) {
247 return nil;
248 }
murgatroid996cc46802015-04-28 09:35:48 -0700249 _call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue,
250 method.UTF8String, host.UTF8String, gpr_inf_future);
murgatroid9969927d62015-04-24 13:32:48 -0700251 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700252 return nil;
253 }
254 }
255 return self;
256}
257
murgatroid9954e93d42015-04-27 09:29:49 -0700258- (void)startBatchWithOperations:(NSArray *)operations {
259 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700260}
261
murgatroid9954e93d42015-04-27 09:29:49 -0700262- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
murgatroid9969927d62015-04-24 13:32:48 -0700263 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700264 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700265 size_t i = 0;
Jorge Canizales8d997752015-06-21 21:43:26 -0700266 for (GRPCOperation *operation in operations) {
267 ops_array[i++] = operation.op;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700268 }
murgatroid996cc46802015-04-28 09:35:48 -0700269 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
murgatroid996b542442015-05-08 10:40:01 -0700270 (__bridge_retained void *)(^(bool success){
271 if (!success) {
murgatroid9969927d62015-04-24 13:32:48 -0700272 if (errorHandler) {
273 errorHandler();
274 } else {
murgatroid99def47aa2015-04-29 10:28:26 -0700275 return;
murgatroid9969927d62015-04-24 13:32:48 -0700276 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700277 }
Jorge Canizales8d997752015-06-21 21:43:26 -0700278 for (GRPCOperation *operation in operations) {
murgatroid992101a482015-04-29 11:42:26 -0700279 [operation finish];
murgatroid9969927d62015-04-24 13:32:48 -0700280 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700281 }));
Jorge Canizales8d997752015-06-21 21:43:26 -0700282 gpr_free(ops_array);
283
murgatroid9930b7d4e2015-04-24 10:36:43 -0700284 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700285 [NSException raise:NSInternalInconsistencyException
Jorge Canizalesbae38d92015-06-20 20:21:25 -0700286 format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i",
287 error];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700288 }
289}
290
291- (void)cancel {
murgatroid9969927d62015-04-24 13:32:48 -0700292 grpc_call_cancel(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700293}
294
295- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700296 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700297}
298
299@end