diff options
author | Craig Tiller <ctiller@google.com> | 2017-01-20 18:11:52 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-20 18:11:52 -0800 |
commit | 5e01e2ac977655aa074faf7fde0a74298f5e4c55 (patch) | |
tree | 9acab9c5952f292683a6d474861e2997e2a9d664 /src/csharp | |
parent | c84725fd02dc58a819c8c4e8acdc321e81f44764 (diff) |
Revert "Metadata handling rewrite"
Diffstat (limited to 'src/csharp')
-rw-r--r-- | src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs | 4 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs | 10 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Internal/NativeMethods.cs | 13 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs | 10 | ||||
-rw-r--r-- | src/csharp/ext/grpc_csharp_ext.c | 73 |
5 files changed, 56 insertions, 54 deletions
diff --git a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs index 0e4a77be81..26449ee539 100644 --- a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs @@ -71,9 +71,7 @@ namespace Grpc.Core.Internal // Gets data of recv_status_on_client completion. public ClientSideStatus GetReceivedStatusOnClient() { - UIntPtr detailsLength; - IntPtr detailsPtr = Native.grpcsharp_batch_context_recv_status_on_client_details(this, out detailsLength); - string details = Marshal.PtrToStringAnsi(detailsPtr, (int) detailsLength.ToUInt32()); + string details = Marshal.PtrToStringAnsi(Native.grpcsharp_batch_context_recv_status_on_client_details(this)); var status = new Status(Native.grpcsharp_batch_context_recv_status_on_client_status(this), details); IntPtr metadataArrayPtr = Native.grpcsharp_batch_context_recv_status_on_client_trailing_metadata(this); diff --git a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs index d5b87a6c94..05dda5b148 100644 --- a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs @@ -79,13 +79,9 @@ namespace Grpc.Core.Internal for (ulong i = 0; i < count; i++) { var index = new UIntPtr(i); - UIntPtr keyLen; - IntPtr keyPtr = Native.grpcsharp_metadata_array_get_key(metadataArray, index, out keyLen); - string key = Marshal.PtrToStringAnsi(keyPtr, (int)keyLen.ToUInt32()); - UIntPtr valueLen; - IntPtr valuePtr = Native.grpcsharp_metadata_array_get_value(metadataArray, index, out valueLen); - var bytes = new byte[valueLen.ToUInt64()]; - Marshal.Copy(valuePtr, bytes, 0, bytes.Length); + string key = Marshal.PtrToStringAnsi(Native.grpcsharp_metadata_array_get_key(metadataArray, index)); + var bytes = new byte[Native.grpcsharp_metadata_array_get_value_length(metadataArray, index).ToUInt64()]; + Marshal.Copy(Native.grpcsharp_metadata_array_get_value(metadataArray, index), bytes, 0, bytes.Length); metadata.Add(Metadata.Entry.CreateUnsafe(key, bytes)); } return metadata; diff --git a/src/csharp/Grpc.Core/Internal/NativeMethods.cs b/src/csharp/Grpc.Core/Internal/NativeMethods.cs index 2f377071f7..ce38e37093 100644 --- a/src/csharp/Grpc.Core/Internal/NativeMethods.cs +++ b/src/csharp/Grpc.Core/Internal/NativeMethods.cs @@ -128,6 +128,7 @@ namespace Grpc.Core.Internal public readonly Delegates.grpcsharp_metadata_array_count_delegate grpcsharp_metadata_array_count; public readonly Delegates.grpcsharp_metadata_array_get_key_delegate grpcsharp_metadata_array_get_key; public readonly Delegates.grpcsharp_metadata_array_get_value_delegate grpcsharp_metadata_array_get_value; + public readonly Delegates.grpcsharp_metadata_array_get_value_length_delegate grpcsharp_metadata_array_get_value_length; public readonly Delegates.grpcsharp_metadata_array_destroy_full_delegate grpcsharp_metadata_array_destroy_full; public readonly Delegates.grpcsharp_redirect_log_delegate grpcsharp_redirect_log; @@ -236,6 +237,7 @@ namespace Grpc.Core.Internal this.grpcsharp_metadata_array_count = GetMethodDelegate<Delegates.grpcsharp_metadata_array_count_delegate>(library); this.grpcsharp_metadata_array_get_key = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_key_delegate>(library); this.grpcsharp_metadata_array_get_value = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_value_delegate>(library); + this.grpcsharp_metadata_array_get_value_length = GetMethodDelegate<Delegates.grpcsharp_metadata_array_get_value_length_delegate>(library); this.grpcsharp_metadata_array_destroy_full = GetMethodDelegate<Delegates.grpcsharp_metadata_array_destroy_full_delegate>(library); this.grpcsharp_redirect_log = GetMethodDelegate<Delegates.grpcsharp_redirect_log_delegate>(library); @@ -304,15 +306,15 @@ namespace Grpc.Core.Internal public delegate IntPtr grpcsharp_batch_context_recv_message_length_delegate(BatchContextSafeHandle ctx); public delegate void grpcsharp_batch_context_recv_message_to_buffer_delegate(BatchContextSafeHandle ctx, byte[] buffer, UIntPtr bufferLen); public delegate StatusCode grpcsharp_batch_context_recv_status_on_client_status_delegate(BatchContextSafeHandle ctx); - public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_details_delegate(BatchContextSafeHandle ctx, out UIntPtr detailsLength); + public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_details_delegate(BatchContextSafeHandle ctx); // returns const char* public delegate IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata_delegate(BatchContextSafeHandle ctx); public delegate int grpcsharp_batch_context_recv_close_on_server_cancelled_delegate(BatchContextSafeHandle ctx); public delegate void grpcsharp_batch_context_destroy_delegate(IntPtr ctx); public delegate RequestCallContextSafeHandle grpcsharp_request_call_context_create_delegate(); public delegate CallSafeHandle grpcsharp_request_call_context_call_delegate(RequestCallContextSafeHandle ctx); - public delegate IntPtr grpcsharp_request_call_context_method_delegate(RequestCallContextSafeHandle ctx, out UIntPtr methodLength); - public delegate IntPtr grpcsharp_request_call_context_host_delegate(RequestCallContextSafeHandle ctx, out UIntPtr hostLength); + public delegate IntPtr grpcsharp_request_call_context_method_delegate(RequestCallContextSafeHandle ctx); // returns const char* + public delegate IntPtr grpcsharp_request_call_context_host_delegate(RequestCallContextSafeHandle ctx); // returns const char* public delegate Timespec grpcsharp_request_call_context_deadline_delegate(RequestCallContextSafeHandle ctx); public delegate IntPtr grpcsharp_request_call_context_request_metadata_delegate(RequestCallContextSafeHandle ctx); public delegate void grpcsharp_request_call_context_destroy_delegate(IntPtr ctx); @@ -382,8 +384,9 @@ namespace Grpc.Core.Internal public delegate MetadataArraySafeHandle grpcsharp_metadata_array_create_delegate(UIntPtr capacity); public delegate void grpcsharp_metadata_array_add_delegate(MetadataArraySafeHandle array, string key, byte[] value, UIntPtr valueLength); public delegate UIntPtr grpcsharp_metadata_array_count_delegate(IntPtr metadataArray); - public delegate IntPtr grpcsharp_metadata_array_get_key_delegate(IntPtr metadataArray, UIntPtr index, out UIntPtr keyLength); - public delegate IntPtr grpcsharp_metadata_array_get_value_delegate(IntPtr metadataArray, UIntPtr index, out UIntPtr valueLength); + public delegate IntPtr grpcsharp_metadata_array_get_key_delegate(IntPtr metadataArray, UIntPtr index); + public delegate IntPtr grpcsharp_metadata_array_get_value_delegate(IntPtr metadataArray, UIntPtr index); + public delegate UIntPtr grpcsharp_metadata_array_get_value_length_delegate(IntPtr metadataArray, UIntPtr index); public delegate void grpcsharp_metadata_array_destroy_full_delegate(IntPtr array); public delegate void grpcsharp_redirect_log_delegate(GprLogDelegate callback); diff --git a/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs b/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs index c1560bc8bf..ea7819d7b1 100644 --- a/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs @@ -66,14 +66,8 @@ namespace Grpc.Core.Internal { var call = Native.grpcsharp_request_call_context_call(this); - UIntPtr methodLen; - IntPtr methodPtr = Native.grpcsharp_request_call_context_method(this, out methodLen); - var method = Marshal.PtrToStringAnsi(methodPtr, (int) methodLen.ToUInt32()); - - UIntPtr hostLen; - IntPtr hostPtr = Native.grpcsharp_request_call_context_host(this, out hostLen); - var host = Marshal.PtrToStringAnsi(hostPtr, (int) hostLen.ToUInt32()); - + var method = Marshal.PtrToStringAnsi(Native.grpcsharp_request_call_context_method(this)); + var host = Marshal.PtrToStringAnsi(Native.grpcsharp_request_call_context_host(this)); var deadline = Native.grpcsharp_request_call_context_deadline(this); IntPtr metadataArrayPtr = Native.grpcsharp_request_call_context_request_metadata(this); diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index 12c747b800..946f5872c0 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -73,13 +73,15 @@ typedef struct grpcsharp_batch_context { grpc_byte_buffer *send_message; struct { grpc_metadata_array trailing_metadata; + char *status_details; } send_status_from_server; grpc_metadata_array recv_initial_metadata; grpc_byte_buffer *recv_message; struct { grpc_metadata_array trailing_metadata; grpc_status_code status; - grpc_slice status_details; + char *status_details; + size_t status_details_capacity; } recv_status_on_client; int recv_close_on_server_cancelled; } grpcsharp_batch_context; @@ -120,8 +122,8 @@ void grpcsharp_metadata_array_destroy_metadata_including_entries( size_t i; if (array->metadata) { for (i = 0; i < array->count; i++) { - grpc_slice_unref(array->metadata[i].key); - grpc_slice_unref(array->metadata[i].value); + gpr_free((void *)array->metadata[i].key); + gpr_free((void *)array->metadata[i].value); } } gpr_free(array->metadata); @@ -165,8 +167,10 @@ grpcsharp_metadata_array_add(grpc_metadata_array *array, const char *key, const char *value, size_t value_length) { size_t i = array->count; GPR_ASSERT(array->count < array->capacity); - array->metadata[i].key = grpc_slice_from_copied_string(key); - array->metadata[i].value = grpc_slice_from_copied_buffer(value, value_length); + array->metadata[i].key = gpr_strdup(key); + array->metadata[i].value = (char *)gpr_malloc(value_length); + memcpy((void *)array->metadata[i].value, value, value_length); + array->metadata[i].value_length = value_length; array->count++; } @@ -176,17 +180,21 @@ grpcsharp_metadata_array_count(grpc_metadata_array *array) { } GPR_EXPORT const char *GPR_CALLTYPE -grpcsharp_metadata_array_get_key(grpc_metadata_array *array, size_t index, size_t *key_length) { +grpcsharp_metadata_array_get_key(grpc_metadata_array *array, size_t index) { GPR_ASSERT(index < array->count); - *key_length = GRPC_SLICE_LENGTH(array->metadata[index].key); - return (char *)GRPC_SLICE_START_PTR(array->metadata[index].key); + return array->metadata[index].key; } GPR_EXPORT const char *GPR_CALLTYPE -grpcsharp_metadata_array_get_value(grpc_metadata_array *array, size_t index, size_t *value_length) { +grpcsharp_metadata_array_get_value(grpc_metadata_array *array, size_t index) { GPR_ASSERT(index < array->count); - *value_length = GRPC_SLICE_LENGTH(array->metadata[index].value); - return (char *)GRPC_SLICE_START_PTR(array->metadata[index].value); + return array->metadata[index].value; +} + +GPR_EXPORT intptr_t GPR_CALLTYPE grpcsharp_metadata_array_get_value_length( + grpc_metadata_array *array, size_t index) { + GPR_ASSERT(index < array->count); + return (intptr_t)array->metadata[index].value_length; } /* Move contents of metadata array */ @@ -219,6 +227,7 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_con grpcsharp_metadata_array_destroy_metadata_including_entries( &(ctx->send_status_from_server.trailing_metadata)); + gpr_free(ctx->send_status_from_server.status_details); grpcsharp_metadata_array_destroy_metadata_only(&(ctx->recv_initial_metadata)); @@ -226,7 +235,7 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_con grpcsharp_metadata_array_destroy_metadata_only( &(ctx->recv_status_on_client.trailing_metadata)); - grpc_slice_unref(ctx->recv_status_on_client.status_details); + gpr_free((void *)ctx->recv_status_on_client.status_details); gpr_free(ctx); } @@ -298,9 +307,8 @@ grpcsharp_batch_context_recv_status_on_client_status( GPR_EXPORT const char *GPR_CALLTYPE grpcsharp_batch_context_recv_status_on_client_details( - const grpcsharp_batch_context *ctx, size_t *details_length) { - *details_length = GRPC_SLICE_LENGTH(ctx->recv_status_on_client.status_details); - return (char *)GRPC_SLICE_START_PTR(ctx->recv_status_on_client.status_details); + const grpcsharp_batch_context *ctx) { + return ctx->recv_status_on_client.status_details; } GPR_EXPORT const grpc_metadata_array *GPR_CALLTYPE @@ -316,15 +324,13 @@ GPR_EXPORT grpc_call *GPR_CALLTYPE grpcsharp_request_call_context_call( GPR_EXPORT const char *GPR_CALLTYPE grpcsharp_request_call_context_method( - const grpcsharp_request_call_context *ctx, size_t *method_length) { - *method_length = GRPC_SLICE_LENGTH(ctx->call_details.method); - return (char *)GRPC_SLICE_START_PTR(ctx->call_details.method); + const grpcsharp_request_call_context *ctx) { + return ctx->call_details.method; } GPR_EXPORT const char *GPR_CALLTYPE grpcsharp_request_call_context_host( - const grpcsharp_request_call_context *ctx, size_t *host_length) { - *host_length = GRPC_SLICE_LENGTH(ctx->call_details.host); - return (char *)GRPC_SLICE_START_PTR(ctx->call_details.host); + const grpcsharp_request_call_context *ctx) { + return ctx->call_details.host; } GPR_EXPORT gpr_timespec GPR_CALLTYPE @@ -398,15 +404,8 @@ grpcsharp_channel_create_call(grpc_channel *channel, grpc_call *parent_call, grpc_completion_queue *cq, const char *method, const char *host, gpr_timespec deadline) { - grpc_slice method_slice = grpc_slice_from_copied_string(method); - grpc_slice *host_slice_ptr = NULL; - grpc_slice host_slice; - if (host != NULL) { - host_slice = grpc_slice_from_copied_string(host); - host_slice_ptr = &host_slice; - } return grpc_channel_create_call(channel, parent_call, propagation_mask, cq, - method_slice, host_slice_ptr, deadline, NULL); + method, host, deadline, NULL); } GPR_EXPORT grpc_connectivity_state GPR_CALLTYPE @@ -561,8 +560,11 @@ grpcsharp_call_start_unary(grpc_call *call, grpcsharp_batch_context *ctx, &(ctx->recv_status_on_client.trailing_metadata); ops[5].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); + /* not using preallocation for status_details */ ops[5].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); + ops[5].data.recv_status_on_client.status_details_capacity = + &(ctx->recv_status_on_client.status_details_capacity); ops[5].flags = 0; ops[5].reserved = NULL; @@ -602,8 +604,11 @@ grpcsharp_call_start_client_streaming(grpc_call *call, &(ctx->recv_status_on_client.trailing_metadata); ops[3].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); + /* not using preallocation for status_details */ ops[3].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); + ops[3].data.recv_status_on_client.status_details_capacity = + &(ctx->recv_status_on_client.status_details_capacity); ops[3].flags = 0; ops[3].reserved = NULL; @@ -642,8 +647,11 @@ GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_start_server_streaming( &(ctx->recv_status_on_client.trailing_metadata); ops[3].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); + /* not using preallocation for status_details */ ops[3].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); + ops[3].data.recv_status_on_client.status_details_capacity = + &(ctx->recv_status_on_client.status_details_capacity); ops[3].flags = 0; ops[3].reserved = NULL; @@ -673,8 +681,11 @@ grpcsharp_call_start_duplex_streaming(grpc_call *call, &(ctx->recv_status_on_client.trailing_metadata); ops[1].data.recv_status_on_client.status = &(ctx->recv_status_on_client.status); + /* not using preallocation for status_details */ ops[1].data.recv_status_on_client.status_details = &(ctx->recv_status_on_client.status_details); + ops[1].data.recv_status_on_client.status_details_capacity = + &(ctx->recv_status_on_client.status_details_capacity); ops[1].flags = 0; ops[1].reserved = NULL; @@ -738,10 +749,10 @@ GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_send_status_from_server( grpc_op ops[3]; memset(ops, 0, sizeof(ops)); size_t nops = 1; - grpc_slice status_details_slice = grpc_slice_from_copied_string(status_details); ops[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER; ops[0].data.send_status_from_server.status = status_code; - ops[0].data.send_status_from_server.status_details = &status_details_slice; + ops[0].data.send_status_from_server.status_details = + gpr_strdup(status_details); grpcsharp_metadata_array_move( &(ctx->send_status_from_server.trailing_metadata), trailing_metadata); ops[0].data.send_status_from_server.trailing_metadata_count = |