Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | af77b3d | 2015-02-13 11:22:21 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jan Tattermusch | a7fff86 | 2015-02-13 11:08:08 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Jan Tattermusch | 2f2afd2 | 2015-02-18 15:47:41 -0800 | [diff] [blame] | 34 | #include "src/core/support/string.h" |
| 35 | |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 36 | #include <grpc/byte_buffer_reader.h> |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 37 | #include <grpc/support/port_platform.h> |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 39 | #include <grpc/support/log.h> |
| 40 | #include <grpc/support/slice.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 41 | #include <grpc/support/string_util.h> |
Jan Tattermusch | 5339814 | 2015-02-24 08:21:57 -0800 | [diff] [blame] | 42 | #include <grpc/support/thd.h> |
| 43 | #include <grpc/grpc.h> |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 44 | #include <grpc/grpc_security.h> |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 45 | |
| 46 | #include <string.h> |
| 47 | |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 48 | #ifdef GPR_WIN32 |
| 49 | #define GPR_EXPORT __declspec(dllexport) |
| 50 | #define GPR_CALLTYPE __stdcall |
| 51 | #endif |
| 52 | |
| 53 | #ifndef GPR_EXPORT |
| 54 | #define GPR_EXPORT |
| 55 | #endif |
| 56 | |
| 57 | #ifndef GPR_CALLTYPE |
| 58 | #define GPR_CALLTYPE |
| 59 | #endif |
| 60 | |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 61 | grpc_byte_buffer *string_to_byte_buffer(const char *buffer, size_t len) { |
| 62 | gpr_slice slice = gpr_slice_from_copied_buffer(buffer, len); |
David Garcia Quintas | 59f905d | 2015-06-08 16:31:19 -0700 | [diff] [blame] | 63 | grpc_byte_buffer *bb = grpc_raw_byte_buffer_create(&slice, 1); |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 64 | gpr_slice_unref(slice); |
| 65 | return bb; |
| 66 | } |
| 67 | |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 68 | /* |
| 69 | * Helper to maintain lifetime of batch op inputs and store batch op outputs. |
| 70 | */ |
| 71 | typedef struct gprcsharp_batch_context { |
| 72 | grpc_metadata_array send_initial_metadata; |
| 73 | grpc_byte_buffer *send_message; |
| 74 | struct { |
| 75 | grpc_metadata_array trailing_metadata; |
| 76 | char *status_details; |
| 77 | } send_status_from_server; |
| 78 | grpc_metadata_array recv_initial_metadata; |
| 79 | grpc_byte_buffer *recv_message; |
| 80 | struct { |
| 81 | grpc_metadata_array trailing_metadata; |
| 82 | grpc_status_code status; |
| 83 | char *status_details; |
| 84 | size_t status_details_capacity; |
| 85 | } recv_status_on_client; |
| 86 | int recv_close_on_server_cancelled; |
| 87 | struct { |
| 88 | grpc_call *call; |
| 89 | grpc_call_details call_details; |
| 90 | grpc_metadata_array request_metadata; |
| 91 | } server_rpc_new; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 92 | } grpcsharp_batch_context; |
| 93 | |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 94 | GPR_EXPORT grpcsharp_batch_context *GPR_CALLTYPE grpcsharp_batch_context_create() { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 95 | grpcsharp_batch_context *ctx = gpr_malloc(sizeof(grpcsharp_batch_context)); |
| 96 | memset(ctx, 0, sizeof(grpcsharp_batch_context)); |
| 97 | return ctx; |
| 98 | } |
| 99 | |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Destroys array->metadata. |
| 102 | * The array pointer itself is not freed. |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 103 | */ |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 104 | void grpcsharp_metadata_array_destroy_metadata_only( |
| 105 | grpc_metadata_array *array) { |
| 106 | gpr_free(array->metadata); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Destroys keys, values and array->metadata. |
| 111 | * The array pointer itself is not freed. |
| 112 | */ |
| 113 | void grpcsharp_metadata_array_destroy_metadata_including_entries( |
| 114 | grpc_metadata_array *array) { |
| 115 | size_t i; |
| 116 | if (array->metadata) { |
| 117 | for (i = 0; i < array->count; i++) { |
| 118 | gpr_free((void *)array->metadata[i].key); |
| 119 | gpr_free((void *)array->metadata[i].value); |
| 120 | } |
| 121 | } |
| 122 | gpr_free(array->metadata); |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Fully destroys the metadata array. |
| 127 | */ |
| 128 | GPR_EXPORT void GPR_CALLTYPE |
| 129 | grpcsharp_metadata_array_destroy_full(grpc_metadata_array *array) { |
| 130 | if (!array) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 131 | return; |
| 132 | } |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 133 | grpcsharp_metadata_array_destroy_metadata_including_entries(array); |
| 134 | gpr_free(array); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Creates an empty metadata array with given capacity. |
| 139 | * Array can later be destroyed by grpc_metadata_array_destroy_full. |
| 140 | */ |
| 141 | GPR_EXPORT grpc_metadata_array *GPR_CALLTYPE |
| 142 | grpcsharp_metadata_array_create(size_t capacity) { |
| 143 | grpc_metadata_array *array = |
| 144 | (grpc_metadata_array *)gpr_malloc(sizeof(grpc_metadata_array)); |
| 145 | grpc_metadata_array_init(array); |
| 146 | array->capacity = capacity; |
| 147 | array->count = 0; |
| 148 | if (capacity > 0) { |
| 149 | array->metadata = |
| 150 | (grpc_metadata *)gpr_malloc(sizeof(grpc_metadata) * capacity); |
| 151 | memset(array->metadata, 0, sizeof(grpc_metadata) * capacity); |
| 152 | } else { |
| 153 | array->metadata = NULL; |
| 154 | } |
| 155 | return array; |
| 156 | } |
| 157 | |
| 158 | GPR_EXPORT void GPR_CALLTYPE |
| 159 | grpcsharp_metadata_array_add(grpc_metadata_array *array, const char *key, |
| 160 | const char *value, size_t value_length) { |
| 161 | size_t i = array->count; |
| 162 | GPR_ASSERT(array->count < array->capacity); |
| 163 | array->metadata[i].key = gpr_strdup(key); |
| 164 | array->metadata[i].value = (char *)gpr_malloc(value_length); |
| 165 | memcpy((void *)array->metadata[i].value, value, value_length); |
| 166 | array->metadata[i].value_length = value_length; |
| 167 | array->count++; |
| 168 | } |
| 169 | |
| 170 | /* Move contents of metadata array */ |
| 171 | void grpcsharp_metadata_array_move(grpc_metadata_array *dest, |
| 172 | grpc_metadata_array *src) { |
| 173 | if (!src) { |
| 174 | dest->capacity = 0; |
| 175 | dest->count = 0; |
| 176 | dest->metadata = NULL; |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | dest->capacity = src->capacity; |
| 181 | dest->count = src->count; |
| 182 | dest->metadata = src->metadata; |
| 183 | |
| 184 | src->capacity = 0; |
| 185 | src->count = 0; |
| 186 | src->metadata = NULL; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 189 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 190 | if (!ctx) { |
| 191 | return; |
| 192 | } |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 193 | grpcsharp_metadata_array_destroy_metadata_including_entries( |
| 194 | &(ctx->send_initial_metadata)); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 195 | |
| 196 | grpc_byte_buffer_destroy(ctx->send_message); |
| 197 | |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 198 | grpcsharp_metadata_array_destroy_metadata_including_entries( |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 199 | &(ctx->send_status_from_server.trailing_metadata)); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 200 | gpr_free(ctx->send_status_from_server.status_details); |
| 201 | |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 202 | grpcsharp_metadata_array_destroy_metadata_only(&(ctx->recv_initial_metadata)); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 203 | |
| 204 | grpc_byte_buffer_destroy(ctx->recv_message); |
| 205 | |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 206 | grpcsharp_metadata_array_destroy_metadata_only( |
| 207 | &(ctx->recv_status_on_client.trailing_metadata)); |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 208 | gpr_free((void *)ctx->recv_status_on_client.status_details); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 209 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 210 | /* NOTE: ctx->server_rpc_new.call is not destroyed because callback handler is |
| 211 | supposed |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 212 | to take its ownership. */ |
| 213 | |
| 214 | grpc_call_details_destroy(&(ctx->server_rpc_new.call_details)); |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 215 | grpcsharp_metadata_array_destroy_metadata_only( |
| 216 | &(ctx->server_rpc_new.request_metadata)); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 217 | |
| 218 | gpr_free(ctx); |
| 219 | } |
| 220 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 221 | GPR_EXPORT gpr_intptr GPR_CALLTYPE grpcsharp_batch_context_recv_message_length( |
| 222 | const grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 223 | if (!ctx->recv_message) { |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | return grpc_byte_buffer_length(ctx->recv_message); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Copies data from recv_message to a buffer. Fatal error occurs if |
| 231 | * buffer is too small. |
| 232 | */ |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 233 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_recv_message_to_buffer( |
| 234 | const grpcsharp_batch_context *ctx, char *buffer, size_t buffer_len) { |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 235 | grpc_byte_buffer_reader reader; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 236 | gpr_slice slice; |
| 237 | size_t offset = 0; |
| 238 | |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 239 | grpc_byte_buffer_reader_init(&reader, ctx->recv_message); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 240 | |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 241 | while (grpc_byte_buffer_reader_next(&reader, &slice)) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 242 | size_t len = GPR_SLICE_LENGTH(slice); |
| 243 | GPR_ASSERT(offset + len <= buffer_len); |
| 244 | memcpy(buffer + offset, GPR_SLICE_START_PTR(slice), |
| 245 | GPR_SLICE_LENGTH(slice)); |
| 246 | offset += len; |
| 247 | gpr_slice_unref(slice); |
| 248 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | GPR_EXPORT grpc_status_code GPR_CALLTYPE |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 252 | grpcsharp_batch_context_recv_status_on_client_status( |
| 253 | const grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 254 | return ctx->recv_status_on_client.status; |
| 255 | } |
| 256 | |
| 257 | GPR_EXPORT const char *GPR_CALLTYPE |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 258 | grpcsharp_batch_context_recv_status_on_client_details( |
| 259 | const grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 260 | return ctx->recv_status_on_client.status_details; |
| 261 | } |
| 262 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 263 | GPR_EXPORT grpc_call *GPR_CALLTYPE grpcsharp_batch_context_server_rpc_new_call( |
| 264 | const grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 265 | return ctx->server_rpc_new.call; |
| 266 | } |
| 267 | |
| 268 | GPR_EXPORT const char *GPR_CALLTYPE |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 269 | grpcsharp_batch_context_server_rpc_new_method( |
| 270 | const grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 271 | return ctx->server_rpc_new.call_details.method; |
| 272 | } |
| 273 | |
Jan Tattermusch | 8c2dd9d | 2015-05-04 09:20:43 -0700 | [diff] [blame] | 274 | GPR_EXPORT gpr_int32 GPR_CALLTYPE |
| 275 | grpcsharp_batch_context_recv_close_on_server_cancelled( |
| 276 | const grpcsharp_batch_context *ctx) { |
| 277 | return (gpr_int32) ctx->recv_close_on_server_cancelled; |
| 278 | } |
| 279 | |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 280 | /* Init & shutdown */ |
| 281 | |
Jan Tattermusch | a9edeef | 2015-02-12 14:29:10 -0800 | [diff] [blame] | 282 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_init(void) { grpc_init(); } |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 283 | |
Jan Tattermusch | a9edeef | 2015-02-12 14:29:10 -0800 | [diff] [blame] | 284 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_shutdown(void) { grpc_shutdown(); } |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 285 | |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 286 | /* Completion queue */ |
| 287 | |
Jan Tattermusch | a9edeef | 2015-02-12 14:29:10 -0800 | [diff] [blame] | 288 | GPR_EXPORT grpc_completion_queue *GPR_CALLTYPE |
| 289 | grpcsharp_completion_queue_create(void) { |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 290 | return grpc_completion_queue_create(); |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Jan Tattermusch | a9edeef | 2015-02-12 14:29:10 -0800 | [diff] [blame] | 293 | GPR_EXPORT void GPR_CALLTYPE |
| 294 | grpcsharp_completion_queue_shutdown(grpc_completion_queue *cq) { |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 295 | grpc_completion_queue_shutdown(cq); |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Jan Tattermusch | a9edeef | 2015-02-12 14:29:10 -0800 | [diff] [blame] | 298 | GPR_EXPORT void GPR_CALLTYPE |
| 299 | grpcsharp_completion_queue_destroy(grpc_completion_queue *cq) { |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 300 | grpc_completion_queue_destroy(cq); |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 303 | GPR_EXPORT grpc_event GPR_CALLTYPE |
| 304 | grpcsharp_completion_queue_next(grpc_completion_queue *cq) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 305 | return grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME)); |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 306 | } |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 307 | |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 308 | GPR_EXPORT grpc_event GPR_CALLTYPE |
| 309 | grpcsharp_completion_queue_pluck(grpc_completion_queue *cq, void *tag) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 310 | return grpc_completion_queue_pluck(cq, tag, |
| 311 | gpr_inf_future(GPR_CLOCK_REALTIME)); |
Jan Tattermusch | 94c3653 | 2015-01-21 10:36:12 -0800 | [diff] [blame] | 312 | } |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 313 | |
| 314 | /* Channel */ |
| 315 | |
Jan Tattermusch | a9edeef | 2015-02-12 14:29:10 -0800 | [diff] [blame] | 316 | GPR_EXPORT grpc_channel *GPR_CALLTYPE |
| 317 | grpcsharp_channel_create(const char *target, const grpc_channel_args *args) { |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 318 | return grpc_channel_create(target, args); |
| 319 | } |
| 320 | |
| 321 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_channel_destroy(grpc_channel *channel) { |
| 322 | grpc_channel_destroy(channel); |
| 323 | } |
| 324 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 325 | GPR_EXPORT grpc_call *GPR_CALLTYPE |
| 326 | grpcsharp_channel_create_call(grpc_channel *channel, grpc_completion_queue *cq, |
| 327 | const char *method, const char *host, |
| 328 | gpr_timespec deadline) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 329 | return grpc_channel_create_call(channel, cq, method, host, deadline); |
| 330 | } |
| 331 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 332 | /* Channel args */ |
| 333 | |
| 334 | GPR_EXPORT grpc_channel_args *GPR_CALLTYPE |
| 335 | grpcsharp_channel_args_create(size_t num_args) { |
| 336 | grpc_channel_args *args = |
| 337 | (grpc_channel_args *)gpr_malloc(sizeof(grpc_channel_args)); |
| 338 | memset(args, 0, sizeof(grpc_channel_args)); |
| 339 | |
| 340 | args->num_args = num_args; |
| 341 | args->args = (grpc_arg *)gpr_malloc(sizeof(grpc_arg) * num_args); |
| 342 | memset(args->args, 0, sizeof(grpc_arg) * num_args); |
| 343 | return args; |
| 344 | } |
| 345 | |
| 346 | GPR_EXPORT void GPR_CALLTYPE |
| 347 | grpcsharp_channel_args_set_string(grpc_channel_args *args, size_t index, |
| 348 | const char *key, const char *value) { |
| 349 | GPR_ASSERT(args); |
| 350 | GPR_ASSERT(index < args->num_args); |
| 351 | args->args[index].type = GRPC_ARG_STRING; |
| 352 | args->args[index].key = gpr_strdup(key); |
| 353 | args->args[index].value.string = gpr_strdup(value); |
| 354 | } |
| 355 | |
| 356 | GPR_EXPORT void GPR_CALLTYPE |
Jan Tattermusch | 2ddb5a6 | 2015-06-08 17:51:36 -0700 | [diff] [blame] | 357 | grpcsharp_channel_args_set_integer(grpc_channel_args *args, size_t index, |
| 358 | const char *key, int value) { |
| 359 | GPR_ASSERT(args); |
| 360 | GPR_ASSERT(index < args->num_args); |
| 361 | args->args[index].type = GRPC_ARG_INTEGER; |
| 362 | args->args[index].key = gpr_strdup(key); |
| 363 | args->args[index].value.integer = value; |
| 364 | } |
| 365 | |
| 366 | GPR_EXPORT void GPR_CALLTYPE |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 367 | grpcsharp_channel_args_destroy(grpc_channel_args *args) { |
| 368 | size_t i; |
| 369 | if (args) { |
| 370 | for (i = 0; i < args->num_args; i++) { |
| 371 | gpr_free(args->args[i].key); |
| 372 | if (args->args[i].type == GRPC_ARG_STRING) { |
| 373 | gpr_free(args->args[i].value.string); |
| 374 | } |
| 375 | } |
| 376 | gpr_free(args->args); |
| 377 | gpr_free(args); |
| 378 | } |
| 379 | } |
| 380 | |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 381 | /* Timespec */ |
| 382 | |
Craig Tiller | f3756c1 | 2015-07-01 17:21:01 -0700 | [diff] [blame] | 383 | GPR_EXPORT gpr_timespec GPR_CALLTYPE gprsharp_now(void) { return gpr_now(GPR_CLOCK_REALTIME); } |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 384 | |
| 385 | GPR_EXPORT gpr_timespec GPR_CALLTYPE gprsharp_inf_future(void) { |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 386 | return gpr_inf_future(GPR_CLOCK_REALTIME); |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | GPR_EXPORT gpr_int32 GPR_CALLTYPE gprsharp_sizeof_timespec(void) { |
| 390 | return sizeof(gpr_timespec); |
| 391 | } |
| 392 | |
| 393 | /* Call */ |
| 394 | |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 395 | GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_cancel(grpc_call *call) { |
| 396 | return grpc_call_cancel(call); |
| 397 | } |
| 398 | |
Jan Tattermusch | a9edeef | 2015-02-12 14:29:10 -0800 | [diff] [blame] | 399 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
| 400 | grpcsharp_call_cancel_with_status(grpc_call *call, grpc_status_code status, |
| 401 | const char *description) { |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 402 | return grpc_call_cancel_with_status(call, status, description); |
| 403 | } |
| 404 | |
Jan Tattermusch | 35e4280 | 2015-02-12 10:09:05 -0800 | [diff] [blame] | 405 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_call_destroy(grpc_call *call) { |
| 406 | grpc_call_destroy(call); |
| 407 | } |
| 408 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 409 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 410 | grpcsharp_call_start_unary(grpc_call *call, grpcsharp_batch_context *ctx, |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 411 | const char *send_buffer, size_t send_buffer_len, |
| 412 | grpc_metadata_array *initial_metadata) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 413 | /* TODO: don't use magic number */ |
| 414 | grpc_op ops[6]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 415 | ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 416 | grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), |
| 417 | initial_metadata); |
| 418 | ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; |
| 419 | ops[0].data.send_initial_metadata.metadata = |
| 420 | ctx->send_initial_metadata.metadata; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 421 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 422 | |
| 423 | ops[1].op = GRPC_OP_SEND_MESSAGE; |
| 424 | ctx->send_message = string_to_byte_buffer(send_buffer, send_buffer_len); |
| 425 | ops[1].data.send_message = ctx->send_message; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 426 | ops[1].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 427 | |
| 428 | ops[2].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 429 | ops[2].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 430 | |
| 431 | ops[3].op = GRPC_OP_RECV_INITIAL_METADATA; |
| 432 | ops[3].data.recv_initial_metadata = &(ctx->recv_initial_metadata); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 433 | ops[3].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 434 | |
| 435 | ops[4].op = GRPC_OP_RECV_MESSAGE; |
| 436 | ops[4].data.recv_message = &(ctx->recv_message); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 437 | ops[4].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 438 | |
| 439 | ops[5].op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 440 | ops[5].data.recv_status_on_client.trailing_metadata = |
| 441 | &(ctx->recv_status_on_client.trailing_metadata); |
| 442 | ops[5].data.recv_status_on_client.status = |
| 443 | &(ctx->recv_status_on_client.status); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 444 | /* not using preallocation for status_details */ |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 445 | ops[5].data.recv_status_on_client.status_details = |
| 446 | &(ctx->recv_status_on_client.status_details); |
| 447 | ops[5].data.recv_status_on_client.status_details_capacity = |
| 448 | &(ctx->recv_status_on_client.status_details_capacity); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 449 | ops[5].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 450 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 451 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 454 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
| 455 | grpcsharp_call_start_client_streaming(grpc_call *call, |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 456 | grpcsharp_batch_context *ctx, |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 457 | grpc_metadata_array *initial_metadata) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 458 | /* TODO: don't use magic number */ |
| 459 | grpc_op ops[4]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 460 | ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 461 | grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), |
| 462 | initial_metadata); |
| 463 | ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; |
| 464 | ops[0].data.send_initial_metadata.metadata = |
| 465 | ctx->send_initial_metadata.metadata; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 466 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 467 | |
| 468 | ops[1].op = GRPC_OP_RECV_INITIAL_METADATA; |
| 469 | ops[1].data.recv_initial_metadata = &(ctx->recv_initial_metadata); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 470 | ops[1].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 471 | |
| 472 | ops[2].op = GRPC_OP_RECV_MESSAGE; |
| 473 | ops[2].data.recv_message = &(ctx->recv_message); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 474 | ops[2].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 475 | |
| 476 | ops[3].op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 477 | ops[3].data.recv_status_on_client.trailing_metadata = |
| 478 | &(ctx->recv_status_on_client.trailing_metadata); |
| 479 | ops[3].data.recv_status_on_client.status = |
| 480 | &(ctx->recv_status_on_client.status); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 481 | /* not using preallocation for status_details */ |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 482 | ops[3].data.recv_status_on_client.status_details = |
| 483 | &(ctx->recv_status_on_client.status_details); |
| 484 | ops[3].data.recv_status_on_client.status_details_capacity = |
| 485 | &(ctx->recv_status_on_client.status_details_capacity); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 486 | ops[3].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 487 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 488 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 491 | GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_start_server_streaming( |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 492 | grpc_call *call, grpcsharp_batch_context *ctx, const char *send_buffer, |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 493 | size_t send_buffer_len, grpc_metadata_array *initial_metadata) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 494 | /* TODO: don't use magic number */ |
| 495 | grpc_op ops[5]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 496 | ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 497 | grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), |
| 498 | initial_metadata); |
| 499 | ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; |
| 500 | ops[0].data.send_initial_metadata.metadata = |
| 501 | ctx->send_initial_metadata.metadata; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 502 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 503 | |
| 504 | ops[1].op = GRPC_OP_SEND_MESSAGE; |
| 505 | ctx->send_message = string_to_byte_buffer(send_buffer, send_buffer_len); |
| 506 | ops[1].data.send_message = ctx->send_message; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 507 | ops[1].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 508 | |
| 509 | ops[2].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 510 | ops[2].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 511 | |
| 512 | ops[3].op = GRPC_OP_RECV_INITIAL_METADATA; |
| 513 | ops[3].data.recv_initial_metadata = &(ctx->recv_initial_metadata); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 514 | ops[3].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 515 | |
| 516 | ops[4].op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 517 | ops[4].data.recv_status_on_client.trailing_metadata = |
| 518 | &(ctx->recv_status_on_client.trailing_metadata); |
| 519 | ops[4].data.recv_status_on_client.status = |
| 520 | &(ctx->recv_status_on_client.status); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 521 | /* not using preallocation for status_details */ |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 522 | ops[4].data.recv_status_on_client.status_details = |
| 523 | &(ctx->recv_status_on_client.status_details); |
| 524 | ops[4].data.recv_status_on_client.status_details_capacity = |
| 525 | &(ctx->recv_status_on_client.status_details_capacity); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 526 | ops[4].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 527 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 528 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 531 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
| 532 | grpcsharp_call_start_duplex_streaming(grpc_call *call, |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 533 | grpcsharp_batch_context *ctx, |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 534 | grpc_metadata_array *initial_metadata) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 535 | /* TODO: don't use magic number */ |
| 536 | grpc_op ops[3]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 537 | ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
Jan Tattermusch | c0b3721 | 2015-03-13 08:35:41 -0700 | [diff] [blame] | 538 | grpcsharp_metadata_array_move(&(ctx->send_initial_metadata), |
| 539 | initial_metadata); |
| 540 | ops[0].data.send_initial_metadata.count = ctx->send_initial_metadata.count; |
| 541 | ops[0].data.send_initial_metadata.metadata = |
| 542 | ctx->send_initial_metadata.metadata; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 543 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 544 | |
| 545 | ops[1].op = GRPC_OP_RECV_INITIAL_METADATA; |
| 546 | ops[1].data.recv_initial_metadata = &(ctx->recv_initial_metadata); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 547 | ops[1].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 548 | |
| 549 | ops[2].op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 550 | ops[2].data.recv_status_on_client.trailing_metadata = |
| 551 | &(ctx->recv_status_on_client.trailing_metadata); |
| 552 | ops[2].data.recv_status_on_client.status = |
| 553 | &(ctx->recv_status_on_client.status); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 554 | /* not using preallocation for status_details */ |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 555 | ops[2].data.recv_status_on_client.status_details = |
| 556 | &(ctx->recv_status_on_client.status_details); |
| 557 | ops[2].data.recv_status_on_client.status_details_capacity = |
| 558 | &(ctx->recv_status_on_client.status_details_capacity); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 559 | ops[2].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 560 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 561 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 564 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 565 | grpcsharp_call_send_message(grpc_call *call, grpcsharp_batch_context *ctx, |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 566 | const char *send_buffer, size_t send_buffer_len) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 567 | /* TODO: don't use magic number */ |
| 568 | grpc_op ops[1]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 569 | ops[0].op = GRPC_OP_SEND_MESSAGE; |
| 570 | ctx->send_message = string_to_byte_buffer(send_buffer, send_buffer_len); |
| 571 | ops[0].data.send_message = ctx->send_message; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 572 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 573 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 574 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 577 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
| 578 | grpcsharp_call_send_close_from_client(grpc_call *call, |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 579 | grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 580 | /* TODO: don't use magic number */ |
| 581 | grpc_op ops[1]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 582 | ops[0].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 583 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 584 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 585 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 588 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
| 589 | grpcsharp_call_send_status_from_server(grpc_call *call, |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 590 | grpcsharp_batch_context *ctx, |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 591 | grpc_status_code status_code, |
| 592 | const char *status_details) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 593 | /* TODO: don't use magic number */ |
| 594 | grpc_op ops[1]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 595 | ops[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 596 | ops[0].data.send_status_from_server.status = status_code; |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 597 | ops[0].data.send_status_from_server.status_details = |
| 598 | gpr_strdup(status_details); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 599 | ops[0].data.send_status_from_server.trailing_metadata = NULL; |
| 600 | ops[0].data.send_status_from_server.trailing_metadata_count = 0; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 601 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 602 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 603 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 606 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 607 | grpcsharp_call_recv_message(grpc_call *call, grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 608 | /* TODO: don't use magic number */ |
| 609 | grpc_op ops[1]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 610 | ops[0].op = GRPC_OP_RECV_MESSAGE; |
| 611 | ops[0].data.recv_message = &(ctx->recv_message); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 612 | ops[0].flags = 0; |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 613 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 616 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 617 | grpcsharp_call_start_serverside(grpc_call *call, grpcsharp_batch_context *ctx) { |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 618 | /* TODO: don't use magic number */ |
| 619 | grpc_op ops[2]; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 620 | ops[0].op = GRPC_OP_SEND_INITIAL_METADATA; |
| 621 | ops[0].data.send_initial_metadata.count = 0; |
| 622 | ops[0].data.send_initial_metadata.metadata = NULL; |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 623 | ops[0].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 624 | |
| 625 | ops[1].op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 626 | ops[1].data.recv_close_on_server.cancelled = |
| 627 | (&ctx->recv_close_on_server_cancelled); |
David Garcia Quintas | d7e0bea | 2015-06-15 13:27:17 -0700 | [diff] [blame] | 628 | ops[1].flags = 0; |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 629 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 630 | return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Jan Tattermusch | 3f8962c | 2015-02-17 19:20:39 -0800 | [diff] [blame] | 633 | /* Server */ |
| 634 | |
| 635 | GPR_EXPORT grpc_server *GPR_CALLTYPE |
| 636 | grpcsharp_server_create(grpc_completion_queue *cq, |
| 637 | const grpc_channel_args *args) { |
Craig Tiller | 4d706b8 | 2015-05-08 07:42:13 -0700 | [diff] [blame] | 638 | grpc_server *server = grpc_server_create(args); |
| 639 | grpc_server_register_completion_queue(server, cq); |
| 640 | return server; |
Jan Tattermusch | 3f8962c | 2015-02-17 19:20:39 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Jan Tattermusch | 8d2e572 | 2015-02-18 12:41:14 -0800 | [diff] [blame] | 643 | GPR_EXPORT gpr_int32 GPR_CALLTYPE |
Jan Tattermusch | 3f8962c | 2015-02-17 19:20:39 -0800 | [diff] [blame] | 644 | grpcsharp_server_add_http2_port(grpc_server *server, const char *addr) { |
| 645 | return grpc_server_add_http2_port(server, addr); |
| 646 | } |
| 647 | |
Jan Tattermusch | 3f8962c | 2015-02-17 19:20:39 -0800 | [diff] [blame] | 648 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_start(grpc_server *server) { |
| 649 | grpc_server_start(server); |
| 650 | } |
| 651 | |
Jan Tattermusch | 3f8962c | 2015-02-17 19:20:39 -0800 | [diff] [blame] | 652 | GPR_EXPORT void GPR_CALLTYPE |
Jan Tattermusch | 1215c33 | 2015-05-04 19:39:59 -0700 | [diff] [blame] | 653 | grpcsharp_server_shutdown_and_notify_callback(grpc_server *server, |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 654 | grpc_completion_queue *cq, |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 655 | grpcsharp_batch_context *ctx) { |
Jan Tattermusch | c4e81ad | 2015-05-29 17:39:07 -0700 | [diff] [blame] | 656 | grpc_server_shutdown_and_notify(server, cq, ctx); |
| 657 | } |
| 658 | |
| 659 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_cancel_all_calls(grpc_server *server) { |
| 660 | grpc_server_cancel_all_calls(server); |
Jan Tattermusch | 3f8962c | 2015-02-17 19:20:39 -0800 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_destroy(grpc_server *server) { |
| 664 | grpc_server_destroy(server); |
| 665 | } |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 666 | |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 667 | GPR_EXPORT grpc_call_error GPR_CALLTYPE |
| 668 | grpcsharp_server_request_call(grpc_server *server, grpc_completion_queue *cq, |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 669 | grpcsharp_batch_context *ctx) { |
Jan Tattermusch | fa21673 | 2015-02-18 11:06:44 -0800 | [diff] [blame] | 670 | return grpc_server_request_call( |
| 671 | server, &(ctx->server_rpc_new.call), &(ctx->server_rpc_new.call_details), |
Craig Tiller | 4d706b8 | 2015-05-08 07:42:13 -0700 | [diff] [blame] | 672 | &(ctx->server_rpc_new.request_metadata), cq, cq, ctx); |
Jan Tattermusch | 337a2dd | 2015-02-13 15:41:41 -0800 | [diff] [blame] | 673 | } |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 674 | |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 675 | /* Security */ |
| 676 | |
| 677 | GPR_EXPORT grpc_credentials *GPR_CALLTYPE |
| 678 | grpcsharp_ssl_credentials_create(const char *pem_root_certs, |
| 679 | const char *key_cert_pair_cert_chain, |
| 680 | const char *key_cert_pair_private_key) { |
| 681 | grpc_ssl_pem_key_cert_pair key_cert_pair; |
| 682 | if (key_cert_pair_cert_chain || key_cert_pair_private_key) { |
| 683 | key_cert_pair.cert_chain = key_cert_pair_cert_chain; |
| 684 | key_cert_pair.private_key = key_cert_pair_private_key; |
| 685 | return grpc_ssl_credentials_create(pem_root_certs, &key_cert_pair); |
| 686 | } else { |
| 687 | GPR_ASSERT(!key_cert_pair_cert_chain); |
| 688 | GPR_ASSERT(!key_cert_pair_private_key); |
| 689 | return grpc_ssl_credentials_create(pem_root_certs, NULL); |
| 690 | } |
| 691 | } |
| 692 | |
Jan Tattermusch | 8c5d68b | 2015-04-07 12:34:50 -0700 | [diff] [blame] | 693 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_credentials_release(grpc_credentials *creds) { |
Jan Tattermusch | 1532923 | 2015-03-02 15:32:47 -0800 | [diff] [blame] | 694 | grpc_credentials_release(creds); |
| 695 | } |
| 696 | |
| 697 | GPR_EXPORT grpc_channel *GPR_CALLTYPE |
| 698 | grpcsharp_secure_channel_create(grpc_credentials *creds, const char *target, |
| 699 | const grpc_channel_args *args) { |
| 700 | return grpc_secure_channel_create(creds, target, args); |
| 701 | } |
| 702 | |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 703 | GPR_EXPORT grpc_server_credentials *GPR_CALLTYPE |
| 704 | grpcsharp_ssl_server_credentials_create( |
| 705 | const char *pem_root_certs, const char **key_cert_pair_cert_chain_array, |
| 706 | const char **key_cert_pair_private_key_array, size_t num_key_cert_pairs) { |
| 707 | size_t i; |
| 708 | grpc_server_credentials *creds; |
| 709 | grpc_ssl_pem_key_cert_pair *key_cert_pairs = |
| 710 | gpr_malloc(sizeof(grpc_ssl_pem_key_cert_pair) * num_key_cert_pairs); |
| 711 | memset(key_cert_pairs, 0, |
| 712 | sizeof(grpc_ssl_pem_key_cert_pair) * num_key_cert_pairs); |
| 713 | |
| 714 | for (i = 0; i < num_key_cert_pairs; i++) { |
| 715 | if (key_cert_pair_cert_chain_array[i] || |
| 716 | key_cert_pair_private_key_array[i]) { |
| 717 | key_cert_pairs[i].cert_chain = key_cert_pair_cert_chain_array[i]; |
| 718 | key_cert_pairs[i].private_key = key_cert_pair_private_key_array[i]; |
| 719 | } |
| 720 | } |
| 721 | creds = grpc_ssl_server_credentials_create(pem_root_certs, key_cert_pairs, |
| 722 | num_key_cert_pairs); |
| 723 | gpr_free(key_cert_pairs); |
| 724 | return creds; |
| 725 | } |
| 726 | |
Jan Tattermusch | 8c5d68b | 2015-04-07 12:34:50 -0700 | [diff] [blame] | 727 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_server_credentials_release( |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 728 | grpc_server_credentials *creds) { |
| 729 | grpc_server_credentials_release(creds); |
| 730 | } |
| 731 | |
| 732 | GPR_EXPORT gpr_int32 GPR_CALLTYPE |
| 733 | grpcsharp_server_add_secure_http2_port(grpc_server *server, const char *addr, |
| 734 | grpc_server_credentials *creds) { |
| 735 | return grpc_server_add_secure_http2_port(server, addr, creds); |
| 736 | } |
| 737 | |
Jan Tattermusch | 5339814 | 2015-02-24 08:21:57 -0800 | [diff] [blame] | 738 | /* Logging */ |
| 739 | |
Jan Tattermusch | 44553b5 | 2015-02-25 09:52:18 -0800 | [diff] [blame] | 740 | typedef void(GPR_CALLTYPE *grpcsharp_log_func)(const char *file, gpr_int32 line, |
| 741 | gpr_uint64 thd_id, |
| 742 | const char *severity_string, |
| 743 | const char *msg); |
Jan Tattermusch | 5339814 | 2015-02-24 08:21:57 -0800 | [diff] [blame] | 744 | static grpcsharp_log_func log_func = NULL; |
| 745 | |
| 746 | /* Redirects gpr_log to log_func callback */ |
| 747 | static void grpcsharp_log_handler(gpr_log_func_args *args) { |
Jan Tattermusch | 44553b5 | 2015-02-25 09:52:18 -0800 | [diff] [blame] | 748 | log_func(args->file, args->line, gpr_thd_currentid(), |
| 749 | gpr_log_severity_string(args->severity), args->message); |
Jan Tattermusch | 5339814 | 2015-02-24 08:21:57 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Jan Tattermusch | 44553b5 | 2015-02-25 09:52:18 -0800 | [diff] [blame] | 752 | GPR_EXPORT void GPR_CALLTYPE grpcsharp_redirect_log(grpcsharp_log_func func) { |
| 753 | GPR_ASSERT(func); |
| 754 | log_func = func; |
| 755 | gpr_set_log_function(grpcsharp_log_handler); |
Jan Tattermusch | 5339814 | 2015-02-24 08:21:57 -0800 | [diff] [blame] | 756 | } |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 757 | |
Jan Tattermusch | eb741d1 | 2015-06-11 19:09:15 -0700 | [diff] [blame] | 758 | typedef void(GPR_CALLTYPE *test_callback_funcptr)(gpr_int32 success); |
| 759 | |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 760 | /* For testing */ |
| 761 | GPR_EXPORT void GPR_CALLTYPE |
Jan Tattermusch | eb741d1 | 2015-06-11 19:09:15 -0700 | [diff] [blame] | 762 | grpcsharp_test_callback(test_callback_funcptr callback) { |
| 763 | callback(1); |
Jan Tattermusch | 50faa8f | 2015-02-21 17:51:52 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /* For testing */ |
Jan Tattermusch | 44553b5 | 2015-02-25 09:52:18 -0800 | [diff] [blame] | 767 | GPR_EXPORT void *GPR_CALLTYPE grpcsharp_test_nop(void *ptr) { return ptr; } |
Jan Tattermusch | d367748 | 2015-06-01 19:27:40 -0700 | [diff] [blame] | 768 | |
| 769 | /* For testing */ |
| 770 | GPR_EXPORT gpr_int32 GPR_CALLTYPE grpcsharp_sizeof_grpc_event(void) { |
| 771 | return sizeof(grpc_event); |
| 772 | } |