murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 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" |
Jorge Canizales | 3a5253e | 2015-07-31 23:48:56 -0700 | [diff] [blame] | 35 | |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 36 | #import <Foundation/Foundation.h> |
| 37 | #include <grpc/grpc.h> |
| 38 | #include <grpc/byte_buffer.h> |
| 39 | #include <grpc/support/alloc.h> |
Jorge Canizales | 3a5253e | 2015-07-31 23:48:56 -0700 | [diff] [blame] | 40 | |
Jorge Canizales | e8543b0 | 2015-08-01 17:37:40 -0700 | [diff] [blame] | 41 | #import "GRPCCompletionQueue.h" |
| 42 | #import "GRPCHost.h" |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 43 | #import "NSDictionary+GRPC.h" |
| 44 | #import "NSData+GRPC.h" |
| 45 | #import "NSError+GRPC.h" |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 46 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 47 | @implementation GRPCOperation { |
| 48 | @protected |
Jorge Canizales | 8848704 | 2015-06-21 21:59:37 -0700 | [diff] [blame] | 49 | // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being |
| 50 | // initialized to zero. |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 51 | grpc_op _op; |
| 52 | void(^_handler)(); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 55 | - (void)finish { |
| 56 | if (_handler) { |
Nicolas "Pixel" Noble | 7022436 | 2016-03-21 22:19:43 +0100 | [diff] [blame] | 57 | void(^handler)() = _handler; |
| 58 | _handler = nil; |
| 59 | handler(); |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | @end |
| 63 | |
| 64 | @implementation GRPCOpSendMetadata |
| 65 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 66 | - (instancetype)init { |
Muxi Yan | e97f7c0 | 2016-09-28 11:25:57 -0700 | [diff] [blame] | 67 | return [self initWithMetadata:nil flags:0 handler:nil]; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Muxi Yan | fdea83d | 2016-09-27 16:11:18 -0700 | [diff] [blame] | 70 | - (instancetype)initWithMetadata:(NSDictionary *)metadata |
| 71 | handler:(void (^)())handler { |
Muxi Yan | e97f7c0 | 2016-09-28 11:25:57 -0700 | [diff] [blame] | 72 | return [self initWithMetadata:metadata flags:0 handler:handler]; |
Muxi Yan | fdea83d | 2016-09-27 16:11:18 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | - (instancetype)initWithMetadata:(NSDictionary *)metadata |
Muxi Yan | e97f7c0 | 2016-09-28 11:25:57 -0700 | [diff] [blame] | 76 | flags:(uint32_t)flags |
Muxi Yan | fdea83d | 2016-09-27 16:11:18 -0700 | [diff] [blame] | 77 | handler:(void (^)())handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 78 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 79 | _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 Quintas | e825df7 | 2016-05-17 22:11:18 -0700 | [diff] [blame] | 82 | _op.data.send_initial_metadata.maybe_compression_level.is_set = false; |
David Garcia Quintas | 8ba42be | 2016-06-07 17:30:20 -0700 | [diff] [blame] | 83 | _op.data.send_initial_metadata.maybe_compression_level.level = 0; |
Muxi Yan | e97f7c0 | 2016-09-28 11:25:57 -0700 | [diff] [blame] | 84 | _op.flags = flags; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 85 | _handler = handler; |
| 86 | } |
| 87 | return self; |
| 88 | } |
| 89 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 90 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 91 | gpr_free(_op.data.send_initial_metadata.metadata); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 92 | } |
| 93 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 94 | @end |
| 95 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 96 | @implementation GRPCOpSendMessage |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 97 | |
| 98 | - (instancetype)init { |
| 99 | return [self initWithMessage:nil handler:nil]; |
| 100 | } |
| 101 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 102 | - (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 103 | if (!message) { |
murgatroid99 | 2101a48 | 2015-04-29 11:42:26 -0700 | [diff] [blame] | 104 | [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"]; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 105 | } |
| 106 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 107 | _op.op = GRPC_OP_SEND_MESSAGE; |
| 108 | _op.data.send_message = message.grpc_byteBuffer; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 109 | _handler = handler; |
| 110 | } |
| 111 | return self; |
| 112 | } |
| 113 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 114 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 115 | gpr_free(_op.data.send_message); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 116 | } |
| 117 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 118 | @end |
| 119 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 120 | @implementation GRPCOpSendClose |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 121 | |
| 122 | - (instancetype)init { |
| 123 | return [self initWithHandler:nil]; |
| 124 | } |
| 125 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 126 | - (instancetype)initWithHandler:(void (^)())handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 127 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 128 | _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 129 | _handler = handler; |
| 130 | } |
| 131 | return self; |
| 132 | } |
| 133 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 134 | @end |
| 135 | |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 136 | @implementation GRPCOpRecvMetadata { |
| 137 | grpc_metadata_array _headers; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | - (instancetype) init { |
| 141 | return [self initWithHandler:nil]; |
| 142 | } |
| 143 | |
| 144 | - (instancetype) initWithHandler:(void (^)(NSDictionary *))handler { |
| 145 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 146 | _op.op = GRPC_OP_RECV_INITIAL_METADATA; |
| 147 | grpc_metadata_array_init(&_headers); |
| 148 | _op.data.recv_initial_metadata = &_headers; |
| 149 | if (handler) { |
murgatroid99 | 231103b | 2015-06-25 10:14:09 -0700 | [diff] [blame] | 150 | // Prevent reference cycle with _handler |
murgatroid99 | dbda969 | 2015-06-24 16:55:55 -0700 | [diff] [blame] | 151 | __weak typeof(self) weakSelf = self; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 152 | _handler = ^{ |
murgatroid99 | dbda969 | 2015-06-24 16:55:55 -0700 | [diff] [blame] | 153 | __strong typeof(self) strongSelf = weakSelf; |
| 154 | NSDictionary *metadata = [NSDictionary |
| 155 | grpc_dictionaryFromMetadataArray:strongSelf->_headers]; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 156 | handler(metadata); |
| 157 | }; |
| 158 | } |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 159 | } |
| 160 | return self; |
| 161 | } |
| 162 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 163 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 164 | grpc_metadata_array_destroy(&_headers); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 167 | @end |
| 168 | |
| 169 | @implementation GRPCOpRecvMessage{ |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 170 | grpc_byte_buffer *_receivedMessage; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | - (instancetype)init { |
| 174 | return [self initWithHandler:nil]; |
| 175 | } |
| 176 | |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 177 | - (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 178 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 179 | _op.op = GRPC_OP_RECV_MESSAGE; |
| 180 | _op.data.recv_message = &_receivedMessage; |
| 181 | if (handler) { |
murgatroid99 | 231103b | 2015-06-25 10:14:09 -0700 | [diff] [blame] | 182 | // Prevent reference cycle with _handler |
murgatroid99 | dbda969 | 2015-06-24 16:55:55 -0700 | [diff] [blame] | 183 | __weak typeof(self) weakSelf = self; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 184 | _handler = ^{ |
murgatroid99 | dbda969 | 2015-06-24 16:55:55 -0700 | [diff] [blame] | 185 | __strong typeof(self) strongSelf = weakSelf; |
| 186 | handler(strongSelf->_receivedMessage); |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 187 | }; |
| 188 | } |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 189 | } |
| 190 | return self; |
| 191 | } |
| 192 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 193 | @end |
| 194 | |
| 195 | @implementation GRPCOpRecvStatus{ |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 196 | grpc_status_code _statusCode; |
| 197 | char *_details; |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 198 | size_t _detailsCapacity; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 199 | grpc_metadata_array _trailers; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | - (instancetype) init { |
| 203 | return [self initWithHandler:nil]; |
| 204 | } |
| 205 | |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 206 | - (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler { |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 207 | if (self = [super init]) { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 208 | _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 209 | _op.data.recv_status_on_client.status = &_statusCode; |
| 210 | _op.data.recv_status_on_client.status_details = &_details; |
| 211 | _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity; |
| 212 | grpc_metadata_array_init(&_trailers); |
| 213 | _op.data.recv_status_on_client.trailing_metadata = &_trailers; |
| 214 | if (handler) { |
murgatroid99 | 231103b | 2015-06-25 10:14:09 -0700 | [diff] [blame] | 215 | // Prevent reference cycle with _handler |
murgatroid99 | dbda969 | 2015-06-24 16:55:55 -0700 | [diff] [blame] | 216 | __weak typeof(self) weakSelf = self; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 217 | _handler = ^{ |
murgatroid99 | dbda969 | 2015-06-24 16:55:55 -0700 | [diff] [blame] | 218 | __strong typeof(self) strongSelf = weakSelf; |
| 219 | NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode |
| 220 | details:strongSelf->_details]; |
| 221 | NSDictionary *trailers = [NSDictionary |
| 222 | grpc_dictionaryFromMetadataArray:strongSelf->_trailers]; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 223 | handler(error, trailers); |
| 224 | }; |
| 225 | } |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 226 | } |
| 227 | return self; |
| 228 | } |
| 229 | |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 230 | - (void)dealloc { |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 231 | grpc_metadata_array_destroy(&_trailers); |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 232 | gpr_free(_details); |
murgatroid99 | 33655f9 | 2015-04-29 11:15:35 -0700 | [diff] [blame] | 233 | } |
| 234 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 235 | @end |
| 236 | |
Jorge Canizales | 3a5253e | 2015-07-31 23:48:56 -0700 | [diff] [blame] | 237 | #pragma mark GRPCWrappedCall |
| 238 | |
Jorge Canizales | e8543b0 | 2015-08-01 17:37:40 -0700 | [diff] [blame] | 239 | @implementation GRPCWrappedCall { |
| 240 | GRPCCompletionQueue *_queue; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 241 | grpc_call *_call; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | - (instancetype)init { |
Jorge Canizales | 3a5253e | 2015-07-31 23:48:56 -0700 | [diff] [blame] | 245 | return [self initWithHost:nil path:nil]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Jorge Canizales | 3a5253e | 2015-07-31 23:48:56 -0700 | [diff] [blame] | 248 | - (instancetype)initWithHost:(NSString *)host |
| 249 | path:(NSString *)path { |
| 250 | if (!path || !host) { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 251 | [NSException raise:NSInvalidArgumentException |
Jorge Canizales | 3a5253e | 2015-07-31 23:48:56 -0700 | [diff] [blame] | 252 | format:@"path and host cannot be nil."]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 253 | } |
Jorge Canizales | bd993df | 2015-08-01 02:51:22 -0700 | [diff] [blame] | 254 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 255 | if (self = [super init]) { |
Jorge Canizales | e8543b0 | 2015-08-01 17:37:40 -0700 | [diff] [blame] | 256 | // Each completion queue consumes one thread. There's a trade to be made between creating and |
| 257 | // consuming too many threads and having contention of multiple calls in a single completion |
Jorge Canizales | 5e824fa | 2016-06-24 11:32:51 -0700 | [diff] [blame] | 258 | // queue. Currently we use a singleton queue. |
Jorge Canizales | e8543b0 | 2015-08-01 17:37:40 -0700 | [diff] [blame] | 259 | _queue = [GRPCCompletionQueue completionQueue]; |
| 260 | |
| 261 | _call = [[GRPCHost hostWithAddress:host] unmanagedCallWithPath:path completionQueue:_queue]; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 262 | if (_call == NULL) { |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 263 | return nil; |
| 264 | } |
| 265 | } |
| 266 | return self; |
| 267 | } |
| 268 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 269 | - (void)startBatchWithOperations:(NSArray *)operations { |
| 270 | [self startBatchWithOperations:operations errorHandler:nil]; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 271 | } |
| 272 | |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 273 | - (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 274 | size_t nops = operations.count; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 275 | grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op)); |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 276 | size_t i = 0; |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 277 | for (GRPCOperation *operation in operations) { |
| 278 | ops_array[i++] = operation.op; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 279 | } |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 280 | grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops, |
murgatroid99 | 6b54244 | 2015-05-08 10:40:01 -0700 | [diff] [blame] | 281 | (__bridge_retained void *)(^(bool success){ |
| 282 | if (!success) { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 283 | if (errorHandler) { |
| 284 | errorHandler(); |
| 285 | } else { |
murgatroid99 | def47aa | 2015-04-29 10:28:26 -0700 | [diff] [blame] | 286 | return; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 287 | } |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 288 | } |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 289 | for (GRPCOperation *operation in operations) { |
murgatroid99 | 2101a48 | 2015-04-29 11:42:26 -0700 | [diff] [blame] | 290 | [operation finish]; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 291 | } |
Jorge Canizales | b3584aa | 2015-08-08 15:01:24 -0700 | [diff] [blame] | 292 | }), NULL); |
Jorge Canizales | 8d99775 | 2015-06-21 21:43:26 -0700 | [diff] [blame] | 293 | gpr_free(ops_array); |
| 294 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 295 | if (error != GRPC_CALL_OK) { |
murgatroid99 | 6a084f4 | 2015-04-29 10:18:05 -0700 | [diff] [blame] | 296 | [NSException raise:NSInternalInconsistencyException |
Jorge Canizales | bae38d9 | 2015-06-20 20:21:25 -0700 | [diff] [blame] | 297 | format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i", |
| 298 | error]; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | - (void)cancel { |
Jorge Canizales | b3584aa | 2015-08-08 15:01:24 -0700 | [diff] [blame] | 303 | grpc_call_cancel(_call, NULL); |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | - (void)dealloc { |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 307 | grpc_call_destroy(_call); |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Jorge Canizales | bd993df | 2015-08-01 02:51:22 -0700 | [diff] [blame] | 310 | @end |