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 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 44 | @implementation GRPCOpSendMetadata{ |
| 45 | void(^_handler)(void); |
| 46 | grpc_metadata *_send_metadata; |
| 47 | size_t _count; |
| 48 | } |
| 49 | |
| 50 | - (instancetype)init { |
| 51 | return [self initWithMetadata:nil handler:nil]; |
| 52 | } |
| 53 | |
| 54 | - (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler { |
| 55 | if (self = [super init]) { |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 56 | _send_metadata = [metadata grpc_getMetadataArray]; |
| 57 | _count = metadata.count; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 58 | _handler = handler; |
| 59 | } |
| 60 | return self; |
| 61 | } |
| 62 | |
| 63 | - (void)getOp:(grpc_op *)op { |
| 64 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 65 | op->data.send_initial_metadata.count = _count; |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 66 | op->data.send_initial_metadata.metadata = _send_metadata; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | - (void (^)(void))opProcessor { |
| 70 | return ^{ |
| 71 | gpr_free(_send_metadata); |
| 72 | if (_handler) { |
| 73 | _handler(); |
| 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | @end |
| 79 | |
| 80 | @implementation GRPCOpSendMessage{ |
| 81 | void(^_handler)(void); |
| 82 | grpc_byte_buffer *_byte_buffer; |
| 83 | } |
| 84 | |
| 85 | - (instancetype)init { |
| 86 | return [self initWithMessage:nil handler:nil]; |
| 87 | } |
| 88 | |
| 89 | - (instancetype)initWithMessage:(NSData *)message handler:(void (^)(void))handler { |
| 90 | if (!message) { |
| 91 | [NSException raise:NSInvalidArgumentException format:@"message cannot be null"]; |
| 92 | } |
| 93 | if (self = [super init]) { |
| 94 | _byte_buffer = [message grpc_byteBuffer]; |
| 95 | _handler = handler; |
| 96 | } |
| 97 | return self; |
| 98 | } |
| 99 | |
| 100 | - (void)getOp:(grpc_op *)op { |
| 101 | op->op = GRPC_OP_SEND_MESSAGE; |
| 102 | op->data.send_message = _byte_buffer; |
| 103 | } |
| 104 | |
| 105 | - (void (^)(void))opProcessor { |
| 106 | return ^{ |
| 107 | gpr_free(_byte_buffer); |
| 108 | if (_handler) { |
| 109 | _handler(); |
| 110 | } |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | @end |
| 115 | |
| 116 | @implementation GRPCOpSendClose{ |
| 117 | void(^_handler)(void); |
| 118 | } |
| 119 | |
| 120 | - (instancetype)init { |
| 121 | return [self initWithHandler:nil]; |
| 122 | } |
| 123 | |
| 124 | - (instancetype)initWithHandler:(void (^)(void))handler { |
| 125 | if (self = [super init]) { |
| 126 | _handler = handler; |
| 127 | } |
| 128 | return self; |
| 129 | } |
| 130 | |
| 131 | - (void)getOp:(grpc_op *)op { |
| 132 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 133 | } |
| 134 | |
| 135 | - (void (^)(void))opProcessor { |
| 136 | return ^{ |
| 137 | if (_handler) { |
| 138 | _handler(); |
| 139 | } |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | @end |
| 144 | |
| 145 | @implementation GRPCOpRecvMetadata{ |
| 146 | void(^_handler)(NSDictionary *); |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 147 | grpc_metadata_array _recv_initial_metadata; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | - (instancetype) init { |
| 151 | return [self initWithHandler:nil]; |
| 152 | } |
| 153 | |
| 154 | - (instancetype) initWithHandler:(void (^)(NSDictionary *))handler { |
| 155 | if (self = [super init]) { |
| 156 | _handler = handler; |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 157 | grpc_metadata_array_init(&_recv_initial_metadata); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 158 | } |
| 159 | return self; |
| 160 | } |
| 161 | |
| 162 | - (void)getOp:(grpc_op *)op { |
| 163 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 164 | op->data.recv_initial_metadata = &_recv_initial_metadata; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | - (void (^)(void))opProcessor { |
| 168 | return ^{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 169 | NSDictionary *metadata = [NSDictionary |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 170 | grpc_dictionaryFromMetadata:_recv_initial_metadata.metadata |
| 171 | count:_recv_initial_metadata.count]; |
| 172 | grpc_metadata_array_destroy(&_recv_initial_metadata); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 173 | if (_handler) { |
| 174 | _handler(metadata); |
| 175 | } |
| 176 | }; |
| 177 | } |
| 178 | |
| 179 | @end |
| 180 | |
| 181 | @implementation GRPCOpRecvMessage{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 182 | void(^_handler)(grpc_byte_buffer *); |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 183 | grpc_byte_buffer *_recv_message; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | - (instancetype)init { |
| 187 | return [self initWithHandler:nil]; |
| 188 | } |
| 189 | |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 190 | - (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 191 | if (self = [super init]) { |
| 192 | _handler = handler; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 193 | } |
| 194 | return self; |
| 195 | } |
| 196 | |
| 197 | - (void)getOp:(grpc_op *)op { |
| 198 | op->op = GRPC_OP_RECV_MESSAGE; |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 199 | op->data.recv_message = &_recv_message; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | - (void (^)(void))opProcessor { |
| 203 | return ^{ |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 204 | if (_handler) { |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 205 | _handler(_recv_message); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 206 | } |
| 207 | }; |
| 208 | } |
| 209 | |
| 210 | @end |
| 211 | |
| 212 | @implementation GRPCOpRecvStatus{ |
| 213 | void(^_handler)(NSError *); |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 214 | size_t _details_capacity; |
| 215 | grpc_status _status; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | - (instancetype) init { |
| 219 | return [self initWithHandler:nil]; |
| 220 | } |
| 221 | |
| 222 | - (instancetype) initWithHandler:(void (^)(NSError *))handler { |
| 223 | if (self = [super init]) { |
| 224 | _handler = handler; |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 225 | _status.details = NULL; |
| 226 | _details_capacity = 0; |
| 227 | grpc_metadata_array_init(&_status.metadata); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 228 | } |
| 229 | return self; |
| 230 | } |
| 231 | |
| 232 | - (void)getOp:(grpc_op *)op { |
| 233 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 234 | op->data.recv_status_on_client.status = &_status.status; |
| 235 | op->data.recv_status_on_client.status_details = &_status.details; |
| 236 | op->data.recv_status_on_client.status_details_capacity = &_details_capacity; |
| 237 | op->data.recv_status_on_client.trailing_metadata = &_status.metadata; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | - (void (^)(void))opProcessor { |
| 241 | return ^{ |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 242 | if (_handler) { |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 243 | NSError *error = [NSError grpc_errorFromStatus:&_status]; |
| 244 | grpc_metadata_array_destroy(&_status.metadata); |
| 245 | _handler(error); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 246 | } |
| 247 | }; |
| 248 | } |
| 249 | |
| 250 | @end |
| 251 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 252 | @implementation GRPCWrappedCall{ |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 253 | grpc_call *_call; |
| 254 | GRPCCompletionQueue *_queue; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | - (instancetype)init { |
murgatroid99 | 463a7a8 | 2015-04-24 11:41:43 -0700 | [diff] [blame] | 258 | return [self initWithChannel:nil method:nil host:nil]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 259 | } |
| 260 | |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 261 | - (instancetype)initWithChannel:(GRPCChannel *)channel |
| 262 | method:(NSString *)method |
| 263 | host:(NSString *)host { |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 264 | if (!channel || !method || !host) { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 265 | [NSException raise:NSInvalidArgumentException |
| 266 | format:@"channel, method, and host cannot be nil."]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (self = [super init]) { |
| 270 | static dispatch_once_t initialization; |
| 271 | dispatch_once(&initialization, ^{ |
| 272 | grpc_init(); |
| 273 | }); |
| 274 | |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 275 | _queue = [GRPCCompletionQueue completionQueue]; |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 276 | _call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue, |
| 277 | method.UTF8String, host.UTF8String, gpr_inf_future); |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 278 | if (_call == NULL) { |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 279 | return nil; |
| 280 | } |
| 281 | } |
| 282 | return self; |
| 283 | } |
| 284 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 285 | - (void)startBatchWithOperations:(NSArray *)operations { |
| 286 | [self startBatchWithOperations:operations errorHandler:nil]; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 287 | } |
| 288 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 289 | - (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler { |
| 290 | NSMutableArray *opProcessors = [NSMutableArray array]; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 291 | size_t nops = operations.count; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 292 | grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op)); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 293 | size_t i = 0; |
| 294 | for (id op in operations) { |
| 295 | [op getOp:&ops_array[i]]; |
| 296 | [opProcessors addObject:[op opProcessor]]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 297 | } |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 298 | grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops, |
| 299 | (__bridge_retained void *)(^(grpc_op_error error){ |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 300 | if (error != GRPC_OP_OK) { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 301 | if (errorHandler) { |
| 302 | errorHandler(); |
| 303 | } else { |
murgatroid99 | def47aa | 2015-04-29 10:28:26 -0700 | [diff] [blame^] | 304 | return; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 305 | } |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 306 | } |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 307 | for (void(^processor)(void) in opProcessors) { |
| 308 | processor(); |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 309 | } |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 310 | })); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 311 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 312 | if (error != GRPC_CALL_OK) { |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 313 | [NSException raise:NSInternalInconsistencyException |
| 314 | format:@"A precondition for calling grpc_call_start_batch wasn't met"]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | - (void)cancel { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 319 | grpc_call_cancel(_call); |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | - (void)dealloc { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 323 | grpc_call_destroy(_call); |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @end |