Encapsulate grpc_call creation inside GRPCChannel

pull/2754/head
Jorge Canizales 10 years ago
parent 3a5253eb12
commit bd993df3f6
  1. 14
      src/objective-c/GRPCClient/private/GRPCChannel.h
  2. 28
      src/objective-c/GRPCClient/private/GRPCChannel.m
  3. 3
      src/objective-c/GRPCClient/private/GRPCSecureChannel.m
  4. 3
      src/objective-c/GRPCClient/private/GRPCUnsecuredChannel.m
  5. 15
      src/objective-c/GRPCClient/private/GRPCWrappedCall.m

@ -33,19 +33,19 @@
#import <Foundation/Foundation.h>
struct grpc_channel;
#include <grpc/grpc.h>
// Each separate instance of this class represents at least one TCP
// connection to the provided host. To create a grpc_call, pass the
// value of the unmanagedChannel property to grpc_channel_create_call.
// Release this object when the call is finished.
// Each separate instance of this class represents at least one TCP connection to the provided host.
// To create a grpc_call to that host, use |unmanagedCallWithPath|. Release this object when the
// call is finished.
@interface GRPCChannel : NSObject
@property(nonatomic, readonly) struct grpc_channel *unmanagedChannel;
// Convenience constructor to allow for reuse of connections.
+ (instancetype)channelToHost:(NSString *)host;
- (instancetype)initWithHost:(NSString *)host NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithChannel:(grpc_channel *)unmanagedChannel
hostName:(NSString *)hostName NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithChannel:(struct grpc_channel *)unmanagedChannel NS_DESIGNATED_INITIALIZER;
- (grpc_call *)unmanagedCallWithPath:(NSString *)path;
@end

@ -35,10 +35,15 @@
#include <grpc/grpc.h>
#import "GRPCCompletionQueue.h"
#import "GRPCSecureChannel.h"
#import "GRPCUnsecuredChannel.h"
@implementation GRPCChannel
@implementation GRPCChannel {
grpc_channel *_unmanagedChannel;
NSString *_hostName;
GRPCCompletionQueue *_queue;
}
+ (instancetype)channelToHost:(NSString *)host {
// TODO(mlumish): Investigate whether a cache with strong links is a good idea
@ -81,13 +86,32 @@
return nil; // silence warning.
}
- (instancetype)initWithChannel:(struct grpc_channel *)unmanagedChannel {
- (instancetype)initWithChannel:(struct grpc_channel *)unmanagedChannel
hostName:(NSString *)hostName {
if (!unmanagedChannel || !hostName) {
[NSException raise:NSInvalidArgumentException
format:@"Neither unmanagedChannel nor hostName can be nil."];
}
if ((self = [super init])) {
_unmanagedChannel = unmanagedChannel;
_hostName = hostName;
// In case sharing the queue creates contention, we can change it to one per grpc_call.
_queue = [GRPCCompletionQueue completionQueue];
if (!_queue) {
return nil;
}
}
return self;
}
- (grpc_call *)unmanagedCallWithPath:(NSString *)path {
return grpc_channel_create_call(_unmanagedChannel,
_queue.unmanagedQueue,
path.UTF8String,
_hostName.UTF8String,
gpr_inf_future(GPR_CLOCK_REALTIME));
}
- (void)dealloc {
// _unmanagedChannel is NULL when deallocating an object of the base class (because the
// initializer returns a different object).

@ -54,7 +54,8 @@
});
return (self = [super initWithChannel:grpc_secure_channel_create(kCredentials,
host.UTF8String,
NULL)]);
NULL)
hostName:host]);
}
@end

@ -38,7 +38,8 @@
@implementation GRPCUnsecuredChannel
- (instancetype)initWithHost:(NSString *)host {
return (self = [super initWithChannel:grpc_insecure_channel_create(host.UTF8String, NULL)]);
return (self = [super initWithChannel:grpc_insecure_channel_create(host.UTF8String, NULL)
hostName:host]);
}
@end

@ -39,7 +39,6 @@
#include <grpc/support/alloc.h>
#import "GRPCChannel.h"
#import "GRPCCompletionQueue.h"
#import "NSDictionary+GRPC.h"
#import "NSData+GRPC.h"
#import "NSError+GRPC.h"
@ -227,7 +226,6 @@
@implementation GRPCWrappedCall{
GRPCChannel *_channel;
grpc_call *_call;
GRPCCompletionQueue *_queue;
}
- (instancetype)init {
@ -247,19 +245,8 @@
grpc_init();
});
_queue = [GRPCCompletionQueue completionQueue];
if (!_queue) {
return nil;
}
_channel = [GRPCChannel channelToHost:host];
if (!_channel) {
return nil;
}
_call = grpc_channel_create_call(_channel.unmanagedChannel,
_queue.unmanagedQueue,
path.UTF8String,
host.UTF8String,
gpr_inf_future(GPR_CLOCK_REALTIME));
_call = [_channel unmanagedCallWithPath:path];
if (_call == NULL) {
return nil;
}

Loading…
Cancel
Save