Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [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 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 34 | #import "GRPCCall.h" |
| 35 | |
Jorge Canizales | c2d7ecb | 2015-02-27 01:22:41 -0800 | [diff] [blame] | 36 | #include <grpc/grpc.h> |
Jorge Canizales | 59bb9d7 | 2015-06-22 19:04:15 -0700 | [diff] [blame] | 37 | #include <grpc/support/time.h> |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 38 | #import <RxLibrary/GRXConcurrentWriteable.h> |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 39 | |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 40 | #import "private/GRPCConnectivityMonitor.h" |
| 41 | #import "private/GRPCHost.h" |
Jorge Canizales | 2f10127 | 2015-09-02 21:55:37 -0700 | [diff] [blame] | 42 | #import "private/GRPCRequestHeaders.h" |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 43 | #import "private/GRPCWrappedCall.h" |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 44 | #import "private/NSData+GRPC.h" |
| 45 | #import "private/NSDictionary+GRPC.h" |
| 46 | #import "private/NSError+GRPC.h" |
| 47 | |
Jorge Canizales | 1ab2a71 | 2015-08-12 20:32:11 -0700 | [diff] [blame] | 48 | NSString * const kGRPCHeadersKey = @"io.grpc.HeadersKey"; |
| 49 | NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey"; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 50 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 51 | @interface GRPCCall () <GRXWriteable> |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 52 | // Make them read-write. |
| 53 | @property(atomic, strong) NSDictionary *responseHeaders; |
| 54 | @property(atomic, strong) NSDictionary *responseTrailers; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 55 | @end |
| 56 | |
| 57 | // The following methods of a C gRPC call object aren't reentrant, and thus |
| 58 | // calls to them must be serialized: |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 59 | // - start_batch |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 60 | // - destroy |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 61 | // |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 62 | // start_batch with a SEND_MESSAGE argument can only be called after the |
| 63 | // OP_COMPLETE event for any previous write is received. This is achieved by |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 64 | // pausing the requests writer immediately every time it writes a value, and |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 65 | // resuming it again when OP_COMPLETE is received. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 66 | // |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 67 | // Similarly, start_batch with a RECV_MESSAGE argument can only be called after |
| 68 | // the OP_COMPLETE event for any previous read is received.This is easier to |
| 69 | // enforce, as we're writing the received messages into the writeable: |
| 70 | // start_batch is enqueued once upon receiving the OP_COMPLETE event for the |
| 71 | // RECV_METADATA batch, and then once after receiving each OP_COMPLETE event for |
| 72 | // each RECV_MESSAGE batch. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 73 | @implementation GRPCCall { |
| 74 | dispatch_queue_t _callQueue; |
| 75 | |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 76 | NSString *_host; |
| 77 | NSString *_path; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 78 | GRPCWrappedCall *_wrappedCall; |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 79 | GRPCConnectivityMonitor *_connectivityMonitor; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 80 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 81 | // The C gRPC library has less guarantees on the ordering of events than we |
| 82 | // do. Particularly, in the face of errors, there's no ordering guarantee at |
| 83 | // all. This wrapper over our actual writeable ensures thread-safety and |
| 84 | // correct ordering. |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 85 | GRXConcurrentWriteable *_responseWriteable; |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 86 | |
| 87 | // The network thread wants the requestWriter to resume (when the server is ready for more input), |
| 88 | // or to stop (on errors), concurrently with user threads that want to start it, pause it or stop |
| 89 | // it. Because a writer isn't thread-safe, we'll synchronize those operations on it. |
| 90 | // We don't use a dispatch queue for that purpose, because the writer can call writeValue: or |
| 91 | // writesFinishedWithError: on this GRPCCall as part of those operations. We want to be able to |
| 92 | // pause the writer immediately on writeValue:, so we need our locking to be recursive. |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 93 | GRXWriter *_requestWriter; |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 94 | |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 95 | // To create a retain cycle when a call is started, up until it finishes. See |
Jorge Canizales | eb87b46 | 2015-08-08 16:16:43 -0700 | [diff] [blame] | 96 | // |startWithWriteable:| and |finishWithError:|. This saves users from having to retain a |
| 97 | // reference to the call object if all they're interested in is the handler being executed when |
| 98 | // the response arrives. |
| 99 | GRPCCall *_retainSelf; |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 100 | |
murgatroid99 | 84fa531 | 2015-08-28 10:55:55 -0700 | [diff] [blame] | 101 | GRPCRequestHeaders *_requestHeaders; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @synthesize state = _state; |
| 105 | |
Jorge Canizales | 7603d64 | 2016-08-24 18:23:24 -0700 | [diff] [blame^] | 106 | // TODO(jcanizales): If grpc_init is idempotent, this should be changed from load to initialize. |
| 107 | + (void)load { |
| 108 | grpc_init(); |
| 109 | } |
| 110 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 111 | - (instancetype)init { |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 112 | return [self initWithHost:nil path:nil requestsWriter:nil]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Designated initializer |
| 116 | - (instancetype)initWithHost:(NSString *)host |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 117 | path:(NSString *)path |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 118 | requestsWriter:(GRXWriter *)requestWriter { |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 119 | if (!host || !path) { |
Jorge Canizales | 597ef98 | 2015-07-31 23:31:56 -0700 | [diff] [blame] | 120 | [NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 121 | } |
Jorge Canizales | 6bbfcc3 | 2015-06-17 14:10:52 -0700 | [diff] [blame] | 122 | if (requestWriter.state != GRXWriterStateNotStarted) { |
Jorge Canizales | 597ef98 | 2015-07-31 23:31:56 -0700 | [diff] [blame] | 123 | [NSException raise:NSInvalidArgumentException |
| 124 | format:@"The requests writer can't be already started."]; |
Jorge Canizales | 6bbfcc3 | 2015-06-17 14:10:52 -0700 | [diff] [blame] | 125 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 126 | if ((self = [super init])) { |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 127 | _host = [host copy]; |
| 128 | _path = [path copy]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 129 | |
| 130 | // Serial queue to invoke the non-reentrant methods of the grpc_call object. |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 131 | _callQueue = dispatch_queue_create("io.grpc.call", NULL); |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 132 | |
| 133 | _requestWriter = requestWriter; |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 134 | |
murgatroid99 | 84fa531 | 2015-08-28 10:55:55 -0700 | [diff] [blame] | 135 | _requestHeaders = [[GRPCRequestHeaders alloc] initWithCall:self]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 136 | } |
| 137 | return self; |
| 138 | } |
| 139 | |
| 140 | #pragma mark Finish |
| 141 | |
| 142 | - (void)finishWithError:(NSError *)errorOrNil { |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 143 | @synchronized(self) { |
| 144 | _state = GRXWriterStateFinished; |
| 145 | } |
| 146 | |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 147 | // If the call isn't retained anywhere else, it can be deallocated now. |
Jorge Canizales | eb87b46 | 2015-08-08 16:16:43 -0700 | [diff] [blame] | 148 | _retainSelf = nil; |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 149 | |
| 150 | // If there were still request messages coming, stop them. |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 151 | @synchronized(_requestWriter) { |
| 152 | _requestWriter.state = GRXWriterStateFinished; |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 153 | } |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 154 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 155 | if (errorOrNil) { |
| 156 | [_responseWriteable cancelWithError:errorOrNil]; |
| 157 | } else { |
| 158 | [_responseWriteable enqueueSuccessfulCompletion]; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | - (void)cancelCall { |
| 163 | // Can be called from any thread, any number of times. |
murgatroid99 | b56609c | 2015-04-28 16:41:11 -0700 | [diff] [blame] | 164 | [_wrappedCall cancel]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | - (void)cancel { |
| 168 | [self finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 169 | code:GRPCErrorCodeCancelled |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 170 | userInfo:@{NSLocalizedDescriptionKey: @"Canceled by app"}]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 171 | [self cancelCall]; |
| 172 | } |
| 173 | |
| 174 | - (void)dealloc { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 175 | __block GRPCWrappedCall *wrappedCall = _wrappedCall; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 176 | dispatch_async(_callQueue, ^{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 177 | wrappedCall = nil; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 178 | }); |
| 179 | } |
| 180 | |
| 181 | #pragma mark Read messages |
| 182 | |
| 183 | // Only called from the call queue. |
| 184 | // The handler will be called from the network queue. |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 185 | - (void)startReadWithHandler:(void(^)(grpc_byte_buffer *))handler { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 186 | // TODO(jcanizales): Add error handlers for async failures |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 187 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMessage alloc] initWithHandler:handler]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Called initially from the network queue once response headers are received, |
| 191 | // then "recursively" from the responseWriteable queue after each response from the |
| 192 | // server has been written. |
| 193 | // If the call is currently paused, this is a noop. Restarting the call will invoke this |
| 194 | // method. |
| 195 | // TODO(jcanizales): Rename to readResponseIfNotPaused. |
| 196 | - (void)startNextRead { |
| 197 | if (self.state == GRXWriterStatePaused) { |
| 198 | return; |
| 199 | } |
| 200 | __weak GRPCCall *weakSelf = self; |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 201 | __weak GRXConcurrentWriteable *weakWriteable = _responseWriteable; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 202 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 203 | dispatch_async(_callQueue, ^{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 204 | [weakSelf startReadWithHandler:^(grpc_byte_buffer *message) { |
| 205 | if (message == NULL) { |
murgatroid99 | b56609c | 2015-04-28 16:41:11 -0700 | [diff] [blame] | 206 | // No more messages from the server |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 207 | return; |
| 208 | } |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 209 | NSData *data = [NSData grpc_dataWithByteBuffer:message]; |
| 210 | grpc_byte_buffer_destroy(message); |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 211 | if (!data) { |
| 212 | // The app doesn't have enough memory to hold the server response. We |
| 213 | // don't want to throw, because the app shouldn't crash for a behavior |
| 214 | // that's on the hands of any server to have. Instead we finish and ask |
| 215 | // the server to cancel. |
| 216 | // |
| 217 | // TODO(jcanizales): No canonical code is appropriate for this situation |
| 218 | // (because it's just a client problem). Use another domain and an |
| 219 | // appropriately-documented code. |
| 220 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 221 | code:GRPCErrorCodeInternal |
| 222 | userInfo:nil]]; |
| 223 | [weakSelf cancelCall]; |
| 224 | return; |
| 225 | } |
Jorge Canizales | 4c6f778 | 2015-07-17 23:13:36 -0700 | [diff] [blame] | 226 | [weakWriteable enqueueValue:data completionHandler:^{ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 227 | [weakSelf startNextRead]; |
| 228 | }]; |
| 229 | }]; |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | #pragma mark Send headers |
| 234 | |
Jorge Canizales | f4f150f | 2015-11-01 22:31:12 -0800 | [diff] [blame] | 235 | - (void)sendHeaders:(NSDictionary *)headers { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 236 | // TODO(jcanizales): Add error handlers for async failures |
Jorge Canizales | 5c339d1 | 2015-09-02 17:41:43 -0700 | [diff] [blame] | 237 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc] initWithMetadata:headers |
| 238 | handler:nil]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | #pragma mark GRXWriteable implementation |
| 242 | |
| 243 | // Only called from the call queue. The error handler will be called from the |
| 244 | // network queue if the write didn't succeed. |
| 245 | - (void)writeMessage:(NSData *)message withErrorHandler:(void (^)())errorHandler { |
| 246 | |
| 247 | __weak GRPCCall *weakSelf = self; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 248 | void(^resumingHandler)(void) = ^{ |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 249 | // Resume the request writer. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 250 | GRPCCall *strongSelf = weakSelf; |
| 251 | if (strongSelf) { |
Jorge Canizales | 578ab16 | 2015-08-08 17:11:43 -0700 | [diff] [blame] | 252 | @synchronized(strongSelf->_requestWriter) { |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 253 | strongSelf->_requestWriter.state = GRXWriterStateStarted; |
| 254 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 255 | } |
| 256 | }; |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 257 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMessage alloc] initWithMessage:message |
| 258 | handler:resumingHandler]] |
| 259 | errorHandler:errorHandler]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 262 | - (void)writeValue:(id)value { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 263 | // TODO(jcanizales): Throw/assert if value isn't NSData. |
| 264 | |
| 265 | // Pause the input and only resume it when the C layer notifies us that writes |
| 266 | // can proceed. |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 267 | @synchronized(_requestWriter) { |
| 268 | _requestWriter.state = GRXWriterStatePaused; |
| 269 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 270 | |
| 271 | __weak GRPCCall *weakSelf = self; |
| 272 | dispatch_async(_callQueue, ^{ |
| 273 | [weakSelf writeMessage:value withErrorHandler:^{ |
| 274 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 275 | code:GRPCErrorCodeInternal |
| 276 | userInfo:nil]]; |
| 277 | }]; |
| 278 | }); |
| 279 | } |
| 280 | |
| 281 | // Only called from the call queue. The error handler will be called from the |
| 282 | // network queue if the requests stream couldn't be closed successfully. |
| 283 | - (void)finishRequestWithErrorHandler:(void (^)())errorHandler { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 284 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendClose alloc] init]] |
| 285 | errorHandler:errorHandler]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 288 | - (void)writesFinishedWithError:(NSError *)errorOrNil { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 289 | if (errorOrNil) { |
| 290 | [self cancel]; |
| 291 | } else { |
| 292 | __weak GRPCCall *weakSelf = self; |
| 293 | dispatch_async(_callQueue, ^{ |
| 294 | [weakSelf finishRequestWithErrorHandler:^{ |
| 295 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 296 | code:GRPCErrorCodeInternal |
| 297 | userInfo:nil]]; |
| 298 | }]; |
| 299 | }); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | #pragma mark Invoke |
| 304 | |
| 305 | // Both handlers will eventually be called, from the network queue. Writes can start immediately |
| 306 | // after this. |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 307 | // The first one (headersHandler), when the response headers are received. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 308 | // The second one (completionHandler), whenever the RPC finishes for any reason. |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 309 | - (void)invokeCallWithHeadersHandler:(void(^)(NSDictionary *))headersHandler |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 310 | completionHandler:(void(^)(NSError *, NSDictionary *))completionHandler { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 311 | // TODO(jcanizales): Add error handlers for async failures |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 312 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc] |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 313 | initWithHandler:headersHandler]]]; |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 314 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvStatus alloc] |
| 315 | initWithHandler:completionHandler]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | - (void)invokeCall { |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 319 | [self invokeCallWithHeadersHandler:^(NSDictionary *headers) { |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 320 | // Response headers received. |
Nicolas "Pixel" Noble | 7022436 | 2016-03-21 22:19:43 +0100 | [diff] [blame] | 321 | self.responseHeaders = headers; |
| 322 | [self startNextRead]; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 323 | } completionHandler:^(NSError *error, NSDictionary *trailers) { |
Nicolas "Pixel" Noble | 7022436 | 2016-03-21 22:19:43 +0100 | [diff] [blame] | 324 | self.responseTrailers = trailers; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 325 | |
Nicolas "Pixel" Noble | 7022436 | 2016-03-21 22:19:43 +0100 | [diff] [blame] | 326 | if (error) { |
| 327 | NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; |
| 328 | if (error.userInfo) { |
| 329 | [userInfo addEntriesFromDictionary:error.userInfo]; |
Jorge Canizales | c58a110 | 2015-06-15 19:03:41 -0700 | [diff] [blame] | 330 | } |
Nicolas "Pixel" Noble | 7022436 | 2016-03-21 22:19:43 +0100 | [diff] [blame] | 331 | userInfo[kGRPCTrailersKey] = self.responseTrailers; |
| 332 | // TODO(jcanizales): The C gRPC library doesn't guarantee that the headers block will be |
| 333 | // called before this one, so an error might end up with trailers but no headers. We |
| 334 | // shouldn't call finishWithError until ater both blocks are called. It is also when this is |
| 335 | // done that we can provide a merged view of response headers and trailers in a thread-safe |
| 336 | // way. |
| 337 | if (self.responseHeaders) { |
| 338 | userInfo[kGRPCHeadersKey] = self.responseHeaders; |
| 339 | } |
| 340 | error = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo]; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 341 | } |
Nicolas "Pixel" Noble | 7022436 | 2016-03-21 22:19:43 +0100 | [diff] [blame] | 342 | [self finishWithError:error]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 343 | }]; |
| 344 | // Now that the RPC has been initiated, request writes can start. |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 345 | @synchronized(_requestWriter) { |
| 346 | [_requestWriter startWithWriteable:self]; |
| 347 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | #pragma mark GRXWriter implementation |
| 351 | |
| 352 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 353 | @synchronized(self) { |
| 354 | _state = GRXWriterStateStarted; |
| 355 | } |
| 356 | |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 357 | // Create a retain cycle so that this instance lives until the RPC finishes (or is cancelled). |
| 358 | // This makes RPCs in which the call isn't externally retained possible (as long as it is started |
| 359 | // before being autoreleased). |
| 360 | // Care is taken not to retain self strongly in any of the blocks used in this implementation, so |
| 361 | // that the life of the instance is determined by this retain cycle. |
Jorge Canizales | eb87b46 | 2015-08-08 16:16:43 -0700 | [diff] [blame] | 362 | _retainSelf = self; |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 363 | |
| 364 | _responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable]; |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 365 | |
| 366 | _wrappedCall = [[GRPCWrappedCall alloc] initWithHost:_host path:_path]; |
| 367 | NSAssert(_wrappedCall, @"Error allocating RPC objects. Low memory?"); |
| 368 | |
Jorge Canizales | 6512d26 | 2015-08-12 17:33:23 -0700 | [diff] [blame] | 369 | [self sendHeaders:_requestHeaders]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 370 | [self invokeCall]; |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 371 | // TODO(jcanizales): Extract this logic somewhere common. |
| 372 | NSString *host = [NSURL URLWithString:[@"https://" stringByAppendingString:_host]].host; |
| 373 | if (!host) { |
| 374 | // TODO(jcanizales): Check this on init. |
| 375 | [NSException raise:NSInvalidArgumentException format:@"host of %@ is nil", _host]; |
| 376 | } |
| 377 | __weak typeof(self) weakSelf = self; |
| 378 | _connectivityMonitor = [GRPCConnectivityMonitor monitorWithHost:host]; |
| 379 | [_connectivityMonitor handleLossWithHandler:^{ |
| 380 | typeof(self) strongSelf = weakSelf; |
| 381 | if (strongSelf) { |
| 382 | [strongSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 383 | code:GRPCErrorCodeUnavailable |
| 384 | userInfo:@{NSLocalizedDescriptionKey: @"Connectivity lost."}]]; |
Jorge Canizales | 3785d53 | 2016-06-28 10:48:20 -0700 | [diff] [blame] | 385 | [[GRPCHost hostWithAddress:strongSelf->_host] disconnect]; |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 386 | } |
| 387 | }]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | - (void)setState:(GRXWriterState)newState { |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 391 | @synchronized(self) { |
| 392 | // Manual transitions are only allowed from the started or paused states. |
| 393 | if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) { |
| 394 | return; |
| 395 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 396 | |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 397 | switch (newState) { |
| 398 | case GRXWriterStateFinished: |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 399 | _state = newState; |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 400 | // Per GRXWriter's contract, setting the state to Finished manually |
| 401 | // means one doesn't wish the writeable to be messaged anymore. |
| 402 | [_responseWriteable cancelSilently]; |
| 403 | _responseWriteable = nil; |
| 404 | return; |
| 405 | case GRXWriterStatePaused: |
| 406 | _state = newState; |
| 407 | return; |
| 408 | case GRXWriterStateStarted: |
| 409 | if (_state == GRXWriterStatePaused) { |
| 410 | _state = newState; |
| 411 | [self startNextRead]; |
| 412 | } |
| 413 | return; |
| 414 | case GRXWriterStateNotStarted: |
| 415 | return; |
| 416 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 419 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 420 | @end |