Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 3 | * Copyright 2015 gRPC authors. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 19 | #import "GRPCCall.h" |
| 20 | |
Jorge Canizales | c2d7ecb | 2015-02-27 01:22:41 -0800 | [diff] [blame] | 21 | #include <grpc/grpc.h> |
Jorge Canizales | 59bb9d7 | 2015-06-22 19:04:15 -0700 | [diff] [blame] | 22 | #include <grpc/support/time.h> |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 23 | #import <RxLibrary/GRXConcurrentWriteable.h> |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 24 | #import <RxLibrary/GRXImmediateSingleWriter.h> |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 25 | |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 26 | #import "private/GRPCConnectivityMonitor.h" |
| 27 | #import "private/GRPCHost.h" |
Jorge Canizales | 2f10127 | 2015-09-02 21:55:37 -0700 | [diff] [blame] | 28 | #import "private/GRPCRequestHeaders.h" |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 29 | #import "private/GRPCWrappedCall.h" |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 30 | #import "private/NSData+GRPC.h" |
| 31 | #import "private/NSDictionary+GRPC.h" |
| 32 | #import "private/NSError+GRPC.h" |
| 33 | |
Muxi Yan | 016d108 | 2017-03-07 18:26:42 -0800 | [diff] [blame] | 34 | // At most 6 ops can be in an op batch for a client: SEND_INITIAL_METADATA, |
| 35 | // SEND_MESSAGE, SEND_CLOSE_FROM_CLIENT, RECV_INITIAL_METADATA, RECV_MESSAGE, |
| 36 | // and RECV_STATUS_ON_CLIENT. |
| 37 | NSInteger kMaxClientBatch = 6; |
| 38 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 39 | NSString * const kGRPCHeadersKey = @"io.grpc.HeadersKey"; |
| 40 | NSString * const kGRPCTrailersKey = @"io.grpc.TrailersKey"; |
Muxi Yan | 22f7973 | 2016-09-30 14:24:56 -0700 | [diff] [blame] | 41 | static NSMutableDictionary *callFlags; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 42 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 43 | @interface GRPCCall () <GRXWriteable> |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 44 | // Make them read-write. |
| 45 | @property(atomic, strong) NSDictionary *responseHeaders; |
| 46 | @property(atomic, strong) NSDictionary *responseTrailers; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 47 | @end |
| 48 | |
| 49 | // The following methods of a C gRPC call object aren't reentrant, and thus |
| 50 | // calls to them must be serialized: |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 51 | // - start_batch |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 52 | // - destroy |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 53 | // |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 54 | // start_batch with a SEND_MESSAGE argument can only be called after the |
| 55 | // 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] | 56 | // pausing the requests writer immediately every time it writes a value, and |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 57 | // resuming it again when OP_COMPLETE is received. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 58 | // |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 59 | // Similarly, start_batch with a RECV_MESSAGE argument can only be called after |
| 60 | // the OP_COMPLETE event for any previous read is received.This is easier to |
| 61 | // enforce, as we're writing the received messages into the writeable: |
| 62 | // start_batch is enqueued once upon receiving the OP_COMPLETE event for the |
| 63 | // RECV_METADATA batch, and then once after receiving each OP_COMPLETE event for |
| 64 | // each RECV_MESSAGE batch. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 65 | @implementation GRPCCall { |
| 66 | dispatch_queue_t _callQueue; |
| 67 | |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 68 | NSString *_host; |
| 69 | NSString *_path; |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 70 | GRPCWrappedCall *_wrappedCall; |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 71 | GRPCConnectivityMonitor *_connectivityMonitor; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 72 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 73 | // The C gRPC library has less guarantees on the ordering of events than we |
| 74 | // do. Particularly, in the face of errors, there's no ordering guarantee at |
| 75 | // all. This wrapper over our actual writeable ensures thread-safety and |
| 76 | // correct ordering. |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 77 | GRXConcurrentWriteable *_responseWriteable; |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 78 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 79 | // The network thread wants the requestWriter to resume (when the server is ready for more input), |
| 80 | // or to stop (on errors), concurrently with user threads that want to start it, pause it or stop |
| 81 | // it. Because a writer isn't thread-safe, we'll synchronize those operations on it. |
| 82 | // We don't use a dispatch queue for that purpose, because the writer can call writeValue: or |
| 83 | // writesFinishedWithError: on this GRPCCall as part of those operations. We want to be able to |
| 84 | // 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] | 85 | GRXWriter *_requestWriter; |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 86 | |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 87 | // To create a retain cycle when a call is started, up until it finishes. See |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 88 | // |startWithWriteable:| and |finishWithError:|. This saves users from having to retain a |
| 89 | // reference to the call object if all they're interested in is the handler being executed when |
Jorge Canizales | eb87b46 | 2015-08-08 16:16:43 -0700 | [diff] [blame] | 90 | // the response arrives. |
| 91 | GRPCCall *_retainSelf; |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 92 | |
murgatroid99 | 84fa531 | 2015-08-28 10:55:55 -0700 | [diff] [blame] | 93 | GRPCRequestHeaders *_requestHeaders; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 94 | |
Muxi Yan | c2e53b5 | 2017-03-22 14:30:16 -0700 | [diff] [blame] | 95 | // In the case that the call is a unary call (i.e. the writer to GRPCCall is of type |
| 96 | // GRXImmediateSingleWriter), GRPCCall will delay sending ops (not send them to C core |
| 97 | // immediately) and buffer them into a batch _unaryOpBatch. The batch is sent to C core when |
| 98 | // the SendClose op is added. |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 99 | BOOL _unaryCall; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 100 | NSMutableArray *_unaryOpBatch; |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 101 | |
| 102 | // The dispatch queue to be used for enqueuing responses to user. Defaulted to the main dispatch |
| 103 | // queue |
| 104 | dispatch_queue_t _responseQueue; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @synthesize state = _state; |
| 108 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 109 | // TODO(jcanizales): If grpc_init is idempotent, this should be changed from load to initialize. |
Jorge Canizales | 7603d64 | 2016-08-24 18:23:24 -0700 | [diff] [blame] | 110 | + (void)load { |
| 111 | grpc_init(); |
Muxi Yan | 22f7973 | 2016-09-30 14:24:56 -0700 | [diff] [blame] | 112 | callFlags = [NSMutableDictionary dictionary]; |
| 113 | } |
| 114 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 115 | + (void)setCallSafety:(GRPCCallSafety)callSafety host:(NSString *)host path:(NSString *)path { |
Muxi Yan | 6c0b960 | 2016-10-02 14:32:06 -0700 | [diff] [blame] | 116 | NSString *hostAndPath = [NSString stringWithFormat:@"%@/%@", host, path]; |
| 117 | switch (callSafety) { |
| 118 | case GRPCCallSafetyDefault: |
Muxi Yan | 22f7973 | 2016-09-30 14:24:56 -0700 | [diff] [blame] | 119 | callFlags[hostAndPath] = @0; |
| 120 | break; |
Muxi Yan | 6c0b960 | 2016-10-02 14:32:06 -0700 | [diff] [blame] | 121 | case GRPCCallSafetyIdempotentRequest: |
Muxi Yan | 22f7973 | 2016-09-30 14:24:56 -0700 | [diff] [blame] | 122 | callFlags[hostAndPath] = @GRPC_INITIAL_METADATA_IDEMPOTENT_REQUEST; |
| 123 | break; |
Muxi Yan | 6c0b960 | 2016-10-02 14:32:06 -0700 | [diff] [blame] | 124 | case GRPCCallSafetyCacheableRequest: |
Muxi Yan | 22f7973 | 2016-09-30 14:24:56 -0700 | [diff] [blame] | 125 | callFlags[hostAndPath] = @GRPC_INITIAL_METADATA_CACHEABLE_REQUEST; |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
Muxi Yan | 6c0b960 | 2016-10-02 14:32:06 -0700 | [diff] [blame] | 132 | + (uint32_t)callFlagsForHost:(NSString *)host path:(NSString *)path { |
| 133 | NSString *hostAndPath = [NSString stringWithFormat:@"%@/%@", host, path]; |
| 134 | return [callFlags[hostAndPath] intValue]; |
Jorge Canizales | 7603d64 | 2016-08-24 18:23:24 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 137 | - (instancetype)init { |
Muxi Yan | 22f7973 | 2016-09-30 14:24:56 -0700 | [diff] [blame] | 138 | return [self initWithHost:nil path:nil requestsWriter:nil]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Designated initializer |
| 142 | - (instancetype)initWithHost:(NSString *)host |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 143 | path:(NSString *)path |
Muxi Yan | 22f7973 | 2016-09-30 14:24:56 -0700 | [diff] [blame] | 144 | requestsWriter:(GRXWriter *)requestWriter { |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 145 | if (!host || !path) { |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 146 | [NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 147 | } |
Jorge Canizales | 6bbfcc3 | 2015-06-17 14:10:52 -0700 | [diff] [blame] | 148 | if (requestWriter.state != GRXWriterStateNotStarted) { |
Jorge Canizales | 597ef98 | 2015-07-31 23:31:56 -0700 | [diff] [blame] | 149 | [NSException raise:NSInvalidArgumentException |
| 150 | format:@"The requests writer can't be already started."]; |
Jorge Canizales | 6bbfcc3 | 2015-06-17 14:10:52 -0700 | [diff] [blame] | 151 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 152 | if ((self = [super init])) { |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 153 | _host = [host copy]; |
| 154 | _path = [path copy]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 155 | |
| 156 | // 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] | 157 | _callQueue = dispatch_queue_create("io.grpc.call", NULL); |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 158 | |
| 159 | _requestWriter = requestWriter; |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 160 | |
murgatroid99 | 84fa531 | 2015-08-28 10:55:55 -0700 | [diff] [blame] | 161 | _requestHeaders = [[GRPCRequestHeaders alloc] initWithCall:self]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 162 | |
| 163 | if ([requestWriter isKindOfClass:[GRXImmediateSingleWriter class]]) { |
Muxi Yan | bf803b9 | 2017-02-08 11:31:26 -0800 | [diff] [blame] | 164 | _unaryCall = YES; |
Muxi Yan | 016d108 | 2017-03-07 18:26:42 -0800 | [diff] [blame] | 165 | _unaryOpBatch = [NSMutableArray arrayWithCapacity:kMaxClientBatch]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 166 | } |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 167 | |
| 168 | _responseQueue = dispatch_get_main_queue(); |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 169 | } |
| 170 | return self; |
| 171 | } |
| 172 | |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 173 | - (void)setResponseDispatchQueue:(dispatch_queue_t)queue { |
| 174 | if (_state != GRXWriterStateNotStarted) { |
| 175 | return; |
| 176 | } |
| 177 | _responseQueue = queue; |
| 178 | } |
| 179 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 180 | #pragma mark Finish |
| 181 | |
| 182 | - (void)finishWithError:(NSError *)errorOrNil { |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 183 | @synchronized(self) { |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 184 | if (_state == GRXWriterStateFinished) { |
| 185 | return; |
| 186 | } |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 187 | _state = GRXWriterStateFinished; |
| 188 | } |
| 189 | |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 190 | // 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] | 191 | _retainSelf = nil; |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 192 | |
| 193 | // If there were still request messages coming, stop them. |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 194 | @synchronized(_requestWriter) { |
| 195 | _requestWriter.state = GRXWriterStateFinished; |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 196 | } |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 197 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 198 | if (errorOrNil) { |
| 199 | [_responseWriteable cancelWithError:errorOrNil]; |
| 200 | } else { |
| 201 | [_responseWriteable enqueueSuccessfulCompletion]; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | - (void)cancelCall { |
| 206 | // Can be called from any thread, any number of times. |
murgatroid99 | b56609c | 2015-04-28 16:41:11 -0700 | [diff] [blame] | 207 | [_wrappedCall cancel]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | - (void)cancel { |
| 211 | [self finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 212 | code:GRPCErrorCodeCancelled |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 213 | userInfo:@{NSLocalizedDescriptionKey: @"Canceled by app"}]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 214 | [self cancelCall]; |
| 215 | } |
| 216 | |
| 217 | - (void)dealloc { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 218 | __block GRPCWrappedCall *wrappedCall = _wrappedCall; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 219 | dispatch_async(_callQueue, ^{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 220 | wrappedCall = nil; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 221 | }); |
| 222 | } |
| 223 | |
| 224 | #pragma mark Read messages |
| 225 | |
| 226 | // Only called from the call queue. |
| 227 | // The handler will be called from the network queue. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 228 | - (void)startReadWithHandler:(void(^)(grpc_byte_buffer *))handler { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 229 | // TODO(jcanizales): Add error handlers for async failures |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 230 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMessage alloc] initWithHandler:handler]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // Called initially from the network queue once response headers are received, |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 234 | // then "recursively" from the responseWriteable queue after each response from the |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 235 | // server has been written. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 236 | // If the call is currently paused, this is a noop. Restarting the call will invoke this |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 237 | // method. |
| 238 | // TODO(jcanizales): Rename to readResponseIfNotPaused. |
| 239 | - (void)startNextRead { |
| 240 | if (self.state == GRXWriterStatePaused) { |
| 241 | return; |
| 242 | } |
| 243 | __weak GRPCCall *weakSelf = self; |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 244 | __weak GRXConcurrentWriteable *weakWriteable = _responseWriteable; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 245 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 246 | dispatch_async(_callQueue, ^{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 247 | [weakSelf startReadWithHandler:^(grpc_byte_buffer *message) { |
| 248 | if (message == NULL) { |
murgatroid99 | b56609c | 2015-04-28 16:41:11 -0700 | [diff] [blame] | 249 | // No more messages from the server |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 250 | return; |
| 251 | } |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 252 | NSData *data = [NSData grpc_dataWithByteBuffer:message]; |
| 253 | grpc_byte_buffer_destroy(message); |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 254 | if (!data) { |
| 255 | // The app doesn't have enough memory to hold the server response. We |
| 256 | // don't want to throw, because the app shouldn't crash for a behavior |
| 257 | // that's on the hands of any server to have. Instead we finish and ask |
| 258 | // the server to cancel. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 259 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 260 | code:GRPCErrorCodeResourceExhausted |
| 261 | userInfo:@{NSLocalizedDescriptionKey: @"Client does not have enough memory to hold the server response."}]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 262 | [weakSelf cancelCall]; |
| 263 | return; |
| 264 | } |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 265 | [weakWriteable enqueueValue:data completionHandler:^{ |
| 266 | [weakSelf startNextRead]; |
| 267 | }]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 268 | }]; |
| 269 | }); |
| 270 | } |
| 271 | |
| 272 | #pragma mark Send headers |
| 273 | |
Jorge Canizales | f4f150f | 2015-11-01 22:31:12 -0800 | [diff] [blame] | 274 | - (void)sendHeaders:(NSDictionary *)headers { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 275 | // TODO(jcanizales): Add error handlers for async failures |
Muxi Yan | bf803b9 | 2017-02-08 11:31:26 -0800 | [diff] [blame] | 276 | GRPCOpSendMetadata *op = [[GRPCOpSendMetadata alloc] initWithMetadata:headers |
| 277 | flags:[GRPCCall callFlagsForHost:_host path:_path] |
Muxi Yan | d5bac0d | 2017-03-07 18:44:19 -0800 | [diff] [blame] | 278 | handler:nil]; // No clean-up needed after SEND_INITIAL_METADATA |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 279 | if (!_unaryCall) { |
Muxi Yan | bf803b9 | 2017-02-08 11:31:26 -0800 | [diff] [blame] | 280 | [_wrappedCall startBatchWithOperations:@[op]]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 281 | } else { |
Muxi Yan | bf803b9 | 2017-02-08 11:31:26 -0800 | [diff] [blame] | 282 | [_unaryOpBatch addObject:op]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 283 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | #pragma mark GRXWriteable implementation |
| 287 | |
| 288 | // Only called from the call queue. The error handler will be called from the |
| 289 | // network queue if the write didn't succeed. |
Muxi Yan | 3565301 | 2017-03-07 18:54:24 -0800 | [diff] [blame] | 290 | // If the call is a unary call, parameter \a errorHandler will be ignored and |
| 291 | // the error handler of GRPCOpSendClose will be executed in case of error. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 292 | - (void)writeMessage:(NSData *)message withErrorHandler:(void (^)())errorHandler { |
| 293 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 294 | __weak GRPCCall *weakSelf = self; |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 295 | void(^resumingHandler)(void) = ^{ |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 296 | // Resume the request writer. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 297 | GRPCCall *strongSelf = weakSelf; |
| 298 | if (strongSelf) { |
Jorge Canizales | 578ab16 | 2015-08-08 17:11:43 -0700 | [diff] [blame] | 299 | @synchronized(strongSelf->_requestWriter) { |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 300 | strongSelf->_requestWriter.state = GRXWriterStateStarted; |
| 301 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 302 | } |
| 303 | }; |
Muxi Yan | bf803b9 | 2017-02-08 11:31:26 -0800 | [diff] [blame] | 304 | |
| 305 | GRPCOpSendMessage *op = [[GRPCOpSendMessage alloc] initWithMessage:message |
| 306 | handler:resumingHandler]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 307 | if (!_unaryCall) { |
Muxi Yan | bf803b9 | 2017-02-08 11:31:26 -0800 | [diff] [blame] | 308 | [_wrappedCall startBatchWithOperations:@[op] |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 309 | errorHandler:errorHandler]; |
| 310 | } else { |
Muxi Yan | 44e1837 | 2017-03-10 14:52:51 -0800 | [diff] [blame] | 311 | // Ignored errorHandler since it is the same as the one for GRPCOpSendClose. |
| 312 | // TODO (mxyan): unify the error handlers of all Ops into a single closure. |
Muxi Yan | bf803b9 | 2017-02-08 11:31:26 -0800 | [diff] [blame] | 313 | [_unaryOpBatch addObject:op]; |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 314 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 317 | - (void)writeValue:(id)value { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 318 | // TODO(jcanizales): Throw/assert if value isn't NSData. |
| 319 | |
| 320 | // Pause the input and only resume it when the C layer notifies us that writes |
| 321 | // can proceed. |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 322 | @synchronized(_requestWriter) { |
| 323 | _requestWriter.state = GRXWriterStatePaused; |
| 324 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 325 | |
| 326 | __weak GRPCCall *weakSelf = self; |
| 327 | dispatch_async(_callQueue, ^{ |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 328 | [weakSelf writeMessage:value withErrorHandler:^{ |
| 329 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 330 | code:GRPCErrorCodeInternal |
| 331 | userInfo:nil]]; |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 332 | }]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 333 | }); |
| 334 | } |
| 335 | |
| 336 | // Only called from the call queue. The error handler will be called from the |
| 337 | // network queue if the requests stream couldn't be closed successfully. |
| 338 | - (void)finishRequestWithErrorHandler:(void (^)())errorHandler { |
Muxi Yan | d5bac0d | 2017-03-07 18:44:19 -0800 | [diff] [blame] | 339 | if (!_unaryCall) { |
Muxi Yan | a40ccd8 | 2016-11-05 21:39:44 -0700 | [diff] [blame] | 340 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendClose alloc] init]] |
| 341 | errorHandler:errorHandler]; |
| 342 | } else { |
| 343 | [_unaryOpBatch addObject:[[GRPCOpSendClose alloc] init]]; |
| 344 | [_wrappedCall startBatchWithOperations:_unaryOpBatch |
| 345 | errorHandler:errorHandler]; |
| 346 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 349 | - (void)writesFinishedWithError:(NSError *)errorOrNil { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 350 | if (errorOrNil) { |
| 351 | [self cancel]; |
| 352 | } else { |
| 353 | __weak GRPCCall *weakSelf = self; |
| 354 | dispatch_async(_callQueue, ^{ |
| 355 | [weakSelf finishRequestWithErrorHandler:^{ |
| 356 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 357 | code:GRPCErrorCodeInternal |
| 358 | userInfo:nil]]; |
| 359 | }]; |
| 360 | }); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | #pragma mark Invoke |
| 365 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 366 | // Both handlers will eventually be called, from the network queue. Writes can start immediately |
| 367 | // after this. |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 368 | // The first one (headersHandler), when the response headers are received. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 369 | // The second one (completionHandler), whenever the RPC finishes for any reason. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 370 | - (void)invokeCallWithHeadersHandler:(void(^)(NSDictionary *))headersHandler |
| 371 | completionHandler:(void(^)(NSError *, NSDictionary *))completionHandler { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 372 | // TODO(jcanizales): Add error handlers for async failures |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 373 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc] |
| 374 | initWithHandler:headersHandler]]]; |
| 375 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvStatus alloc] |
| 376 | initWithHandler:completionHandler]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | - (void)invokeCall { |
Jorge Canizales | 0b34c89 | 2015-08-12 20:19:20 -0700 | [diff] [blame] | 380 | [self invokeCallWithHeadersHandler:^(NSDictionary *headers) { |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 381 | // Response headers received. |
Nicolas "Pixel" Noble | 7022436 | 2016-03-21 22:19:43 +0100 | [diff] [blame] | 382 | self.responseHeaders = headers; |
| 383 | [self startNextRead]; |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 384 | } completionHandler:^(NSError *error, NSDictionary *trailers) { |
| 385 | self.responseTrailers = trailers; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 386 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 387 | if (error) { |
| 388 | NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; |
| 389 | if (error.userInfo) { |
| 390 | [userInfo addEntriesFromDictionary:error.userInfo]; |
| 391 | } |
| 392 | userInfo[kGRPCTrailersKey] = self.responseTrailers; |
| 393 | // TODO(jcanizales): The C gRPC library doesn't guarantee that the headers block will be |
| 394 | // called before this one, so an error might end up with trailers but no headers. We |
| 395 | // shouldn't call finishWithError until ater both blocks are called. It is also when this is |
| 396 | // done that we can provide a merged view of response headers and trailers in a thread-safe |
| 397 | // way. |
| 398 | if (self.responseHeaders) { |
| 399 | userInfo[kGRPCHeadersKey] = self.responseHeaders; |
| 400 | } |
| 401 | error = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo]; |
| 402 | } |
| 403 | [self finishWithError:error]; |
| 404 | }]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 405 | // Now that the RPC has been initiated, request writes can start. |
Jorge Canizales | 238ad78 | 2015-08-07 23:11:29 -0700 | [diff] [blame] | 406 | @synchronized(_requestWriter) { |
| 407 | [_requestWriter startWithWriteable:self]; |
| 408 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | #pragma mark GRXWriter implementation |
| 412 | |
| 413 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 414 | @synchronized(self) { |
| 415 | _state = GRXWriterStateStarted; |
| 416 | } |
| 417 | |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 418 | // Create a retain cycle so that this instance lives until the RPC finishes (or is cancelled). |
| 419 | // This makes RPCs in which the call isn't externally retained possible (as long as it is started |
| 420 | // before being autoreleased). |
| 421 | // Care is taken not to retain self strongly in any of the blocks used in this implementation, so |
| 422 | // that the life of the instance is determined by this retain cycle. |
Muxi Yan | e1443b1 | 2016-10-20 11:53:13 -0700 | [diff] [blame] | 423 | _retainSelf = self; |
| 424 | |
Muxi Yan | 895f3d8 | 2017-04-05 13:12:30 -0700 | [diff] [blame] | 425 | _responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable |
| 426 | dispatchQueue:_responseQueue]; |
Muxi Yan | e1443b1 | 2016-10-20 11:53:13 -0700 | [diff] [blame] | 427 | |
| 428 | _wrappedCall = [[GRPCWrappedCall alloc] initWithHost:_host path:_path]; |
| 429 | NSAssert(_wrappedCall, @"Error allocating RPC objects. Low memory?"); |
| 430 | |
| 431 | [self sendHeaders:_requestHeaders]; |
| 432 | [self invokeCall]; |
Muxi Yan | 4f6a19b | 2016-10-25 14:40:06 -0700 | [diff] [blame] | 433 | |
| 434 | // TODO(jcanizales): Extract this logic somewhere common. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 435 | NSString *host = [NSURL URLWithString:[@"https://" stringByAppendingString:_host]].host; |
Muxi Yan | 4f6a19b | 2016-10-25 14:40:06 -0700 | [diff] [blame] | 436 | if (!host) { |
| 437 | // TODO(jcanizales): Check this on init. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 438 | [NSException raise:NSInvalidArgumentException format:@"host of %@ is nil", _host]; |
Muxi Yan | 4f6a19b | 2016-10-25 14:40:06 -0700 | [diff] [blame] | 439 | } |
| 440 | __weak typeof(self) weakSelf = self; |
| 441 | _connectivityMonitor = [GRPCConnectivityMonitor monitorWithHost:host]; |
| 442 | void (^handler)() = ^{ |
| 443 | typeof(self) strongSelf = weakSelf; |
| 444 | if (strongSelf) { |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 445 | [strongSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 446 | code:GRPCErrorCodeUnavailable |
| 447 | userInfo:@{ NSLocalizedDescriptionKey : @"Connectivity lost." }]]; |
Muxi Yan | 4f6a19b | 2016-10-25 14:40:06 -0700 | [diff] [blame] | 448 | } |
| 449 | }; |
| 450 | [_connectivityMonitor handleLossWithHandler:handler |
Muxi Yan | 2a25f33 | 2016-10-28 13:27:04 -0700 | [diff] [blame] | 451 | wifiStatusChangeHandler:nil]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | - (void)setState:(GRXWriterState)newState { |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 455 | @synchronized(self) { |
| 456 | // Manual transitions are only allowed from the started or paused states. |
Muxi Yan | 61274ca | 2016-10-28 12:09:59 -0700 | [diff] [blame] | 457 | if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) { |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 458 | return; |
| 459 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 460 | |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 461 | switch (newState) { |
| 462 | case GRXWriterStateFinished: |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 463 | _state = newState; |
Jorge Canizales | 0803bb0 | 2016-04-30 10:40:18 -0700 | [diff] [blame] | 464 | // Per GRXWriter's contract, setting the state to Finished manually |
| 465 | // means one doesn't wish the writeable to be messaged anymore. |
| 466 | [_responseWriteable cancelSilently]; |
| 467 | _responseWriteable = nil; |
| 468 | return; |
| 469 | case GRXWriterStatePaused: |
| 470 | _state = newState; |
| 471 | return; |
| 472 | case GRXWriterStateStarted: |
| 473 | if (_state == GRXWriterStatePaused) { |
| 474 | _state = newState; |
| 475 | [self startNextRead]; |
| 476 | } |
| 477 | return; |
| 478 | case GRXWriterStateNotStarted: |
| 479 | return; |
| 480 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 481 | } |
| 482 | } |
Jorge Canizales | f1d084a | 2015-10-30 11:14:09 -0700 | [diff] [blame] | 483 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 484 | @end |