Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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