mirror of https://github.com/grpc/grpc.git
parent
ea67c96a99
commit
30b7d4e62e
12 changed files with 7033 additions and 22330 deletions
@ -0,0 +1,51 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef Pods_GRPCWrappedCall_h |
||||
#define Pods_GRPCWrappedCall_h |
||||
|
||||
#import <Foundation/Foundation.h> |
||||
#import "GRPCChannel.h" |
||||
|
||||
typedef void(^GRPCCompletionHandler)(NSDictionary *); |
||||
|
||||
@interface GRPCWrappedCall:NSObject; |
||||
|
||||
- (instancetype)initWithChannel:(GRPCChannel *)channel method:(NSString *)method host:(NSString *)host; |
||||
|
||||
- (void)startBatch:(NSDictionary *)ops handleCompletion:(GRPCCompletionHandler)handleCompletion; |
||||
|
||||
- (void)cancel; |
||||
@end |
||||
|
||||
#endif |
@ -0,0 +1,193 @@ |
||||
/* |
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import <Foundation/Foundation.h> |
||||
#import "GRPCWrappedCall.h" |
||||
#import "GRPCCompletionQueue.h" |
||||
#import "NSDictionary+GRPC.h" |
||||
#import "NSData+GRPC.h" |
||||
#import "NSError+GRPC.h" |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/support/alloc.h> |
||||
|
||||
@implementation GRPCWrappedCall{ |
||||
grpc_call *call; |
||||
GRPCCompletionQueue *queue; |
||||
} |
||||
|
||||
- (instancetype)init { |
||||
return [self initWithChannel:nil method:nil host:nil]; |
||||
} |
||||
|
||||
- (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."]; |
||||
} |
||||
|
||||
if (self = [super init]) { |
||||
static dispatch_once_t initialization; |
||||
dispatch_once(&initialization, ^{ |
||||
grpc_init(); |
||||
}); |
||||
|
||||
const char *method_str = [method UTF8String]; |
||||
const char *host_str = [host UTF8String]; |
||||
queue = [GRPCCompletionQueue completionQueue]; |
||||
call = grpc_channel_create_call(channel.unmanagedChannel, queue.unmanagedQueue, method_str, host_str, gpr_inf_future); |
||||
if (call == NULL) { |
||||
return nil; |
||||
} |
||||
} |
||||
return self; |
||||
} |
||||
|
||||
- (void)startBatch:(NSDictionary *)ops handleCompletion:(GRPCCompletionHandler)handleCompletion { |
||||
size_t nops = ops.count; |
||||
grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op)); |
||||
size_t index = 0; |
||||
NSMutableDictionary * __block opProcessors = [NSMutableDictionary new]; |
||||
|
||||
grpc_metadata *send_metadata = NULL; |
||||
grpc_metadata_array *recv_initial_metadata; |
||||
grpc_metadata_array *recv_trailing_metadata; |
||||
grpc_byte_buffer *send_message; |
||||
grpc_byte_buffer **recv_message = NULL; |
||||
grpc_status_code *status_code; |
||||
char **status_details; |
||||
size_t *status_details_capacity; |
||||
for (id key in ops) { |
||||
id (^opBlock)(void); |
||||
grpc_op *current = &ops_array[index]; |
||||
switch ([key intValue]) { |
||||
case GRPC_OP_SEND_INITIAL_METADATA: |
||||
current->data.send_initial_metadata.count = [ops[key] grpc_toMetadataArray:&send_metadata]; |
||||
current->data.send_initial_metadata.metadata = send_metadata; |
||||
opBlock = ^{ |
||||
gpr_free(send_metadata); |
||||
return @YES; |
||||
}; |
||||
break; |
||||
case GRPC_OP_SEND_MESSAGE: |
||||
send_message = [ops[key] grpc_byteBuffer]; |
||||
current->data.send_message = send_message; |
||||
opBlock = ^{ |
||||
grpc_byte_buffer_destroy(send_message); |
||||
return @YES; |
||||
}; |
||||
break; |
||||
case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
||||
opBlock = ^{ |
||||
return @YES; |
||||
}; |
||||
break; |
||||
case GRPC_OP_RECV_INITIAL_METADATA: |
||||
recv_initial_metadata = gpr_malloc(sizeof(grpc_metadata_array)); |
||||
grpc_metadata_array_init(recv_initial_metadata); |
||||
current->data.recv_initial_metadata = recv_initial_metadata; |
||||
opBlock = ^{ |
||||
NSDictionary *metadata = [NSDictionary grpc_dictionaryFromMetadata:recv_initial_metadata->metadata count:recv_initial_metadata->count]; |
||||
grpc_metadata_array_destroy(recv_initial_metadata); |
||||
return metadata; |
||||
}; |
||||
break; |
||||
case GRPC_OP_RECV_MESSAGE: |
||||
recv_message = gpr_malloc(sizeof(grpc_byte_buffer*)); |
||||
current->data.recv_message = recv_message; |
||||
opBlock = ^{ |
||||
NSData *data = [NSData grpc_dataWithByteBuffer:*recv_message]; |
||||
grpc_byte_buffer_destroy(*recv_message); |
||||
gpr_free(recv_message); |
||||
return data; |
||||
}; |
||||
break; |
||||
case GRPC_OP_RECV_STATUS_ON_CLIENT: |
||||
status_code = gpr_malloc(sizeof(status_code)); |
||||
current->data.recv_status_on_client.status = status_code; |
||||
status_details = gpr_malloc(sizeof(char*)); |
||||
*status_details = NULL; |
||||
current->data.recv_status_on_client.status_details = status_details; |
||||
status_details_capacity = gpr_malloc(sizeof(grpc_status_code)); |
||||
*status_details_capacity = 0; |
||||
current->data.recv_status_on_client.status_details_capacity = status_details_capacity; |
||||
recv_trailing_metadata = gpr_malloc(sizeof(grpc_metadata_array)); |
||||
grpc_metadata_array_init(recv_trailing_metadata); |
||||
current->data.recv_status_on_client.trailing_metadata = recv_trailing_metadata; |
||||
opBlock = ^{ |
||||
grpc_status status; |
||||
status.status = *status_code; |
||||
status.details = *status_details; |
||||
status.metadata = recv_trailing_metadata; |
||||
gpr_free(status_code); |
||||
gpr_free(status_details); |
||||
gpr_free(status_details_capacity); |
||||
return [NSError grpc_errorFromStatus:&status]; |
||||
}; |
||||
break; |
||||
case GRPC_OP_SEND_STATUS_FROM_SERVER: |
||||
[NSException raise:NSInvalidArgumentException format:@"Not a server: cannot send status"]; |
||||
default: |
||||
[NSException raise:NSInvalidArgumentException format:@"Unrecognized dictionary key"]; |
||||
} |
||||
current->op = [key intValue]; |
||||
[opProcessors setObject:opBlock forKey:key]; |
||||
} |
||||
grpc_call_error error = grpc_call_start_batch(call, ops_array, nops, (__bridge_retained void *)(^(grpc_op_error error){ |
||||
if (error != GRPC_OP_OK) { |
||||
[NSException raise:@"Operation Exception" format:@"The batch failed with an unknown error"]; |
||||
} |
||||
NSMutableDictionary *result = [NSMutableDictionary new]; |
||||
for (id key in opProcessors) { |
||||
id(^block)(void) = opProcessors[key]; |
||||
id value = block(); |
||||
if (value == nil) { |
||||
value = [NSNull null]; |
||||
} |
||||
[result setObject:value forKey:key]; |
||||
} |
||||
handleCompletion(result); |
||||
})); |
||||
if (error != GRPC_CALL_OK) { |
||||
[NSException raise:NSInvalidArgumentException format:@"The batch did not start successfully"]; |
||||
} |
||||
} |
||||
|
||||
- (void)cancel { |
||||
grpc_call_cancel(call); |
||||
} |
||||
|
||||
- (void)dealloc { |
||||
grpc_call_destroy(call); |
||||
} |
||||
|
||||
@end |
@ -1,20 +1,35 @@ |
||||
PODS: |
||||
- GRPCClient (0.0.1): |
||||
- RxLibrary (~> 0.0) |
||||
- RxLibrary (0.0.1) |
||||
- gRPC (0.0.1): |
||||
- gRPC/C-Core (= 0.0.1) |
||||
- gRPC/RxLibrary (= 0.0.1) |
||||
- gRPC/C-Core (0.0.1): |
||||
- OpenSSL (~> 1.0.200) |
||||
- gRPC/RxLibrary (0.0.1) |
||||
- OpenSSL (1.0.201) |
||||
- ProtocolBuffers (1.9.8) |
||||
- RemoteTest (0.0.1): |
||||
- ProtocolBuffers (~> 1.9) |
||||
- Route_guide (0.0.1): |
||||
- ProtocolBuffers (~> 1.9) |
||||
|
||||
DEPENDENCIES: |
||||
- GRPCClient (from `../../GRPCClient`) |
||||
- RxLibrary (from `../../RxLibrary`) |
||||
- gRPC (from `../../../..`) |
||||
- RemoteTest (from `RemoteTestClient`) |
||||
- Route_guide (from `RouteGuideClient`) |
||||
|
||||
EXTERNAL SOURCES: |
||||
GRPCClient: |
||||
:path: ../../GRPCClient |
||||
RxLibrary: |
||||
:path: ../../RxLibrary |
||||
gRPC: |
||||
:path: ../../../.. |
||||
RemoteTest: |
||||
:path: RemoteTestClient |
||||
Route_guide: |
||||
:path: RouteGuideClient |
||||
|
||||
SPEC CHECKSUMS: |
||||
GRPCClient: 05c58faab99661384178bb7c5f93b60c2bfc89f8 |
||||
RxLibrary: 70cfcf1573ec16a375b4fe61d976a3188aab9303 |
||||
gRPC: 70fefb183437c880dbe8f9a477ff0d409e81e390 |
||||
OpenSSL: 4e990d04b14015c49c800c400b86ae44a4818a5c |
||||
ProtocolBuffers: 9a4a171c0c7cc8f21dd29aeca4f9ac775d84a880 |
||||
RemoteTest: d7bbf2e0646a886ea9502375f0f79e8fe551aa71 |
||||
Route_guide: a277da8eef182774abb050d7b81109f5878f8652 |
||||
|
||||
COCOAPODS: 0.35.0 |
||||
COCOAPODS: 0.36.4 |
||||
|
@ -1,20 +1,35 @@ |
||||
PODS: |
||||
- GRPCClient (0.0.1): |
||||
- RxLibrary (~> 0.0) |
||||
- RxLibrary (0.0.1) |
||||
- gRPC (0.0.1): |
||||
- gRPC/C-Core (= 0.0.1) |
||||
- gRPC/RxLibrary (= 0.0.1) |
||||
- gRPC/C-Core (0.0.1): |
||||
- OpenSSL (~> 1.0.200) |
||||
- gRPC/RxLibrary (0.0.1) |
||||
- OpenSSL (1.0.201) |
||||
- ProtocolBuffers (1.9.8) |
||||
- RemoteTest (0.0.1): |
||||
- ProtocolBuffers (~> 1.9) |
||||
- Route_guide (0.0.1): |
||||
- ProtocolBuffers (~> 1.9) |
||||
|
||||
DEPENDENCIES: |
||||
- GRPCClient (from `../../GRPCClient`) |
||||
- RxLibrary (from `../../RxLibrary`) |
||||
- gRPC (from `../../../..`) |
||||
- RemoteTest (from `RemoteTestClient`) |
||||
- Route_guide (from `RouteGuideClient`) |
||||
|
||||
EXTERNAL SOURCES: |
||||
GRPCClient: |
||||
:path: ../../GRPCClient |
||||
RxLibrary: |
||||
:path: ../../RxLibrary |
||||
gRPC: |
||||
:path: ../../../.. |
||||
RemoteTest: |
||||
:path: RemoteTestClient |
||||
Route_guide: |
||||
:path: RouteGuideClient |
||||
|
||||
SPEC CHECKSUMS: |
||||
GRPCClient: 05c58faab99661384178bb7c5f93b60c2bfc89f8 |
||||
RxLibrary: 70cfcf1573ec16a375b4fe61d976a3188aab9303 |
||||
gRPC: 70fefb183437c880dbe8f9a477ff0d409e81e390 |
||||
OpenSSL: 4e990d04b14015c49c800c400b86ae44a4818a5c |
||||
ProtocolBuffers: 9a4a171c0c7cc8f21dd29aeca4f9ac775d84a880 |
||||
RemoteTest: d7bbf2e0646a886ea9502375f0f79e8fe551aa71 |
||||
Route_guide: a277da8eef182774abb050d7b81109f5878f8652 |
||||
|
||||
COCOAPODS: 0.35.0 |
||||
COCOAPODS: 0.36.4 |
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue