murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 34 | #import "GRPCWrappedCall.h" |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 35 | #import <Foundation/Foundation.h> |
| 36 | #include <grpc/grpc.h> |
| 37 | #include <grpc/byte_buffer.h> |
| 38 | #include <grpc/support/alloc.h> |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 39 | #import "GRPCCompletionQueue.h" |
| 40 | #import "NSDictionary+GRPC.h" |
| 41 | #import "NSData+GRPC.h" |
| 42 | #import "NSError+GRPC.h" |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 43 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 44 | @implementation GRPCOperation { |
| 45 | @protected |
Jorge Canizales | 8848704 | 2015-06-21 21:59:37 -0700 | [diff] [blame] | 46 | // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being |
| 47 | // initialized to zero. |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 48 | grpc_op _op; |
| 49 | void(^_handler)(); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 52 | - (void)finish { |
| 53 | if (_handler) { |
| 54 | _handler(); |
| 55 | } |
| 56 | } |
| 57 | @end |
| 58 | |
| 59 | @implementation GRPCOpSendMetadata |
| 60 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 61 | - (instancetype)init { |
| 62 | return [self initWithMetadata:nil handler:nil]; |
| 63 | } |
| 64 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 65 | - (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 66 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 67 | _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; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 70 | _handler = handler; |
| 71 | } |
| 72 | return self; |
| 73 | } |
| 74 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 75 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 76 | gpr_free(_op.data.send_initial_metadata.metadata); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 77 | } |
| 78 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 79 | @end |
| 80 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 81 | @implementation GRPCOpSendMessage |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 82 | |
| 83 | - (instancetype)init { |
| 84 | return [self initWithMessage:nil handler:nil]; |
| 85 | } |
| 86 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 87 | - (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 88 | if (!message) { |
murgatroid99 | 2101a48 | 2015-04-29 11:42:26 -0700 | [diff] [blame] | 89 | [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"]; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 90 | } |
| 91 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 92 | _op.op = GRPC_OP_SEND_MESSAGE; |
| 93 | _op.data.send_message = message.grpc_byteBuffer; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 94 | _handler = handler; |
| 95 | } |
| 96 | return self; |
| 97 | } |
| 98 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 99 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 100 | gpr_free(_op.data.send_message); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 101 | } |
| 102 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 103 | @end |
| 104 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 105 | @implementation GRPCOpSendClose |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 106 | |
| 107 | - (instancetype)init { |
| 108 | return [self initWithHandler:nil]; |
| 109 | } |
| 110 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 111 | - (instancetype)initWithHandler:(void (^)())handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 112 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 113 | _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 114 | _handler = handler; |
| 115 | } |
| 116 | return self; |
| 117 | } |
| 118 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 119 | @end |
| 120 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 121 | @implementation GRPCOpRecvMetadata { |
| 122 | grpc_metadata_array _headers; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | - (instancetype) init { |
| 126 | return [self initWithHandler:nil]; |
| 127 | } |
| 128 | |
| 129 | - (instancetype) initWithHandler:(void (^)(NSDictionary *))handler { |
| 130 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 131 | _op.op = GRPC_OP_RECV_INITIAL_METADATA; |
| 132 | grpc_metadata_array_init(&_headers); |
| 133 | _op.data.recv_initial_metadata = &_headers; |
| 134 | if (handler) { |
| 135 | _handler = ^{ |
| 136 | NSDictionary *metadata = [NSDictionary grpc_dictionaryFromMetadataArray:_headers]; |
| 137 | handler(metadata); |
| 138 | }; |
| 139 | } |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 140 | } |
| 141 | return self; |
| 142 | } |
| 143 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 144 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 145 | grpc_metadata_array_destroy(&_headers); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 146 | } |
| 147 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 148 | @end |
| 149 | |
| 150 | @implementation GRPCOpRecvMessage{ |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 151 | grpc_byte_buffer *_receivedMessage; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | - (instancetype)init { |
| 155 | return [self initWithHandler:nil]; |
| 156 | } |
| 157 | |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 158 | - (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 159 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 160 | _op.op = GRPC_OP_RECV_MESSAGE; |
| 161 | _op.data.recv_message = &_receivedMessage; |
| 162 | if (handler) { |
| 163 | _handler = ^{ |
| 164 | handler(_receivedMessage); |
| 165 | }; |
| 166 | } |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 167 | } |
| 168 | return self; |
| 169 | } |
| 170 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 171 | @end |
| 172 | |
| 173 | @implementation GRPCOpRecvStatus{ |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 174 | grpc_status_code _statusCode; |
| 175 | char *_details; |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 176 | size_t _detailsCapacity; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 177 | grpc_metadata_array _trailers; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | - (instancetype) init { |
| 181 | return [self initWithHandler:nil]; |
| 182 | } |
| 183 | |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 184 | - (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 185 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 186 | _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 187 | _op.data.recv_status_on_client.status = &_statusCode; |
| 188 | _op.data.recv_status_on_client.status_details = &_details; |
| 189 | _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity; |
| 190 | grpc_metadata_array_init(&_trailers); |
| 191 | _op.data.recv_status_on_client.trailing_metadata = &_trailers; |
| 192 | if (handler) { |
| 193 | _handler = ^{ |
| 194 | NSError *error = [NSError grpc_errorFromStatusCode:_statusCode details:_details]; |
| 195 | NSDictionary *trailers = [NSDictionary grpc_dictionaryFromMetadataArray:_trailers]; |
| 196 | handler(error, trailers); |
| 197 | }; |
| 198 | } |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 199 | } |
| 200 | return self; |
| 201 | } |
| 202 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 203 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 204 | grpc_metadata_array_destroy(&_trailers); |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 205 | gpr_free(_details); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 206 | } |
| 207 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 208 | @end |
| 209 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 210 | @implementation GRPCWrappedCall{ |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 211 | grpc_call *_call; |
| 212 | GRPCCompletionQueue *_queue; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | - (instancetype)init { |
murgatroid99 | 463a7a8 | 2015-04-24 11:41:43 -0700 | [diff] [blame] | 216 | return [self initWithChannel:nil method:nil host:nil]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 217 | } |
| 218 | |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 219 | - (instancetype)initWithChannel:(GRPCChannel *)channel |
| 220 | method:(NSString *)method |
| 221 | host:(NSString *)host { |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 222 | if (!channel || !method || !host) { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 223 | [NSException raise:NSInvalidArgumentException |
| 224 | format:@"channel, method, and host cannot be nil."]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if (self = [super init]) { |
| 228 | static dispatch_once_t initialization; |
| 229 | dispatch_once(&initialization, ^{ |
| 230 | grpc_init(); |
| 231 | }); |
| 232 | |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 233 | _queue = [GRPCCompletionQueue completionQueue]; |
murgatroid99 | fe2c0c6 | 2015-04-29 10:59:43 -0700 | [diff] [blame] | 234 | if (!_queue) { |
| 235 | return nil; |
| 236 | } |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 237 | _call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue, |
| 238 | method.UTF8String, host.UTF8String, gpr_inf_future); |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 239 | if (_call == NULL) { |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 240 | return nil; |
| 241 | } |
| 242 | } |
| 243 | return self; |
| 244 | } |
| 245 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 246 | - (void)startBatchWithOperations:(NSArray *)operations { |
| 247 | [self startBatchWithOperations:operations errorHandler:nil]; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 248 | } |
| 249 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 250 | - (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 251 | size_t nops = operations.count; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 252 | grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op)); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 253 | size_t i = 0; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 254 | for (GRPCOperation *operation in operations) { |
| 255 | ops_array[i++] = operation.op; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 256 | } |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 257 | grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops, |
murgatroid99 | 6b54244 | 2015-05-08 10:40:01 -0700 | [diff] [blame] | 258 | (__bridge_retained void *)(^(bool success){ |
| 259 | if (!success) { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 260 | if (errorHandler) { |
| 261 | errorHandler(); |
| 262 | } else { |
murgatroid99 | def47aa | 2015-04-29 10:28:26 -0700 | [diff] [blame] | 263 | return; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 264 | } |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 265 | } |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 266 | for (GRPCOperation *operation in operations) { |
murgatroid99 | 2101a48 | 2015-04-29 11:42:26 -0700 | [diff] [blame] | 267 | [operation finish]; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 268 | } |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 269 | })); |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 270 | gpr_free(ops_array); |
| 271 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 272 | if (error != GRPC_CALL_OK) { |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 273 | [NSException raise:NSInternalInconsistencyException |
Jorge Canizales | bae38d9 | 2015-06-20 20:21:25 -0700 | [diff] [blame] | 274 | format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i", |
| 275 | error]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | - (void)cancel { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 280 | grpc_call_cancel(_call); |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | - (void)dealloc { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 284 | grpc_call_destroy(_call); |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @end |