|
|
|
@ -415,40 +415,26 @@ static NSString * const kBearerPrefix = @"Bearer "; |
|
|
|
|
|
|
|
|
|
#pragma mark GRXWriter implementation |
|
|
|
|
|
|
|
|
|
- (void)startWithWriteable:(id<GRXWriteable>)writeable { |
|
|
|
|
@synchronized(self) { |
|
|
|
|
_state = GRXWriterStateStarted; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a retain cycle so that this instance lives until the RPC finishes (or is cancelled). |
|
|
|
|
// This makes RPCs in which the call isn't externally retained possible (as long as it is started |
|
|
|
|
// before being autoreleased). |
|
|
|
|
// Care is taken not to retain self strongly in any of the blocks used in this implementation, so |
|
|
|
|
// that the life of the instance is determined by this retain cycle. |
|
|
|
|
_retainSelf = self; |
|
|
|
|
|
|
|
|
|
__weak typeof(self) weakSelf = self; |
|
|
|
|
void (^performCall)() = ^{ |
|
|
|
|
typeof(self) strongSelf = weakSelf; |
|
|
|
|
if (strongSelf) { |
|
|
|
|
strongSelf->_responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable |
|
|
|
|
dispatchQueue:strongSelf->_responseQueue]; |
|
|
|
|
- (void)startCallWithWriteable:(id<GRXWriteable>)writeable { |
|
|
|
|
_responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable |
|
|
|
|
dispatchQueue:_responseQueue]; |
|
|
|
|
|
|
|
|
|
strongSelf->_wrappedCall = [[GRPCWrappedCall alloc] initWithHost:strongSelf->_host |
|
|
|
|
serverName:strongSelf->_serverName |
|
|
|
|
path:strongSelf->_path]; |
|
|
|
|
_wrappedCall = [[GRPCWrappedCall alloc] initWithHost:_host |
|
|
|
|
serverName:_serverName |
|
|
|
|
path:_path]; |
|
|
|
|
NSAssert(_wrappedCall, @"Error allocating RPC objects. Low memory?"); |
|
|
|
|
|
|
|
|
|
[strongSelf sendHeaders:_requestHeaders]; |
|
|
|
|
[strongSelf invokeCall]; |
|
|
|
|
[self sendHeaders:_requestHeaders]; |
|
|
|
|
[self invokeCall]; |
|
|
|
|
|
|
|
|
|
// TODO(jcanizales): Extract this logic somewhere common. |
|
|
|
|
NSString *host = [NSURL URLWithString:[@"https://" stringByAppendingString:strongSelf->_host]].host; |
|
|
|
|
NSString *host = [NSURL URLWithString:[@"https://" stringByAppendingString:_host]].host; |
|
|
|
|
if (!host) { |
|
|
|
|
// TODO(jcanizales): Check this on init. |
|
|
|
|
[NSException raise:NSInvalidArgumentException format:@"host of %@ is nil", strongSelf->_host]; |
|
|
|
|
[NSException raise:NSInvalidArgumentException format:@"host of %@ is nil", _host]; |
|
|
|
|
} |
|
|
|
|
strongSelf->_connectivityMonitor = [GRPCConnectivityMonitor monitorWithHost:host]; |
|
|
|
|
_connectivityMonitor = [GRPCConnectivityMonitor monitorWithHost:host]; |
|
|
|
|
__weak typeof(self) weakSelf = self; |
|
|
|
|
void (^handler)() = ^{ |
|
|
|
|
typeof(self) strongSelf = weakSelf; |
|
|
|
|
[strongSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
|
|
|
@ -458,10 +444,22 @@ static NSString * const kBearerPrefix = @"Bearer "; |
|
|
|
|
[_connectivityMonitor handleLossWithHandler:handler |
|
|
|
|
wifiStatusChangeHandler:nil]; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
- (void)startWithWriteable:(id<GRXWriteable>)writeable { |
|
|
|
|
@synchronized(self) { |
|
|
|
|
_state = GRXWriterStateStarted; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Create a retain cycle so that this instance lives until the RPC finishes (or is cancelled). |
|
|
|
|
// This makes RPCs in which the call isn't externally retained possible (as long as it is started |
|
|
|
|
// before being autoreleased). |
|
|
|
|
// Care is taken not to retain self strongly in any of the blocks used in this implementation, so |
|
|
|
|
// that the life of the instance is determined by this retain cycle. |
|
|
|
|
_retainSelf = self; |
|
|
|
|
|
|
|
|
|
if (self.oauthToken != nil) { |
|
|
|
|
self.isWaitingForToken = YES; |
|
|
|
|
__weak typeof(self) weakSelf = self; |
|
|
|
|
[self.oauthToken getTokenWithHandler:^(NSString *token){ |
|
|
|
|
typeof(self) strongSelf = weakSelf; |
|
|
|
|
if (strongSelf && strongSelf.isWaitingForToken) { |
|
|
|
@ -469,12 +467,12 @@ static NSString * const kBearerPrefix = @"Bearer "; |
|
|
|
|
NSString *t = [kBearerPrefix stringByAppendingString:token]; |
|
|
|
|
strongSelf.requestHeaders[kAuthorizationHeader] = t; |
|
|
|
|
} |
|
|
|
|
performCall(); |
|
|
|
|
[strongSelf startCallWithWriteable:writeable]; |
|
|
|
|
strongSelf.isWaitingForToken = NO; |
|
|
|
|
} |
|
|
|
|
}]; |
|
|
|
|
} else { |
|
|
|
|
performCall(); |
|
|
|
|
[self startCallWithWriteable:writeable]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|