OLD | NEW |
(Empty) | |
| 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 |
| 34 #import "GRPCWrappedCall.h" |
| 35 |
| 36 #import <Foundation/Foundation.h> |
| 37 #include <grpc/grpc.h> |
| 38 #include <grpc/byte_buffer.h> |
| 39 #include <grpc/support/alloc.h> |
| 40 |
| 41 #import "GRPCCompletionQueue.h" |
| 42 #import "GRPCHost.h" |
| 43 #import "NSDictionary+GRPC.h" |
| 44 #import "NSData+GRPC.h" |
| 45 #import "NSError+GRPC.h" |
| 46 |
| 47 @implementation GRPCOperation { |
| 48 @protected |
| 49 // Most operation subclasses don't set any flags in the grpc_op, and rely on t
he flag member being |
| 50 // initialized to zero. |
| 51 grpc_op _op; |
| 52 void(^_handler)(); |
| 53 } |
| 54 |
| 55 - (void)finish { |
| 56 if (_handler) { |
| 57 _handler(); |
| 58 } |
| 59 } |
| 60 @end |
| 61 |
| 62 @implementation GRPCOpSendMetadata |
| 63 |
| 64 - (instancetype)init { |
| 65 return [self initWithMetadata:nil handler:nil]; |
| 66 } |
| 67 |
| 68 - (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())ha
ndler { |
| 69 if (self = [super init]) { |
| 70 _op.op = GRPC_OP_SEND_INITIAL_METADATA; |
| 71 _op.data.send_initial_metadata.count = metadata.count; |
| 72 _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray; |
| 73 _handler = handler; |
| 74 } |
| 75 return self; |
| 76 } |
| 77 |
| 78 - (void)dealloc { |
| 79 gpr_free(_op.data.send_initial_metadata.metadata); |
| 80 } |
| 81 |
| 82 @end |
| 83 |
| 84 @implementation GRPCOpSendMessage |
| 85 |
| 86 - (instancetype)init { |
| 87 return [self initWithMessage:nil handler:nil]; |
| 88 } |
| 89 |
| 90 - (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler { |
| 91 if (!message) { |
| 92 [NSException raise:NSInvalidArgumentException format:@"message cannot be nil
"]; |
| 93 } |
| 94 if (self = [super init]) { |
| 95 _op.op = GRPC_OP_SEND_MESSAGE; |
| 96 _op.data.send_message = message.grpc_byteBuffer; |
| 97 _handler = handler; |
| 98 } |
| 99 return self; |
| 100 } |
| 101 |
| 102 - (void)dealloc { |
| 103 gpr_free(_op.data.send_message); |
| 104 } |
| 105 |
| 106 @end |
| 107 |
| 108 @implementation GRPCOpSendClose |
| 109 |
| 110 - (instancetype)init { |
| 111 return [self initWithHandler:nil]; |
| 112 } |
| 113 |
| 114 - (instancetype)initWithHandler:(void (^)())handler { |
| 115 if (self = [super init]) { |
| 116 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 117 _handler = handler; |
| 118 } |
| 119 return self; |
| 120 } |
| 121 |
| 122 @end |
| 123 |
| 124 @implementation GRPCOpRecvMetadata { |
| 125 grpc_metadata_array _headers; |
| 126 } |
| 127 |
| 128 - (instancetype) init { |
| 129 return [self initWithHandler:nil]; |
| 130 } |
| 131 |
| 132 - (instancetype) initWithHandler:(void (^)(NSDictionary *))handler { |
| 133 if (self = [super init]) { |
| 134 _op.op = GRPC_OP_RECV_INITIAL_METADATA; |
| 135 grpc_metadata_array_init(&_headers); |
| 136 _op.data.recv_initial_metadata = &_headers; |
| 137 if (handler) { |
| 138 // Prevent reference cycle with _handler |
| 139 __weak typeof(self) weakSelf = self; |
| 140 _handler = ^{ |
| 141 __strong typeof(self) strongSelf = weakSelf; |
| 142 NSDictionary *metadata = [NSDictionary |
| 143 grpc_dictionaryFromMetadataArray:strongSelf->_
headers]; |
| 144 handler(metadata); |
| 145 }; |
| 146 } |
| 147 } |
| 148 return self; |
| 149 } |
| 150 |
| 151 - (void)dealloc { |
| 152 grpc_metadata_array_destroy(&_headers); |
| 153 } |
| 154 |
| 155 @end |
| 156 |
| 157 @implementation GRPCOpRecvMessage{ |
| 158 grpc_byte_buffer *_receivedMessage; |
| 159 } |
| 160 |
| 161 - (instancetype)init { |
| 162 return [self initWithHandler:nil]; |
| 163 } |
| 164 |
| 165 - (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler { |
| 166 if (self = [super init]) { |
| 167 _op.op = GRPC_OP_RECV_MESSAGE; |
| 168 _op.data.recv_message = &_receivedMessage; |
| 169 if (handler) { |
| 170 // Prevent reference cycle with _handler |
| 171 __weak typeof(self) weakSelf = self; |
| 172 _handler = ^{ |
| 173 __strong typeof(self) strongSelf = weakSelf; |
| 174 handler(strongSelf->_receivedMessage); |
| 175 }; |
| 176 } |
| 177 } |
| 178 return self; |
| 179 } |
| 180 |
| 181 @end |
| 182 |
| 183 @implementation GRPCOpRecvStatus{ |
| 184 grpc_status_code _statusCode; |
| 185 char *_details; |
| 186 size_t _detailsCapacity; |
| 187 grpc_metadata_array _trailers; |
| 188 } |
| 189 |
| 190 - (instancetype) init { |
| 191 return [self initWithHandler:nil]; |
| 192 } |
| 193 |
| 194 - (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler { |
| 195 if (self = [super init]) { |
| 196 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 197 _op.data.recv_status_on_client.status = &_statusCode; |
| 198 _op.data.recv_status_on_client.status_details = &_details; |
| 199 _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity; |
| 200 grpc_metadata_array_init(&_trailers); |
| 201 _op.data.recv_status_on_client.trailing_metadata = &_trailers; |
| 202 if (handler) { |
| 203 // Prevent reference cycle with _handler |
| 204 __weak typeof(self) weakSelf = self; |
| 205 _handler = ^{ |
| 206 __strong typeof(self) strongSelf = weakSelf; |
| 207 NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCo
de |
| 208 details:strongSelf->_details]
; |
| 209 NSDictionary *trailers = [NSDictionary |
| 210 grpc_dictionaryFromMetadataArray:strongSelf->_
trailers]; |
| 211 handler(error, trailers); |
| 212 }; |
| 213 } |
| 214 } |
| 215 return self; |
| 216 } |
| 217 |
| 218 - (void)dealloc { |
| 219 grpc_metadata_array_destroy(&_trailers); |
| 220 gpr_free(_details); |
| 221 } |
| 222 |
| 223 @end |
| 224 |
| 225 #pragma mark GRPCWrappedCall |
| 226 |
| 227 @implementation GRPCWrappedCall { |
| 228 GRPCCompletionQueue *_queue; |
| 229 grpc_call *_call; |
| 230 } |
| 231 |
| 232 - (instancetype)init { |
| 233 return [self initWithHost:nil path:nil]; |
| 234 } |
| 235 |
| 236 - (instancetype)initWithHost:(NSString *)host |
| 237 path:(NSString *)path { |
| 238 if (!path || !host) { |
| 239 [NSException raise:NSInvalidArgumentException |
| 240 format:@"path and host cannot be nil."]; |
| 241 } |
| 242 |
| 243 if (self = [super init]) { |
| 244 static dispatch_once_t initialization; |
| 245 dispatch_once(&initialization, ^{ |
| 246 grpc_init(); |
| 247 }); |
| 248 |
| 249 // Each completion queue consumes one thread. There's a trade to be made bet
ween creating and |
| 250 // consuming too many threads and having contention of multiple calls in a s
ingle completion |
| 251 // queue. Currently we favor latency and use one per call. |
| 252 _queue = [GRPCCompletionQueue completionQueue]; |
| 253 |
| 254 _call = [[GRPCHost hostWithAddress:host] unmanagedCallWithPath:path completi
onQueue:_queue]; |
| 255 if (_call == NULL) { |
| 256 return nil; |
| 257 } |
| 258 } |
| 259 return self; |
| 260 } |
| 261 |
| 262 - (void)startBatchWithOperations:(NSArray *)operations { |
| 263 [self startBatchWithOperations:operations errorHandler:nil]; |
| 264 } |
| 265 |
| 266 - (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())
errorHandler { |
| 267 size_t nops = operations.count; |
| 268 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op)); |
| 269 size_t i = 0; |
| 270 for (GRPCOperation *operation in operations) { |
| 271 ops_array[i++] = operation.op; |
| 272 } |
| 273 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops, |
| 274 (__bridge_retained void *)(^(boo
l success){ |
| 275 if (!success) { |
| 276 if (errorHandler) { |
| 277 errorHandler(); |
| 278 } else { |
| 279 return; |
| 280 } |
| 281 } |
| 282 for (GRPCOperation *operation in operations) { |
| 283 [operation finish]; |
| 284 } |
| 285 }), NULL); |
| 286 gpr_free(ops_array); |
| 287 |
| 288 if (error != GRPC_CALL_OK) { |
| 289 [NSException raise:NSInternalInconsistencyException |
| 290 format:@"A precondition for calling grpc_call_start_batch wasn't
met. Error %i", |
| 291 error]; |
| 292 } |
| 293 } |
| 294 |
| 295 - (void)cancel { |
| 296 grpc_call_cancel(_call, NULL); |
| 297 } |
| 298 |
| 299 - (void)dealloc { |
| 300 grpc_call_destroy(_call); |
| 301 } |
| 302 |
| 303 @end |
OLD | NEW |