Skip to content
Snippets Groups Projects
Commit 6cc4680c authored by murgatroid99's avatar murgatroid99
Browse files

Changed message handling, fixed line lengths

parent b5c076f1
No related branches found
No related tags found
No related merge requests found
......@@ -46,19 +46,6 @@
#import "private/NSDictionary+GRPC.h"
#import "private/NSError+GRPC.h"
// A grpc_call_error represents a precondition failure when invoking the
// grpc_call_* functions. If one ever happens, it's a bug in this library.
//
// TODO(jcanizales): Can an application shut down gracefully when a thread other
// than the main one throws an exception?
static void AssertNoErrorInCall(grpc_call_error error) {
if (error != GRPC_CALL_OK) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Precondition of grpc_call_* not met."
userInfo:nil];
}
}
@interface GRPCCall () <GRXWriteable>
// Makes it readwrite.
@property(atomic, strong) NSDictionary *responseMetadata;
......@@ -121,7 +108,9 @@ static void AssertNoErrorInCall(grpc_call_error error) {
_channel = [GRPCChannel channelToHost:host];
_wrappedCall = [[GRPCWrappedCall alloc] initWithChannel:_channel method:method.HTTP2Path host:host];
_wrappedCall = [[GRPCWrappedCall alloc] initWithChannel:_channel
method:method.HTTP2Path
host:host];
// Serial queue to invoke the non-reentrant methods of the grpc_call object.
_callQueue = dispatch_queue_create("org.grpc.call", NULL);
......@@ -156,8 +145,9 @@ static void AssertNoErrorInCall(grpc_call_error error) {
}
- (void)dealloc {
__block GRPCWrappedCall *wrappedCall = _wrappedCall;
dispatch_async(_callQueue, ^{
_wrappedCall = nil;
wrappedCall = nil;
});
}
......@@ -165,7 +155,7 @@ static void AssertNoErrorInCall(grpc_call_error error) {
// Only called from the call queue.
// The handler will be called from the network queue.
- (void)startReadWithHandler:(void(^)(NSData *))handler {
- (void)startReadWithHandler:(void(^)(grpc_byte_buffer *))handler {
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMessage alloc] initWithHandler:handler]]];
}
......@@ -183,10 +173,12 @@ static void AssertNoErrorInCall(grpc_call_error error) {
__weak GRPCDelegateWrapper *weakWriteable = _responseWriteable;
dispatch_async(_callQueue, ^{
[weakSelf startReadWithHandler:^(NSData *data) {
if (data == nil) {
[weakSelf startReadWithHandler:^(grpc_byte_buffer *message) {
if (message == NULL) {
return;
}
NSData *data = [NSData grpc_dataWithByteBuffer:message];
grpc_byte_buffer_destroy(message);
if (!data) {
// The app doesn't have enough memory to hold the server response. We
// don't want to throw, because the app shouldn't crash for a behavior
......@@ -213,7 +205,8 @@ static void AssertNoErrorInCall(grpc_call_error error) {
// TODO(jcanizales): Rename to commitHeaders.
- (void)sendHeaders:(NSDictionary *)metadata {
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc] initWithMetadata:metadata ?: @{} handler:nil]]];
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc]
initWithMetadata:metadata ?: @{} handler:nil]]];
}
#pragma mark GRXWriteable implementation
......@@ -231,7 +224,9 @@ static void AssertNoErrorInCall(grpc_call_error error) {
strongSelf->_requestWriter.state = GRXWriterStateStarted;
}
};
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMessage alloc] initWithMessage:message handler:resumingHandler]] errorHandler:errorHandler];
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMessage alloc]
initWithMessage:message
handler:resumingHandler]] errorHandler:errorHandler];
}
- (void)didReceiveValue:(id)value {
......@@ -254,7 +249,8 @@ static void AssertNoErrorInCall(grpc_call_error error) {
// Only called from the call queue. The error handler will be called from the
// network queue if the requests stream couldn't be closed successfully.
- (void)finishRequestWithErrorHandler:(void (^)())errorHandler {
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendClose alloc] init]] errorHandler:errorHandler];
[_wrappedCall startBatchWithOperations:@[[[GRPCOpSendClose alloc] init]]
errorHandler:errorHandler];
}
- (void)didFinishWithError:(NSError *)errorOrNil {
......@@ -280,8 +276,10 @@ static void AssertNoErrorInCall(grpc_call_error error) {
// The second one (completionHandler), whenever the RPC finishes for any reason.
- (void)invokeCallWithMetadataHandler:(void(^)(NSDictionary *))metadataHandler
completionHandler:(void(^)(NSError *))completionHandler {
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc] initWithHandler:metadataHandler]]];
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvStatus alloc] initWithHandler:completionHandler]]];
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc]
initWithHandler:metadataHandler]]];
[_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvStatus alloc]
initWithHandler:completionHandler]]];
}
- (void)invokeCall {
......
......@@ -47,13 +47,15 @@ typedef void(^GRPCCompletionHandler)(NSDictionary *);
@interface GRPCOpSendMetadata : NSObject <GRPCOp>
- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithMetadata:(NSDictionary *)metadata
handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER;
@end
@interface GRPCOpSendMessage : NSObject <GRPCOp>
- (instancetype)initWithMessage:(NSData *)message handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithMessage:(NSData *)message
handler:(void(^)(void))handler NS_DESIGNATED_INITIALIZER;
@end
......@@ -71,7 +73,7 @@ typedef void(^GRPCCompletionHandler)(NSDictionary *);
@interface GRPCOpRecvMessage : NSObject <GRPCOp>
- (instancetype)initWithHandler:(void(^)(NSData *))handler NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithHandler:(void(^)(grpc_byte_buffer *))handler NS_DESIGNATED_INITIALIZER;
@end
......@@ -83,7 +85,9 @@ typedef void(^GRPCCompletionHandler)(NSDictionary *);
@interface GRPCWrappedCall : NSObject
- (instancetype)initWithChannel:(GRPCChannel *)channel method:(NSString *)method host:(NSString *)host NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithChannel:(GRPCChannel *)channel
method:(NSString *)method
host:(NSString *)host NS_DESIGNATED_INITIALIZER;
- (void)startBatchWithOperations:(NSArray *)ops errorHandler:(void(^)())errorHandler;
......
......@@ -171,7 +171,9 @@
- (void (^)(void))opProcessor {
return ^{
NSDictionary *metadata = [NSDictionary grpc_dictionaryFromMetadata:_recv_initial_metadata->metadata count:_recv_initial_metadata->count];
NSDictionary *metadata = [NSDictionary
grpc_dictionaryFromMetadata:_recv_initial_metadata->metadata
count:_recv_initial_metadata->count];
grpc_metadata_array_destroy(_recv_initial_metadata);
if (_handler) {
_handler(metadata);
......@@ -182,7 +184,7 @@
@end
@implementation GRPCOpRecvMessage{
void(^_handler)(NSData *);
void(^_handler)(grpc_byte_buffer *);
grpc_byte_buffer **_recv_message;
}
......@@ -190,7 +192,7 @@
return [self initWithHandler:nil];
}
- (instancetype)initWithHandler:(void (^)(NSData *))handler {
- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
if (self = [super init]) {
_handler = handler;
_recv_message = gpr_malloc(sizeof(grpc_byte_buffer*));
......@@ -205,11 +207,9 @@
- (void (^)(void))opProcessor {
return ^{
NSData *message = [NSData grpc_dataWithByteBuffer:*_recv_message];
grpc_byte_buffer_destroy(*_recv_message);
gpr_free(_recv_message);
if (_handler) {
_handler(message);
_handler(*_recv_message);
gpr_free(_recv_message);
}
};
}
......@@ -274,9 +274,12 @@
return [self initWithChannel:nil method:nil host:nil];
}
- (instancetype)initWithChannel:(GRPCChannel *)channel method:(NSString *)method host:(NSString *)host {
- (instancetype)initWithChannel:(GRPCChannel *)channel
method:(NSString *)method
host:(NSString *)host {
if (!channel || !method || !host) {
[NSException raise:NSInvalidArgumentException format:@"channel, method, and host cannot be nil."];
[NSException raise:NSInvalidArgumentException
format:@"channel, method, and host cannot be nil."];
}
if (self = [super init]) {
......@@ -286,7 +289,8 @@
});
_queue = [GRPCCompletionQueue completionQueue];
_call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue, method.UTF8String, host.UTF8String, gpr_inf_future);
_call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue,
method.UTF8String, host.UTF8String, gpr_inf_future);
if (_call == NULL) {
return nil;
}
......@@ -307,7 +311,8 @@
[op getOp:&ops_array[i]];
[opProcessors addObject:[op opProcessor]];
}
grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops, (__bridge_retained void *)(^(grpc_op_error error){
grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
(__bridge_retained void *)(^(grpc_op_error error){
if (error != GRPC_OP_OK) {
if (errorHandler) {
errorHandler();
......
......@@ -67,7 +67,8 @@
} else if ([value isKindOfClass:[NSString class]]) {
current->value = [value UTF8String];
} else {
[NSException raise:NSInvalidArgumentException format:@"Metadata values must be NSString or NSData."];
[NSException raise:NSInvalidArgumentException
format:@"Metadata values must be NSString or NSData."];
}
current->value = [value UTF8String];
i += 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment