Simplified some batch code, resolved comments

pull/1363/head
murgatroid99 10 years ago
parent 3767f8bdee
commit 6a084f46b4
  1. 3
      src/objective-c/GRPCClient/private/GRPCCompletionQueue.m
  2. 65
      src/objective-c/GRPCClient/private/GRPCWrappedCall.m
  3. 2
      src/objective-c/GRPCClient/private/NSDictionary+GRPC.h
  4. 9
      src/objective-c/GRPCClient/private/NSDictionary+GRPC.m
  5. 2
      src/objective-c/GRPCClient/private/NSError+GRPC.h

@ -71,11 +71,14 @@
case GRPC_OP_COMPLETE: case GRPC_OP_COMPLETE:
handler = (__bridge_transfer GRPCQueueCompletionHandler)event->tag; handler = (__bridge_transfer GRPCQueueCompletionHandler)event->tag;
handler(event->data.op_complete); handler(event->data.op_complete);
grpc_event_finish(event);
break; break;
case GRPC_QUEUE_SHUTDOWN: case GRPC_QUEUE_SHUTDOWN:
grpc_event_finish(event);
grpc_completion_queue_destroy(unmanagedQueue); grpc_completion_queue_destroy(unmanagedQueue);
return; return;
default: default:
grpc_event_finish(event);
[NSException raise:@"Unrecognized completion type" format:@""]; [NSException raise:@"Unrecognized completion type" format:@""];
} }
}; };

@ -53,13 +53,8 @@
- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler { - (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler {
if (self = [super init]) { if (self = [super init]) {
if (metadata) { _send_metadata = [metadata grpc_getMetadataArray];
[metadata grpc_getMetadataArray:&_send_metadata];
_count = metadata.count; _count = metadata.count;
} else {
_send_metadata = NULL;
_count = 0;
}
_handler = handler; _handler = handler;
} }
return self; return self;
@ -68,6 +63,7 @@
- (void)getOp:(grpc_op *)op { - (void)getOp:(grpc_op *)op {
op->op = GRPC_OP_SEND_INITIAL_METADATA; op->op = GRPC_OP_SEND_INITIAL_METADATA;
op->data.send_initial_metadata.count = _count; op->data.send_initial_metadata.count = _count;
op->data.send_initial_metadata.metadata = _send_metadata;
} }
- (void (^)(void))opProcessor { - (void (^)(void))opProcessor {
@ -148,7 +144,7 @@
@implementation GRPCOpRecvMetadata{ @implementation GRPCOpRecvMetadata{
void(^_handler)(NSDictionary *); void(^_handler)(NSDictionary *);
grpc_metadata_array *_recv_initial_metadata; grpc_metadata_array _recv_initial_metadata;
} }
- (instancetype) init { - (instancetype) init {
@ -158,23 +154,22 @@
- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler { - (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
if (self = [super init]) { if (self = [super init]) {
_handler = handler; _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; return self;
} }
- (void)getOp:(grpc_op *)op { - (void)getOp:(grpc_op *)op {
op->op = GRPC_OP_RECV_INITIAL_METADATA; 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 { - (void (^)(void))opProcessor {
return ^{ return ^{
NSDictionary *metadata = [NSDictionary NSDictionary *metadata = [NSDictionary
grpc_dictionaryFromMetadata:_recv_initial_metadata->metadata grpc_dictionaryFromMetadata:_recv_initial_metadata.metadata
count:_recv_initial_metadata->count]; count:_recv_initial_metadata.count];
grpc_metadata_array_destroy(_recv_initial_metadata); grpc_metadata_array_destroy(&_recv_initial_metadata);
if (_handler) { if (_handler) {
_handler(metadata); _handler(metadata);
} }
@ -185,7 +180,7 @@
@implementation GRPCOpRecvMessage{ @implementation GRPCOpRecvMessage{
void(^_handler)(grpc_byte_buffer *); void(^_handler)(grpc_byte_buffer *);
grpc_byte_buffer **_recv_message; grpc_byte_buffer *_recv_message;
} }
- (instancetype)init { - (instancetype)init {
@ -195,21 +190,19 @@
- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler { - (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
if (self = [super init]) { if (self = [super init]) {
_handler = handler; _handler = handler;
_recv_message = gpr_malloc(sizeof(grpc_byte_buffer*));
} }
return self; return self;
} }
- (void)getOp:(grpc_op *)op { - (void)getOp:(grpc_op *)op {
op->op = GRPC_OP_RECV_MESSAGE; op->op = GRPC_OP_RECV_MESSAGE;
op->data.recv_message = _recv_message; op->data.recv_message = &_recv_message;
} }
- (void (^)(void))opProcessor { - (void (^)(void))opProcessor {
return ^{ return ^{
if (_handler) { if (_handler) {
_handler(*_recv_message); _handler(_recv_message);
gpr_free(_recv_message);
} }
}; };
} }
@ -218,10 +211,8 @@
@implementation GRPCOpRecvStatus{ @implementation GRPCOpRecvStatus{
void(^_handler)(NSError *); void(^_handler)(NSError *);
grpc_status_code *_code; size_t _details_capacity;
char **_details; grpc_status _status;
size_t *_details_capacity;
grpc_metadata_array *_recv_trailing_metadata;
} }
- (instancetype) init { - (instancetype) init {
@ -231,34 +222,27 @@
- (instancetype) initWithHandler:(void (^)(NSError *))handler { - (instancetype) initWithHandler:(void (^)(NSError *))handler {
if (self = [super init]) { if (self = [super init]) {
_handler = handler; _handler = handler;
_code = gpr_malloc(sizeof(grpc_status_code)); _status.details = NULL;
_details = gpr_malloc(sizeof(char*)); _details_capacity = 0;
_details_capacity = gpr_malloc(sizeof(size_t)); grpc_metadata_array_init(&_status.metadata);
*_details_capacity = 0;
_recv_trailing_metadata = gpr_malloc(sizeof(grpc_metadata_array));
} }
return self; return self;
} }
- (void)getOp:(grpc_op *)op { - (void)getOp:(grpc_op *)op {
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
op->data.recv_status_on_client.status = _code; op->data.recv_status_on_client.status = &_status.status;
op->data.recv_status_on_client.status_details = _details; 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.status_details_capacity = &_details_capacity;
op->data.recv_status_on_client.trailing_metadata = _recv_trailing_metadata; op->data.recv_status_on_client.trailing_metadata = &_status.metadata;
} }
- (void (^)(void))opProcessor { - (void (^)(void))opProcessor {
return ^{ 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) { 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) { 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"];
} }
} }

@ -36,5 +36,5 @@
@interface NSDictionary (GRPC) @interface NSDictionary (GRPC)
+ (instancetype)grpc_dictionaryFromMetadata:(struct grpc_metadata *)entries count:(size_t)count; + (instancetype)grpc_dictionaryFromMetadata:(struct grpc_metadata *)entries count:(size_t)count;
- (void)grpc_getMetadataArray:(grpc_metadata **)metadata; - (grpc_metadata *)grpc_getMetadataArray;
@end @end

@ -33,7 +33,6 @@
#import "NSDictionary+GRPC.h" #import "NSDictionary+GRPC.h"
#include <grpc/grpc.h>
#include <grpc/support/alloc.h> #include <grpc/support/alloc.h>
@implementation NSDictionary (GRPC) @implementation NSDictionary (GRPC)
@ -55,12 +54,12 @@
return metadata; return metadata;
} }
- (void)grpc_getMetadataArray:(grpc_metadata **)metadata { - (grpc_metadata *)grpc_getMetadataArray {
*metadata = gpr_malloc([self count] * sizeof(grpc_metadata)); grpc_metadata *metadata = gpr_malloc([self count] * sizeof(grpc_metadata));
int i = 0; int i = 0;
for (id key in self) { for (id key in self) {
id value = self[key]; id value = self[key];
grpc_metadata *current = &(*metadata)[i]; grpc_metadata *current = &metadata[i];
current->key = [key UTF8String]; current->key = [key UTF8String];
if ([value isKindOfClass:[NSData class]]) { if ([value isKindOfClass:[NSData class]]) {
current->value = [value bytes]; current->value = [value bytes];
@ -70,8 +69,8 @@
[NSException raise:NSInvalidArgumentException [NSException raise:NSInvalidArgumentException
format:@"Metadata values must be NSString or NSData."]; format:@"Metadata values must be NSString or NSData."];
} }
current->value = [value UTF8String];
i += 1; i += 1;
} }
return metadata;
} }
@end @end

@ -62,7 +62,7 @@ typedef NS_ENUM(NSInteger, GRPCErrorCode) {
typedef struct grpc_status { typedef struct grpc_status {
grpc_status_code status; grpc_status_code status;
const char *details; const char *details;
grpc_metadata_array *metadata; grpc_metadata_array metadata;
} grpc_status; } grpc_status;
@interface NSError (GRPC) @interface NSError (GRPC)

Loading…
Cancel
Save