From 6a084f46b421ee1f4ec9ff0c9e16d8678baef20f Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Wed, 29 Apr 2015 10:18:05 -0700 Subject: [PATCH] Simplified some batch code, resolved comments --- .../GRPCClient/private/GRPCCompletionQueue.m | 3 + .../GRPCClient/private/GRPCWrappedCall.m | 67 +++++++------------ .../GRPCClient/private/NSDictionary+GRPC.h | 2 +- .../GRPCClient/private/NSDictionary+GRPC.m | 9 ++- .../GRPCClient/private/NSError+GRPC.h | 2 +- 5 files changed, 35 insertions(+), 48 deletions(-) diff --git a/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m b/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m index cae21b7807e..0e20664a02c 100644 --- a/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m +++ b/src/objective-c/GRPCClient/private/GRPCCompletionQueue.m @@ -71,11 +71,14 @@ case GRPC_OP_COMPLETE: handler = (__bridge_transfer GRPCQueueCompletionHandler)event->tag; handler(event->data.op_complete); + grpc_event_finish(event); break; case GRPC_QUEUE_SHUTDOWN: + grpc_event_finish(event); grpc_completion_queue_destroy(unmanagedQueue); return; default: + grpc_event_finish(event); [NSException raise:@"Unrecognized completion type" format:@""]; } }; diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m index ad6aca0c775..934557ff75c 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m @@ -53,13 +53,8 @@ - (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler { if (self = [super init]) { - if (metadata) { - [metadata grpc_getMetadataArray:&_send_metadata]; - _count = metadata.count; - } else { - _send_metadata = NULL; - _count = 0; - } + _send_metadata = [metadata grpc_getMetadataArray]; + _count = metadata.count; _handler = handler; } return self; @@ -68,6 +63,7 @@ - (void)getOp:(grpc_op *)op { op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = _count; + op->data.send_initial_metadata.metadata = _send_metadata; } - (void (^)(void))opProcessor { @@ -148,7 +144,7 @@ @implementation GRPCOpRecvMetadata{ void(^_handler)(NSDictionary *); - grpc_metadata_array *_recv_initial_metadata; + grpc_metadata_array _recv_initial_metadata; } - (instancetype) init { @@ -158,23 +154,22 @@ - (instancetype) initWithHandler:(void (^)(NSDictionary *))handler { if (self = [super init]) { _handler = handler; - _recv_initial_metadata = gpr_malloc(sizeof(grpc_metadata_array)); - grpc_metadata_array_init(_recv_initial_metadata); + grpc_metadata_array_init(&_recv_initial_metadata); } return self; } - (void)getOp:(grpc_op *)op { op->op = GRPC_OP_RECV_INITIAL_METADATA; - op->data.recv_initial_metadata = _recv_initial_metadata; + op->data.recv_initial_metadata = &_recv_initial_metadata; } - (void (^)(void))opProcessor { return ^{ NSDictionary *metadata = [NSDictionary - grpc_dictionaryFromMetadata:_recv_initial_metadata->metadata - count:_recv_initial_metadata->count]; - grpc_metadata_array_destroy(_recv_initial_metadata); + grpc_dictionaryFromMetadata:_recv_initial_metadata.metadata + count:_recv_initial_metadata.count]; + grpc_metadata_array_destroy(&_recv_initial_metadata); if (_handler) { _handler(metadata); } @@ -185,7 +180,7 @@ @implementation GRPCOpRecvMessage{ void(^_handler)(grpc_byte_buffer *); - grpc_byte_buffer **_recv_message; + grpc_byte_buffer *_recv_message; } - (instancetype)init { @@ -195,21 +190,19 @@ - (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler { if (self = [super init]) { _handler = handler; - _recv_message = gpr_malloc(sizeof(grpc_byte_buffer*)); } return self; } - (void)getOp:(grpc_op *)op { op->op = GRPC_OP_RECV_MESSAGE; - op->data.recv_message = _recv_message; + op->data.recv_message = &_recv_message; } - (void (^)(void))opProcessor { return ^{ if (_handler) { - _handler(*_recv_message); - gpr_free(_recv_message); + _handler(_recv_message); } }; } @@ -218,10 +211,8 @@ @implementation GRPCOpRecvStatus{ void(^_handler)(NSError *); - grpc_status_code *_code; - char **_details; - size_t *_details_capacity; - grpc_metadata_array *_recv_trailing_metadata; + size_t _details_capacity; + grpc_status _status; } - (instancetype) init { @@ -231,34 +222,27 @@ - (instancetype) initWithHandler:(void (^)(NSError *))handler { if (self = [super init]) { _handler = handler; - _code = gpr_malloc(sizeof(grpc_status_code)); - _details = gpr_malloc(sizeof(char*)); - _details_capacity = gpr_malloc(sizeof(size_t)); - *_details_capacity = 0; - _recv_trailing_metadata = gpr_malloc(sizeof(grpc_metadata_array)); + _status.details = NULL; + _details_capacity = 0; + grpc_metadata_array_init(&_status.metadata); } return self; } - (void)getOp:(grpc_op *)op { op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; - op->data.recv_status_on_client.status = _code; - op->data.recv_status_on_client.status_details = _details; - op->data.recv_status_on_client.status_details_capacity = _details_capacity; - op->data.recv_status_on_client.trailing_metadata = _recv_trailing_metadata; + op->data.recv_status_on_client.status = &_status.status; + op->data.recv_status_on_client.status_details = &_status.details; + op->data.recv_status_on_client.status_details_capacity = &_details_capacity; + op->data.recv_status_on_client.trailing_metadata = &_status.metadata; } - (void (^)(void))opProcessor { return ^{ - grpc_status status; - status.status = *_code; - status.details = *_details; - status.metadata = _recv_trailing_metadata; - gpr_free(_code); - gpr_free(_details); - gpr_free(_details_capacity); if (_handler) { - _handler([NSError grpc_errorFromStatus:&status]); + NSError *error = [NSError grpc_errorFromStatus:&_status]; + grpc_metadata_array_destroy(&_status.metadata); + _handler(error); } }; } @@ -326,7 +310,8 @@ })); if (error != GRPC_CALL_OK) { - [NSException raise:NSInvalidArgumentException format:@"The batch did not start successfully"]; + [NSException raise:NSInternalInconsistencyException + format:@"A precondition for calling grpc_call_start_batch wasn't met"]; } } diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h index fec2adb2120..f0604217274 100644 --- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h +++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.h @@ -36,5 +36,5 @@ @interface NSDictionary (GRPC) + (instancetype)grpc_dictionaryFromMetadata:(struct grpc_metadata *)entries count:(size_t)count; -- (void)grpc_getMetadataArray:(grpc_metadata **)metadata; +- (grpc_metadata *)grpc_getMetadataArray; @end diff --git a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m index 83c09cd37e1..6ddb35cab69 100644 --- a/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m +++ b/src/objective-c/GRPCClient/private/NSDictionary+GRPC.m @@ -33,7 +33,6 @@ #import "NSDictionary+GRPC.h" -#include #include @implementation NSDictionary (GRPC) @@ -55,12 +54,12 @@ return metadata; } -- (void)grpc_getMetadataArray:(grpc_metadata **)metadata { - *metadata = gpr_malloc([self count] * sizeof(grpc_metadata)); +- (grpc_metadata *)grpc_getMetadataArray { + grpc_metadata *metadata = gpr_malloc([self count] * sizeof(grpc_metadata)); int i = 0; for (id key in self) { id value = self[key]; - grpc_metadata *current = &(*metadata)[i]; + grpc_metadata *current = &metadata[i]; current->key = [key UTF8String]; if ([value isKindOfClass:[NSData class]]) { current->value = [value bytes]; @@ -70,8 +69,8 @@ [NSException raise:NSInvalidArgumentException format:@"Metadata values must be NSString or NSData."]; } - current->value = [value UTF8String]; i += 1; } + return metadata; } @end diff --git a/src/objective-c/GRPCClient/private/NSError+GRPC.h b/src/objective-c/GRPCClient/private/NSError+GRPC.h index aa032a4a22c..af1f3aecc5b 100644 --- a/src/objective-c/GRPCClient/private/NSError+GRPC.h +++ b/src/objective-c/GRPCClient/private/NSError+GRPC.h @@ -62,7 +62,7 @@ typedef NS_ENUM(NSInteger, GRPCErrorCode) { typedef struct grpc_status { grpc_status_code status; const char *details; - grpc_metadata_array *metadata; + grpc_metadata_array metadata; } grpc_status; @interface NSError (GRPC)