clang-format

pull/16190/head
Muxi Yan 6 years ago
parent 33022c9172
commit 2895ee9b6a
  1. 22
      src/objective-c/GRPCClient/GRPCCall.m
  2. 11
      src/objective-c/GRPCClient/private/GRPCChannel.h
  3. 55
      src/objective-c/GRPCClient/private/GRPCChannel.m
  4. 3
      src/objective-c/GRPCClient/private/GRPCChannelPool.h
  5. 16
      src/objective-c/GRPCClient/private/GRPCChannelPool.m
  6. 5
      src/objective-c/GRPCClient/private/GRPCHost.m
  7. 3
      src/objective-c/GRPCClient/private/GRPCSecureChannelFactory.m
  8. 27
      src/objective-c/GRPCClient/private/utilities.h

@ -68,7 +68,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
@implementation GRPCRequestOptions
- (instancetype)initWithHost:(NSString *)host path:(NSString *)path safety:(GRPCCallSafety)safety {
GRPCAssert(host.length != 0 && path.length != 0, NSInvalidArgumentException, @"Host and Path cannot be empty");
GRPCAssert(host.length != 0 && path.length != 0, NSInvalidArgumentException,
@"Host and Path cannot be empty");
if ((self = [super init])) {
_host = [host copy];
_path = [path copy];
@ -115,8 +116,10 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (instancetype)initWithRequestOptions:(GRPCRequestOptions *)requestOptions
responseHandler:(id<GRPCResponseHandler>)responseHandler
callOptions:(GRPCCallOptions *_Nullable)callOptions {
GRPCAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0, NSInvalidArgumentException, @"Neither host nor path can be nil.");
GRPCAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, @"Invalid call safety value.");
GRPCAssert(requestOptions.host.length != 0 && requestOptions.path.length != 0,
NSInvalidArgumentException, @"Neither host nor path can be nil.");
GRPCAssert(requestOptions.safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException,
@"Invalid call safety value.");
GRPCAssert(responseHandler != nil, NSInvalidArgumentException, @"Response handler required.");
if ((self = [super init])) {
@ -240,7 +243,8 @@ const char *kCFStreamVarName = "grpc_cfstream";
- (void)writeData:(NSData *)data {
dispatch_async(_dispatchQueue, ^{
GRPCAssert(!self->_canceled, NSInternalInconsistencyException, @"Call arleady canceled.");
GRPCAssert(!self->_finished, NSInternalInconsistencyException, @"Call is half-closed before sending data.");
GRPCAssert(!self->_finished, NSInternalInconsistencyException,
@"Call is half-closed before sending data.");
if (self->_call) {
[self->_pipe writeValue:data];
}
@ -400,8 +404,10 @@ const char *kCFStreamVarName = "grpc_cfstream";
callOptions:(GRPCCallOptions *)callOptions {
// Purposely using pointer rather than length ([host length] == 0) for backwards compatibility.
GRPCAssert(host && path, NSInvalidArgumentException, @"Neither host nor path can be nil.");
GRPCAssert(safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException, @"Invalid call safety value.");
GRPCAssert(requestWriter.state == GRXWriterStateNotStarted, NSInvalidArgumentException, @"The requests writer can't be already started.");
GRPCAssert(safety <= GRPCCallSafetyCacheableRequest, NSInvalidArgumentException,
@"Invalid call safety value.");
GRPCAssert(requestWriter.state == GRXWriterStateNotStarted, NSInvalidArgumentException,
@"The requests writer can't be already started.");
if ((self = [super init])) {
_host = [host copy];
_path = [path copy];
@ -786,7 +792,9 @@ const char *kCFStreamVarName = "grpc_cfstream";
_callOptions = callOptions;
}
GRPCAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil, NSInvalidArgumentException, @"authTokenProvider and oauth2AccessToken cannot be set at the same time");
GRPCAssert(_callOptions.authTokenProvider == nil || _callOptions.oauth2AccessToken == nil,
NSInvalidArgumentException,
@"authTokenProvider and oauth2AccessToken cannot be set at the same time");
if (_callOptions.authTokenProvider != nil) {
@synchronized(self) {
self.isWaitingForToken = YES;

@ -64,14 +64,17 @@ NS_ASSUME_NONNULL_BEGIN
* Create a channel with remote \a host and signature \a channelConfigurations. Destroy delay is
* defaulted to 30 seconds.
*/
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration;
- (nullable instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration;
/**
* Create a channel with remote \a host, signature \a channelConfigurations, and destroy delay of
* \a destroyDelay.
*/
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration
destroyDelay:(NSTimeInterval)destroyDelay NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration
destroyDelay:(NSTimeInterval)destroyDelay
NS_DESIGNATED_INITIALIZER;
/**
* Create a grpc core call object from this channel. The channel's refcount is added by 1. If no
@ -82,7 +85,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable grpc_call *)unmanagedCallWithPath:(NSString *)path
completionQueue:(GRPCCompletionQueue *)queue
callOptions:(GRPCCallOptions *)callOptions
disconnected:(BOOL * _Nullable)disconnected;
disconnected:(BOOL *_Nullable)disconnected;
/**
* Unref the channel when a call is done. It also decreases the channel's refcount. If the refcount

@ -20,6 +20,7 @@
#include <grpc/support/log.h>
#import "../internal/GRPCCallOptions+Internal.h"
#import "ChannelArgsUtil.h"
#import "GRPCChannelFactory.h"
#import "GRPCChannelPool.h"
@ -27,9 +28,8 @@
#import "GRPCCronetChannelFactory.h"
#import "GRPCInsecureChannelFactory.h"
#import "GRPCSecureChannelFactory.h"
#import "version.h"
#import "utilities.h"
#import "../internal/GRPCCallOptions+Internal.h"
#import "version.h"
#import <GRPCClient/GRPCCall+Cronet.h>
#import <GRPCClient/GRPCCallOptions.h>
@ -60,10 +60,10 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
if (![GRPCCall isUsingCronet]) {
#endif
factory = [GRPCSecureChannelFactory
factoryWithPEMRootCertificates:_callOptions.PEMRootCertificates
privateKey:_callOptions.PEMPrivateKey
certChain:_callOptions.PEMCertChain
error:&error];
factoryWithPEMRootCertificates:_callOptions.PEMRootCertificates
privateKey:_callOptions.PEMPrivateKey
certChain:_callOptions.PEMCertChain
error:&error];
if (factory == nil) {
NSLog(@"Error creating secure channel factory: %@", error);
}
@ -86,7 +86,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
NSString *userAgentPrefix = _callOptions.userAgentPrefix;
if (userAgentPrefix) {
args[@GRPC_ARG_PRIMARY_USER_AGENT_STRING] =
[_callOptions.userAgentPrefix stringByAppendingFormat:@" %@", userAgent];
[_callOptions.userAgentPrefix stringByAppendingFormat:@" %@", userAgent];
} else {
args[@GRPC_ARG_PRIMARY_USER_AGENT_STRING] = userAgent;
}
@ -98,19 +98,19 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
if (_callOptions.responseSizeLimit) {
args[@GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH] =
[NSNumber numberWithUnsignedInteger:_callOptions.responseSizeLimit];
[NSNumber numberWithUnsignedInteger:_callOptions.responseSizeLimit];
}
if (_callOptions.compressionAlgorithm != GRPC_COMPRESS_NONE) {
args[@GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM] =
[NSNumber numberWithInt:_callOptions.compressionAlgorithm];
[NSNumber numberWithInt:_callOptions.compressionAlgorithm];
}
if (_callOptions.keepaliveInterval != 0) {
args[@GRPC_ARG_KEEPALIVE_TIME_MS] =
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.keepaliveInterval * 1000)];
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.keepaliveInterval * 1000)];
args[@GRPC_ARG_KEEPALIVE_TIMEOUT_MS] =
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.keepaliveTimeout * 1000)];
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.keepaliveTimeout * 1000)];
}
if (_callOptions.retryEnabled == NO) {
@ -119,15 +119,15 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
if (_callOptions.connectMinTimeout > 0) {
args[@GRPC_ARG_MIN_RECONNECT_BACKOFF_MS] =
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.connectMinTimeout * 1000)];
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.connectMinTimeout * 1000)];
}
if (_callOptions.connectInitialBackoff > 0) {
args[@GRPC_ARG_INITIAL_RECONNECT_BACKOFF_MS] = [NSNumber
numberWithUnsignedInteger:(NSUInteger)(_callOptions.connectInitialBackoff * 1000)];
numberWithUnsignedInteger:(NSUInteger)(_callOptions.connectInitialBackoff * 1000)];
}
if (_callOptions.connectMaxBackoff > 0) {
args[@GRPC_ARG_MAX_RECONNECT_BACKOFF_MS] =
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.connectMaxBackoff * 1000)];
[NSNumber numberWithUnsignedInteger:(NSUInteger)(_callOptions.connectMaxBackoff * 1000)];
}
if (_callOptions.logContext != nil) {
@ -145,7 +145,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
GRPCChannelConfiguration *newConfig =
[[GRPCChannelConfiguration alloc] initWithHost:_host callOptions:_callOptions];
[[GRPCChannelConfiguration alloc] initWithHost:_host callOptions:_callOptions];
return newConfig;
}
@ -172,8 +172,6 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
@end
@implementation GRPCChannel {
GRPCChannelConfiguration *_configuration;
@ -186,21 +184,24 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
}
@synthesize disconnected = _disconnected;
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration {
- (nullable instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration {
return [self initWithChannelConfiguration:channelConfiguration
destroyDelay:kDefaultChannelDestroyDelay];
}
- (nullable instancetype)initWithChannelConfiguration:(GRPCChannelConfiguration *)channelConfiguration
- (nullable instancetype)initWithChannelConfiguration:
(GRPCChannelConfiguration *)channelConfiguration
destroyDelay:(NSTimeInterval)destroyDelay {
GRPCAssert(channelConfiguration != nil, NSInvalidArgumentException, @"channelConfiguration must not be empty.");
GRPCAssert(channelConfiguration != nil, NSInvalidArgumentException,
@"channelConfiguration must not be empty.");
GRPCAssert(destroyDelay > 0, NSInvalidArgumentException, @"destroyDelay must be greater than 0.");
if ((self = [super init])) {
_configuration = [channelConfiguration copy];
if (@available(iOS 8.0, *)) {
_dispatchQueue = dispatch_queue_create(
NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1));
NULL,
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, -1));
} else {
_dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
}
@ -244,7 +245,7 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
GRPCAssert(self->_unmanagedChannel != NULL, NSInvalidArgumentException, @"Invalid channel.");
NSString *serverAuthority =
callOptions.transportType == GRPCTransportTypeCronet ? nil : callOptions.serverAuthority;
callOptions.transportType == GRPCTransportTypeCronet ? nil : callOptions.serverAuthority;
NSTimeInterval timeout = callOptions.timeout;
GRPCAssert(timeout >= 0, NSInvalidArgumentException, @"Invalid timeout");
grpc_slice host_slice = grpc_empty_slice();
@ -253,10 +254,10 @@ static const NSTimeInterval kDefaultChannelDestroyDelay = 30;
}
grpc_slice path_slice = grpc_slice_from_copied_string(path.UTF8String);
gpr_timespec deadline_ms =
timeout == 0
? gpr_inf_future(GPR_CLOCK_REALTIME)
: gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN));
timeout == 0
? gpr_inf_future(GPR_CLOCK_REALTIME)
: gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC),
gpr_time_from_millis((int64_t)(timeout * 1000), GPR_TIMESPAN));
call = grpc_channel_create_call(self->_unmanagedChannel, NULL, GRPC_PROPAGATE_DEFAULTS,
queue.unmanagedQueue, path_slice,
serverAuthority ? &host_slice : NULL, deadline_ms, NULL);

@ -44,8 +44,7 @@ NS_ASSUME_NONNULL_BEGIN
* Return a channel with a particular configuration. If the channel does not exist, execute \a
* createChannel then add it in the pool. If the channel exists, increase its reference count.
*/
- (GRPCChannel *)channelWithHost:(NSString *)host
callOptions:(GRPCCallOptions *)callOptions;
- (GRPCChannel *)channelWithHost:(NSString *)host callOptions:(GRPCCallOptions *)callOptions;
/**
* This method is deprecated.

@ -26,8 +26,8 @@
#import "GRPCCronetChannelFactory.h"
#import "GRPCInsecureChannelFactory.h"
#import "GRPCSecureChannelFactory.h"
#import "version.h"
#import "utilities.h"
#import "version.h"
#import <GRPCClient/GRPCCall+Cronet.h>
#include <grpc/support/log.h>
@ -62,11 +62,8 @@ static dispatch_once_t gInitChannelPool;
return self;
}
- (GRPCChannel *)channelWithHost:(NSString *)host
callOptions:(GRPCCallOptions *)callOptions {
return [self channelWithHost:host
callOptions:callOptions
destroyDelay:0];
- (GRPCChannel *)channelWithHost:(NSString *)host callOptions:(GRPCCallOptions *)callOptions {
return [self channelWithHost:host callOptions:callOptions destroyDelay:0];
}
- (GRPCChannel *)channelWithHost:(NSString *)host
@ -76,14 +73,15 @@ static dispatch_once_t gInitChannelPool;
GRPCAssert(callOptions != nil, NSInvalidArgumentException, @"callOptions must not be empty.");
GRPCChannel *channel;
GRPCChannelConfiguration *configuration =
[[GRPCChannelConfiguration alloc] initWithHost:host callOptions:callOptions];
[[GRPCChannelConfiguration alloc] initWithHost:host callOptions:callOptions];
@synchronized(self) {
channel = _channelPool[configuration];
if (channel == nil || channel.disconnected) {
if (destroyDelay == 0) {
channel = [[GRPCChannel alloc] initWithChannelConfiguration:configuration];
} else {
channel = [[GRPCChannel alloc] initWithChannelConfiguration:configuration destroyDelay:destroyDelay];
channel = [[GRPCChannel alloc] initWithChannelConfiguration:configuration
destroyDelay:destroyDelay];
}
_channelPool[configuration] = channel;
}
@ -91,8 +89,6 @@ static dispatch_once_t gInitChannelPool;
return channel;
}
+ (void)closeOpenConnections {
[[GRPCChannelPool sharedInstance] destroyAllChannels];
}

@ -32,8 +32,8 @@
#import "GRPCCronetChannelFactory.h"
#import "GRPCSecureChannelFactory.h"
#import "NSDictionary+GRPC.h"
#import "version.h"
#import "utilities.h"
#import "version.h"
NS_ASSUME_NONNULL_BEGIN
@ -122,7 +122,8 @@ static NSMutableDictionary *gHostCache;
if (_transportType == GRPCTransportTypeInsecure) {
options.transportType = GRPCTransportTypeInsecure;
} else {
GRPCAssert(_transportType == GRPCTransportTypeDefault, NSInvalidArgumentException, @"Invalid transport type");
GRPCAssert(_transportType == GRPCTransportTypeDefault, NSInvalidArgumentException,
@"Invalid transport type");
options.transportType = GRPCTransportTypeCronet;
}
} else

@ -84,8 +84,7 @@ NS_ASSUME_NONNULL_BEGIN
*errorPtr = defaultRootsError;
}
GRPCAssertWithArgument(
defaultRootsASCII,
NSObjectNotAvailableException,
defaultRootsASCII, NSObjectNotAvailableException,
@"Could not read gRPCCertificates.bundle/roots.pem. This file, "
"with the root certificates, is needed to establish secure (TLS) connections. "
"Because the file is distributed with the gRPC library, this error is usually a sign "

@ -19,17 +19,18 @@
#import <Foundation/Foundation.h>
/** Raise exception when condition not met. Disregard NS_BLOCK_ASSERTIONS. */
#define GRPCAssert(condition, errorType, errorString) \
do { \
if (!(condition)) { \
[NSException raise:(errorType) format:(errorString)]; \
} \
} while (0)
#define GRPCAssert(condition, errorType, errorString) \
do { \
if (!(condition)) { \
[NSException raise:(errorType)format:(errorString)]; \
} \
} while (0)
/** The same as GRPCAssert but allows arguments to be put in the raised string. */
#define GRPCAssertWithArgument(condition, errorType, errorFormat, ...) \
do { \
if (!(condition)) { \
[NSException raise:(errorType) format:(errorFormat), __VA_ARGS__]; \
} \
} while (0)
/** The same as GRPCAssert but allows arguments to be put in the raised string.
*/
#define GRPCAssertWithArgument(condition, errorType, errorFormat, ...) \
do { \
if (!(condition)) { \
[NSException raise:(errorType)format:(errorFormat), __VA_ARGS__]; \
} \
} while (0)

Loading…
Cancel
Save